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
 /**
  * 第三方绑定方法
  */
 public function thirdBind($client)
 {
     $session = Yii::$app->getSession();
     $mTplogin = new UserThirdPartyLogin();
     $id = $client->getId();
     $key = $client->openid();
     $result = '';
     $userAttrs = $client->getUserAttributes();
     $userProfile = $client->getUserInfo();
     $openid = $userAttrs[$key];
     $data = ['type' => $id, 'open_id' => $openid, 'profile_info' => json_encode($userProfile), 'channel' => UserThirdPartyLogin::BIND_WEB_CHANNEL];
     if ($session->has(UserBaseInfo::SESSION_KEY_USER)) {
         $data['user_id'] = $session[UserBaseInfo::SESSION_KEY_USER]['id'];
         $checkBindByUAT = $mTplogin->checkBindByUAT($data['user_id'], UserThirdPartyLogin::BIND_WEB_CHANNEL, $id, true);
         $checkResult = $mTplogin->checkBindByOpenid($openid, $data['channel']);
         try {
             if ($checkBindByUAT && !is_array($checkBindByUAT)) {
                 $reBind = ['orig_open_id' => $checkBindByUAT, 'new_open_id' => $openid, 'user_id' => $data['user_id'], 'type' => $id, 'profile' => json_encode($userProfile), 'channel' => UserThirdPartyLogin::BIND_WEB_CHANNEL];
                 if (!$checkResult) {
                     $mTplogin->reBind($reBind);
                     return;
                 }
                 if ($checkResult && is_array($checkResult)) {
                     if ($data['user_id'] != $checkResult['user_id']) {
                         $status = UserThirdPartyLogin::STATUS_LOGIN_UNBIND;
                         $mTplogin->bindDel($checkResult['user_id'], $status, $id, $data['channel']);
                         $mTplogin->reBind($reBind);
                     } else {
                         $mTplogin->bind($data);
                     }
                     return;
                 }
             } else {
                 if ($checkBindByUAT && is_array($checkBindByUAT)) {
                     return "<script type='text/javascript'>alert('用户已绑定');history.go(-1);</script>";
                 }
             }
             if ($checkResult === false) {
                 $bindResult = $mTplogin->bind($data);
             } else {
                 if ($checkResult > 0 && !is_array($checkResult)) {
                     return "<script type='text/javascript'>alert('该" . $id . "已被其他账户绑定');history.go(-1);</script>";
                 } else {
                     if (is_array($checkResult)) {
                         $mTplogin->bind($data);
                     } else {
                         if ($checkResult === true) {
                             return "<script type='text/javascript'>alert('该" . $id . "已被其他账户绑定');history.go(-1);</script>";
                         }
                     }
                 }
             }
             return;
         } catch (\Exception $e) {
             return $e->getMessage();
         }
     }
 }