header('appid'); if (!$appid) { return $this->error('appid缺失'); } $agent_id = Agent::where('appid', $appid)->value('id'); if (!$agent_id) { return $this->error('appid无效'); } $code = request()->input('code'); if (!$code) { return $this->error('Invalid code!'); } $agent = Agent::select(['id', 'appid', 'appsecret'])->find($agent_id); //代理商数据 $base_url = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=JSCODE&grant_type=authorization_code'; $url = sprintf($base_url, $agent['appid'], $agent['appsecret']); $res = Http::get($url)->json(); if (array_key_exists('errcode', $res) || $res['errcode'] != 0 || empty($res['openid'])) { $res['openid'] = session_create_id(); // return $this->error($res['errmsg'] ?? 'login fail!'); TODO 测试时注释掉 } // TODO 登录部分待优化 $userModel = new User(); $userModel->firstOrCreate(['openid' => $res['openid'], 'agent_id' => $agent_id], ['nickname' => '未设置昵称', 'avatar' => '/static/images/avatar.png']); // TODO 用于测试 $token_key = md5($userModel->id); Cache::put($token_key, 1); Cache::put($agent['appid'], $agent['id']); // 测试代码结束 return $this->success(['token' => md5($userModel->id)]); } }