auth = $auth->guard('user'); } /** * 创建登 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/7/10 10:55 * */ public function login() { $sid = md5($this->request->header('cookie') . strtotime(date('YmdHi'))); // 查找 $load = Cache::get($sid); // 解码 $load = dejson($load); // 不存在或者过期 if (!$load || $load['expired'] <= time()){ // 数据 $data = [ 'client_id' => $sid, 'status' => 'wait', 'expired' => time() + 300 ]; // 创建登录记 Cache::set($sid,enjson($data),259200); // 返回编码数据 return IResponse::success($data); } // 登录成功,删除本次缓存 if (isset($load['token'])){ Cache::rm($sid); } return IResponse::success($load); } /** * 商家小程序传来登录 * * @author 许祖兴 < zuxing.xu@lettered.cn> * @date 2020/6/16 11:57 * * @return \think\response\Json * @throws \Lettered\Support\Exceptions\FailedException */ public function auth() { $param = $this->request->param(); // 内置验证 $valid = $this->validate($param, [ 'client_id|登录客户端ID' => 'require' ]); // 错误 if (true !== $valid){ return json([ 'code' => -1, 'message' => $valid ]); } // 读取缓存 $load = Cache::get($param['client_id']); if ($load){ // 1. 验证商户状态 $userId = $this->auth->user()['id']; $seller = model('common/Seller')->getBy(['user_id' => $userId]); if($seller['status'] == 1){ // 解码 $load = dejson($load); // 二维码是否过期 if ($load['expired']>= time()){ if ($load['status'] == 'wait'){ // 2. 变更登录状态 // 数据 $data = [ 'client_id' => $param['client_id'], 'token' => $this->request->header('Authorization'), 'status' => 'active', 'expired' => time() + (5 * 60 * 60) ]; // 创建登录记 Cache::set($param['client_id'], enjson($data),259200); return json([ 'code' => 0, 'message' => "登录成功!" ]); } }else { return json([ 'code' => -1, 'message' => "二维码过期,请重新获取!" ]); } } return json([ 'code' => -1, 'message' => "商户状态异常,暂无法登录!" ]); } return json([ 'code' => -1, 'message' => "二维码状态异常,请重新获取!" ]); } }