On this page
绑定第三方api
controller层:application\api\controller\v1\User.php
php
// 绑定第三方
public function bindother(){
(new UserValidate())->goCheck('bindother');
(new UserModel())->bindother();
return self::showResCodeWithOutData('绑定成功');
}
route层:route\route.php
php
// 用户操作(绑定手机)
Route::group('api/:v1/',function(){
// 绑定第三方
Route::post('user/bindother','api/v1.User/bindother');
})->middleware(['ApiUserAuth','ApiUserBindPhone','ApiUserStatus']);
validate层:application\common\validate\UserValidate.php
php
protected $scene = [
'bindother'=>['provider','openid','nickName','avatarUrl'],
];
model层:application\common\model\User.php
php
// 绑定第三方登录
public function bindother(){
// 获取所有参数
$params = request()->param();
$currentUserInfo = request()->userTokenUserInfo;
$currentUserId = request()->userId;
// 当前登录类型
$currentLoginType = $currentUserInfo['logintype'];
// 验证绑定类型是否冲突
$this->checkBindType($currentLoginType,$params['provider']);
// 查询该手机是否绑定了其他用户
$binduser = $this->isExist(['provider'=>$params['provider'],'openid'=>$params['openid']]);
// 存在
if ($binduser) {
if ($binduser->user_id) TApiException('已被绑定',20006,200);
$binduser->user_id = $currentUserId;
return $binduser->save();
}
// 不存在
return $this->userbind()->create([
'type'=>$params['provider'],
'openid'=>$params['openid'],
'nickname'=>$params['nickName'],
'avatarurl'=>$params['avatarUrl'],
'user_id'=>$currentUserId
]);
}