DataLogService.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\data\Logs as DataLogs;
  4. use app\common\traits\ServiceTrait;
  5. use think\Log;
  6. class DataLogService
  7. {
  8. use ServiceTrait;
  9. /** @var DataLogs */
  10. public static $Model = DataLogs::class;
  11. /**
  12. * @param $table
  13. * @param $log_table
  14. * @param $log_table_id
  15. * @param $before_data
  16. * @param $after_data
  17. * @param string $remark
  18. * @return mixed
  19. * @throws \Exception
  20. */
  21. public static function addLog($table, $log_table, $log_table_id, $before_data, $after_data, string $remark = '')
  22. {
  23. $data = [
  24. 'table' => $table,
  25. 'log_table' => $log_table,
  26. 'log_table_id' => $log_table_id,
  27. 'before_data' => $before_data,
  28. 'after_data' => $after_data,
  29. 'remark' => $remark,
  30. ];
  31. $res = self::create($data);
  32. if (!$res) {
  33. Log::info(__CLASS__ . ' 写入数据变动日志失败:' . json_encode($data, JSON_UNESCAPED_UNICODE));
  34. exception('写入数据变动日志失败');
  35. }
  36. return $res;
  37. }
  38. }