MainWalletController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * hdApp 中央钱包(可用余额)接口
  4. * 用途:变动明细、微信充值下单
  5. */
  6. namespace hd\controllers;
  7. use bizHd\shop\classes\MainWalletChangeClass;
  8. use bizHd\shop\classes\MainWalletRechargeClass;
  9. use bizHd\wx\classes\WxOpenClass;
  10. use common\components\dateUtil;
  11. use common\components\dict;
  12. use common\components\util;
  13. use Yii;
  14. class MainWalletController extends BaseController
  15. {
  16. public $guestAccess = [];
  17. /**
  18. * 钱包变动明细分页 + 区间汇总
  19. * GET:page、searchTime、startTime、endTime、capitalType(可选)
  20. */
  21. public function actionChangeList()
  22. {
  23. $get = Yii::$app->request->get();
  24. $where = [];
  25. $where['mainId'] = $this->mainId;
  26. if (isset($get['capitalType']) && $get['capitalType'] !== '') {
  27. $where['capitalType'] = (int) $get['capitalType'];
  28. }
  29. $searchTime = $get['searchTime'] ?? 'today';
  30. if (!empty($searchTime)) {
  31. $startTime = $get['startTime'] ?? '';
  32. $endTime = $get['endTime'] ?? '';
  33. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  34. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  35. }
  36. $list = MainWalletChangeClass::getChangeList($where);
  37. $summaryAmount = MainWalletChangeClass::getSummaryAmount($where);
  38. $main = $this->main;
  39. $walletBalance = $main->walletBalance ?? '0.00';
  40. $list['summary'] = [
  41. 'totalRecharge' => $summaryAmount['recharge'],
  42. 'totalConsume' => $summaryAmount['consume'],
  43. 'walletBalance' => round((float) $walletBalance, 2),
  44. ];
  45. util::success($list);
  46. }
  47. /**
  48. * 中央钱包微信充值:建未支付单并返回 JSAPI 支付参数
  49. * POST:amount、miniOpenId(可选,缺省取当前 admin)
  50. */
  51. public function actionRecharge()
  52. {
  53. ini_set('date.timezone', 'Asia/Shanghai');
  54. $post = Yii::$app->request->post();
  55. $amount = isset($post['amount']) ? floor(((float) $post['amount']) * 100) / 100 : 0;
  56. if ($amount <= 0) {
  57. util::fail('请输入充值金额');
  58. }
  59. $admin = !empty($this->admin) ? $this->admin->attributes : [];
  60. $openId = trim((string) ($post['miniOpenId'] ?? ($admin['miniOpenId'] ?? '')));
  61. if ($openId === '') {
  62. // 前端可据此走 uni.login 补 openId 后重试
  63. util::success(['hasNoMiniOpenId' => 1]);
  64. }
  65. $recharge = MainWalletRechargeClass::createPending(
  66. (int) $this->mainId,
  67. (int) $this->shopId,
  68. $amount
  69. );
  70. $orderId = (int) ($recharge->id ?? 0);
  71. $orderSn = (string) ($recharge->orderSn ?? '');
  72. if ($orderId <= 0 || $orderSn === '') {
  73. util::fail('创建充值单失败');
  74. }
  75. $capitalType = dict::getDict('capitalType', 'mainWalletRecharge', 'id');
  76. $attach = 'capitalType=' . $capitalType . '&mainId=' . (int) $this->mainId;
  77. $now = time();
  78. $expireTime = $now + 300;
  79. $wx = Yii::getAlias('@vendor/weixin');
  80. require_once $wx . '/lib/WxPay.Api.php';
  81. require_once $wx . '/example/WxPay.JsApiPay.php';
  82. $input = new \WxPayUnifiedOrder();
  83. $input->SetBody('中央钱包充值');
  84. $input->SetOut_trade_no($orderSn);
  85. $input->SetTotal_fee((int) round($amount * 100));
  86. $input->SetTime_start(date('YmdHis', $now));
  87. $input->SetAttach($attach);
  88. $input->SetTime_expire(date('YmdHis', $expireTime));
  89. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  90. $input->SetTrade_type('JSAPI');
  91. $merchantExtend = WxOpenClass::getWxInfo();
  92. $input->SetOpenid($openId);
  93. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  94. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  95. $tools = new \JsApiPay();
  96. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  97. $newParams = json_decode($jsApiParameters, true) ?: [];
  98. $newParams['orderId'] = $orderId;
  99. $newParams['orderSn'] = $orderSn;
  100. $newParams['needPay'] = 1;
  101. $newParams['paid'] = 0;
  102. $newParams['amount'] = $amount;
  103. $newParams['hasNoMiniOpenId'] = 0;
  104. util::success($newParams);
  105. }
  106. }