Pay.php 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use blockchain\TronService;
  5. use blockchain\Web3Service;
  6. use fast\Random;
  7. use GuzzleHttp\Exception\RequestException;
  8. use think\Exception;
  9. use think\Validate;
  10. use think\Db;
  11. use app\common\model\MoneyLog;
  12. /**
  13. * 会员接口
  14. */
  15. class Pay extends Api
  16. {
  17. protected $noNeedLogin = ['notify', 'notifycz', 'notifydsf'];
  18. protected $noNeedRight = '*';
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->Usermodel = model('User');
  23. }
  24. public function index()
  25. {
  26. // // 启动事务
  27. // Db::startTrans();
  28. // try {
  29. if (input('id', 0) > 0) {
  30. $id = input('id');
  31. $data = Db::name('task')->where('id', $id)->find();
  32. if ($data) {
  33. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  34. Db::name('task')->where('id', $id)->update(['out_trade_no' => $number, 'buytype' => input('buytype')]);
  35. $data = Db::name('task')->where('id', $id)->find();
  36. } else {
  37. $this->error(__('订单数据不对'));
  38. }
  39. } else {
  40. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  41. if (!input('price')) {
  42. $this->error(__('金额不对'));
  43. }
  44. $datapost = input('param.');//订单数据
  45. $datapost['buzs'] = htmlspecialchars_decode($datapost['buzs']);
  46. if (input('endtime') == '数量完成自动结束') {
  47. $endtimesjc = 100000000000;
  48. } else {
  49. $endtimesjc = strtotime(input('endtime'));
  50. }
  51. $up = [
  52. 'type' => input('type'),
  53. 'uid' => input('uid'),
  54. 'name' => input('name'),
  55. 'rwurl' => input('rwurl'),
  56. 'buz' => $datapost['buzs'],
  57. 'price' => input('price'),
  58. 'sum' => input('sum'),
  59. 'sumsy' => input('sum'),
  60. 'endtime' => input('endtime'),
  61. 'subtime' => input('subtime'),
  62. 'shetime' => input('shetime'),
  63. 'endtimesjc' => $endtimesjc,
  64. 'buytype' => input('buytype'),
  65. 'zprice' => input('price') * input('sum'),
  66. 'out_trade_no' => $number,
  67. 'createtime' => time(),
  68. ];
  69. $id = Db::name('task')->insertGetId($up);
  70. $data = $up;
  71. }
  72. $site = config('site');
  73. $amount = $data['zprice'];
  74. if (!$amount) {
  75. $this->error(__('金额不对'));
  76. }
  77. // 异步通知地址
  78. $notify_url = $site['zfb']['notifyurl'];
  79. // 订单标题
  80. $subject = 'wool订单';
  81. // 订单详情
  82. $body = 'wool致力于打造最好的移动服务平台';
  83. // 订单号,示例代码使用时间值作为唯一的订单ID号
  84. if ($data['buytype'] == '支付宝') {
  85. $this->allpay($amount, $subject, $body, $number, $notify_url);
  86. } else if ($data['buytype'] == '余额') {
  87. $this->yuepay($amount, input('uid'), $id);
  88. }
  89. // // 提交事务
  90. // Db::commit();
  91. // } catch (\Exception $e) {
  92. // // 回滚事务
  93. // var_dump($e);
  94. // Db::rollback();
  95. // }
  96. }
  97. public function videopay()
  98. {
  99. $datavideo_order = Db::name('video_order')->where(['sid' => input('id'), 'uid' => input('uid'), 'lx' => 2])->find();
  100. $datavideo_order1 = Db::name('video_order')->where(['sid' => input('id'), 'uid' => input('uid'), 'lx' => 1])->find();
  101. $sad = 1;
  102. $lx = input('lx');
  103. if ($datavideo_order1) {
  104. if ($datavideo_order1['type'] == 2) {
  105. $sad = 2;
  106. $this->error(__('已经全集购买'));
  107. }
  108. }
  109. if ($datavideo_order and $lx == 2) {
  110. if ($datavideo_order['type'] == 2) {
  111. $this->error(__('已经单集购买'));
  112. }
  113. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  114. $price = input('priceq');
  115. $pricebuy = input('pricebuy');
  116. if (input('lx') == 2) {
  117. $price = input('priced');
  118. $pricebuy = input('pricebuy');
  119. }
  120. $up = [
  121. 'type' => 1,
  122. 'lx' => $lx,//1 全章购买 2单集购买
  123. 'uid' => input('uid'),
  124. 'name' => input('namet'),
  125. 'img' => input('img'),
  126. 'price' => $price,
  127. 'sum' => input('sum'),
  128. 'sid' => input('pid'),
  129. 'smid' => input('smid'),
  130. 'paytype' => input('buytype'),
  131. 'zprice' => $pricebuy * input('sum'),
  132. 'out_trade_no' => $number,
  133. ];
  134. Db::name('video_order')->where(['id' => $datavideo_order['id']])->update($up);
  135. $id = $datavideo_order['id'];
  136. $data = $up;
  137. } else {
  138. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  139. if (!input('pricebuy')) {
  140. $this->error(__('金额不对'));
  141. }
  142. $price = input('priceq');
  143. $pricebuy = input('pricebuy');
  144. if (input('lx') == 2) {
  145. $price = input('priced');
  146. $pricebuy = input('pricebuy');
  147. }
  148. $up = [
  149. 'type' => 1,
  150. 'lx' => $lx,//1 全章购买 2单集购买
  151. 'uid' => input('uid'),
  152. 'name' => input('namet'),
  153. 'img' => input('img'),
  154. 'price' => $price,
  155. 'sum' => input('sum'),
  156. 'sid' => input('pid'),
  157. 'smid' => input('smid'),
  158. 'paytype' => input('buytype'),
  159. 'zprice' => $pricebuy * input('sum'),
  160. 'out_trade_no' => $number,
  161. 'createtime' => time(),
  162. 'updatetime' => time(),
  163. ];
  164. //$this->error($up);
  165. $id = Db::name('video_order')->insertGetId($up);
  166. $data = $up;
  167. }
  168. $site = config('site');
  169. $amount = $data['zprice'];
  170. if (!$amount) {
  171. $this->error(__('金额不对'));
  172. }
  173. // 异步通知地址
  174. $notify_url = $site['zfb']['notifyurl'];
  175. // 订单标题
  176. $subject = 'wool订单';
  177. // 订单详情
  178. $body = 'wool致力于打造最好的移动服务平台';
  179. // 订单号,示例代码使用时间值作为唯一的订单ID号
  180. //var_dump($data['paytype']);
  181. if ($data['paytype'] == '支付宝') {
  182. $this->allpay($amount, $subject, $body, $number, $notify_url);
  183. } else if ($data['paytype'] == '余额') {
  184. $this->videoyuepay($amount, input('uid'), $id);
  185. } else if ($data['paytype'] == '微信') {
  186. $wxlx = input('wxlx');
  187. if ($wxlx == 'wxxcx') {
  188. $openid = input('openid');
  189. if (!$openid) {
  190. $this->error(__('openid不对'));
  191. }
  192. $this->paywxxcx($id, $number, $amount, $openid);
  193. } else if ($wxlx == 'wxh5') {
  194. $buytype = '微信h5';
  195. $this->paywxh5($id, $number, $amount, $buytype);
  196. } else if ($wxlx == 'wxgzh') {
  197. $buytype = '微信公众号';
  198. $this->paywxh5($id, $number, $amount, $buytype);
  199. }
  200. } else if ($data['paytype'] == '汇聚') {
  201. $paid_price = 0;
  202. // if ($recharId) {
  203. // $data = SystemGroupData::getDateValue($recharId);
  204. // if ($data === false) {
  205. // return app('json')->fail('您选择的充值方式已下架!');
  206. // } else {
  207. // $paid_price = $data['give_money'] ?? 0;
  208. // }
  209. // }
  210. // $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'huifu', $paid_price);
  211. $recharge = Huifu::wxJsPay(session('openid'), $number, $price, $paid_price, 'video_pay');
  212. // return app('json')->successful(['type' => $from, 'data' => $recharge]);
  213. }
  214. }
  215. public function paywxh5($oid, $number, $amount, $buytype)
  216. {
  217. $site = config('site');
  218. $user = $this->auth->getUser();
  219. if (!$user) {
  220. $this->error(__('user不对'));
  221. }
  222. if ($amount <= 0) {
  223. $this->error(__('金额不对'));
  224. }
  225. $up = [
  226. 'type' => 1,
  227. 'uid' => $user['id'],
  228. 'tdnum' => input('tdnum'),//支付通道
  229. 'amount' => $amount,
  230. 'out_trade_no' => $number,
  231. 'createtime' => time(),
  232. 'buytype' => $buytype,
  233. 'oid' => $oid,
  234. ];
  235. $id = Db::name('paylog')->insertGetId($up);
  236. $urls = $site['imgurl'] . '/index/paydsf/wxh5?out_trade_no=' . $number;
  237. if ($buytype == '微信公众号') {
  238. $urls = $site['imgurl'] . '/index/index/pay?id=' . $id;
  239. }
  240. $this->success('订单提交成功 正在跳转支付', $urls);
  241. }
  242. public function paywxxcx($oid, $number, $amount, $openid)
  243. {
  244. $site = config('site');
  245. $user = $this->auth->getUser();
  246. if (!$user) {
  247. $this->error(__('user不对'));
  248. }
  249. if ($amount <= 0) {
  250. $this->error(__('金额不对'));
  251. }
  252. $up = [
  253. 'type' => 1,
  254. 'uid' => $user['id'],
  255. 'amount' => $amount,
  256. 'out_trade_no' => $number,
  257. 'createtime' => time(),
  258. 'buytype' => '微信小程序',
  259. 'oid' => $oid,
  260. ];
  261. $id = Db::name('paylog')->insertGetId($up);
  262. $modelxcx = new \app\api\controller\Paywx;
  263. $trs = $modelxcx->wxxcx($number, $amount, $openid);
  264. $this->success('提交成功', $trs);
  265. }
  266. public function videoyuepay($money, $user_id, $id)//余额支付
  267. {
  268. $user = $this->Usermodel::get($user_id);
  269. if ($user && $money != 0) {
  270. $data = Db::name('video_order')->where('id', $id)->find();
  271. if ($user->money < $money) {
  272. $this->error(__('余额不足'));
  273. }
  274. $before = $user->money;
  275. $after = $user->money - $money;
  276. if ($data['type'] == 1) {
  277. Db::startTrans();
  278. try {
  279. //更新会员信息
  280. $user->save(['money' => $after]);
  281. //写入日志
  282. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => '用户消费']);
  283. Db::name('video_order')->where('id', $id)->update(['type' => 2, 'paytype' => '余额', 'ftime' => time()]);
  284. $this->yongjin($id, $user_id, $money, '级消费佣金结算');//佣金结算
  285. $this->dailiyongjin($id, $data['sid'], $data['smid'], $user_id, $money, '佣金结算');//后台代理佣金结算
  286. Db::commit();
  287. $this->success(__('操作成功'));
  288. } catch (Exception $e) {
  289. Db::rollback();
  290. $this->error($e->getMessage());
  291. }
  292. } else if ($data['type'] > 1) {
  293. $this->error(__('已经支付'));
  294. }
  295. } else {
  296. $this->error(__('金额不对'));
  297. }
  298. }
  299. public function yuepay($money, $user_id, $id)//余额支付
  300. {
  301. $user = $this->Usermodel::get($user_id);
  302. if ($user && $money != 0) {
  303. $data = Db::name('task')->where('id', $id)->find();
  304. if ($user->money < $money) {
  305. $this->error(__('余额不足'));
  306. }
  307. $before = $user->money;
  308. $after = $user->money - $money;
  309. //更新会员信息
  310. $user->save(['money' => $after]);
  311. //写入日志
  312. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => '用户消费']);
  313. if ($data['paytype'] == 1) {
  314. $up = [
  315. 'paytype' => 2,
  316. 'buytype' => '余额',
  317. 'ftime' => time()
  318. ];
  319. if (Db::name('task')->where('id', $id)->update($up)) {
  320. $this->success(__('操作成功'));
  321. }
  322. } else if ($data['paytype'] > 1) {
  323. $this->error(__('已经支付'));
  324. }
  325. } else {
  326. $this->error(__('金额不对'));
  327. }
  328. }
  329. public function czpay()
  330. {
  331. $site = config('site');
  332. $user = $this->auth->getUser();
  333. if (!$user) {
  334. $this->error(__('user不对'));
  335. }
  336. $number = date('ymdh', time()) . rand(10000, 99999);//订单编
  337. $amount = input('total', 0);
  338. $notify_url = $site['zfb']['notifyurlcz'];
  339. // 订单标题
  340. $subject = 'wool充值';
  341. // 订单详情
  342. $body = 'wool致力于打造最好的移动服务平台';
  343. if ($amount <= 0) {
  344. $this->error(__('金额不对'));
  345. }
  346. $up = [
  347. 'type' => 1,
  348. 'uid' => $user['id'],
  349. 'amount' => input('total'),
  350. 'out_trade_no' => $number,
  351. 'createtime' => time(),
  352. ];
  353. $id = Db::name('paylog')->insertGetId($up);
  354. if (input('type', 'allpay') == 'iap') {
  355. $this->success('请求成功', $number);
  356. } else {
  357. $this->allpay($amount, $subject, $body, $number, $notify_url);
  358. }
  359. }
  360. public function dsfczpay()
  361. {
  362. $site = config('site');
  363. $user = $this->auth->getUser();
  364. if (!$user) {
  365. $this->error(__('user不对'));
  366. }
  367. $number = date('ymdh', time()) . rand(10000, 99999);//订单编
  368. $amount = input('total', 0);
  369. if ($amount <= 0) {
  370. $this->error(__('金额不对'));
  371. }
  372. $up = [
  373. 'type' => 1,
  374. 'uid' => $user['id'],
  375. 'tdnum' => input('tdnum'),
  376. 'amount' => $amount,
  377. 'out_trade_no' => $number,
  378. 'createtime' => time(),
  379. 'buytype' => isset($site['dsfzf']['zfname']) ? $site['dsfzf']['zfname'] : '',
  380. ];
  381. $id = Db::name('paylog')->insertGetId($up);
  382. $urls = $site['imgurl'] . '/index/paydsf?out_trade_no=' . $number;
  383. $this->success('订单提交成功 正在跳转支付', $urls);
  384. }
  385. public function czpayiaptz()
  386. {
  387. $out_trade_no = input('out_trade_no');
  388. $order = Db::name('paylog')->where('out_trade_no', $out_trade_no)->find();
  389. //$this->dingding_log($order);
  390. if ($order['type'] == 1) {
  391. $up = [
  392. 'type' => 2,
  393. 'payment' => null,
  394. 'trade_no' => null,
  395. 'buyer' => null,
  396. 'buytype' => '苹果',
  397. 'ftime' => time()
  398. ];
  399. Db::startTrans();
  400. try {
  401. $this->money($order['amount'], $order['uid'], 0, $sxf = 0);
  402. Db::name('paylog')->where('out_trade_no', $out_trade_no)->update($up);
  403. Db::commit();
  404. $this->success('支付成功');
  405. } catch (Exception $e) {
  406. Db::rollback();
  407. $this->error($e->getMessage());
  408. }
  409. } else {
  410. $this->success('已经支付');
  411. }
  412. }
  413. public function allpay($amount, $subject, $body, $out_trade_no, $notify_url)//支付宝支付
  414. {
  415. $total = floatval($amount);
  416. if (!$total) {
  417. $this->error(__('金额不对'));
  418. }
  419. //require_once('/vendor/alipayrsa2/AopSdk.php');
  420. //Vendor('AopSdk.alipayrsa2');
  421. // require_once './vendor/alipayrsa2/aop/AopClient.php';
  422. // require_once './vendor/alipayrsa2/aop/request/AlipayTradeAppPayRequest.php';
  423. vendor('alipayrsa2.AopSdk');
  424. $site = config('site');
  425. $aop = new \AopClient;
  426. $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
  427. $aop->appId = $site['zfb']['appId'];
  428. //开发者私钥去头去尾去回车,一行字符串
  429. $aop->rsaPrivateKey = $site['zfb']['rsaPrivateKey'];
  430. $aop->format = "json";
  431. $aop->charset = "UTF-8";
  432. $aop->signType = "RSA2";
  433. //请填写支付宝公钥,一行字符串
  434. $aop->alipayrsaPublicKey = $site['zfb']['alipayrsaPublicKey'];
  435. //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
  436. $request = new \AlipayTradeAppPayRequest();
  437. //SDK已经封装掉了公共参数,这里只需要传入业务参数
  438. $bizcontent = "{\"body\":\"" . $body . "\","
  439. . "\"subject\": \"" . $subject . "\","
  440. . "\"out_trade_no\": \"" . $out_trade_no . "\","
  441. . "\"timeout_express\": \"30m\","
  442. . "\"total_amount\": \"" . $total . "\","
  443. . "\"product_code\":\"QUICK_MSECURITY_PAY\""
  444. . "}";
  445. $request->setNotifyUrl($notify_url);
  446. $request->setBizContent($bizcontent);
  447. //这里和普通的接口调用不同,使用的是sdkExecute
  448. $response = $aop->sdkExecute($request);
  449. // 注意:这里不需要使用htmlspecialchars进行转义,直接返回即可
  450. //$this->success(__('获取成功'),$response);
  451. echo $response;
  452. }
  453. //支付宝回调
  454. public function notify()
  455. {
  456. if ($_POST) {
  457. //Db::transaction(function () {
  458. if ($_POST['trade_status'] == 'TRADE_SUCCESS' or $_POST['trade_status'] == 'TRADE_FINISHED') {//处理交易完成或者支付成功的通知 //获取订单号
  459. $out_trade_no = $_POST['out_trade_no'];//交易号
  460. $trade_no = $_POST['trade_no'];//订单支付时间
  461. $gmt_payment = $_POST['gmt_payment'];//转换为时间戳
  462. $order = Db::name('task')->where('out_trade_no', $out_trade_no)->find();
  463. if ($order['paytype'] == 1) {
  464. $up = [
  465. 'paytype' => 2,
  466. 'payment' => $gmt_payment,
  467. 'trade_no' => $trade_no,
  468. 'buyer' => $_POST['buyer_logon_id'],
  469. 'buytype' => '支付宝',
  470. 'ftime' => time()
  471. ];
  472. if (Db::name('task')->where('out_trade_no', $out_trade_no)->update($up)) {
  473. echo 'success';
  474. }
  475. } else {
  476. echo 'success';
  477. }
  478. }
  479. //});
  480. }
  481. }
  482. public function notifycz()
  483. {
  484. if ($_POST) {
  485. //$this->dingding_log($_POST);
  486. //Db::transaction(function () {
  487. if ($_POST['trade_status'] == 'TRADE_SUCCESS' or $_POST['trade_status'] == 'TRADE_FINISHED') {//处理交易完成或者支付成功的通知 //获取订单号
  488. $out_trade_no = $_POST['out_trade_no'];//交易号
  489. $trade_no = $_POST['trade_no'];//订单支付时间
  490. $gmt_payment = $_POST['gmt_payment'];//转换为时间戳
  491. $order = Db::name('paylog')->where('out_trade_no', $out_trade_no)->find();
  492. //$this->dingding_log($order);
  493. if ($order['type'] == 1) {
  494. $up = [
  495. 'type' => 2,
  496. 'payment' => $gmt_payment,
  497. 'trade_no' => $trade_no,
  498. 'buyer' => $_POST['buyer_logon_id'],
  499. 'buytype' => '支付宝',
  500. 'ftime' => time()
  501. ];
  502. Db::startTrans();
  503. try {
  504. $this->money($order['amount'], $order['uid'], 0, $sxf = 0);
  505. Db::name('paylog')->where('out_trade_no', $out_trade_no)->update($up);
  506. Db::commit();
  507. echo 'success';
  508. } catch (Exception $e) {
  509. Db::rollback();
  510. $this->error($e->getMessage());
  511. }
  512. } else {
  513. echo 'success';
  514. }
  515. }
  516. //});
  517. }
  518. }
  519. public function notifydsf()//第三方充值回调
  520. {
  521. $this->dingding_log($_REQUEST);
  522. //Db::transaction(function () {
  523. $returncode = isset($_REQUEST["returncode"]) ? $_REQUEST["returncode"] : 1;
  524. if ($returncode == "00") {//处理交易完成或者支付成功的通知 //获取订单号
  525. $out_trade_no = isset($_REQUEST["orderid"]) ? $_REQUEST["orderid"] : '';
  526. $order = Db::name('paylog')->where('out_trade_no', $out_trade_no)->find();
  527. $this->dingding_log($order);
  528. if ($order['type'] == 1) {
  529. $up = [
  530. 'type' => 2,
  531. 'ftime' => time()
  532. ];
  533. Db::startTrans();
  534. try {
  535. $this->money($order['amount'], $order['uid'], 0, $sxf = 0);
  536. Db::name('paylog')->where('out_trade_no', $out_trade_no)->update($up);
  537. Db::commit();
  538. echo 'OK';
  539. } catch (Exception $e) {
  540. Db::rollback();
  541. $this->error($e->getMessage());
  542. }
  543. } else {
  544. echo 'OK';
  545. }
  546. }
  547. //});
  548. }
  549. public function money($money, $user_id, $id, $sxf = 0)
  550. {
  551. $user = $this->Usermodel::get($user_id);
  552. if ($user && $money != 0) {
  553. $before = $user->money;
  554. $after = $user->money + $money;
  555. //更新会员信息
  556. $user->save(['money' => $after]);
  557. //写入日志
  558. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'fid' => $id, 'sxf' => $sxf, 'memo' => '用户充值']);
  559. } else {
  560. $this->error(__('金额不对'));
  561. }
  562. }
  563. public function dingding_log($content)
  564. {
  565. $r = $_SERVER['DOCUMENT_ROOT'] . '/api_log/' . date('Y-m-d_H-i-s', time()) . '.txt';
  566. $fp = fopen($r, 'w+');
  567. fwrite($fp, "执行日期:" . date('Y-m-d H:i:s', time()) . ' ' . var_export($content, true));
  568. fclose($fp);
  569. }
  570. public function FromXml($xml)
  571. {
  572. //将XML转为array
  573. //禁止引用外部xml实体
  574. libxml_disable_entity_loader(true);
  575. $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  576. return $values;
  577. }
  578. public function sharepay()
  579. {
  580. // $datavideo_order=Db::name('video_share')->where(['vid'=>input('id'),'uid'=>input('uid')])->find();
  581. // $datavideo_order1=Db::name('video_share')->where(['sid'=>input('id'),'uid'=>input('uid'),'lx'=>1])->find();
  582. // $lx=input('lx');
  583. $num = input('num'); //购买的版权数量
  584. $uid = input('uid');
  585. $vid = input('vid');
  586. $user = Db::name('user')->where('id', $uid)->find();
  587. if (empty($user)){
  588. $this->error(__('用户不存在'));
  589. }
  590. if (empty($num)){
  591. $this->error(__('请输入数量'));
  592. }
  593. // 验证数量
  594. $user_num = Db::name('video_user')->where('uid', $uid)->where('vid', $vid)->find();
  595. if ($user_num) {
  596. $cs_num = $user_num['share'] + $num;
  597. if ($cs_num > 10 || $num > 10) {
  598. $this->error(__('单剧集购买版权不能超过10份'));
  599. }
  600. }
  601. $user_share = Db::name('videolist')->where('id', $vid)->value('share');
  602. if (empty($user_share)){
  603. $this->error(__('短剧不存在'));
  604. }
  605. $user_shares = bcsub($user_share , $num, 0);
  606. if ($user_shares < 0) {
  607. $this->error(__('该局总版权只剩下' . $user_share . '份'));
  608. }
  609. // if($datavideo_order){
  610. // if($datavideo_order['type']==2){
  611. // $this->error(__('该订单已支付'));
  612. // }
  613. // $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  614. // $price=input('priceq');
  615. // $pricebuy=input('pricebuy');
  616. // if(input('lx')==2){
  617. // $price=input('priced');
  618. // $pricebuy=input('pricebuy');
  619. // }
  620. // $up=[
  621. // 'type'=>1,
  622. // 'lx'=>$lx,//1 全章购买 2单集购买
  623. // 'uid'=>input('uid'),
  624. // 'name'=>input('namet'),
  625. // 'img'=>input('img'),
  626. // 'price'=>$price,
  627. // 'sum'=>input('sum'),
  628. // 'sid'=>input('pid'),
  629. // 'smid'=>input('smid'),
  630. // 'paytype'=>input('buytype'),
  631. // 'zprice'=>$pricebuy*input('sum'),
  632. // 'out_trade_no'=>$number,
  633. // ];
  634. // Db::name('video_order')->where(['id'=>$datavideo_order['id']])->update($up);
  635. // $id=$datavideo_order['id'];
  636. // $data=$up;
  637. // }else{
  638. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  639. // if (!input('pricebuy')) {
  640. // $this->error(__('金额不对'));
  641. // }
  642. // $price = input('price');
  643. $price = Db::name('videolist')->where('id', input('vid'))->value('sharesprice');;
  644. // $pricebuy = input('pricebuy');
  645. // if(input('lx')==2){
  646. // $price=input('priced');
  647. // $pricebuy=input('pricebuy');
  648. // }
  649. // 获取当前时间的时间戳
  650. $now = time();
  651. // 计算三年后的时间戳
  652. $threeYearsLater = strtotime('+3 years', $now);
  653. $vname = Db::name('videolist')->where('id', input('vid'))->value('name');
  654. $total_price = $price * $num;
  655. $up = [
  656. 'type' => 1,
  657. 'uid' => input('uid'),
  658. 'vid' => input('vid'),
  659. 'vname' => $vname,
  660. 'num' => input('num'),
  661. 'price' => $price,
  662. 'total_price' => $total_price,
  663. 'paytype' => input('buytype'),
  664. 'out_trade_no' => $number,
  665. 'createtime' => time(),
  666. 'endtime' => $threeYearsLater,
  667. 'updatetime' => time(),
  668. ];
  669. //$this->error($up);
  670. $id = Db::name('video_share')->insertGetId($up);
  671. $data = $up;
  672. // }
  673. $site = config('site');
  674. $amount = $price;
  675. // $amount = $data['zprice'];
  676. // if (!$amount) {
  677. // $this->error(__('金额不对'));
  678. // }
  679. // 异步通知地址
  680. $notify_url = $site['zfb']['notifyurl'];
  681. // 订单标题
  682. $subject = 'wool订单';
  683. // 订单详情
  684. $body = 'wool致力于打造最好的移动服务平台';
  685. // 订单号,示例代码使用时间值作为唯一的订单ID号
  686. //var_dump($data['paytype']);
  687. if ($data['paytype'] == '支付宝') {
  688. $this->allpay($amount, $subject, $body, $number, $notify_url);
  689. } else if ($data['paytype'] == '余额') {
  690. $this->shareyuepay($amount, input('uid'), $id);
  691. } else if ($data['paytype'] == '微信') {
  692. $wxlx = input('wxlx');
  693. if ($wxlx == 'wxxcx') {
  694. $openid = input('openid');
  695. if (!$openid) {
  696. $this->error(__('openid不对'));
  697. }
  698. $this->paywxxcx($id, $number, $amount, $openid);
  699. } else if ($wxlx == 'wxh5') {
  700. $buytype = '微信h5';
  701. $this->paywxh5($id, $number, $amount, $buytype);
  702. } else if ($wxlx == 'wxgzh') {
  703. $buytype = '微信公众号';
  704. $this->paywxh5($id, $number, $amount, $buytype);
  705. } else if ($data['paytype'] == '汇聚') {
  706. $paid_price = 0;
  707. // if ($recharId) {
  708. // $data = SystemGroupData::getDateValue($recharId);
  709. // if ($data === false) {
  710. // return app('json')->fail('您选择的充值方式已下架!');
  711. // } else {
  712. // $paid_price = $data['give_money'] ?? 0;
  713. // }
  714. // }
  715. // $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'huifu', $paid_price);
  716. $recharge = Huifu::wxJsPay(session('openid'), $number, $price, $paid_price, 'share_pay');
  717. // return app('json')->successful(['type' => $from, 'data' => $recharge]);
  718. }
  719. }
  720. }
  721. // 判断订单的用户是否有上级以及判断推荐等级
  722. public function grade_sell($out_trade_no)
  723. {
  724. $order = Db::name('video_share')->where('out_trade_no', $out_trade_no)->find();
  725. $site = config('site');
  726. if ($order) {
  727. $user = Db::name('user')->where('id', $order['uid'])->find();
  728. while ($user['pid'] != 1) { //默认账号pid为1
  729. $user = Db::name('user')->where('id', $user['pid'])->find(); //上级用户信息
  730. $grade = $user['grade'];
  731. $video_user = Db::name('video_user')->where('uid', $user['id'])->where('vid', $order['vid'])->find();
  732. if (empty($video_user)) { //如果上级没有购买过该视频版权
  733. $up = [
  734. 'vid' => $order['vid'],
  735. 'vname' => $order['vname'],
  736. 'uid' => $user['id'],
  737. 'share' => 0,
  738. 'contribution' => 0,
  739. 'income' => 0,
  740. 'createtime' => time(),
  741. ];
  742. $id = Db::name('VideoUser')->insertGetId($up); //添加用户版权
  743. }
  744. $contribution = 0;
  745. $check = 0;
  746. // $sell = $video_user['sell'] + $order['num'];
  747. $sell = $user['sell'] + $order['num'];
  748. // switch ($video_user['grade']) {
  749. switch ($user['grade']) {
  750. case 0:
  751. $contribution = $site['gxz']['普通会员'] * $order['num'];
  752. if ($sell >= 30) {
  753. $check = 1;
  754. $sell = $sell - 30;
  755. }
  756. break;
  757. case 1:
  758. $contribution = $site['gxz']['初级合伙人'] * $order['num'];
  759. if ($sell >= 300) {
  760. $check = 1;
  761. $sell = $sell - 300;
  762. }
  763. break;
  764. case 2:
  765. $contribution = $site['gxz']['高级合伙人'] * $order['num'];
  766. if ($sell >= 3000) {
  767. $check = 1;
  768. $sell = $sell - 3000;
  769. }
  770. break;
  771. case 3:
  772. $contribution = $site['gxz']['董事'] * $order['num'];
  773. default:
  774. }
  775. if ($check == 1) {
  776. // $grade = $video_user['grade'] + 1;
  777. $grade = $user['grade'] + 1;
  778. }
  779. $after = $video_user['contribution'] + $contribution;
  780. // $res = Db::name('video_user')->where('uid', $user['id'])->where('vid', $order['vid'])->update(['sell' => $sell, 'contribution' => $after, 'grade' => $grade]);
  781. $res = Db::name('video_user')->where('uid', $user['id'])->where('vid', $order['vid'])->update(['contribution' => $after]);
  782. $res2 = Db::name('user')->where('id', $user['id'])->update(['grade' => $grade, 'sell' => $sell]);
  783. $up = [
  784. 'vid' => $order['vid'],
  785. 'vname' => $order['vname'],
  786. 'uid' => $user['id'],
  787. 'link_id' => $order['id'],
  788. 'num' => $contribution,
  789. 'after' => $contribution,
  790. 'content' => '下级用户购买版权,获得贡献值',
  791. 'createtime' => time(),
  792. 'pm' => 0,
  793. ];
  794. $id = Db::name('VideoContributionRecord')->insertGetId($up);
  795. }
  796. }
  797. }
  798. // 添加贡献值记录 订单id
  799. public function share_record($id)
  800. {
  801. $order = Db::name('video_share')->where('id', $id)->find();
  802. if ($order) {
  803. $user = Db::name('video_user')->where('uid', $order['uid'])->where('vid', $order['vid'])->find();
  804. $contribution = $order['num'] + $user['contribution'];
  805. $up = [
  806. 'vid' => $order['vid'],
  807. 'vname' => $order['vname'],
  808. 'uid' => $user['id'],
  809. 'link_id' => $order['id'],
  810. 'num' => $order['num'],
  811. 'after' => $contribution,
  812. 'content' => '用户购买版权,获得贡献值',
  813. 'createtime' => time(),
  814. 'pm' => 0,
  815. ];
  816. $id = Db::name('VideoContributionRecord')->insertGetId($up);
  817. $share = $user['share'] + $order['num'];
  818. // 添加版权和贡献值
  819. $res = Db::name('video_user')->where('uid', $user['uid'])->where('vid', $order['vid'])->update(['contribution' => $contribution, 'share' => $share]);
  820. }
  821. }
  822. public function shareyuepay($money, $user_id, $id)//版权余额支付
  823. {
  824. $user = $this->Usermodel::get($user_id);
  825. if ($user) {
  826. $data = Db::name('video_share')->where('id', $id)->find();
  827. if ($user->money < $money) {
  828. $this->error(__('余额不足'));
  829. }
  830. $before = $user->money;
  831. $after = $user->money - $money;
  832. if ($data['type'] == 1) {
  833. Db::startTrans();
  834. try {
  835. //更新会员信息
  836. $user->save(['money' => $after]);
  837. //写入日志
  838. MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => '用户消费']);
  839. Db::name('video_share')->where('id', $id)->update(['type' => 2, 'paytype' => '余额', 'pay_time' => time()]);
  840. $video_user = Db::name('video_user')->where('uid', $user_id)->where('vid', $data['vid'])->find();
  841. if (empty($video_user)) { //判断用户是否购买过该视频版权
  842. $up = [
  843. 'vid' => $data['vid'],
  844. 'vname' => $data['vname'],
  845. 'uid' => $data['uid'],
  846. 'share' => $data['num'],
  847. 'contribution' => $data['num'],
  848. 'income' => 0,
  849. 'createtime' => time(),
  850. ];
  851. $id = Db::name('VideoUser')->insertGetId($up);
  852. }
  853. $this->share_record($id);//添加贡献值记录
  854. $this->yongjin($id, $user_id, $money, '级消费佣金结算');//佣金结算
  855. $this->dailiyongjin($id, $data['vid'], 0, $user_id, $money, '佣金结算');//后台代理佣金结算
  856. $this->grade_sell($data['out_trade_no']);//判断订单的用户是否有上级以及判断推荐等级)
  857. // 扣除短剧总版权
  858. $shares = Db::name('videolist')->where('id', $data['vid'])->value('share');
  859. $share = $shares - $data['num'];
  860. Db::name('videolist')->where('id', $data['vid'])->update(['share' => $share]);
  861. Db::commit();
  862. $this->success(__('操作成功'));
  863. } catch (Exception $e) {
  864. Db::rollback();
  865. $this->error($e->getMessage());
  866. }
  867. } else if ($data['type'] > 1) {
  868. $this->error(__('已经支付'));
  869. }
  870. } else {
  871. $this->error(__('用户不存在'));
  872. }
  873. }
  874. // u币充值(展示二维码)
  875. public function recharge()
  876. {
  877. $uid = input('uid');
  878. $recharge_type = input('type');
  879. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  880. $user = Db::name('user')->where('id', $uid)->find();
  881. if (empty($user)) {
  882. $this->error(__('用户不存在'));
  883. }
  884. // $data = [
  885. // 'order_id'=> $number,
  886. // 'uid' => $uid,
  887. // 'paid' => 0,
  888. // 'add_time' => time()
  889. // ];
  890. $return = [];
  891. $address = Db::name('user_usdt_address')->where('uid', $uid)->find();
  892. if ($recharge_type == 'trx') {
  893. // $data['recharge_type']='trx';
  894. $return['address'] = $address['trx_address'];
  895. $return['16_address'] = $address['trx_16_address'];
  896. } elseif ($recharge_type == 'bsc') {
  897. // $data['recharge_type']='bsc';
  898. $return['address'] = $address['bsc_address'];
  899. }
  900. // Db::name('user_recharge')->insert($data);
  901. Db::name('user_usdt_address')->where('uid', $uid)->update(['last_recharge' => time()]);
  902. $this->success($return);
  903. }
  904. // 查询u币转账记录
  905. public function search_recharge()
  906. {
  907. $time = strtotime("-1 hours", time());
  908. $user_address = Db::name('user_usdt_address')->where('last_recharge', '>', $time)->order('last_recharge desc')->select();
  909. foreach ($user_address as $k => $v) {
  910. $user_info = Db::name('user')->where('id', $v['uid'])->find();
  911. // trx部分
  912. $service = TronService::instance('usdt');
  913. $time =bcmul($v['last_time'], 1000, 0);
  914. $info = $service->getTransfer($v['trx_address'], $time);
  915. @file_put_contents("usdt.txt",json_encode($info). "-trx转账记录\r\n", 8);
  916. foreach ($info['data'] as $k2 => $v2){
  917. $transaction_id = $v2['transaction_id'];
  918. $value =bcdiv($v2['value'], 1000000, 0);
  919. $be = DB::name('user_recharge')->where('hash', $transaction_id)->find();
  920. if ($be) { //hash重复
  921. continue;
  922. }
  923. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  924. $data = [
  925. 'order_id' => $number,
  926. 'uid' => $v['uid'],
  927. 'paid' => 1,
  928. 'price' => $value,
  929. 'hash' => $transaction_id,
  930. 'recharge_type' => 'trx',
  931. 'pay_time' => time(),
  932. 'add_time' => time()
  933. ];
  934. Db::name('user_recharge')->insert($data);
  935. Db::name('user_usdt_address')->where('uid', $v['uid'])->update(['last_time' => time()]);
  936. $money = bcadd($user_info['money'], $value, 18);
  937. Db::name('user')->where('id', $v['uid'])->update(['money' => $money]);
  938. MoneyLog::create(['user_id' => $v['uid'], 'money' => $value, 'before' => $user_info['money'], 'after' => $money, 'memo' => '用户trx余额充值']);
  939. }
  940. // bsc部分
  941. $service = Web3Service::instance('bsc', 'usdt', '');
  942. $info = $service->getLastTransfer([], [$v['bsc_address']], $v['last_time']);
  943. @file_put_contents("usdt.txt",json_encode($info). "-bsc转账记录\r\n", 8);
  944. foreach ($info as $k2 => $v2) {
  945. $value = $v2->params['value']->toString(); //金额
  946. $value2 = bcdiv($value, bcpow(10, get_token_info('bsc', 'USDT', 'decimal')), 12);
  947. $transactionHash = $v2->transactionHash;
  948. $be = DB::name('user_recharge')->where('hash', $transactionHash)->find();
  949. if ($be) { //hash重复
  950. continue;
  951. }
  952. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  953. $data = [
  954. 'order_id' => $number,
  955. 'uid' => $v['uid'],
  956. 'paid' => 1,
  957. 'price' => $value2,
  958. 'hash' => $transactionHash,
  959. 'recharge_type' => 'bsc',
  960. 'pay_time' => time(),
  961. 'add_time' => time()
  962. ];
  963. Db::name('user_recharge')->insert($data);
  964. Db::name('user_usdt_address')->where('uid', $v['uid'])->update(['last_time' => time()]);
  965. $money = bcadd($user_info['money'], $value2, 18);
  966. Db::name('user')->where('id', $v['uid'])->update(['money' => $money]);
  967. MoneyLog::create(['user_id' => $v['uid'], 'money' => $value2, 'before' => $user_info['money'], 'after' => $money, 'memo' => '用户bsc余额充值']);
  968. }
  969. }
  970. $this->success('查询完成');
  971. }
  972. public function withdrawal(){
  973. $uid = input('uid');
  974. $address = input('address');
  975. $type = input('type');
  976. $img = input('img');
  977. $price = input('price');
  978. if (empty($address)&&empty($img)){
  979. $this->error(__('请上传地址或上传收款码'));
  980. }
  981. Db::name('txjl')->insert(['uid' => $uid, 'address' => $address, 'type' => $type, 'img' => $img,'money'=> $price,'iscl'=>1]);
  982. $this->success('提交成功');
  983. }
  984. public function test()
  985. {
  986. $base58_addr = "TFRpdAs4SP1uoWcq8Kkx4769QQnJJGyPUX";
  987. $hex =self::base58ToHex($base58_addr);
  988. var_dump($hex);
  989. // $site = config('site');
  990. // var_dump($site['gxz']['普通会员']);
  991. // var_dump($site['gxz']['初级合伙人']);
  992. // var_dump($site['gxz']['高级合伙人']);
  993. // var_dump($site['gxz']['董事']);
  994. die();
  995. $service = Web3Service::instance('bsc', 'usdt', '');
  996. $info = $service->getLastTransfer([], ['0xbfc3b2193653ebf94bfa3fe586627b9c01f26db9'], 1748293200);
  997. $user_info = Db::name('user')->where('id', 1571)->find();
  998. foreach ($info as $k2 => $v2) {
  999. $value = $v2->params['value']->toString(); //金额
  1000. $value2 = bcdiv($value, bcpow(10, get_token_info('bsc', 'TUSD', 'decimal')), 12);
  1001. $transactionHash = $v2->transactionHash;
  1002. $be = DB::name('user_recharge')->where('hash', $transactionHash)->find();
  1003. if ($be) { //hash重复
  1004. continue;
  1005. }
  1006. $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  1007. $data = [
  1008. 'order_id' => $number,
  1009. 'uid' => $user_info['id'],
  1010. 'paid' => 1,
  1011. 'price' => $value2,
  1012. 'hash' => $transactionHash,
  1013. 'recharge_type' => 'bsc',
  1014. 'pay_time' => time(),
  1015. 'add_time' => time()
  1016. ];
  1017. Db::name('user_recharge')->insert($data);
  1018. Db::name('user_usdt_address')->where('uid', $user_info['id'])->update(['last_time' => time()]);
  1019. $money = bcadd($user_info['money'], $value2, 18);
  1020. Db::name('user')->where('id', $user_info['id'])->update(['money' => $money]);
  1021. MoneyLog::create(['user_id' => $user_info['id'], 'money' => $value2, 'before' => $user_info['money'], 'after' => $money, 'memo' => '用户bsc余额充值']);
  1022. }
  1023. // foreach ($info as $k => $v) {
  1024. // $value = $v->params['value']->toString();
  1025. // var_dump($value);
  1026. // $transactionHash = $v->transactionHash;
  1027. // var_dump($transactionHash);
  1028. // $value = bcdiv($value, bcpow(10, get_token_info('bsc', 'TUSD', 'decimal')), 12); //把后面的0去掉
  1029. // var_dump($value);
  1030. // }
  1031. var_dump($info);
  1032. die();
  1033. // trx TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t
  1034. // $service = TronService::instance('usdt');
  1035. //
  1036. // $info = $service->getTransfer('TFRpdAs4SP1uoWcq8Kkx4769QQnJJGyPUX', 1748293200000);
  1037. // $user_info = Db::name('user')->where('id', 1571)->find();
  1038. // foreach ($info['data'] as $k2 => $v2){
  1039. // $transaction_id = $v2['transaction_id'];
  1040. // $value =bcdiv($v2['value'], 1000000, 0);
  1041. // $be = DB::name('user_recharge')->where('hash', $transaction_id)->find();
  1042. // if ($be) { //hash重复
  1043. // continue;
  1044. // }
  1045. // $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
  1046. // $data = [
  1047. // 'order_id' => $number,
  1048. // 'uid' => 1571,
  1049. // 'paid' => 1,
  1050. // 'price' => $value,
  1051. // 'hash' => $transaction_id,
  1052. // 'type' => 'trx',
  1053. // 'pay_time' => time(),
  1054. // 'add_time' => time()
  1055. // ];
  1056. // Db::name('user_recharge')->insert($data);
  1057. // Db::name('user_usdt_address')->where('uid', 1571)->update(['last_time' => time()]);
  1058. // $money = bcadd($user_info['money'], $value, 18);
  1059. // Db::name('user')->where('id', 1571)->update(['money' => $money]);
  1060. // MoneyLog::create(['user_id' => 1571, 'money' => $value, 'before' => $user_info['money'], 'after' => $money, 'memo' => '用户trx余额充值']);
  1061. //
  1062. // }
  1063. // var_dump($info);die();
  1064. }
  1065. function base58ToHex($base58) {
  1066. // TRON 专用 Base58 字符集
  1067. $alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
  1068. // 1. 将 Base58 转换为十进制大数
  1069. $num = '0';
  1070. for ($i = 0; $i < strlen($base58); $i++) {
  1071. $char = $base58[$i];
  1072. $pos = strpos($alphabet, $char);
  1073. if ($pos === false) {
  1074. throw new Exception("无效的 Base58 字符: $char");
  1075. }
  1076. $num = bcmul($num, '58');
  1077. $num = bcadd($num, (string)$pos);
  1078. }
  1079. // 2. 将十进制大数转换为十六进制
  1080. $hex = '';
  1081. while ($num !== '0') {
  1082. $remainder = bcmod($num, '16');
  1083. $hex = dechex((int)$remainder) . $hex;
  1084. $num = bcdiv($num, '16', 0);
  1085. }
  1086. // 3. 添加前导零(每个前导'1'对应一个00)
  1087. $leadingZeros = '';
  1088. for ($i = 0; $i < strlen($base58) && $base58[$i] === '1'; $i++) {
  1089. $leadingZeros .= '00';
  1090. }
  1091. // 4. 组合并确保长度为50字符(25字节)
  1092. $hex = $leadingZeros . $hex;
  1093. $hex = str_pad($hex, 50, '0', STR_PAD_LEFT);
  1094. // 5. 提取地址部分并添加 TRON 前缀
  1095. // 跳过版本字节(0x41)和校验码(最后4字节)
  1096. $addressPart = substr($hex, 2, 40); // 取20字节地址
  1097. return '0x41' . $addressPart; // TRON 地址以0x41开头
  1098. }
  1099. }