Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: chen chao
Added: Sep 23, 2018 2:19 PM
Views: 7
Tags: no tags
  1. public function doLogin()
  2.     {
  3.  
  4.         $mobile = $this->request->post('mobile');
  5.         $password = $this->request->post('password');
  6.         if (!trim($mobile)) {
  7.             return $this->buildFailed(ReturnCode::INVALID, '请输入手机号');
  8.         }
  9.         if (!preg_match("/^1[34578]{1}\d{9}$/", $mobile)) {
  10.             return $this->buildFailed(ReturnCode::PARAM_INVALID, '手机号码格式不正确');
  11.         }
  12.         if (!trim($password)) {
  13.             return $this->buildFailed(ReturnCode::INVALID, '请输入密码');
  14.         }
  15.         if (strlen($password) < 6) {
  16.             return $this->buildFailed(ReturnCode::PARAM_INVALID, '密码长度不能小于6位');
  17.         }
  18.         $appId = $this->request->post('app_id');
  19.         $password = $this->request->post('password');
  20.         $password = Tools::userMd5($password);
  21.         $appInfo = (new AdminApp())->where(['app_id' => $appId, 'app_status' => 1])->find();
  22.         if (empty($appInfo)) {
  23.             return $this->buildFailed(ReturnCode::INVALID, '应用ID非法');
  24.         }
  25.         $userInfo = TpUser::get(['mobile' => $mobile, 'password' => $password]);
  26.         $accessToken = (new BuildToken())->buildAccessToken($appInfo['app_id'], $appInfo['app_secret']);
  27.         if (!empty($userInfo)) {
  28.             // TODO 更新登录信息
  29.             (new TpUser)->where('id', $userInfo['id'])->update(['access_token' => $accessToken]);
  30.         } else {
  31.             return $this->buildFailed(ReturnCode::LOGIN_ERROR, '用户名或密码不正确');
  32.         }
  33.         // TODO 回传参数 待完善
  34.         $return['id'] = $userInfo['id'];
  35.         $return['username'] = $userInfo['username'];
  36.         $return['mobile'] = $userInfo['mobile'];
  37.         if(strpos($userInfo['avatar'], '//')){
  38.             $return['avatar'] = $userInfo['avatar'];
  39.         }else{
  40.             $return['avatar'] = $userInfo['avatar'] ? "http://60.174.234.53:2006/Uploads/" . $userInfo['avatar'] : "http://60.174.234.53:2006/Uploads/2018-09-01/5b89f9d19d6c4.png";
  41.         }
  42.         $return['access_token'] = $accessToken;
  43.         // $return2 = Tools::concatenateImagePath($return, 'avatar',2);
  44.         return $this->buildSuccess($return, '登录成功');
  45.     }