Commit 65bc1e6b authored by 郭勇志's avatar 郭勇志

change

parent 9ad00304
...@@ -17,14 +17,16 @@ return [ ...@@ -17,14 +17,16 @@ return [
'csrfParam' => '_csrf-backend', 'csrfParam' => '_csrf-backend',
], ],
'user' => [ 'user' => [
'identityClass' => 'common\models\User', 'identityClass' => 'app\models\v1\user\ShopRbacUser',
'enableAutoLogin' => true, 'enableAutoLogin' => true,
'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true], // 'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true],
], 'enableSession' => false,
'session' => [ 'loginUrl' => NULL
// this is the name of the session cookie used for login on the backend
'name' => 'advanced-backend',
], ],
// 'session' => [
// // this is the name of the session cookie used for login on the backend
// 'name' => 'advanced-backend',
// ],
'log' => [ 'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0, 'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [ 'targets' => [
...@@ -45,10 +47,11 @@ return [ ...@@ -45,10 +47,11 @@ return [
'rules' => [ 'rules' => [
[ [
'class'=>'yii\rest\UrlRule', 'class'=>'yii\rest\UrlRule',
'controller'=>['swagger','v1/shop/branch/branch'], 'controller'=>['swagger','v1/shop/branch/branch','v1/user/user'],
'extraPatterns'=>[ 'extraPatterns'=>[
'GET test'=>'test', 'GET test'=>'test',
'GET swagger'=>'swagger', 'GET swagger'=>'swagger',
'POST login'=>'login',
], ],
], ],
'GET swaggers/swagger/<id>'=>'swagger/swagger', 'GET swaggers/swagger/<id>'=>'swagger/swagger',
......
...@@ -20,6 +20,7 @@ class SwaggerController extends Controller ...@@ -20,6 +20,7 @@ class SwaggerController extends Controller
} }
$swaggerRoot = Yii::getAlias('@swagger'); $swaggerRoot = Yii::getAlias('@swagger');
$swagger = \OpenApi\scan($projectRoot); $swagger = \OpenApi\scan($projectRoot);
$swagger = json_encode($swagger) ; $swagger = json_encode($swagger) ;
$json_file = $swaggerRoot . '/v1/swagger.yaml'; $json_file = $swaggerRoot . '/v1/swagger.yaml';
$is_write = file_put_contents($json_file, $swagger); $is_write = file_put_contents($json_file, $swagger);
......
...@@ -4,6 +4,8 @@ namespace backend\controllers\v1; ...@@ -4,6 +4,8 @@ namespace backend\controllers\v1;
use Yii; use Yii;
use yii\web\Response; use yii\web\Response;
use yii\rest\ActiveController; use yii\rest\ActiveController;
use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBearerAuth;
/** /**
* Base controller * Base controller
* 基类 * 基类
...@@ -23,6 +25,13 @@ class BaseController extends ActiveController ...@@ -23,6 +25,13 @@ class BaseController extends ActiveController
{ {
$behaviors = parent::behaviors(); $behaviors = parent::behaviors();
$behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON; $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
$behaviors['authenticator'] = [
'class' => CompositeAuth::className(),
'class' => HttpBearerAuth::className(),
'optional' => [
'login',
],
];
return $behaviors; return $behaviors;
} }
} }
...@@ -20,6 +20,7 @@ class BranchController extends BaseController ...@@ -20,6 +20,7 @@ class BranchController extends BaseController
* summary="swagger事例", * summary="swagger事例",
* operationId="returnGetParam", * operationId="returnGetParam",
* @OA\Parameter(name="param",in="query",required=true,@OA\Schema(type="string")), * @OA\Parameter(name="param",in="query",required=true,@OA\Schema(type="string")),
* @OA\Parameter(name="Authorization",in="header",required=true,@OA\Schema(type="string")),
* @OA\Response(response="200",description="OK。一切正常"), * @OA\Response(response="200",description="OK。一切正常"),
* @OA\Response(response="201",description="响应 POST 请求时成功创建一个资源"), * @OA\Response(response="201",description="响应 POST 请求时成功创建一个资源"),
* @OA\Response(response="204",description="该请求被成功处理,响应不包含正文内容 (类似 DELETE 请求)"), * @OA\Response(response="204",description="该请求被成功处理,响应不包含正文内容 (类似 DELETE 请求)"),
...@@ -36,6 +37,7 @@ class BranchController extends BaseController ...@@ -36,6 +37,7 @@ class BranchController extends BaseController
* *
* ) * )
*/ */
public function actionTest() public function actionTest()
{ {
$get=Yii::$app->request->get(); $get=Yii::$app->request->get();
......
...@@ -2,18 +2,54 @@ ...@@ -2,18 +2,54 @@
namespace backend\controllers\v1\user; namespace backend\controllers\v1\user;
use Yii;
use backend\controllers\v1\BaseController; use backend\controllers\v1\BaseController;
use backend\models\v1\user\LoginForm;
class UserController extends BaseController class UserController extends BaseController
{ {
public $modelClass='app\models\v1\user\RbacUser'; public $modelClass='app\models\v1\user\ShopRbacUser';
public $serializer = [ public $serializer = [
'class' => 'yii\rest\Serializer', 'class' => 'yii\rest\Serializer',
'collectionEnvelope' => 'user', 'collectionEnvelope' => 'user',
]; ];
public function actionIndex() /**
* @OA\Post(
* path="/guoyongzhi/weiShopNew/backend/web/v1/user/users/login",
* tags={"登陆接口"},
* summary="登陆接口",
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* @OA\Property(
* property="CODE",
* type="string"
* ),
* @OA\Property(
* property="PASSWORD",
* type="string"
* ),
* example={"CODE": 1234567890, "PASSWORD": "123"}
* )
* )
* ),
* @OA\Response(
* response=200,
* description="OK"
* )
* )
*/
public function actionLogin()
{ {
$model=new LoginForm();
$model->code = Yii::$app->request->post('CODE');
$model->password = Yii::$app->request->post('PASSWORD');
if($model->login()){
return array('access_token'=>$model->login());
}else{
$model->validate();
return $model;
}
} }
} }
<?php
namespace backend\models\v1\user;
use Yii;
use yii\base\Model;
use app\models\v1\user\ShopRbacUser;
/**
* Login form
*/
class LoginForm extends Model
{
public $code;
public $password;
private $_shopRbacUser;
/**
* {@inheritdoc}
*/
public function rules()
{
return [
// username and password are both required
[['code', 'password'], 'required'],
// password is validated by validatePassword()
['password', 'validatePassword'],
];
}
/**
* Validates the password.
* This method serves as the inline validation for password.
*
* @param string $attribute the attribute currently being validated
* @param array $params the additional name-value pairs given in the rule
*/
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
$user = $this->getUser();
if (!$user || $user->PASSWORD!=$this->password) {
$this->addError($attribute, '账号或者密码错误.');
}
}
}
/**
* Logs in a user using the provided username and password.
*
* @return bool whether the user is logged in successfully
*/
public function login()
{
if ($this->validate()) {
$accessToken=$this->_shopRbacUser->generateAccessToken();
// 生成的access_token保存一份到mysql和redis中
$this->_shopRbacUser->save();
return $accessToken;
}
return false;
}
/**
* Finds user by [[code]]
*
* @return User|null
*/
protected function getUser()
{
if ($this->_shopRbacUser === null) {
$this->_shopRbacUser = ShopRbacUser::findByUsercode($this->code);
}
return $this->_shopRbacUser;
}
}
...@@ -3,9 +3,10 @@ ...@@ -3,9 +3,10 @@
namespace app\models\v1\user; namespace app\models\v1\user;
use Yii; use Yii;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
/** /**
* This is the model class for table "rbac_user". * This is the model class for table "shop_rbac_user".
* *
* @property string $GUID * @property string $GUID
* @property string $CODE 用户名(手机号&工号) * @property string $CODE 用户名(手机号&工号)
...@@ -32,15 +33,16 @@ use Yii; ...@@ -32,15 +33,16 @@ use Yii;
* @property string $WX_OPENID 小程序OPENID * @property string $WX_OPENID 小程序OPENID
* @property int $AGE 年龄 * @property int $AGE 年龄
* @property string $SHARE_NO 分享码 * @property string $SHARE_NO 分享码
* @property string $ACCESS_TOKEN 验证TOKEN
*/ */
class RbacUser extends ActiveRecord implements IdentityInterface class ShopRbacUser extends ActiveRecord implements IdentityInterface
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function tableName() public static function tableName()
{ {
return 'rbac_user'; return 'shop_rbac_user';
} }
/** /**
...@@ -49,10 +51,11 @@ class RbacUser extends ActiveRecord implements IdentityInterface ...@@ -49,10 +51,11 @@ class RbacUser extends ActiveRecord implements IdentityInterface
public function rules() public function rules()
{ {
return [ return [
[['GUID', 'CODE'], 'required'], [['GUID', 'CODE'], 'required','message' =>'账号密码不能为空'],
[['CREATE_DATE', 'UPDATE_DATE'], 'safe'], [['CREATE_DATE', 'UPDATE_DATE'], 'safe'],
[['IS_FORBID', 'CONTACTSEX', 'IS_BELONG', 'AGE'], 'integer'], [['IS_FORBID', 'CONTACTSEX', 'IS_BELONG', 'AGE'], 'integer'],
[['GUID', 'CODE', 'PASSWORD', 'ORG_GUID', 'EMPLOYEE_GUID', 'CREATE_GUID', 'UPDATE_GUID', 'MOBILEPHONE', 'USER_GUID', 'REMARK', 'ALIPAY_UID', 'WX_UNIONID', 'WX_OPENID', 'SHARE_NO'], 'string', 'max' => 50], [['GUID', 'CODE', 'PASSWORD', 'ORG_GUID', 'EMPLOYEE_GUID', 'CREATE_GUID', 'UPDATE_GUID', 'MOBILEPHONE', 'USER_GUID', 'REMARK', 'ALIPAY_UID', 'WX_UNIONID', 'WX_OPENID', 'SHARE_NO'], 'string', 'max' => 50],
[['PASSWORD'],'required','message' =>'账号密码不能为空'],
[['NAME', 'CREATE_NAME', 'UPDATE_NAME'], 'string', 'max' => 100], [['NAME', 'CREATE_NAME', 'UPDATE_NAME'], 'string', 'max' => 100],
[['DESCRIPTION'], 'string', 'max' => 255], [['DESCRIPTION'], 'string', 'max' => 255],
[['IS_UPLOAD_HX'], 'string', 'max' => 10], [['IS_UPLOAD_HX'], 'string', 'max' => 10],
...@@ -93,4 +96,76 @@ class RbacUser extends ActiveRecord implements IdentityInterface ...@@ -93,4 +96,76 @@ class RbacUser extends ActiveRecord implements IdentityInterface
'SHARE_NO' => 'Share No', 'SHARE_NO' => 'Share No',
]; ];
} }
/**
* Finds user by code
*
* @param string $code
* @return static|null
*/
public static function findByUsercode($code)
{
return static::findOne(['CODE' => $code]);
}
/**
* create access_token
*
* @return access_token|null
*/
public function generateAccessToken()
{
$this->ACCESS_TOKEN= Yii::$app->security->generateRandomString();
return $this->ACCESS_TOKEN;
}
/**
* 根据给到的ID查询身份。
*
* @param string|integer $id 被查询的ID
* @return IdentityInterface|null 通过ID匹配到的身份对象
*/
public static function findIdentity($id)
{
return static::findOne($id);
}
/**
* 根据 token 查询身份。
*
* @param string $token 被查询的 token
* @return IdentityInterface|null 通过 token 得到的身份对象
*/
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['ACCESS_TOKEN' => $token]);
}
/**
* @return int|string 当前用户ID
*/
public function getId()
{
return $this->GUID;
}
/**
* @return string 当前用户的(cookie)认证密钥
*/
public function getAuthKey()
{
return $this->auth_key;
}
/**
* @param string $authKey
* @return boolean if auth key is valid for current user
*/
public function validateAuthKey($authKey)
{
return $this->getAuthKey() === $authKey;
}
/**
* Validates password
*
* @param string $password password to validate
* @return bool if password provided is valid for current user
*/
public function validatePassword($password)
{
return Yii::$app->security->validatePassword($password, $this->password_hash);
}
} }
{"openapi":"3.0.0","info":{"title":"\u540e\u53f0\u63a5\u53e3v1\u7248","contact":{},"version":"v1"},"paths":{"\/guoyongzhi\/weiShopNew\/backend\/web\/v1\/shop\/branch\/branches\/test":{"get":{"tags":["swagger\u4e8b\u4f8b"],"summary":"swagger\u4e8b\u4f8b","description":"swagger\u4e8b\u4f8b","operationId":"returnGetParam","parameters":[{"name":"param","in":"query","required":true,"schema":{"type":"string"}}],"responses":{"200":{"description":"OK\u3002\u4e00\u5207\u6b63\u5e38"},"201":{"description":"\u54cd\u5e94 POST \u8bf7\u6c42\u65f6\u6210\u529f\u521b\u5efa\u4e00\u4e2a\u8d44\u6e90"},"204":{"description":"\u8be5\u8bf7\u6c42\u88ab\u6210\u529f\u5904\u7406\uff0c\u54cd\u5e94\u4e0d\u5305\u542b\u6b63\u6587\u5185\u5bb9 (\u7c7b\u4f3c DELETE \u8bf7\u6c42)"},"304":{"description":"\u8d44\u6e90\u6ca1\u6709\u88ab\u4fee\u6539\u3002\u53ef\u4ee5\u4f7f\u7528\u7f13\u5b58\u7684\u7248\u672c"},"400":{"description":"\u9519\u8bef\u7684\u8bf7\u6c42\u3002\u53ef\u80fd\u901a\u8fc7\u7528\u6237\u65b9\u9762\u7684\u591a\u79cd\u539f\u56e0\u5f15\u8d77\u7684\uff0c\u4f8b\u5982\u5728\u8bf7\u6c42\u4f53\u5185\u6709\u65e0\u6548\u7684JSON \u6570\u636e\uff0c\u65e0\u6548\u7684\u64cd\u4f5c\u53c2\u6570\uff0c\u7b49\u7b49"},"401":{"description":"\u9a8c\u8bc1\u5931\u8d25"},"403":{"description":"\u5df2\u7ecf\u7ecf\u8fc7\u8eab\u4efd\u9a8c\u8bc1\u7684\u7528\u6237\u4e0d\u5141\u8bb8\u8bbf\u95ee\u6307\u5b9a\u7684 API \u672b\u7aef"},"404":{"description":"\u6240\u8bf7\u6c42\u7684\u8d44\u6e90\u4e0d\u5b58\u5728\u3002"},"405":{"description":"\u4e0d\u88ab\u5141\u8bb8\u7684\u65b9\u6cd5\u3002 \u8bf7\u68c0\u67e5 Allow header \u5141\u8bb8\u7684HTTP\u65b9\u6cd5"},"415":{"description":"\u4e0d\u652f\u6301\u7684\u5a92\u4f53\u7c7b\u578b\u3002 \u6240\u8bf7\u6c42\u7684\u5185\u5bb9\u7c7b\u578b\u6216\u7248\u672c\u53f7\u662f\u65e0\u6548\u7684"},"422":{"description":"\u6570\u636e\u9a8c\u8bc1\u5931\u8d25 (\u4f8b\u5982\uff0c\u54cd\u5e94\u4e00\u4e2a POST \u8bf7\u6c42)\u3002 \u8bf7\u68c0\u67e5\u54cd\u5e94\u4f53\u5185\u8be6\u7ec6\u7684\u9519\u8bef\u6d88\u606f"},"429":{"description":"\u8bf7\u6c42\u8fc7\u591a\u3002 \u7531\u4e8e\u9650\u901f\u8bf7\u6c42\u88ab\u62d2\u7edd"},"500":{"description":"\u5185\u90e8\u670d\u52a1\u5668\u9519\u8bef\u3002 \u8fd9\u53ef\u80fd\u662f\u7531\u4e8e\u5185\u90e8\u7a0b\u5e8f\u9519\u8bef\u5f15\u8d77\u7684"}}}}}} {"openapi":"3.0.0","info":{"title":"\u540e\u53f0\u63a5\u53e3v1\u7248","contact":{},"version":"v1"},"paths":{"\/guoyongzhi\/weiShopNew\/backend\/web\/v1\/shop\/branch\/branches\/test":{"get":{"tags":["swagger\u4e8b\u4f8b"],"summary":"swagger\u4e8b\u4f8b","description":"swagger\u4e8b\u4f8b","operationId":"returnGetParam","parameters":[{"name":"param","in":"query","required":true,"schema":{"type":"string"}},{"name":"Authorization","in":"header","required":true,"schema":{"type":"string"}}],"responses":{"200":{"description":"OK\u3002\u4e00\u5207\u6b63\u5e38"},"201":{"description":"\u54cd\u5e94 POST \u8bf7\u6c42\u65f6\u6210\u529f\u521b\u5efa\u4e00\u4e2a\u8d44\u6e90"},"204":{"description":"\u8be5\u8bf7\u6c42\u88ab\u6210\u529f\u5904\u7406\uff0c\u54cd\u5e94\u4e0d\u5305\u542b\u6b63\u6587\u5185\u5bb9 (\u7c7b\u4f3c DELETE \u8bf7\u6c42)"},"304":{"description":"\u8d44\u6e90\u6ca1\u6709\u88ab\u4fee\u6539\u3002\u53ef\u4ee5\u4f7f\u7528\u7f13\u5b58\u7684\u7248\u672c"},"400":{"description":"\u9519\u8bef\u7684\u8bf7\u6c42\u3002\u53ef\u80fd\u901a\u8fc7\u7528\u6237\u65b9\u9762\u7684\u591a\u79cd\u539f\u56e0\u5f15\u8d77\u7684\uff0c\u4f8b\u5982\u5728\u8bf7\u6c42\u4f53\u5185\u6709\u65e0\u6548\u7684JSON \u6570\u636e\uff0c\u65e0\u6548\u7684\u64cd\u4f5c\u53c2\u6570\uff0c\u7b49\u7b49"},"401":{"description":"\u9a8c\u8bc1\u5931\u8d25"},"403":{"description":"\u5df2\u7ecf\u7ecf\u8fc7\u8eab\u4efd\u9a8c\u8bc1\u7684\u7528\u6237\u4e0d\u5141\u8bb8\u8bbf\u95ee\u6307\u5b9a\u7684 API \u672b\u7aef"},"404":{"description":"\u6240\u8bf7\u6c42\u7684\u8d44\u6e90\u4e0d\u5b58\u5728\u3002"},"405":{"description":"\u4e0d\u88ab\u5141\u8bb8\u7684\u65b9\u6cd5\u3002 \u8bf7\u68c0\u67e5 Allow header \u5141\u8bb8\u7684HTTP\u65b9\u6cd5"},"415":{"description":"\u4e0d\u652f\u6301\u7684\u5a92\u4f53\u7c7b\u578b\u3002 \u6240\u8bf7\u6c42\u7684\u5185\u5bb9\u7c7b\u578b\u6216\u7248\u672c\u53f7\u662f\u65e0\u6548\u7684"},"422":{"description":"\u6570\u636e\u9a8c\u8bc1\u5931\u8d25 (\u4f8b\u5982\uff0c\u54cd\u5e94\u4e00\u4e2a POST \u8bf7\u6c42)\u3002 \u8bf7\u68c0\u67e5\u54cd\u5e94\u4f53\u5185\u8be6\u7ec6\u7684\u9519\u8bef\u6d88\u606f"},"429":{"description":"\u8bf7\u6c42\u8fc7\u591a\u3002 \u7531\u4e8e\u9650\u901f\u8bf7\u6c42\u88ab\u62d2\u7edd"},"500":{"description":"\u5185\u90e8\u670d\u52a1\u5668\u9519\u8bef\u3002 \u8fd9\u53ef\u80fd\u662f\u7531\u4e8e\u5185\u90e8\u7a0b\u5e8f\u9519\u8bef\u5f15\u8d77\u7684"}}}},"\/guoyongzhi\/weiShopNew\/backend\/web\/v1\/user\/users\/login":{"post":{"tags":["\u767b\u9646\u63a5\u53e3"],"summary":"\u767b\u9646\u63a5\u53e3","operationId":"backend\\controllers\\v1\\user\\UserController::actionLogin","requestBody":{"content":{"application\/json":{"schema":{"properties":{"CODE":{"type":"string"},"PASSWORD":{"type":"string"}},"type":"object","example":{"CODE":1234567890,"PASSWORD":"123"}}}}},"responses":{"200":{"description":"OK"}}}}}}
\ No newline at end of file \ No newline at end of file
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