AdminLog.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. /**
  5. * This is the model class for table "{{%article}}".
  6. **/
  7. class AdminLog extends \yii\db\ActiveRecord
  8. {
  9. /**
  10. * @inheritdoc
  11. */
  12. public static function tableName()
  13. {
  14. return '{{%admin_log}}';
  15. }
  16. /**
  17. * @inheritdoc
  18. */
  19. public function attributeLabels()
  20. {
  21. return [
  22. 'id'=>'操作记录ID',
  23. 'title'=>'操作记录描述',
  24. 'addtime'=>'记录时间',
  25. 'admin_name'=>'操作人姓名',
  26. 'admin_ip'=>'操作人IP地址',
  27. 'admin_agent'=>'操作人浏览器代理商',
  28. 'handle_id'=>'操作控制器名称',
  29. 'type'=>'操作类型',
  30. 'model'=>'操作数据编号',
  31. 'result'=>'操作结果',
  32. ];
  33. }
  34. public static function saveLog($controller ,$action,$result,$objId){
  35. $model = new self;
  36. $model->admin_ip = Yii::$app->request->userIP;
  37. $headers = Yii::$app->request->headers;
  38. $model->addtime = time();
  39. if ($headers->has('User-Agent')) {
  40. $model->admin_agent = $headers->get('User-Agent');
  41. }
  42. $model->admin_id = Yii::$app->user->identity->id;
  43. $model->admin_name = Yii::$app->user->identity->email;
  44. $controllers = ['article','video','collection','collection-album','category','banner','exchange','user','admin'];
  45. if(!in_array(strtolower($controller),$controllers)) $controller = '';
  46. $actions = ['create','update','delete','login','logout'];
  47. if(!in_array(strtolower($action),$actions))$action = '';
  48. $model->handle_id = $controller;
  49. $model->type = $action;
  50. $model->result = $result;
  51. $model->model = $objId;
  52. $model->title = $model->admin_name.' '.$model->type.' '.$model->handle_id;
  53. $model->save(false);
  54. }
  55. }