Listen.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace catchAdmin\api;
  3. use catchAdmin\api\model\Publish as ModelPublish;
  4. use Exception;
  5. use think\facade\Db;
  6. trait Listen
  7. {
  8. public $imei;
  9. public static function onAfterInsert($obj)
  10. {
  11. $data = null;
  12. if (method_exists($obj, 'addContent')) {
  13. $data = $obj->addContent($obj);
  14. } else {
  15. throw new Exception('addContent函数不存在');
  16. }
  17. //是否要过滤
  18. if (array_key_exists('is_filter', $data)) {
  19. if ($data['is_filter']) {
  20. return;
  21. }
  22. }
  23. $time = msectime();
  24. $content = [
  25. 'Type' => 'add',
  26. 'ContentType' => $data['type'],
  27. 'Imei' => $obj->imei ? '' : $obj->imei,
  28. 'ContentId' => $data['data']['id'],
  29. 'AddTime' => $time,
  30. 'Version' => $time,
  31. 'Status' => 1,
  32. 'Content' => json_encode($data['data'])
  33. ];
  34. Db::name('publish')->save($content);
  35. }
  36. public static function onAfterUpdate($obj)
  37. {
  38. $data = [];
  39. $where = [];
  40. $time = null;
  41. $content = null;
  42. if (method_exists($obj, 'addContent')) {
  43. $data = $obj->addContent($obj);
  44. } else {
  45. throw new Exception('addContent函数不存在');
  46. }
  47. //是否要过滤
  48. if (array_key_exists('is_filter', $data)) {
  49. if ($data['is_filter']) {
  50. return;
  51. }
  52. }
  53. $time = msectime();
  54. $where = [];
  55. $where[] = ['ContentType', '=', $data['type']];
  56. $where[] = ['ContentId', '=', $data['data']['id']];
  57. $content = [
  58. 'Type' => 'update',
  59. 'Imei' => $obj->imei ? '' : $obj->imei,
  60. 'Version' => $time,
  61. 'Status' => 1,
  62. 'Content' => json_encode($data['data'])
  63. ];
  64. Db::name('publish')->where($where)->update($content);
  65. }
  66. public static function onAfterDelete($obj)
  67. {
  68. $data = [];
  69. $where = [];
  70. $time = null;
  71. $content = null;
  72. if (method_exists($obj, 'addContent')) {
  73. $data = $obj->addContent($obj);
  74. } else {
  75. throw new Exception('addContent函数不存在');
  76. }
  77. //是否要过滤
  78. if (array_key_exists('is_filter', $data)) {
  79. if ($data['is_filter']) {
  80. return;
  81. }
  82. }
  83. $time = msectime();
  84. $where = [];
  85. $where[] = ['ContentType', '=', $data['type']];
  86. $where[] = ['ContentId', '=', $data['data']['id']];
  87. $content = [
  88. 'Type' => 'delete',
  89. 'Imei' => $obj->imei ? '' : $obj->imei,
  90. 'Version' => $time,
  91. 'Status' => 1,
  92. //'Content' => json_encode($data['data'])
  93. ];
  94. Db::name('publish')->where($where)->update($content);
  95. }
  96. }