CommonController.php 11 KB

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