Messagefeedback.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\model\MessageFeedback as MessageFeedbackModel;
  5. class Messagefeedback extends Backend
  6. {
  7. protected $modelClass = null;
  8. protected $modelValidate = false;
  9. protected $searchFields = 'id,feedback_type_text,name,phone,email,content,lang,source_page,ip,user_agent,admin_remark';
  10. protected function initialize()
  11. {
  12. parent::initialize();
  13. $this->modelClass = new MessageFeedbackModel();
  14. $this->assign('typeList', $this->modelClass->getTypeList());
  15. $this->assign('statusList', $this->modelClass->getStatusList());
  16. }
  17. public function index()
  18. {
  19. if ($this->request->isAjax()) {
  20. $page = (int)$this->request->param('page/d', 1);
  21. $limit = (int)$this->request->param('limit/d', 20);
  22. $sort = (string)$this->request->param('sort/s', 'id');
  23. $order = (string)$this->request->param('order/s', 'desc');
  24. $allowedSort = ['id', 'status', 'createtime', 'updatetime'];
  25. if (!in_array($sort, $allowedSort, true)) {
  26. $sort = 'id';
  27. }
  28. $order = strtolower($order) === 'asc' ? 'asc' : 'desc';
  29. $query = $this->buildQuery();
  30. $count = (clone $query)->count();
  31. $data = $query->order($sort, $order)->page($page, $limit)->select();
  32. return json(['code' => 0, 'count' => $count, 'data' => $data]);
  33. }
  34. return $this->fetch();
  35. }
  36. public function del()
  37. {
  38. if (!$this->request->isPost()) {
  39. $this->error('请求方式错误');
  40. }
  41. $ids = $this->request->post('ids/a', []);
  42. if (!$ids) {
  43. $this->error('请先选择反馈记录');
  44. }
  45. $count = $this->modelClass->where('id', 'in', $ids)->delete();
  46. $this->success('已删除 ' . (int)$count . ' 条反馈记录');
  47. }
  48. public function changeStatus()
  49. {
  50. if (!$this->request->isPost()) {
  51. $this->error('请求方式错误');
  52. }
  53. $id = (int)$this->request->post('id/d', 0);
  54. $status = (int)$this->request->post('status/d', -1);
  55. if ($id <= 0) {
  56. $this->error('记录不存在');
  57. }
  58. if (!array_key_exists($status, $this->modelClass->getStatusList())) {
  59. $this->error('状态参数错误');
  60. }
  61. $row = $this->modelClass->where('id', $id)->find();
  62. if (!$row) {
  63. $this->error('记录不存在');
  64. }
  65. $row->save(['status' => $status, 'updatetime' => time()]);
  66. $this->success('状态已更新');
  67. }
  68. public function remark()
  69. {
  70. if (!$this->request->isPost()) {
  71. $this->error('请求方式错误');
  72. }
  73. $id = (int)$this->request->post('id/d', 0);
  74. $remark = trim((string)$this->request->post('admin_remark/s', ''));
  75. $remark = mb_substr(strip_tags($remark), 0, 500, 'UTF-8');
  76. if ($id <= 0) {
  77. $this->error('记录不存在');
  78. }
  79. $row = $this->modelClass->where('id', $id)->find();
  80. if (!$row) {
  81. $this->error('记录不存在');
  82. }
  83. $row->save(['admin_remark' => $remark, 'updatetime' => time()]);
  84. $this->success('备注已保存');
  85. }
  86. protected function buildQuery()
  87. {
  88. $keyword = trim((string)$this->request->param('keyword/s', ''));
  89. $feedbackType = trim((string)$this->request->param('feedback_type/s', ''));
  90. $lang = trim((string)$this->request->param('lang/s', ''));
  91. $status = $this->request->param('status/s', '');
  92. $ip = trim((string)$this->request->param('ip/s', ''));
  93. $sourcePage = trim((string)$this->request->param('source_page/s', ''));
  94. $createtimeStart = trim((string)$this->request->param('createtime_start/s', ''));
  95. $createtimeEnd = trim((string)$this->request->param('createtime_end/s', ''));
  96. $query = $this->modelClass;
  97. if ($keyword !== '') {
  98. $query = $query->where(function ($q) use ($keyword) {
  99. $q->where('name|phone|email|content|admin_remark', 'like', '%' . $keyword . '%');
  100. });
  101. }
  102. if ($feedbackType !== '') {
  103. $query = $query->where('feedback_type', $feedbackType);
  104. }
  105. if ($lang !== '') {
  106. $query = $query->where('lang', $lang);
  107. }
  108. if ($status !== '') {
  109. $query = $query->where('status', (int)$status);
  110. }
  111. if ($ip !== '') {
  112. $query = $query->where('ip', 'like', '%' . $ip . '%');
  113. }
  114. if ($sourcePage !== '') {
  115. $query = $query->where('source_page', 'like', '%' . $sourcePage . '%');
  116. }
  117. if ($createtimeStart !== '') {
  118. $timestamp = strtotime($createtimeStart);
  119. if ($timestamp) {
  120. $query = $query->where('createtime', '>=', $timestamp);
  121. }
  122. }
  123. if ($createtimeEnd !== '') {
  124. $timestamp = strtotime($createtimeEnd);
  125. if ($timestamp) {
  126. $query = $query->where('createtime', '<=', $timestamp);
  127. }
  128. }
  129. return $query;
  130. }
  131. }