use yii\helpers\Html; use yii\widgets\ActiveForm; use common\models\UserProfile; use common\z\ZCommonSessionFun; use common\z\ZCommonFun; use common\z\ZController; use common\models\OauthBind; /* @var $this yii\web\View */ /* @var $model common\models\UserProfile */ /* @var $form yii\widgets\ActiveForm */ $this->title = ZController::$site_name . '账号绑定'; echo $this->renderFile(__DIR__ . '/../layouts/head.php'); $sessionUser = ZCommonSessionFun::get_user_session(); $const_BindList = OauthBind::constBindList(); $model_OauthBind = new OauthBind(); $uid = ZCommonSessionFun::get_user_id(); $user_BindList = $model_OauthBind->getUserBindList($uid); // ZCommonFun::print_r_debug($user_BindList); // $user_BindList = ZCommonFun::listData($user_BindList, 'id', 'type'); // ZCommonFun::print_r_debug($user_BindList); ?> <link href="./bag-test/bootstrap/bootstrap.min.css" rel="stylesheet" media="screen"> <link href="./bag-test/bootstrap/datetimepicker.css" rel="stylesheet" media="screen"> <style> div.notice { margin: 0; }
/** * 绑定账号 */ public function actionBindAccount() { $this->layout = false; $old_login_uid = ZCommonSessionFun::get_user_id(); if (ZCommonSessionFun::get_user_id() < 1) { $url = Yii::$app->urlManager->createUrl([ZCommonSessionFun::urlLoginUserStr]); return $this->redirect($url); } $model = new User(); if (isset($_POST['User']['user']) && isset($_POST['op'])) { $post = $_POST['User']; $user = isset($post['user']) ? $post['user'] : ''; $pass = isset($post['pass']) ? $post['pass'] : ''; $post = $_POST['User']; //已有账户绑定 if ($_POST['op'] == 1) { $condition['user'] = $user; $model_find = new User(); $model_find = $model_find->find()->where($condition)->one(); if (!$model_find) { $model->addError('user', '用户没有找到'); } else { if ($model_find->pass != ZCommonFun::getPass($pass)) { $model->addError('pass', '密码错误'); } else { if ($model_find->is_bind_user == 1) { $model->addError('user', '账户已被绑定过'); } else { $connection = Yii::$app->db; $transaction = $connection->beginTransaction(); try { $model_find->is_bind_user = 1; $model_find->save(); $model_Oauth = new OauthBind(); $model_Oauth_condition['uid'] = $old_login_uid; $model_Oauth_attributes['uid'] = $model_find->uid; $model_Oauth->updateAll($model_Oauth_attributes, $model_Oauth_condition); $transaction->commit(); ZCommonSessionFun::set_user_session($model_find->attributes); $url = Yii::$app->urlManager->createUrl(['user-profile/bind-list']); return $this->redirect($url); } catch (\Exception $e) { $transaction->rollBack(); $model->addError('user', '绑定失败'); } } } } } else { $model->pass = ZCommonFun::getPass($model->pass); $model = $model->findOne(ZCommonSessionFun::get_user_id()); if ($model) { $model->user = $user; $model->pass = ZCommonFun::getPass($pass); $model->is_bind_user = 1; if ($model->save()) { ZCommonSessionFun::set_user_session($model->attributes); $url = Yii::$app->urlManager->createUrl(['user-profile/bind-list']); return $this->redirect($url); } } else { $model = new User(); $model->user = $user; $model->pass = $pass; $model->addError('user', '用户已被删除'); } } } return $this->render('bind-account', ['model' => $model]); }
/** * 绑定用户信息 * @param unknown $user 用户名 * @param unknown $pass 密码 * @param unknown $uid 用户id * @param unknown $openid 开放id * @param unknown $type 类型 * @param unknown $nickname 昵称 * @param unknown $head_image 头像 * @param string $is_register true注册用户|false老用户绑定 * @return number */ public function userBind($user, $pass, $uid, $openid, $type, $nickname, $head_image, $is_register = false) { $model_OauthBind = new OauthBind(); $condition['openid'] = $openid; $condition['type'] = $type; $model_OauthBind = $model_OauthBind->findOne($condition); if ($model_OauthBind && $model_OauthBind->uid > 0) { $is_register = false; if ($uid > 0) { $model_User = User::findOne($uid); } else { $model_User = User::findOne($model_OauthBind->uid); } } else { if ($uid > 0) { $is_register = false; $model_User = User::findOne($uid); } } //已存在用户 if (!$is_register) { } else { $model_User = new User(); $max_uid = User::find()->max('uid'); $max_uid++; $model_User->user = $max_uid . ''; $model_User->pass = $model_User->user; $model_User->created = NOW_TIME_YmdHis; $model_User->save(); } // echo $uid; // ZCommonFun::print_r_debug($model_User->attributes); // ZCommonFun::print_r_debug($model_OauthBind->attributes); // exit; // $model_OauthBind = new OauthBind(); // $condition['openid'] = $openid; // $condition['type'] = $type; // $model_OauthBind=$model_OauthBind->findOne($condition); //已经绑定了 if ($model_OauthBind) { $this->operationData['user'] = $model_User; $model_UserProfile = UserProfile::findOne(['uid' => $model_User->uid]); if (!$model_UserProfile) { $model_UserProfile = new UserProfile(); $model_UserProfile->uid = $model_User->uid; $model_UserProfile->nickname = $nickname; $model_UserProfile->head_image = $head_image; $model_UserProfile->money = 0; $model_UserProfile->friend_money = 0; $model_UserProfile->save(); // ZCommonFun::print_r_debug($model_UserProfile); // exit; } $model_OauthBind->uid = $model_User->uid; $model_OauthBind->save(); // ZCommonFun::print_r_debug($model_User->uid); // ZCommonFun::print_r_debug($model_OauthBind); // exit; $this->operationData['oauth_bind'] = $model_OauthBind; $this->operationData['user_profile'] = $model_UserProfile; return 1; } $model_OauthBind = new OauthBind(); $model_OauthBind->openid = $openid . ''; $model_OauthBind->type = $type; $model_OauthBind->uid = $model_User->uid; $model_OauthBind->created = NOW_TIME_YmdHis; if ($model_OauthBind->save()) { $model_UserProfile = UserProfile::findOne(['uid' => $model_User->uid]); //如果没有设置过用户信息,就设置用户信息 if (!$model_UserProfile) { $model_UserProfile = new UserProfile(); $model_UserProfile->uid = $model_User->uid; $model_UserProfile->nickname = $nickname; $model_UserProfile->head_image = $head_image; $model_UserProfile->money = 0; $model_UserProfile->friend_money = 0; $model_UserProfile->save(); } $this->operationData['user'] = $model_User; $this->operationData['user_profile'] = $model_UserProfile; return 0; } return -1; }
/** * 设置账号登录还是第三方登录 * @param string $type OauthBind::constBindList() 返回数组键 * @return boolean */ public static function set_login_type($type) { $user = self::get_user_session(); $bindList = OauthBind::constBindList(); if (isset($bindList[$type])) { $user['login_type'] = $type; $user['login_type_text'] = $bindList[$type]; return true; } return false; }