| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- namespace common\models\forms;
- use common\models\LogAdminLogin;
- use common\models\LogUserLogin;
- use Yii;
- class LogUserLoginForm extends \yii\base\Model
- {
- const KEY_LOG_COLUMN = 'key_log';
- public $_id;
- public $user_name;
- public $ip;
- public $created_at;
- public $user_agent;
- public $period_num;
- public $opt_type;
- public $success_times;
- public $fail_times;
- public $device;
- public $request_route;
- public $return_result;
- /**
- * DB
- * @return \yii\db\Connection the database connection used by this AR class.
- * @throws \yii\base\InvalidConfigException
- */
- public static function getDb()
- {
- return Yii::$app->get('mongodb');
- }
- /**
- * @inheritdoc
- */
- public function rules(){
- return [
- [['user_name', 'ip', 'request_route'], 'required'],
- [['created_at', 'success_times', 'period_num', 'fail_times'], 'filter', 'filter' => function($value){
- return ($value) ? $value : 0;
- }],
- [['user_name', 'ip', 'opt_type'], 'string', 'max' => 16],
- [['_id', 'return_result', 'device',], 'safe'],
- [['user_agent', 'request_route'], 'string', 'max' => 1000],
- ];
- }
- /**
- * @inheritdoc
- */
- public function beforeValidate() {
- return parent::beforeValidate();
- }
- /**
- * @inheritdoc
- */
- public function scenarios() {
- return parent::scenarios();
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels(){
- return [
- '_id' => 'ID',
- 'user_name' => '对象编号',
- 'ip' => '操作ip',
- 'created_at' => '创建时间',
- 'user_agent' => '浏览器特征',
- 'period_num' => '期数',
- 'opt_type' => '操作类型',
- 'success_times' => '登陆成功次数',
- 'fail_times' => '登录失败次数',
- 'device' => '客户端',
- 'request_route' => '请求路径',
- 'return_result' => '返回内容',
- ];
- }
- /**
- * 添加
- * @return bool|LogAdminLogin
- */
- public function add(){
- if(!$this->validate()){
- return false;
- }
- $model = new LogUserLogin();
- $model->user_name = $this->user_name;
- $model->ip = $this->ip;
- $model->created_at = intval($this->created_at);
- $model->user_agent = $this->user_agent;
- $model->period_num = intval($this->period_num);
- $model->opt_type = $this->opt_type;
- $model->success_times = intval($this->success_times);
- $model->fail_times = intval($this->fail_times);
- $model->device = $this->device;
- $model->request_route = $this->request_route;
- $model->return_result = $this->return_result;
- if(!$model->save()){
- return false;
- }
- return $model;
- }
- /**
- * 删除
- * @param $id
- * @return array|bool|null|\yii\mongodb\ActiveRecord
- * @throws \yii\db\StaleObjectException
- */
- public function delete($id){
- $row = LogUserLogin::find()->select(['user_name'])->where(['_id'=>$id])->one();
- if(!$row){
- $this->setError('要删除的日志不存在');
- return false;
- }
- if(!$row->delete()){
- $this->setError('日志删除失败');
- }
- return $row;
- }
- }
|