CommonController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\FrontController;
  4. use App\Jobs\BarrageJob;
  5. use App\Models\AgencySet;
  6. use App\Models\Config;
  7. use App\Models\Contract;
  8. use App\Models\MemberClan;
  9. use App\Models\Region;
  10. use App\Servers\BroadcastServer;
  11. use App\Servers\CommonServer;
  12. use App\Servers\ContractServer;
  13. use App\Servers\Icon\Address;
  14. use App\Servers\Icon\BanRPC;
  15. use App\Servers\Icon\Utils;
  16. use App\Servers\PassServer;
  17. use App\Servers\ShopServer;
  18. use App\Servers\SmsServer;
  19. use App\Servers\TronAnalyzeServer;
  20. use App\Servers\TxMapServer;
  21. use App\Servers\WeixinServer;
  22. use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress;
  23. use BitWasp\Bitcoin\Crypto\Random\Random;
  24. use BitWasp\Bitcoin\Key\Factory\HierarchicalKeyFactory;
  25. use BitWasp\Bitcoin\Mnemonic\Bip39\Bip39Mnemonic;
  26. use BitWasp\Bitcoin\Mnemonic\Bip39\Bip39SeedGenerator;
  27. use BitWasp\Bitcoin\Mnemonic\MnemonicFactory;
  28. use Illuminate\Contracts\Routing\ResponseFactory;
  29. use Illuminate\Http\Request;
  30. use Illuminate\Support\Facades\Storage;
  31. use Web3p\EthereumUtil\Util;
  32. class CommonController extends FrontController
  33. {
  34. public function __construct()
  35. {
  36. parent::__construct();
  37. }
  38. function index()
  39. {
  40. $bnbServer=BanRPC::creatServer();
  41. $from='0x6652548D0783b45FC6D7a92E70B15Cf933A3D775';
  42. $data=[
  43. 'from'=>$from,
  44. 'to'=>'0xB2568EFafA4895236bCf3e34B03856BC48B4E9Ec',
  45. //交易描述,合约为调用函数的方法与参数进行哈希
  46. 'data'=>'0x0',
  47. //转账金额
  48. 'value'=> Utils::dec2hex(Utils::fund2int(0.0001 )),
  49. 'chain_id'=>$bnbServer->chainId,
  50. ];
  51. //交易序列号
  52. $nonce = $bnbServer->getTransactionCount($from);
  53. $data['nonce'] = $nonce;
  54. dd($nonce);
  55. // $gas_price = $bnbServer->getGasPrice();
  56. // $data['gas_price'] =$gas_price;
  57. // $gas_limit =BroadcastServer::creatServer()->getEthGasLimit($data);
  58. // $data['gas_limit'] = $gas_limit;
  59. // $sign =BroadcastServer::creatServer()->ethSign($data,"c1c2169a2cd2b82a6ca4e3ab93d04fb02be23f57ee0ee12c3a4875eec1521276");
  60. // $hash = $bnbServer->sendRawTransaction($sign, $error);//广播交易
  61. // $receipt = $bnbServer->getTransactionReceipt("0xcd1b28551be1a770908dc0c2bacea12ac9fc3b5fb6ae25cf1d23329bba8fd799");
  62. $receipt = $bnbServer->sendBlockByNumber(43679869);
  63. dd($receipt);
  64. //0x6652548D0783b45FC6D7a92E70B15Cf933A3D775
  65. //0xb2568efafa4895236bcf3e34b03856bc48b4e9ec
  66. // $bnb_block = BanRPC::creatServer()->sendBlockNumber();
  67. // dd($bnb_block);
  68. // $block_deal = BanRPC::creatServer()->sendBlockByNumber(43679463);
  69. // dd($block_deal);
  70. // $deal = BanRPC::creatServer()->getTransactionReceipt("0xd520c1b14e4d49064612c92647e2338547e5864fe1401972e356a658d4aa0972");//交易查询
  71. // $deal = BanRPC::creatServer()->getGasPrice();//交易查询
  72. // dd($deal);
  73. // $data=Address::generate();
  74. // dd($data);
  75. // $random = new Random();
  76. // // 生成随机数(initial entropy)
  77. // $entropy = $random->bytes(Bip39Mnemonic::MIN_ENTROPY_BYTE_LEN);
  78. // $bip39 = MnemonicFactory::bip39();
  79. // // 通过随机数生成助记词
  80. // $mnemonic = $bip39->entropyToMnemonic($entropy);
  81. $mnemonic = "poem decide already flush ramp unhappy deputy fiscal refuse history step grape";
  82. // dump($mnemonic);
  83. //
  84. $seedGenerator = new Bip39SeedGenerator();
  85. // // 通过助记词生成种子,传入可选加密串'hello'
  86. $seed = $seedGenerator->getSeed($mnemonic);
  87. dump( "seed: " .$seed->getHex());
  88. $hdFactory = new HierarchicalKeyFactory();
  89. $master = $hdFactory->fromEntropy($seed);
  90. //
  91. $hardened = $master->derivePath("44'/60'/0'/0/0");
  92. dump( 'WIF: ' . $hardened->getPrivateKey()->toWif());
  93. $address = new PayToPubKeyHashAddress($hardened->getPublicKey()->getPubKeyHash());
  94. dump( 'address: ' . $address->getAddress());
  95. dump( " public key: " . $hardened->getPublicKey()->getHex());
  96. dump( " private key: " . $hardened->getPrivateKey()->getHex());
  97. $util = new Util();
  98. dump( " address: " . $util->publicKeyToAddress($util->privateKeyToPublicKey($hardened->getPrivateKey()->getHex())));
  99. // $contract=Contract::where('id',1)->first();
  100. // $memberTeam=MemberClan::where('m_id',$contract->{'m_id'})->select(['id','m_id','p_ids','one_m_id','two_m_id'])->first();
  101. // $pIds=array_reverse(array_filter(explode(',',$memberTeam->{'p_ids'})));
  102. // ContractServer::creatServer()->teamProportion($contract,$pIds);
  103. dd(1221);
  104. // $data=Address::generate();
  105. // $key='1212';
  106. // $key=$data['key'];
  107. // dump($key);
  108. // $key=PassServer::creatServer()->setSecretKey($key);
  109. // dump($key);
  110. // dump($data['key']);
  111. // dump(PassServer::creatServer()->getSecretKey($key));
  112. // dd($data);
  113. }
  114. /**
  115. * 获取城市列表
  116. * @return \Illuminate\Http\JsonResponse
  117. */
  118. function city()
  119. {
  120. $p_id = request()->input('p_id', 0);
  121. $list = Region::where('p_id', $p_id)->select(['id', 'name'])->get();
  122. return $this->apiResponseSuccess('获取数据成功', $list);
  123. }
  124. /**
  125. * 发送短信
  126. * @return \Illuminate\Http\JsonResponse
  127. * @throws \End01here\EasySms\Exceptions\CodeErrorException
  128. * @throws \End01here\EasySms\Exceptions\GatewayErrorException
  129. * @throws \End01here\EasySms\Exceptions\MessageException
  130. */
  131. function sendSms()
  132. {
  133. $phone = request()->input('phone', '');
  134. $send_type = request()->input('send_type', 'default');
  135. if (empty($phone)) {
  136. return $this->apiResponseError('请输入手机号码');
  137. }
  138. $send_server = SmsServer::creatServer();
  139. $ret = $send_server->sendCode($phone, $send_type);
  140. if (!$ret) {
  141. return $this->apiResponseError($send_server->getErrorMsg() ?: '短信发送失败');
  142. } else {
  143. return $this->apiResponseSuccess('短信发送成功');
  144. }
  145. }
  146. /**
  147. * base64 图片上传
  148. * @param Request $request
  149. * @return \Illuminate\Http\JsonResponse
  150. */
  151. public function base64Image(Request $request)
  152. {
  153. $result = $this->base64ToImage($request->input('image', ""), 'attest');
  154. if ($result['code'] === false) {
  155. return $this->apiResponseError('图片上传失败');
  156. } else {
  157. if ($result) {
  158. return $this->apiResponseSuccess('图片上传成功', ['url' => $result['url']]);
  159. } else {
  160. return $this->apiResponseError('图片上传失败');
  161. }
  162. }
  163. }
  164. // base64图片保存
  165. protected function base64ToImage($base64ImageContent, $path)
  166. {
  167. $base64_image_content = $base64ImageContent;
  168. if (!empty($base64_image_content)) {
  169. $upload_path = "storage" . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . date('Ymd', time()) . DIRECTORY_SEPARATOR;
  170. $img_url = md5(uniqid() . time()) . '.jpg';
  171. $upload_path1 = public_path($upload_path);
  172. if (!is_dir($upload_path1)) {
  173. mkdir($upload_path1, 0777, true);
  174. }
  175. try {
  176. $base64_image_content = str_replace('[removed]', '', $base64_image_content);
  177. preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result);
  178. // oss 上传
  179. $oss = Storage::disk('oss');
  180. $pathA = "{$upload_path}{$img_url}";
  181. if (empty($result[1])) {
  182. $update = $oss->put($pathA, base64_decode($base64_image_content));
  183. if (!$update) { // 如果失败上传本地
  184. file_put_contents($upload_path1 . $img_url, base64_decode($base64_image_content));
  185. }
  186. } else {
  187. $update = $oss->put($pathA, base64_decode(str_replace($result[1], '', $base64_image_content)));
  188. if (!$update) { // 如果失败上传本地
  189. file_put_contents($upload_path1 . $img_url, base64_decode(str_replace($result[1], '', $base64_image_content)));
  190. }
  191. }
  192. return [
  193. 'code' => true,
  194. 'url' => $update ? ($oss->url($pathA) . '?x-oss-process=image/auto-orient,1/quality,q_90') : asset($pathA),
  195. ];
  196. } catch (\Exception $e) {
  197. return [
  198. 'code' => false,
  199. 'msg' => "图片错误" . $e->getMessage() . " " . $e->getLine(),
  200. ];
  201. }
  202. } else {
  203. return [
  204. 'code' => false,
  205. 'msg' => "请上传图片",
  206. ];
  207. }
  208. }
  209. /**
  210. * @param Request $request
  211. * @param ResponseFactory $response
  212. * @return mixed
  213. * 单图上传
  214. */
  215. public function fileImage()
  216. {
  217. if (request()->isMethod('post')) {
  218. if (request()->hasFile('file_image')) {
  219. // 文件后缀判断
  220. $ext = request()->file("file_image")->extension();
  221. if (!in_array($ext, ["jpg", "jpeg", "gif", "png", "bmp", "webp"])) {
  222. return $this->apiResponseError('上传图片仅支持png、jpg、jpeg、gif、bmp、webp后缀');
  223. }
  224. // 文件大小是否满足 2M = 2*1024*1024 B
  225. if (request()->file("file_image")->getClientSize() > 20971552) {
  226. return $this->apiResponseError('上传图片大小超过5M');
  227. }
  228. // 上传过程是否出错
  229. if (!request()->file("file_image")->isValid()) {
  230. return $this->apiResponseError('上传文件出错,请重试');
  231. }
  232. // 文件保存 生成一个唯一的文件名称
  233. $fileName = time() . str_random(4) . mt_rand(1000, 9999) . ".{$ext}";
  234. $pathA = 'image/' . date('y-m', time()) . '/' . $fileName;
  235. // oss 上传图片
  236. $oss = Storage::disk('oss');
  237. $update = $oss->put($pathA, file_get_contents(request()->{'file_image'}->getRealPath()));
  238. if ($update) {
  239. return $this->apiResponseSuccess('图片上传成功', ['url' => $oss->url($pathA)]);
  240. } else {
  241. return $this->apiResponseError('图片上传保存失败,请重试');
  242. }
  243. } else {
  244. return $this->apiResponseError('请上传图片文件');
  245. }
  246. }
  247. }
  248. /**
  249. * 获取JS签名信息
  250. * @return \Illuminate\Http\JsonResponse
  251. */
  252. function getJsConfig()
  253. {
  254. $url = request()->input('url');
  255. $ret = WeixinServer::creatServer()->getJsConfig($url);
  256. return $this->apiResponseSuccess('获取信息成功', $ret);
  257. }
  258. /**
  259. * 获取系统简介
  260. * @return \Illuminate\Http\JsonResponse
  261. */
  262. function getSysMessage()
  263. {
  264. $type = request()->input('type', '1');
  265. $v = Config::where('key', 'about_us')->value('value');
  266. return $this->apiResponseSuccess('获取信息成功', $v);
  267. }
  268. /**
  269. * 获取定位信息
  270. */
  271. function getLocation()
  272. {
  273. $ip = CommonServer::creatServer()->getClientIp();
  274. $location = TxMapServer::creatServer()->getLocation($ip);
  275. if ($location['status'] === 0) {
  276. $data = $location['result']['location'];
  277. $address = TxMapServer::creatServer()->getLocationAddress($data['lat'], $data['lng']);
  278. if ($address['status'] != 0) {
  279. return $this->apiResponseError('定位失败');
  280. }
  281. $data['addr'] = $address['result']['address'];
  282. return $this->apiResponseSuccess('获取信息成功', $data);
  283. } else {
  284. return $this->apiResponseError('定位错误');
  285. }
  286. }
  287. /**
  288. * 根据经纬度获取地址信息
  289. * @return \Illuminate\Http\JsonResponse
  290. */
  291. function getLocationAddress()
  292. {
  293. $data['lat'] = request()->input('lat');
  294. $data['lng'] = request()->input('lng');
  295. if (empty($data['lat']) || empty($data['lng'])) {
  296. return $this->apiResponseError('缺少必要参数');
  297. }
  298. $address = TxMapServer::creatServer()->getLocationAddress($data['lat'], $data['lng']);
  299. if ($address['status'] != 0) {
  300. return $this->apiResponseError('定位失败');
  301. }
  302. $data['addr'] = $address['result']['address'];
  303. return $this->apiResponseSuccess('获取信息成功', $data);
  304. }
  305. /**
  306. * 获取阿里云临时权限
  307. * @return \Illuminate\Http\JsonResponse
  308. */
  309. function getAliSts()
  310. {
  311. $ret = CommonServer::creatServer()->getAliSts();
  312. if (empty($ret['code'])) {
  313. return $this->apiResponseError($ret['msg']);
  314. } else {
  315. return $this->apiResponseSuccess($ret['msg'], $ret['data']);
  316. }
  317. }
  318. /**
  319. * 获取隐私协议
  320. * @return \Illuminate\Http\JsonResponse
  321. */
  322. function getPrivacyInfo()
  323. {
  324. $info = Config::where('key', 'privacy_protocol')->value('value');
  325. return $this->apiResponseSuccess('获取成功', $info);
  326. }
  327. }