| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use app\common\model\MessageFeedback as MessageFeedbackModel;
- class Messagefeedback extends Backend
- {
- protected $modelClass = null;
- protected $modelValidate = false;
- protected $searchFields = 'id,feedback_type_text,name,phone,email,content,lang,source_page,ip,user_agent,admin_remark';
- protected function initialize()
- {
- parent::initialize();
- $this->modelClass = new MessageFeedbackModel();
- $this->assign('typeList', $this->modelClass->getTypeList());
- $this->assign('statusList', $this->modelClass->getStatusList());
- }
- public function index()
- {
- if ($this->request->isAjax()) {
- $page = (int)$this->request->param('page/d', 1);
- $limit = (int)$this->request->param('limit/d', 20);
- $sort = (string)$this->request->param('sort/s', 'id');
- $order = (string)$this->request->param('order/s', 'desc');
- $allowedSort = ['id', 'status', 'createtime', 'updatetime'];
- if (!in_array($sort, $allowedSort, true)) {
- $sort = 'id';
- }
- $order = strtolower($order) === 'asc' ? 'asc' : 'desc';
- $query = $this->buildQuery();
- $count = (clone $query)->count();
- $data = $query->order($sort, $order)->page($page, $limit)->select();
- return json(['code' => 0, 'count' => $count, 'data' => $data]);
- }
- return $this->fetch();
- }
- public function del()
- {
- if (!$this->request->isPost()) {
- $this->error('请求方式错误');
- }
- $ids = $this->request->post('ids/a', []);
- if (!$ids) {
- $this->error('请先选择反馈记录');
- }
- $count = $this->modelClass->where('id', 'in', $ids)->delete();
- $this->success('已删除 ' . (int)$count . ' 条反馈记录');
- }
- public function changeStatus()
- {
- if (!$this->request->isPost()) {
- $this->error('请求方式错误');
- }
- $id = (int)$this->request->post('id/d', 0);
- $status = (int)$this->request->post('status/d', -1);
- if ($id <= 0) {
- $this->error('记录不存在');
- }
- if (!array_key_exists($status, $this->modelClass->getStatusList())) {
- $this->error('状态参数错误');
- }
- $row = $this->modelClass->where('id', $id)->find();
- if (!$row) {
- $this->error('记录不存在');
- }
- $row->save(['status' => $status, 'updatetime' => time()]);
- $this->success('状态已更新');
- }
- public function remark()
- {
- if (!$this->request->isPost()) {
- $this->error('请求方式错误');
- }
- $id = (int)$this->request->post('id/d', 0);
- $remark = trim((string)$this->request->post('admin_remark/s', ''));
- $remark = mb_substr(strip_tags($remark), 0, 500, 'UTF-8');
- if ($id <= 0) {
- $this->error('记录不存在');
- }
- $row = $this->modelClass->where('id', $id)->find();
- if (!$row) {
- $this->error('记录不存在');
- }
- $row->save(['admin_remark' => $remark, 'updatetime' => time()]);
- $this->success('备注已保存');
- }
- protected function buildQuery()
- {
- $keyword = trim((string)$this->request->param('keyword/s', ''));
- $feedbackType = trim((string)$this->request->param('feedback_type/s', ''));
- $lang = trim((string)$this->request->param('lang/s', ''));
- $status = $this->request->param('status/s', '');
- $ip = trim((string)$this->request->param('ip/s', ''));
- $sourcePage = trim((string)$this->request->param('source_page/s', ''));
- $createtimeStart = trim((string)$this->request->param('createtime_start/s', ''));
- $createtimeEnd = trim((string)$this->request->param('createtime_end/s', ''));
- $query = $this->modelClass;
- if ($keyword !== '') {
- $query = $query->where(function ($q) use ($keyword) {
- $q->where('name|phone|email|content|admin_remark', 'like', '%' . $keyword . '%');
- });
- }
- if ($feedbackType !== '') {
- $query = $query->where('feedback_type', $feedbackType);
- }
- if ($lang !== '') {
- $query = $query->where('lang', $lang);
- }
- if ($status !== '') {
- $query = $query->where('status', (int)$status);
- }
- if ($ip !== '') {
- $query = $query->where('ip', 'like', '%' . $ip . '%');
- }
- if ($sourcePage !== '') {
- $query = $query->where('source_page', 'like', '%' . $sourcePage . '%');
- }
- if ($createtimeStart !== '') {
- $timestamp = strtotime($createtimeStart);
- if ($timestamp) {
- $query = $query->where('createtime', '>=', $timestamp);
- }
- }
- if ($createtimeEnd !== '') {
- $timestamp = strtotime($createtimeEnd);
- if ($timestamp) {
- $query = $query->where('createtime', '<=', $timestamp);
- }
- }
- return $query;
- }
- }
|