ActionLog.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use yii\db\ActiveRecord;
  9. use yii\behaviors\TimestampBehavior;
  10. use yii\db\BaseActiveRecord;
  11. use yii\helpers\Json;
  12. /**
  13. * Class ActionLog
  14. * @package app\modules\common\models
  15. *
  16. * @property integer $id
  17. * @property string $route
  18. * @property integer $admin_id
  19. * @property string $type
  20. * @property string $data_id
  21. * @property string $created_at
  22. * @property string $created_at_micro
  23. * @property string $get_params
  24. * @property string $post_params
  25. * @property string $header_params
  26. * @property string $response_params
  27. * @property int $status
  28. * @property string $addr_ip
  29. * @property int $spend_time
  30. * @property string $request_method
  31. * @property string $name
  32. * @property string $role
  33. */
  34. class ActionLog extends ActiveRecord
  35. {
  36. public static function tableName()
  37. {
  38. return '{{%action_log}}';
  39. }
  40. public function rules()
  41. {
  42. return [
  43. [['route', 'type'], 'required'],
  44. [['id', 'admin_id','status','spend_time'], 'integer'],
  45. [['route', 'type', 'data_id'], 'string', 'max' => 100],
  46. [['get_params', 'post_params', 'header_params', 'response_params', 'create_at','created_at_micro','addr_ip','request_method','name','role'], 'safe']
  47. ];
  48. }
  49. public function attributeLabels()
  50. {
  51. return [
  52. 'id' => 'ID',
  53. 'route' => '路由',
  54. 'admin_id' => '管理员ID',
  55. 'type' => '类型',
  56. 'data_id' => '数据ID',
  57. 'created_at' => '创建时间',
  58. 'get_params' => 'GET参数',
  59. 'post_params' => 'POST参数',
  60. 'header_params' => 'HEADER参数',
  61. 'response_params' => '响应参数'
  62. ];
  63. }
  64. public function beforeSave($insert)
  65. {
  66. if (parent::beforeSave($insert)) {
  67. if ($this->isNewRecord) {
  68. $this->created_at = date('Y-m-d H:i:s');
  69. }
  70. return true;
  71. }
  72. return false;
  73. }
  74. public static function addLog($code,$route,$data_string){
  75. try {
  76. $log = new self();
  77. $log->type = 'log';
  78. $log->route = $route;
  79. $log->admin_id = 1;
  80. $log->request_method = 'POST';
  81. $log->spend_time = rand(1,100);
  82. $log->addr_ip = '';
  83. $log->status = $code;
  84. $response['response'] =['code'=>$code,'data'=>$data_string] ;
  85. $log->response_params = Json::encode($response);
  86. $log->role = 'admin';
  87. $log->name = 'admin';
  88. $log->created_at_micro = microtime(true)*1000;
  89. if (!$log->save()){
  90. //debug_log([__METHOD__, __LINE__, "actionLog:".$log->getErrorSummary(false)[0]], "app_debug.log");
  91. }
  92. }catch (\Throwable $e){
  93. //debug_log([__METHOD__, __LINE__, "actionLog:".$e->getMessage()], "app_debug.log");
  94. }
  95. }
  96. }