/** * 判断第三方账号是否绑定 * * @param array $params 第三方返回账号唯一标示 * @return array */ public function checkBind($params) { $mTplogin = new UserThirdPartyLogin(); $channel = UserThirdPartyLogin::BIND_API_CHANNEL; $openid = $params['open_id']; if (empty($openid)) { return responseArray(1101, 'id_not_find', '用户open_id丢失'); } $checkResult = $mTplogin->checkBindByOpenid($openid, $channel); if ($checkResult === false) { return responseArray(1001, 'account_not_been_bind', '账号未绑定'); } else { if ($checkResult > 0 && !is_array($checkResult)) { $userInfo = UserBaseInfo::find()->where('id=:id', [':id' => $checkResult])->one(); if ($userInfo) { if (!$userInfo->username) { $userInfo->username = UserBaseInfo::genUsername($userInfo->id); if (!$userInfo->save()) { return responseArray(1, 'network_anomaly', '网络异常,稍后重试'); } } return responseArray(0, 'success', '账号已绑定', $userInfo->attributes); } else { return responseArray(1402, 'user_not_exists', '用户不存在'); } } else { if (is_array($checkResult)) { return responseArray(1201, 'state_solution', '处于解绑状态'); } else { if ($checkResult === UserThirdPartyLogin::NOT_BOUND_USERID) { return responseArray(1401, 'not_find_userid', '账户绑定但找不到user_id'); } } } } }
/** * 第三方绑定方法 */ 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(); } } }