Feedback.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\library\Wechat;
  5. use think\Db;
  6. use think\exception\DbException;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. use Exception;
  10. /**
  11. * 智能助手反馈
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class Feedback extends Backend
  16. {
  17. /**
  18. * Feedback模型对象
  19. * @var \app\admin\model\Feedback
  20. */
  21. protected $model = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\Feedback;
  26. $this->view->assign("statusList", $this->model->getStatusList());
  27. $this->view->assign("noticeStatusList", $this->model->getNoticeStatusList());
  28. $this->view->assign("isHiddenList", $this->model->getIsHiddenList());
  29. }
  30. /**
  31. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  32. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  33. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  34. */
  35. /**
  36. * 查看
  37. */
  38. public function index()
  39. {
  40. //当前是否为关联查询
  41. $this->relationSearch = true;
  42. //设置过滤方法
  43. $this->request->filter(['strip_tags', 'trim']);
  44. if ($this->request->isAjax()) {
  45. //如果发送的来源是Selectpage,则转发到Selectpage
  46. if ($this->request->request('keyField')) {
  47. return $this->selectpage();
  48. }
  49. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  50. $list = $this->model
  51. ->with(['user'])
  52. ->where($where)
  53. ->order($sort, $order)
  54. ->paginate($limit);
  55. foreach ($list as $row) {
  56. $row->getRelation('user')->visible(['nickname']);
  57. }
  58. $result = array("total" => $list->total(), "rows" => $list->items());
  59. return json($result);
  60. }
  61. return $this->view->fetch();
  62. }
  63. /**
  64. * 编辑
  65. *
  66. * @param $ids
  67. * @return string
  68. * @throws DbException
  69. * @throws \think\Exception
  70. */
  71. public function edit($ids = null)
  72. {
  73. $row = $this->model->get($ids);
  74. if (!$row) {
  75. $this->error(__('No Results were found'));
  76. }
  77. $adminIds = $this->getDataLimitAdminIds();
  78. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  79. $this->error(__('You have no permission'));
  80. }
  81. if (false === $this->request->isPost()) {
  82. $this->view->assign('row', $row);
  83. return $this->view->fetch();
  84. }
  85. $params = $this->request->post('row/a');
  86. if (empty($params)) {
  87. $this->error(__('Parameter %s can not be empty', ''));
  88. }
  89. $params = $this->preExcludeFields($params);
  90. $result = false;
  91. Db::startTrans();
  92. try {
  93. //是否采用模型验证
  94. if ($this->modelValidate) {
  95. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  96. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  97. $row->validateFailException()->validate($validate);
  98. }
  99. $result = $row->allowField(true)->save($params);
  100. Db::commit();
  101. //如果已解决,发消息
  102. if($params['status'] == 2){
  103. $config = config('wxMiniProgram');
  104. $wechat = new Wechat($config['appid'],$config['secret']);
  105. //意见反馈
  106. $template_id = $config['mini_msgid_feedback'];
  107. $msg_info = mb_substr($row->info,0,12) . ((mb_strlen($row->info) > 12) ? '...' : '');
  108. $data = [
  109. 'thing4' => ['value' => $row->category ?: '问题反馈'],
  110. 'thing5' => ['value' => $msg_info],
  111. 'thing6' => ['value' => '已经处理完成了'],
  112. ];
  113. $msg_openid = Db::name('user')->where('id',$row->user_id)->value('wxmini_openid');
  114. $rs = $wechat->send($template_id,$msg_openid,$data);
  115. }
  116. } catch (ValidateException|PDOException|Exception $e) {
  117. Db::rollback();
  118. $this->error($e->getMessage());
  119. }
  120. if (false === $result) {
  121. $this->error(__('No rows were updated'));
  122. }
  123. $this->success();
  124. }
  125. }