request->param(); if (empty($params['code'])) { $this->error('The code is required.'); } $wxMiniApp = new MiniAppService(); $wx = $wxMiniApp->getUserPhone($params['code']); if (empty($wx['phone_info']['purePhoneNumber'])) { $this->error('手机号授权失败.', $wx); } $mobile = $wx['phone_info']['purePhoneNumber']; $extend = []; if (!empty($params['openid']) && $wxInfo = Cache::get($params['openid'])){ $extend['mini_openid'] = $wxInfo['openid'] ?? ''; $extend['mini_sessionkey'] = $wxInfo['session_key'] ?? ''; } // 校验手机号登录信息 list($exists, $user_id) = UserModel::checkExists('', $mobile); if (!$exists) { // 手机号不存在 创建账号 $ret = $this->auth->register('1', '1', '', $mobile, $extend); if (!$ret) { $this->error("注册失败!"); } $user_id = $this->auth->id; } // 写入登录Cookies和Token if (!$this->auth->direct($user_id,$extend)) { $this->error($this->auth->getError()); } $userInfo = $this->auth->getUserinfo(); $result = [ 'token' => $userInfo['token'], // 'userInfo' => $userInfo ]; $this->success('登录成功', $result); } /** * 微信小程序授权 * * @return void */ public function loginWxMini() { // 获取参数 $params = $this->request->param(); if (empty($params['code'])) { $this->error('The code is required.'); } $wxMiniApp = new MiniAppService(); $res = $wxMiniApp->login($params['code']); if (empty($res['openid'])) { $this->error('授权失败,请重试'); } $sign = md5($res['openid']); Cache::set($sign, $res, 3600); $this->success('success', [ 'openid' => $sign ]); } }