Skip to content
关注公众号,获取新课通知
【重要通知】uniapp实战社区交友交流群更换为:602180461,靓仔/靓女可以重新申请加入哦~

绑定手机api


controller层:application\api\controller\v1\User.php

php
// 绑定手机
public function bindphone(){
    (new UserValidate())->goCheck('bindphone');
    // 绑定
    (new UserModel())->bindphone();
    return self::showResCodeWithOutData('绑定成功');
}

route层:route\route.php

php
// 用户操作(只验证token)
Route::group('api/:v1/',function(){
    // 绑定手机
    Route::post('user/bindphone','api/v1.User/bindphone');
})->middleware(['ApiUserAuth']);

validate层:application\common\validate\UserValidate.php

php
protected $scene = [
	...
    'bindphone'=>['phone','code'],
];

model层:application\common\model\User.php

php
// 判断用户是否存在(补充前面课程)
public function isExist($arr=[]){
    if(!is_array($arr)) return false;
    if (array_key_exists('phone',$arr)) { // 手机号码
        $user = $this->where('phone',$arr['phone'])->find();
        if ($user) $user->logintype = 'phone';
        return $user;
    }
    // 用户id
    if (array_key_exists('id',$arr)) { // 用户名
        return $this->where('id',$arr['id'])->find();
    }
    if (array_key_exists('email',$arr)) { // 邮箱
        $user = $this->where('email',$arr['email'])->find();
        if ($user) $user->logintype = 'email';
        return $user;
    }
    if (array_key_exists('username',$arr)) { // 用户名
        $user = $this->where('username',$arr['username'])->find();
        if ($user) $user->logintype = 'username';
        return $user;
    }
    // 第三方参数
    if (array_key_exists('provider',$arr)) {
        $where = [
            'type'=>$arr['provider'],
            'openid'=>$arr['openid']
        ];
        $user = $this->userbind()->where($where)->find();
        if ($user) $user->logintype = $arr['provider'];
        return $user;
    }
    return false;
}

// 手机登录(补充前面课程)
public function phoneLogin(){
    // 获取所有参数
    $param = request()->param();
    // 验证用户是否存在
    $user = $this->isExist(['phone'=>$param['phone']]);
    // 用户不存在,直接注册
    if(!$user){
        // 用户主表
        $user = self::create([
            'username'=>$param['phone'],
            'phone'=>$param['phone'],
            // 'password'=>password_hash($param['phone'],PASSWORD_DEFAULT)
        ]);
        // 在用户信息表创建对应的记录(用户存放用户其他信息)
        $user->userinfo()->create([ 'user_id'=>$user->id ]);
        $user->logintype = 'phone';
        return $this->CreateSaveToken($user->toArray());
    }
    // 用户是否被禁用
    $this->checkStatus($user->toArray());
    // 登录成功,返回token
    return $this->CreateSaveToken($user->toArray());
}

// 第三方登录(补充前面课程)
public function otherlogin(){
    // 获取所有参数
    $param = request()->param();
    // 解密过程(待添加)
    // 验证用户是否存在
    $user = $this->isExist(['provider'=>$param['provider'],'openid'=>$param['openid']]);
    // 用户不存在,创建用户
    $arr = [];
    if (!$user) {
        $user = $this->userbind()->create([
            'type'=>$param['provider'],
            'openid'=>$param['openid'],
            'nickname'=>$param['nickName'],
            'avatarurl'=>$param['avatarUrl'],
        ]);
        $arr = $user->toArray();
        $arr['expires_in'] = $param['expires_in']; 
        $arr['logintype'] = $param['provider']; 
        return $this->CreateSaveToken($arr);
    }
    // 用户是否被禁用
    $arr = $this->checkStatus($user->toArray(),true);
    // 登录成功,返回token
    $arr['expires_in'] = $param['expires_in']; 
    return $this->CreateSaveToken($arr);
}

// 验证当前绑定类型是否冲突
public function checkBindType($current,$bindtype){
    // 当前绑定类型
    if($bindtype == $current) TApiException('绑定类型冲突');
    return true;
}

// 绑定手机
public function bindphone(){
    // 获取所有参数
    $params = request()->param();
    $currentUserInfo = request()->userTokenUserInfo;
    $currentUserId = request()->userId;
    // 当前登录类型
    $currentLoginType = $currentUserInfo['logintype'];
    // 验证绑定类型是否冲突
    $this->checkBindType($currentLoginType,'phone');
    // 查询该手机是否绑定了其他用户
    $binduser = $this->isExist(['phone'=>$params['phone']]);
    // 存在
    if ($binduser) {
        // 账号邮箱登录
        if ($currentLoginType == 'username' || $currentLoginType == 'email') 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 == 'email'){
        $user = $this->save([
            'phone'=>$params['phone']
        ],['id'=>$currentUserId]);
        // 更新缓存
        $currentUserInfo['phone'] = $params['phone'];
        Cache::set($currentUserInfo['token'],$currentUserInfo,config('api.token_expire'));
        return true;
    }
    // 第三方登录
    if (!$currentUserId) {
        // 在user表创建账号
        $user = $this->create([
            'username'=>$params['phone'],
            'phone'=>$params['phone'],
        ]);
        // 绑定
        $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([
        'phone'=>$params['phone']
    ],['id'=>$currentUserId])) return true;
    TApiException();
}