From 81f030bdba655fc224f696daaee36487dce7b8b9 Mon Sep 17 00:00:00 2001 From: xieyucheng Date: Thu, 7 May 2020 23:45:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=A4=BA=E4=BE=8B=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 + models/Admin.php | 123 ++++++++++++++++++++++++++++++++++++ models/AdminBehaviorLog.php | 101 +++++++++++++++++++++++++++++ models/BaseActiveRecord.php | 51 +++++++++++++++ 4 files changed, 278 insertions(+) create mode 100644 models/Admin.php create mode 100644 models/AdminBehaviorLog.php create mode 100644 models/BaseActiveRecord.php diff --git a/README.md b/README.md index a9f4d7b..07a101b 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,9 @@ $ composer require xihrni/yii2-behavior-log yii migrate --migrationPath=@vendor/xihrni/yii2-behavior-log/migrations ``` +### Model +复制已准备好的模型到项目中,目录为:`@vendor/xihrni/yii2-behavior-log/models` + ### Controller ```php 否,1=>是 + * @property int $status 状态,0=>禁用,1=>启用 + * @property string $created_at 创建时间 + * @property string $updated_at 更新时间 + * @property string $deleted_at 删除时间 + * @property string $last_login_at 最后登录时间 + * @property string $last_login_ip 最后登录IP + * @property int $allowance 请求剩余次数 + * @property string $allowance_updated_at 请求更新时间 + * + * @property AdminBehaviorLog[] $adminBehaviorLogs + */ +class Admin extends BaseActiveRecord +{ + /** + * {@inheritdoc} + */ + public static function tableName() + { + return '{{%admin}}'; + } + + /** + * {@inheritdoc} + */ + public function rules() + { + return [ + [['id', 'is_trash', 'status'], 'integer', 'min' => 0], + + [['username', 'mobile', 'realname'], 'string', 'max' => 16], + [['password_hash'], 'string', 'max' => 255], + [['password_reset_token', 'auth_key', 'access_token'], 'string', 'max' => 64], + + [['created_at', 'updated_at', 'deleted_at', 'last_login_at'], 'datetime', 'format' => 'yyyy-MM-dd HH:mm:ss'], + + [['last_login_ip'], 'ip'], + [['mobile'], 'match', 'pattern' => '/^1([356789]{1})\d{9}$/'], + + [['password_reset_token', 'auth_key', 'access_token', 'mobile'], 'default', 'value' => null], + [['username', 'password_hash', 'realname'], 'default', 'value' => ''], + [['is_trash', 'allowance', 'allowance_updated_at'], 'default', 'value' => 0], + [['status'], 'default', 'value' => 1], + + [['username'], 'unique'], + [['access_token'], 'unique'], + [['auth_key'], 'unique'], + [['password_reset_token'], 'unique'], + [['mobile'], 'unique'], + ]; + } + + /** + * {@inheritdoc} + */ + public function beforeSave($insert) + { + $isValid = parent::beforeSave($insert); + + if (!$isValid) { + return $isValid; + } + + // 更新操作 + if (!$insert) { + // 不允许更新的字段 + $this->username = $this->oldAttributes['username']; + } + + return true; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'id' => Yii::t('app', 'ID'), + 'username' => Yii::t('app', '用户名'), + 'password_hash' => Yii::t('app', '加密密码'), + 'password_reset_token' => Yii::t('app', '重置密码令牌'), + 'auth_key' => Yii::t('app', '认证密钥'), + 'access_token' => Yii::t('app', '访问令牌'), + 'mobile' => Yii::t('app', '手机号码'), + 'realname' => Yii::t('app', '真实姓名'), + 'is_trash' => Yii::t('app', '是否删除,0=>否,1=>是'), + 'status' => Yii::t('app', '状态,0=>禁用,1=>启用'), + 'created_at' => Yii::t('app', '创建时间'), + 'updated_at' => Yii::t('app', '更新时间'), + 'deleted_at' => Yii::t('app', '删除时间'), + 'last_login_at' => Yii::t('app', '最后登录时间'), + 'last_login_ip' => Yii::t('app', '最后登录IP'), + 'allowance' => Yii::t('app', '请求剩余次数'), + 'allowance_updated_at' => Yii::t('app', '请求更新时间'), + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getAdminBehaviorLogs() + { + return $this->hasMany(AdminBehaviorLog::className(), ['admin_id' => 'id']); + } +} diff --git a/models/AdminBehaviorLog.php b/models/AdminBehaviorLog.php new file mode 100644 index 0000000..9277363 --- /dev/null +++ b/models/AdminBehaviorLog.php @@ -0,0 +1,101 @@ +否,1=>是 + * @property int $status 状态,0=>禁用,1=>启用 + * @property string $created_at 创建时间 + * @property string $updated_at 更新时间 + * @property string $deleted_at 删除时间 + * + * @property Admin $admin + */ +class AdminBehaviorLog extends \yii\db\ActiveRecord +{ + /** + * {@inheritdoc} + */ + public static function tableName() + { + return '{{%admin_behavior_log}}'; + } + + /** + * {@inheritdoc} + */ + public function rules() + { + return [ + [['id', 'admin_id', 'is_trash', 'status'], 'integer', 'min' => 0], + + [['module'], 'string', 'max' => 64], + [['controller', 'action'], 'string', 'max' => 32], + [['route', 'authorization'], 'string', 'max' => 255], + [['method'], 'string', 'max' => 8], + [['request_ip'], 'string', 'max' => 16], + [['headers', 'params', 'body', 'response'], 'string'], + + [['created_at', 'updated_at', 'deleted_at'], 'datetime', 'format' => 'yyyy-MM-dd HH:mm:ss'], + + [['admin_id'], 'default', 'value' => null], + [['module', 'action', 'route', 'controller', 'method', 'headers', 'params', 'body', 'authorization', 'request_ip', 'response',], 'default', 'value' => ''], + [['is_trash'], 'default', 'value' => 0], + [['status'], 'default', 'value' => 1], + + [['admin_id'], 'exist', 'skipOnError' => true, 'targetClass' => Admin::className(), 'targetAttribute' => ['admin_id' => 'id', 0 => 'is_trash']], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'id' => Yii::t('app', 'ID'), + 'admin_id' => Yii::t('app', '管理员ID'), + 'module' => Yii::t('app', '模块'), + 'controller' => Yii::t('app', '控制器'), + 'action' => Yii::t('app', '操作'), + 'route' => Yii::t('app', '路由'), + 'method' => Yii::t('app', '方法'), + 'headers' => Yii::t('app', '请求头(json)'), + 'params' => Yii::t('app', '请求参数(json)'), + 'body' => Yii::t('app', '请求体(json)'), + 'authorization' => Yii::t('app', '身份认证'), + 'request_ip' => Yii::t('app', '请求IP'), + 'response' => Yii::t('app', '响应结果(json)'), + 'is_trash' => Yii::t('app', '是否删除,0=>否,1=>是'), + 'status' => Yii::t('app', '状态,0=>禁用,1=>启用'), + 'created_at' => Yii::t('app', '创建时间'), + 'updated_at' => Yii::t('app', '更新时间'), + 'deleted_at' => Yii::t('app', '删除时间'), + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getAdmin() + { + return $this->hasOne(Admin::className(), ['id' => 'admin_id']); + } +} diff --git a/models/BaseActiveRecord.php b/models/BaseActiveRecord.php new file mode 100644 index 0000000..5b6340d --- /dev/null +++ b/models/BaseActiveRecord.php @@ -0,0 +1,51 @@ + [ + 'class' => AttributeTypecastBehavior::className(), + 'typecastAfterValidate' => true, + 'typecastBeforeSave' => true, + 'typecastAfterFind' => true, + ], + 'time' => [ + 'class' => TimeBehavior::className(), + ], + ]); + } + + /** + * 软删除 + * + * @return bool + */ + public function softDelete() + { + $this->is_trash = 1; + $this->deleted_at = date('Y-m-d H:i:s'); + + // TODO 更新唯一索引值 + + return $this->save(true, ['is_trash', 'deleted_at']); + } +}