On this page
绑定邮箱api
controller层:application\api\controller\v1\User.php
php
// 绑定邮箱
public function bindemail(){
(new UserValidate())->goCheck('bindemail');
// 绑定
(new UserModel())->bindemail();
return self::showResCodeWithOutData('绑定成功');
}
route层:route\route.php
php
// 用户操作(绑定手机)
Route::group('api/:v1/',function(){
// 绑定邮箱
Route::post('user/bindemail','api/v1.User/bindemail');
})->middleware(['ApiUserAuth','ApiUserBindPhone','ApiUserStatus']);
validate层:application\common\validate\UserValidate.php
php
protected $rule = [
'email'=>'require|email',
];
protected $scene = [
...
'bindemail'=>['email'],
];
model层:application\common\model\User.php
php
// 绑定邮箱
public function bindemail(){
// 获取所有参数
$params = request()->param();
$currentUserInfo = request()->userTokenUserInfo;
$currentUserId = request()->userId;
// 当前登录类型
$currentLoginType = $currentUserInfo['logintype'];
// 验证绑定类型是否冲突
$this->checkBindType($currentLoginType,'email');
// 查询该手机是否绑定了其他用户
$binduser = $this->isExist(['email'=>$params['email']]);
// 存在
if ($binduser) {
// 账号手机登录
if ($currentLoginType == 'username' || $currentLoginType == 'phone') TApiException('已被绑定',20006,200);
// 第三方登录
if ($binduser->userbind()->where('type',$currentLoginType)->find()) TApiException('已被绑定',20006,200);
// 直接修改
$userbind = $this->userbind()->find($currentUserInfo['id']);
$userbind->user_id = $binduser->id;
if ($userbind->save()) {
// 更新缓存
$currentUserInfo['user_id'] = $binduser->id;
Cache::set($currentUserInfo['token'],$currentUserInfo,$currentUserInfo['expires_in']);
return true;
}
TApiException();
}
// 不存在
// 账号手机登录
if ($currentLoginType == 'username' || $currentLoginType == 'phone'){
$user = $this->save([
'email'=>$params['email']
],['id'=>$currentUserId]);
// 更新缓存
$currentUserInfo['email'] = $params['email'];
Cache::set($currentUserInfo['token'],$currentUserInfo,config('api.token_expire'));
return true;
}
// 第三方登录
if (!$currentUserId) {
// 在user表创建账号
$user = $this->create([
'username'=>$params['email'],
'email'=>$params['email'],
]);
// 绑定
$userbind = $this->userbind()->find($currentUserInfo['id']);
$userbind->user_id = $user->id;
if ($userbind->save()) {
// 更新缓存
$currentUserInfo['user_id'] = $user->id;
Cache::set($currentUserInfo['token'],$currentUserInfo,$currentUserInfo['expires_in']);
return true;
}
TApiException();
}
// 直接修改
if($this->save([
'email'=>$params['email']
],['id'=>$currentUserId])) return true;
TApiException();
}