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); } }