AdminLog.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/3/4
  6. * Time: 上午9:25
  7. */
  8. namespace common\models;
  9. use Yii;
  10. //use yii\db\ActiveRecord;
  11. //use yii\behaviors\TimestampBehavior;
  12. class AdminLog extends \yii\db\ActiveRecord{
  13. /**
  14. * 修改操作.
  15. * @param obj $event
  16. * @return mixed
  17. */
  18. public static function afterUpdate($event)
  19. {
  20. if(!empty($event->changedAttributes)) {
  21. // 内容
  22. $arr['changedAttributes'] = $event->changedAttributes;
  23. $arr['oldAttributes'] = [];
  24. foreach($event->sender as $key => $value) {
  25. $arr['oldAttributes'][$key] = $value;
  26. }
  27. $description = json_encode($arr);
  28. $log = new \common\models\AdminLog();
  29. $log->admin_id = Yii::$app->session['user_info']['uid'];
  30. $log->create_at =$_SERVER['REQUEST_TIME'];
  31. $log->admin_ip = Yii::$app->request->userIP;
  32. $log->controller =strtolower(Yii::$app->controller->id);
  33. $log->action =strtolower(Yii::$app->controller->action->id);
  34. $log->table =$event->sender->tableName();
  35. $log->class =$event->sender->className();
  36. $log->description = $description;
  37. $log->type = 2;
  38. if(isset($event->sender->primaryKey) && !empty($event->sender->primaryKey))
  39. {
  40. $log->primary_keys = $event->sender->primaryKey;
  41. }
  42. //特殊操作
  43. if($event->sender->className() == 'common\models\CategoryTelCity' || $event->sender->className() == 'common\models\CategoryTel')
  44. {
  45. Yii::$app->redis->del('params:citytel');
  46. }
  47. if($event->sender->className() == 'common\models\Characteristic')
  48. {
  49. Yii::$app->redis->del('params:characteristic');
  50. }
  51. $log->save(false);
  52. }
  53. }
  54. /**
  55. * 添加操作.
  56. * @param obj $event
  57. * @return mixed
  58. */
  59. public static function afterInsert($event)
  60. {
  61. if($event->sender->className() != 'common\models\UserOperationrecord' && $event->sender->tableName() != self::tableName() ){
  62. $arr = [];
  63. foreach($event->sender as $key => $value) {
  64. $arr[$key] = $value;
  65. }
  66. $description = json_encode($arr);
  67. $log = new \common\models\AdminLog();
  68. $log->admin_id = Yii::$app->session['user_info']['uid'];
  69. $log->create_at =$_SERVER['REQUEST_TIME'];
  70. $log->admin_ip = Yii::$app->request->userIP;
  71. $log->controller =strtolower(Yii::$app->controller->id);
  72. $log->action =strtolower(Yii::$app->controller->action->id);
  73. $log->table =$event->sender->tableName();
  74. $log->class =$event->sender->className();
  75. $log->description = $description;
  76. $log->type = 1;
  77. if(isset($event->sender->primaryKey) && !empty($event->sender->primaryKey))
  78. {
  79. $log->primary_keys = $event->sender->primaryKey;
  80. }
  81. //特殊操作
  82. if($event->sender->className() == 'common\models\CategoryTelCity' || $event->sender->className() == 'common\models\CategoryTel')
  83. {
  84. Yii::$app->redis->del('params:citytel');
  85. }
  86. if($event->sender->className() == 'common\models\Characteristic')
  87. {
  88. Yii::$app->redis->del('params:characteristic');
  89. }
  90. $log->save(false);
  91. }
  92. }
  93. public static function afterDels($event)
  94. {
  95. // 内容
  96. $arr = [];
  97. foreach($event->sender as $key => $value) {
  98. $arr[$key] = $value;
  99. }
  100. $description = json_encode($arr);
  101. $log = new \common\models\AdminLog();
  102. $log->admin_id = Yii::$app->session['user_info']['uid'];
  103. $log->create_at =$_SERVER['REQUEST_TIME'];
  104. $log->admin_ip = Yii::$app->request->userIP;
  105. $log->controller =strtolower(Yii::$app->controller->id);
  106. $log->action =strtolower(Yii::$app->controller->action->id);
  107. $log->table =$event->sender->tableName();
  108. $log->class =$event->sender->className();
  109. $log->description = $description;
  110. $log->type = 3;
  111. if(isset($event->sender->primaryKey) && !empty($event->sender->primaryKey))
  112. {
  113. $log->primary_keys = $event->sender->primaryKey;
  114. }
  115. //特殊操作
  116. if($event->sender->className() == 'common\models\CategoryTelCity' || $event->sender->className() == 'common\models\CategoryTel')
  117. {
  118. Yii::$app->redis->del('params:citytel');
  119. }
  120. if($event->sender->className() == 'common\models\Characteristic')
  121. {
  122. Yii::$app->redis->del('params:characteristic');
  123. }
  124. $log->save(false);
  125. }
  126. }