*/ private function initWechat() { $this->app = Factory::officialAccount(Config::get('wechat')); } /** * Notes : 获取微信授权的地址并跳转 * * @Date : 2022/9/9 11:29 * @Author : */ public function url(): Json { $url = $GLOBALS['data']['data']['url']; $redirect = Route::buildUrl('wechat/code', ['callback' => $url]) ->suffix(false) ->domain(true); $this->initWechat(); return show(SUCCESS_MESSAGE, SUCCESS_CODE, $this->app->oauth->scopes(['snsapi_userinfo'])->redirect($redirect)); } /** * Notes : 微信授权回调地址,获取到用户信息后,自行保存 * * @Date : 2022/9/9 11:29 * @Author : */ public function code() { $this->initWechat(); $user = $this->app->oauth->user(); $callback = Request::get('callback'); $user = AppUser::where('openid', $user->getId())->find(); if (! $user) { $user = AppUser::create([ 'nickname' => $user->getNickname(), 'avatar' => $user->getAvatar(), 'identity' => 1, 'openid' => $user->getId(), ]); } $tokenData = ['userid' => $user->id, 'loginTime' => time(), 'rankStr' => strRand(5)]; $token = authcode(json_encode($tokenData), 'ENCODE'); return redirect($callback.'?token='.$token); } /** * Notes : 显示支付的页面,支付逻辑 * * @Date : 2022/9/9 11:32 * @Author : */ public function payment() { $userId = $GLOBALS['data']['userid']; if (empty($userId)) { return show("未登录!", NEED_LOGIN); } $user = AppUser::find($userId); $notifyUrl = Route::buildUrl('wechat/paid') ->suffix(false) ->domain(true); $config = Config::get('wechat.payment'); $payment = Factory::payment($config); $unify = $payment->order->unify([ 'body' => '商品订单', 'out_trade_no' => time(), 'total_fee' => 100, 'notify_url' => $notifyUrl, 'trade_type' => 'JSAPI', 'openid' => $user->openid, ]); dump($unify); // $prepayId = $unify->prepay_id; // $jssdk = $payment->jssdk->bridgeConfig($prepayId); // return View::fetch('', ['jssdk' => $jssdk]); } /** * Notes : 支付完成回调 * * @Date : 2022/9/9 11:33 * @Author : */ public function paid() { } }