CommonController.php 13 KB

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