NoticeEvent.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace app\admin\model\notice;
  3. use think\Model;
  4. use think\db\Query;
  5. use traits\model\SoftDelete;
  6. class NoticeEvent extends Model
  7. {
  8. use SoftDelete;
  9. // 表名
  10. protected $name = 'notice_event';
  11. // 自动写入时间戳字段
  12. protected $autoWriteTimestamp = 'int';
  13. // 定义时间戳字段名
  14. protected $createTime = 'createtime';
  15. protected $updateTime = 'updatetime';
  16. protected $deleteTime = 'deletetime';
  17. // 追加属性
  18. protected $append = [
  19. // 'platform_text',
  20. // 'type_text',
  21. // 'visible_switch_text'
  22. ];
  23. public function getContentArrAttr()
  24. {
  25. $value = $this['content'];
  26. $value = (array) json_decode($value);
  27. return $value;
  28. }
  29. public function getPlatformList()
  30. {
  31. return ['user' => __('Platform user'), 'admin' => __('Platform admin')];
  32. }
  33. public function getTypeList()
  34. {
  35. return ['msg' => __('Type msg'), 'email' => __('Type email'), 'mptemplate' => '模版消息(公众号)'];
  36. }
  37. public function getVisibleSwitchList()
  38. {
  39. return ['0' => __('Visible_switch 0'), '1' => __('Visible_switch 1')];
  40. }
  41. public function getPlatformTextAttr($value, $data)
  42. {
  43. $value = $value ? $value : (isset($data['platform']) ? $data['platform'] : '');
  44. $valueArr = explode(',', $value);
  45. $list = $this->getPlatformList();
  46. return implode(',', array_intersect_key($list, array_flip($valueArr)));
  47. }
  48. public function getTypeTextAttr($value, $data)
  49. {
  50. $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
  51. $valueArr = explode(',', $value);
  52. $list = $this->getTypeList();
  53. return implode(',', array_intersect_key($list, array_flip($valueArr)));
  54. }
  55. public function getVisibleSwitchTextAttr($value, $data)
  56. {
  57. $value = $value ? $value : (isset($data['visible_switch']) ? $data['visible_switch'] : '');
  58. $list = $this->getVisibleSwitchList();
  59. return isset($list[$value]) ? $list[$value] : '';
  60. }
  61. protected function setPlatformAttr($value)
  62. {
  63. return is_array($value) ? implode(',', $value) : $value;
  64. }
  65. protected function setTypeAttr($value)
  66. {
  67. return is_array($value) ? implode(',', $value) : $value;
  68. }
  69. public function scopeFrontend(Query $query, $params = [])
  70. {
  71. $query->where('__TABLE__.visible_switch', 1)
  72. ->order(['__TABLE__.id'=>'desc']);
  73. }
  74. }