OperationBase.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace app\common\services\operation;
  3. use app\common\models\BaseModel;
  4. use app\common\models\OperationLog;
  5. use app\common\models\user\User;
  6. abstract class OperationBase
  7. {
  8. //基础模型
  9. protected $model;
  10. //需要记录的参数数组
  11. protected $logs;
  12. public $modules = '';
  13. public $type = '';
  14. public $modify_fields = [];
  15. /**
  16. * @param $model mixed
  17. * @param $operate_type string 判断记录类型
  18. */
  19. public function __construct($model, $operate_type)
  20. {
  21. $this->uid = intval(\YunShop::app()->uid);
  22. if (empty($this->uid) || is_null($operate_type)) {
  23. return;
  24. }
  25. $this->model = $model;
  26. $this->setDefault();
  27. $this->modifyDefault();
  28. $this->log($operate_type);
  29. }
  30. /**
  31. * 修改一下特殊默认参数
  32. */
  33. protected function modifyDefault()
  34. {
  35. }
  36. /**
  37. * 设置默认保存公共参数
  38. */
  39. protected function setDefault()
  40. {
  41. $user_name = User::where('uid', $this->uid)->value('username'); //todo 由于查询users表太慢,不存操作人
  42. if ($user_name) {
  43. $this->logs['user_name'] = $user_name;
  44. }
  45. $this->logs['user_id'] = $this->uid;
  46. $this->logs['uniacid'] = \YunShop::app()->uniacid;
  47. $this->logs['ip'] = $_SERVER['REMOTE_ADDR'];
  48. $this->logs['modules'] = $this->modules;
  49. $this->logs['type'] = $this->type;
  50. //$this->logs['input'] = json_encode(request()->all(), JSON_UNESCAPED_UNICODE); //todo 数据过大不存,可考虑附表
  51. }
  52. protected function log($type)
  53. {
  54. if ($type == 'create') {
  55. $this->createLog();
  56. } elseif ($type == 'update') {
  57. $this->updateLog();
  58. } elseif ($type == 'special') {
  59. $this->special();
  60. OperationLog::create($this->logs);
  61. }
  62. }
  63. protected function special()
  64. {
  65. }
  66. protected function updateLog()
  67. {
  68. $fields = $this->recordField();
  69. $modify_fields = $this->modifyField();
  70. //没有修改的值,不记录
  71. if (empty($modify_fields)) {
  72. return;
  73. }
  74. foreach ($fields as $key => $value) {
  75. if ($modify_fields[$key]) {
  76. $this->setLog('field', $key);
  77. if (is_string($value)) {
  78. $this->setLog('field_name', $value);
  79. $old_content = $this->dealWith($modify_fields[$key]['old_content']);
  80. $new_content = $this->dealWith($modify_fields[$key]['new_content']);
  81. } elseif (is_array($value)) {
  82. $this->setLog('field_name', $value['field_name']);
  83. $old_content = $value[$modify_fields[$key]['old_content']];
  84. $new_content = $value[$modify_fields[$key]['new_content']];
  85. }
  86. $this->setLog('old_content', $old_content);
  87. $this->setLog('new_content', $new_content);
  88. OperationLog::create($this->logs);
  89. }
  90. }
  91. }
  92. protected function createLog()
  93. {
  94. $fields = $this->recordField();
  95. $modify_fields = $this->modifyField();
  96. //没有值,记录失败
  97. if (empty($modify_fields)) {
  98. $this->setLog('extend', '创建模型的记录失败');
  99. $this->setLog('status', 1);
  100. $this->setLog('input', json_encode($this->model, JSON_UNESCAPED_UNICODE));
  101. OperationLog::create($this->logs);
  102. return;
  103. }
  104. foreach ($fields as $key => $value) {
  105. if ($modify_fields[$key]) {
  106. $this->setLog('field', $key);
  107. if (is_string($value)) {
  108. $this->setLog('field_name', $value);
  109. $old_content = $this->dealWith($modify_fields[$key]['old_content']);
  110. $new_content = $this->dealWith($modify_fields[$key]['new_content']);
  111. } elseif (is_array($value)) {
  112. $this->setLog('field_name', $value['field_name']);
  113. $old_content = $value[$modify_fields[$key]['old_content']];
  114. $new_content = $value[$modify_fields[$key]['new_content']];
  115. }
  116. $this->setLog('old_content', $old_content);
  117. $this->setLog('new_content', $new_content);
  118. OperationLog::create($this->logs);
  119. }
  120. }
  121. }
  122. protected function dealWith($data)
  123. {
  124. if (is_array($data)) {
  125. return $data[$data['key']];
  126. }
  127. return $data;
  128. }
  129. /**
  130. * 获取模型需要记录的字段
  131. * @return mixed
  132. */
  133. abstract protected function recordField();
  134. /**
  135. * 获取模型修改了哪些字段
  136. * @param object array
  137. * @return array
  138. */
  139. abstract protected function modifyField();
  140. /**
  141. * 设置日志值
  142. * @param $log
  143. * @param $logValue
  144. * @return string
  145. */
  146. public function setLog($log, $logValue)
  147. {
  148. $this->logs[$log] = $logValue?:'';
  149. }
  150. /**
  151. * 获取日志值
  152. * @param $log
  153. * @return string
  154. */
  155. public function getLog($log)
  156. {
  157. return isset($this->logs[$log])?$this->logs[$log] : '';
  158. }
  159. /**
  160. *获取所有请求的参数
  161. *@return array
  162. */
  163. public function getAllLogs()
  164. {
  165. return $this->logs;
  166. }
  167. }