Example #1
0
 /**
  * 第三方绑定方法
  */
 public function thirdBind($params)
 {
     $mTplogin = new UserThirdPartyLogin();
     $channel = UserThirdPartyLogin::BIND_API_CHANNEL;
     $result = '';
     if (empty($params['open_id'])) {
         return responseArray(1001, 'id_not_find', '用户open_id丢失');
     }
     if (empty($params['userAttributes']['open_id'])) {
         return responseArray(1101, 'third_id_not_find', '第三方账号关联open_id丢失');
     }
     if (empty($params['type'])) {
         return responseArray(1201, 'type_not_find', '第三方类型丢失');
     }
     if (empty($params['userInfo'])) {
         return responseArray(2101, 'third_user_base_not_find', '第三方用户信息丢失');
     }
     foreach ($params['userInfo'] as $key => $value) {
         if (empty($value) && $value != 0) {
             return responseArray(1301, 'third_user_base_not_find', '第三方用户信息不完整');
         }
     }
     $type = $params['type'];
     $userProfile = $params['userInfo'];
     $openid = $params['userAttributes']['open_id'];
     $data = ['type' => $type, 'open_id' => $openid, 'profile_info' => json_encode($userProfile), 'channel' => $channel];
     $user = UserBaseInfo::find()->where('open_id=:open_id', [':open_id' => $params['open_id']])->one();
     if (!$user) {
         return responseArray(2201, 'user_not_find', '用户不存在');
     }
     if (!$user->username) {
         $user->username = UserBaseInfo::genUsername($user->id);
         if (!$user->save()) {
             return responseArray(1, 'network_anomaly', '网络异常,稍后重试');
         }
     }
     $data['user_id'] = $user->id;
     $data['user_open_id'] = $user->open_id;
     $checkResult = $mTplogin->checkBindByOpenid($openid, $channel);
     $checkBindByUAT = $mTplogin->checkBindByUAT($data['user_id'], $channel, $type, true);
     if ($checkResult && !is_array($checkResult)) {
         //第三方账号被绑定
         return responseArray(1401, 'account_been_bind', '账号已被绑定');
     }
     if (is_array($checkResult) && $data['user_id'] != $checkResult['user_id']) {
         $mTplogin->bindDel($checkResult['user_id'], UserThirdPartyLogin::STATUS_LOGIN_UNBIND, $type, $channel);
     }
     if ($checkBindByUAT && is_array($checkBindByUAT) && $checkBindByUAT['user_id'] == $data['user_id']) {
         $reBind = ['orig_open_id' => $checkBindByUAT['open_id'], 'new_open_id' => $openid, 'user_id' => $data['user_id'], 'type' => $type, 'profile' => json_encode($userProfile), 'channel' => $channel];
         $mTplogin->reBind($reBind);
         // 用户已绑定修改绑定
         return responseArray(0, 'success', '账号绑定成功', $data);
     }
     if ($checkBindByUAT && !is_array($checkBindByUAT)) {
         $reBind = ['orig_open_id' => $checkBindByUAT, 'new_open_id' => $openid, 'user_id' => $data['user_id'], 'type' => $type, 'profile' => json_encode($userProfile), 'channel' => $channel];
         $mTplogin->reBind($reBind);
         // 用户已绑定修改绑定
         return responseArray(0, 'success', '账号绑定成功', $data);
     }
     $checkResult = $mTplogin->checkBindByOpenid($openid, $channel);
     if ($checkResult === false) {
         // 第三方账号没有任何绑定
         unset($data['user_open_id']);
         $bindResult = $mTplogin->bind($data);
         $data['user_open_id'] = $user->open_id;
         $data['username'] = $user->username;
         return responseArray(0, 'success', '账号绑定成功', $data);
     } else {
         if ($checkResult > 0 && !is_array($checkResult)) {
             // 第三方账号处于绑定
             if ($checkResult == $data['user_id']) {
                 // 第三方绑定用户与现操作用户相同
                 $data['username'] = $user->username;
                 return responseArray(0, 'success', '账号绑定成功', $data);
             } else {
                 // 第三方绑定用户与现操作用户不同
                 return responseArray(1401, 'account_been_bind', '账号已被绑定');
             }
         } else {
             if (is_array($checkResult)) {
                 // 第三方账号处于解绑状态
                 if ($checkResult['user_id'] == $data['user_id']) {
                     // 第三方绑定用户与现操作用户相同
                     $mTplogin->toggleBind($data['user_id'], $type, $channel, UserThirdPartyLogin::STATUS_LOGIN_UNBIND);
                     $data['username'] = $user->username;
                     return responseArray(0, 'success', '账号绑定成功', $data);
                 } else {
                     // 第三方绑定用户与现操作用户不同
                     unset($data['user_open_id']);
                     $bindResult = $mTplogin->bind($data);
                     $data['user_open_id'] = $user->open_id;
                     $data['username'] = $user->username;
                     return responseArray(0, 'success', '账号绑定成功', $data);
                 }
             } else {
                 if ($checkResult === UserThirdPartyLogin::NOT_BOUND_USERID) {
                     // 账号绑定但没有用户user_id
                     return responseArray(1401, 'account_been_bind', '账号已被绑定');
                 }
             }
         }
     }
     return responseArray(1, 'network_anomaly', '网络异常,请稍后重试');
 }
Example #2
0
 /**
  * 第三方解绑方法
  *
  * @param OAuth $client OAuth客户端对象
  */
 public function actionUnbind()
 {
     $request = Yii::$app->getRequest();
     $session = Yii::$app->session;
     $channel = UserThirdPartyLogin::BIND_WEB_CHANNEL;
     $mTplogin = new UserThirdPartyLogin();
     $userOperateLog = new UserOperateLog();
     $status = UserThirdPartyLogin::STATUS_LOGIN_BIND;
     $util = Yii::$app->util;
     if (!$request->getIsAjax()) {
         return $util->formatResData(1301, 'illegal_request', ['msg' => '非法请求']);
     }
     // 业务不同要求是否具有登录状也不同
     if (!$this->isLogin) {
         return $this->redirect(Url::toRoute('user/login'));
     }
     $id = $session[UserBaseInfo::SESSION_KEY_USER]['id'];
     $type = $request->post();
     if (empty($type['type'])) {
         return $util->formatResData(1101, 'type_unfound', ['msg' => '解绑类型缺失']);
     }
     try {
         $result = $mTplogin->toggleBind($id, $type['type'], $channel, $status);
         if (!$result) {
             $userOperateLog->store('第三方' . $type['type'] . '解绑', '第三方解绑标识', $result);
             return $util->formatResData(1201, 'type_unfound', ['msg' => '解绑失败']);
         }
         $userOperateLog->store('第三方' . $type['type'] . '解绑', '第三方解绑标识', $result, true);
         return $util->formatResData(0, 'success', ['msg' => '解绑成功']);
     } catch (\Exception $e) {
     }
 }