| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- /**
- * hdApp 中央钱包(可用余额)接口
- * 用途:变动明细、微信充值下单
- */
- namespace hd\controllers;
- use bizHd\shop\classes\MainWalletChangeClass;
- use bizHd\shop\classes\MainWalletRechargeClass;
- use bizHd\wx\classes\WxOpenClass;
- use common\components\dateUtil;
- use common\components\dict;
- use common\components\util;
- use Yii;
- class MainWalletController extends BaseController
- {
- public $guestAccess = [];
- /**
- * 钱包变动明细分页 + 区间汇总
- * GET:page、searchTime、startTime、endTime、capitalType(可选)
- */
- public function actionChangeList()
- {
- $get = Yii::$app->request->get();
- $where = [];
- $where['mainId'] = $this->mainId;
- if (isset($get['capitalType']) && $get['capitalType'] !== '') {
- $where['capitalType'] = (int) $get['capitalType'];
- }
- $searchTime = $get['searchTime'] ?? 'today';
- if (!empty($searchTime)) {
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
- $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
- }
- $list = MainWalletChangeClass::getChangeList($where);
- $summaryAmount = MainWalletChangeClass::getSummaryAmount($where);
- $main = $this->main;
- $walletBalance = $main->walletBalance ?? '0.00';
- $list['summary'] = [
- 'totalRecharge' => $summaryAmount['recharge'],
- 'totalConsume' => $summaryAmount['consume'],
- 'walletBalance' => round((float) $walletBalance, 2),
- ];
- util::success($list);
- }
- /**
- * 中央钱包微信充值:建未支付单并返回 JSAPI 支付参数
- * POST:amount、miniOpenId(可选,缺省取当前 admin)
- */
- public function actionRecharge()
- {
- ini_set('date.timezone', 'Asia/Shanghai');
- $post = Yii::$app->request->post();
- $amount = isset($post['amount']) ? floor(((float) $post['amount']) * 100) / 100 : 0;
- if ($amount <= 0) {
- util::fail('请输入充值金额');
- }
- $admin = !empty($this->admin) ? $this->admin->attributes : [];
- $openId = trim((string) ($post['miniOpenId'] ?? ($admin['miniOpenId'] ?? '')));
- if ($openId === '') {
- // 前端可据此走 uni.login 补 openId 后重试
- util::success(['hasNoMiniOpenId' => 1]);
- }
- $recharge = MainWalletRechargeClass::createPending(
- (int) $this->mainId,
- (int) $this->shopId,
- $amount
- );
- $orderId = (int) ($recharge->id ?? 0);
- $orderSn = (string) ($recharge->orderSn ?? '');
- if ($orderId <= 0 || $orderSn === '') {
- util::fail('创建充值单失败');
- }
- $capitalType = dict::getDict('capitalType', 'mainWalletRecharge', 'id');
- $attach = 'capitalType=' . $capitalType . '&mainId=' . (int) $this->mainId;
- $now = time();
- $expireTime = $now + 300;
- $wx = Yii::getAlias('@vendor/weixin');
- require_once $wx . '/lib/WxPay.Api.php';
- require_once $wx . '/example/WxPay.JsApiPay.php';
- $input = new \WxPayUnifiedOrder();
- $input->SetBody('中央钱包充值');
- $input->SetOut_trade_no($orderSn);
- $input->SetTotal_fee((int) round($amount * 100));
- $input->SetTime_start(date('YmdHis', $now));
- $input->SetAttach($attach);
- $input->SetTime_expire(date('YmdHis', $expireTime));
- $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
- $input->SetTrade_type('JSAPI');
- $merchantExtend = WxOpenClass::getWxInfo();
- $input->SetOpenid($openId);
- $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
- $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
- $tools = new \JsApiPay();
- $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
- $newParams = json_decode($jsApiParameters, true) ?: [];
- $newParams['orderId'] = $orderId;
- $newParams['orderSn'] = $orderSn;
- $newParams['needPay'] = 1;
- $newParams['paid'] = 0;
- $newParams['amount'] = $amount;
- $newParams['hasNoMiniOpenId'] = 0;
- util::success($newParams);
- }
- }
|