CommonController.php 12 KB

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