1.接口地址:
// 用户授权后回调的页面 Route::put('authCallback', 'LoginController@authCallback')->name("authCallback");
2.
public function authCallback(Request $request) { /** * 1.验证数据合法性 type=1微信登录 type=2 QQ登录 * 2.是否登录过 未登录过添加信息 * 3.登录返回认证信息 */ $type = $request->type; if(!in_array($type, [1, 2])){ return result('类型不合法'); } $data = $request->data; $is_data = $this->formCheck->isEmpty($data, '数据'); if(!$is_data->code){ return result($is_data->msg); } $is_datajpush = $this->formCheck->isEmpty($request->jpush_reg_id, '极光识别码'); if(!$is_datajpush->code){ return result($is_datajpush->msg); } if(!is_json($data)){ return result('数据不合法'); } $data = json_decode($data, true); if(!in_array($data['sex'], ['男', '女'])){ return result('性别不合法'); } $userData = []; $userData['nickname'] = $data['nickname']; $userData['sex'] = $data['sex']; $userData['create_time'] = dateFormat(); $userData['jpush_reg_id'] = $request->jpush_reg_id; if(1 == $type){ $userData['wechat_openid'] = $data['openid']; $openid = 'wechat_openid'; }elseif(2 == $type){ $userData['qq_openid'] = $data['openid']; $openid = 'qq_openid'; } $userInfo = $this->login->one([$openid => $data['openid']]); if(!$userInfo){ $id = $this->login->add($userData); if(!$id){ return result('登录失败'); }else{ $userInfo = $this->login->one([$openid => $data['openid']]); } } if(1 == $userInfo->is_disable){ return result('账号已被禁用,请联系管理员'); } $loginData['remember_token'] = self::setToken(); $expire_time = strtotime("+7 days"); $loginData['expire_time'] = dateFormat($expire_time); $this->login->updUser(['id' => $userInfo->id], $loginData); unset($loginData['expire_time']); $loginData['user_id'] = $userInfo->id; $loginData['user_grade_id'] = $userInfo->user_grade_id; $loginData['role_id'] = $userInfo->role_id; $loginData['mobile'] = $userInfo->mobile; return result('登录成功', 1, $loginData); }
public static function setToken(){ // 生成一个不会重复的字符串 $str = md5(uniqid(md5(microtime(true)), true)); // 进行加密 $str = sha1($str); return $str; }
3.model模型:
table)->where($data)->first(); } // 更新用户信息 public function updUser($where, $data){ return DB::table($this->table)->where($where)->update($data); } // 添加用户 public function add($data){ return DB::table($this->table)->insertGetId($data); } }
4.注意:
安卓或者IOS传过来数据格式:
{
type = 2,
data = "{"sex":"男","openid":"o5DA61FJwDE9hivVO863PthIDVtc","nickname":"。。。。。。"}",
}
返回给前端的数据格式:
{
msg = "登录成功",
data = {
mobile = "",
remember_token = "9a47aec9951f30908c130f78806fdc038f025826",
user_id = 33,
role_id = 0,
user_grade_id = 0,
},
code = 1,
}