1.第一步php
在中心端添加應用,此處略去,根據官方文檔便可實現.web
第二步.json
用戶表以下,基本用原生的用戶表便可,取決於你的ucenter主機服務端傳送什麼用戶信息:yii2
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(20) UNSIGNED NOT NULL AUTO_INCREMENT, `username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用戶名', `password_hash` varchar(80) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '密碼', `password_reset_token` varchar(60) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密碼token', `email` varchar(60) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '郵箱', `auth_key` varchar(60) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `status` tinyint(5) NULL DEFAULT 1 COMMENT '0禁用 1啓用', `created_at` int(18) NULL DEFAULT NULL COMMENT '建立時間', `updated_at` int(18) NULL DEFAULT NULL COMMENT '更新時間', `realname` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '真實名稱', `user_id` int(11) NULL DEFAULT NULL COMMENT '統一登陸用戶id', `dep_id` int(11) NULL DEFAULT NULL COMMENT '部門id', `title` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `nickname` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '暱稱', PRIMARY KEY (`id`) USING BTREE, UNIQUE INDEX `username`(`username`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 72 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '後臺用戶表' ROW_FORMAT = Compact; SET FOREIGN_KEY_CHECKS = 1;
第三步:app
yii2在配置文件加入:yii
'components' => [ ... 'ucenter' => [ 'class' => 'yii\lyuser\Client', 'baseUrl' => 'http://www-test.lanyife.com.cn/passport', 'appKey' => 'medusa', 'appSecret' => '1f13f6b2508b03b25dc1088588bef3eb', 'jumpUrl' => "http://{$_SERVER['HTTP_HOST']}/site/login", 'url' => "http://{$_SERVER['HTTP_HOST']}", ], ... ],
第四步:this
在默認控制器裏這麼改造url
<?php namespace backend\controllers; use Yii; use yii\helpers\Url; use yii\web\Controller; use yii\filters\VerbFilter; use yii\filters\AccessControl; use common\models\LoginForm; use common\models\User; use yii\web\Response; /** * Site controller */ class SiteController extends Controller { /** * {@inheritdoc} */ public function behaviors() { return [ 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'delete' => ['POST'], ], ], ]; } /** * {@inheritdoc} */ public function actions() { return [ 'error' => [ 'class' => 'yii\web\ErrorAction', ], ]; } /** * Displays homepage. * * @return string */ public function actionIndex() { if(yii::$app->user->isGuest){ return $this->redirect(Url::toRoute('site/login')); } return $this->render('index'); } /** * Login action. * * @return string */ public function actionLogin() { if( \Yii::$app->user->isGuest ){ return \Yii::$app->ucenter->goLogin(); } $this->redirect('/'); } /** * 同步登陸 * @return string */ public function actionSyncLogin() { $r = \Yii::$app->ucenter->synLogin(); // \Yii::$app->response->format = Response::FORMAT_JSONP; if ($r) { if (\Yii::$app->request->get('action') == 'login') { //獲取登陸用戶信息 $userInfo = \Yii::$app->ucenter->getUserInfo(); file_put_contents('/tmp/77.log',json_encode($userInfo).date('y-m-d H:i:s') ); if (empty($userInfo)) { return false; } $res = $this->_loginOrRegister($userInfo); if($res){ $user = User::findOne(['username'=>$userInfo['username'] ]); \Yii::$app->getUser()->login($user, 86400); } } \Yii::$app->response->content = 'login(1)'; } else { \Yii::$app->response->content = 'login(0)'; } \Yii::$app->response->send(); \Yii::$app->end(); } //註銷 public function actionLogout() { //本地 註銷 Yii::$app->user->logout(); // Yii::$app->getUser()->logout(); //跳轉UCenter註銷頁面 $callback 註銷返回的url \Yii::$app->ucenter->goLogout(\Yii::$app->ucenter->jumpUrl); } /** * 第一次登陸或者屢次登陸,實現信息同步並註冊 * @param array $userInfo * @return bool */ private function _loginOrRegister($userInfo) { if (empty($userInfo)||!isset($userInfo['username'])) { return false; } $user = User::findOne(['username'=>$userInfo['username']]); if (empty($user)) { //保存建立本地新用戶 $res = yii::$app->db->createCommand()->insert('user',[ 'username' => $userInfo['username'], 'auth_key'=> $userInfo['auth_key'], 'realname' =>$userInfo['realname'], 'user_id' => $userInfo['id'], 'dep_id' => $userInfo['dep_id'], 'status' => $userInfo['status'], 'created_at' =>$userInfo['created_at'], 'updated_at'=> $userInfo['updated_at'], 'email' =>$userInfo['email'], 'title' =>$userInfo['title'], 'nickname' => $userInfo['nickname'], //模型User的激活狀態要定義,好比我 //見下圖 'status' => User::STATUS_ACTIVE, 'password_hash'=>Yii::$app->security->generatePasswordHash('123456') ])->execute(); } else { yii::$app->db->createCommand()->update('user',['user_id'=>$userInfo['id']] ,[ 'dep_id' => $userInfo['dep_id'], 'realname' => $userInfo['realname'], 'updated_at' => $userInfo['updated_at'], 'title' => $userInfo['title'], ])->execute(); } return true; } }