Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
newShopBack
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
newshop
newShopBack
Commits
01823b2f
Commit
01823b2f
authored
Nov 06, 2019
by
郭勇志
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
分库
parent
8f56d04f
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
76 additions
and
8 deletions
+76
-8
main.php
backend/config/main.php
+9
-0
BaseModel.php
backend/models/v1/BaseModel.php
+43
-0
LoginForm.php
backend/models/v1/user/LoginForm.php
+7
-5
ShopRbacUser.php
backend/models/v1/user/ShopRbacUser.php
+17
-3
No files found.
backend/config/main.php
View file @
01823b2f
...
...
@@ -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'
,
...
...
backend/models/v1/BaseModel.php
0 → 100644
View file @
01823b2f
<?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
backend/models/v1/user/LoginForm.php
View file @
01823b2f
...
...
@@ -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
Base
Model
{
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'
,
'登陆失败'
);
...
...
backend/models/v1/user/ShopRbacUser.php
View file @
01823b2f
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment