|
|
@@ -0,0 +1,124 @@
|
|
|
+<?php
|
|
|
+/**
|
|
|
+ * ghsApp 中央钱包(可用余额)接口
|
|
|
+ * 用途:我的-可用余额-变动明细、微信充值;业务与 hd 共用 xhMain.walletBalance / xhMainWallet*
|
|
|
+ */
|
|
|
+namespace ghs\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['ghsHost'] . '/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);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|