Commit 01823b2f authored by 郭勇志's avatar 郭勇志

分库

parent 8f56d04f
......@@ -62,6 +62,7 @@ return [
'GET swaggers/swagger/<id>'=>'swagger/swagger',
],
],
// default db
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=weishopdb',
......@@ -69,6 +70,14 @@ return [
'password' => '',
'charset' => 'utf8',
],
//add db1,2,3,4,5,6
'db-100' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=weishopdbs',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
'redis' => [
'class' => 'yii\redis\Connection',
'hostname' => 'r-m5ed79b6945c5a14.redis.rds.aliyuncs.com',
......
<?php
namespace app\models\v1;
/**
* Base Model
* 基类
*/
use Yii;
use yii\db\ActiveRecord;
class BaseModel extends ActiveRecord
{
/**
* Returns the database connection used by this AR class.
* By default, the "db" application component is used as the database connection.
* You may override this method if you want to use a different database connection.
* @return Connection the database connection used by this AR class.
*/
public static function getDb()
{
// 重写getDb,分库
if (isset(Yii::$app->user->identity->ORG_GUID)) {
$db='db-'.Yii::$app->user->identity->ORG_GUID;
}else{
$db='db';
}
return Yii::$app->$db;
}
/**
* 格式化redis key
* @return string
*/
public function formatKey($key)
{
$preFix = PREFIX;
if (substr($key,0,strlen($preFix))==$preFix) {
return $key;
}
return $preFix.$key;
}
}
\ No newline at end of file
......@@ -2,14 +2,13 @@
namespace backend\models\v1\user;
use Yii;
use yii\base\Model;
use app\models\v1\user\ShopRbacUser;
use app\models\v1\BaseModel;
use yii\web\HttpException;
/**
* Login form
*/
class LoginForm extends Model
class LoginForm extends BaseModel
{
public $code;
public $password;
......@@ -56,8 +55,11 @@ class LoginForm extends Model
{
if ($this->validate()) {
$accessToken=$this->_shopRbacUser->generateAccessToken();
// 生成的access_token保存一份到mysql和redis中
if ($this->_shopRbacUser->save()) {
// 生成access_token=>array(用户GUID,ORG_GUID)保存到redis
$redis = Yii::$app->redis;
$rtnSet=$redis->set($this->formatKey($accessToken), json_encode(array('USER_GUID'=>$this->_shopRbacUser->GUID,'ORG_GUID'=>$this->_shopRbacUser->ORG_GUID)));
$rtnExpire=$redis->expire($this->formatKey($accessToken), 3600);
if ($rtnSet&&$rtnExpire) {
return $accessToken;
}
throw new HttpException('400','登陆失败');
......
......@@ -3,8 +3,8 @@
namespace app\models\v1\user;
use Yii;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
use app\models\v1\BaseModel;
/**
* This is the model class for table "shop_rbac_user".
*
......@@ -35,7 +35,7 @@ use yii\web\IdentityInterface;
* @property string $SHARE_NO 分享码
* @property string $ACCESS_TOKEN 验证TOKEN
*/
class ShopRbacUser extends ActiveRecord implements IdentityInterface
class ShopRbacUser extends BaseModel implements IdentityInterface
{
/**
* {@inheritdoc}
......@@ -133,7 +133,21 @@ class ShopRbacUser extends ActiveRecord implements IdentityInterface
*/
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['ACCESS_TOKEN' => $token]);
// return static::findOne(['ACCESS_TOKEN' => $token]);
// 取得reids中用户信息
$redis = Yii::$app->redis;
$model = new ShopRbacUser;
$rtnGet=$redis->get('ROMENS_NEW_'.$token);
$userInfo=json_decode($rtnGet,true);
$db='db-'.$userInfo['ORG_GUID'];
$dbObj = \Yii::$app->$db;
$query = new \yii\db\Query();
$temp=$query->where(array('GUID'=>$userInfo['USER_GUID']))->from('shop_rbac_user')->limit(1)->one($dbObj);
$model->attributes = $temp;
return $model;
}
/**
* @return int|string 当前用户ID
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment