Auto.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace app\wechat\controller;
  3. use think\admin\Controller;
  4. use think\admin\extend\CodeExtend;
  5. /**
  6. * 关注自动回复
  7. * Class Auto
  8. * @package app\wechat\controller
  9. */
  10. class Auto extends Controller
  11. {
  12. /**
  13. * 绑定数据表
  14. * @var string
  15. */
  16. private $table = 'WechatAuto';
  17. /**
  18. * 消息类型
  19. * @var array
  20. */
  21. public $types = [
  22. 'text' => '文字', 'news' => '图文',
  23. 'image' => '图片', 'music' => '音乐',
  24. 'video' => '视频', 'voice' => '语音',
  25. ];
  26. /**
  27. * 关注自动回复
  28. * @auth true
  29. * @menu true
  30. * @throws \think\db\exception\DataNotFoundException
  31. * @throws \think\db\exception\DbException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. */
  34. public function index()
  35. {
  36. $this->title = '关注自动回复';
  37. $query = $this->_query($this->table)->like('code,type');
  38. $query->equal('status')->dateBetween('create_at')->order('time asc')->page();
  39. }
  40. /**
  41. * 列表数据处理
  42. * @param array $data
  43. */
  44. protected function _index_page_filter(array &$data)
  45. {
  46. foreach ($data as &$vo) {
  47. $vo['type'] = $this->types[$vo['type']] ?? $vo['type'];
  48. }
  49. }
  50. /**
  51. * 添加自动回复
  52. * @auth true
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\DbException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. */
  57. public function add()
  58. {
  59. $this->title = '添加自动回复';
  60. $this->_form($this->table, 'form');
  61. }
  62. /**
  63. * 编辑自动回复
  64. * @auth true
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\DbException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. */
  69. public function edit()
  70. {
  71. $this->title = '编辑自动回复';
  72. $this->_form($this->table, 'form');
  73. }
  74. /**
  75. * 添加数据处理
  76. * @param array $data
  77. */
  78. protected function _form_filter(array &$data)
  79. {
  80. if (empty($data['code'])) {
  81. $data['code'] = CodeExtend::uniqidNumber(16, 'M');
  82. }
  83. if ($this->request->isGet()) {
  84. $public = dirname($this->request->basefile(true));
  85. $this->defaultImage = "{$public}/static/theme/img/image.png";
  86. }
  87. }
  88. /**
  89. * 表单结果处理
  90. * @param boolean $result
  91. */
  92. protected function _form_result(bool $result)
  93. {
  94. if ($result !== false) {
  95. $this->success('恭喜, 关键字保存成功!', 'javascript:history.back()');
  96. } else {
  97. $this->error('关键字保存失败, 请稍候再试!');
  98. }
  99. }
  100. /**
  101. * 修改规则状态
  102. * @auth true
  103. * @throws \think\db\exception\DbException
  104. */
  105. public function state()
  106. {
  107. $this->_save($this->table, $this->_vali([
  108. 'status.in:0,1' => '状态值范围异常!',
  109. 'status.require' => '状态值不能为空!',
  110. ]));
  111. }
  112. /**
  113. * 删除自动回复
  114. * @auth true
  115. * @throws \think\db\exception\DbException
  116. */
  117. public function remove()
  118. {
  119. $this->_delete($this->table);
  120. }
  121. }