Record.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace app\admin\controller\kefu;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\Exception;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * KeFu消息记录管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Record extends Backend
  14. {
  15. /**
  16. * KeFuRecord模型对象
  17. * @var \app\admin\model\KeFuRecord
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\KeFuRecord;
  24. $this->view->assign("senderIdentityList", $this->model->getSenderIdentityList());
  25. $this->view->assign("messageTypeList", $this->model->getMessageTypeList());
  26. $this->view->assign("statusList", $this->model->getStatusList());
  27. $toolbar = [];
  28. $toolbar_temp = Db::name('kefu_toolbar')
  29. ->field('mark,card_url')
  30. ->whereIn('mark', 'order,goods')
  31. ->where('deletetime', null)
  32. ->select();
  33. // 以mark为键
  34. foreach ($toolbar_temp as $key => $value) {
  35. $toolbar[$value['mark']] = $value;
  36. }
  37. $this->assignconfig('toolbar', $toolbar);
  38. }
  39. /**
  40. * 会话聊天记录
  41. */
  42. public function sessionRecord()
  43. {
  44. //设置过滤方法
  45. $this->request->filter(['strip_tags']);
  46. if ($this->request->isAjax()) {
  47. //如果发送的来源是Selectpage,则转发到Selectpage
  48. if ($this->request->request('keyField')) {
  49. return $this->selectpage();
  50. }
  51. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  52. $total = $this->model->where($where)->order($sort, $order)->count();
  53. $list = $this->model->where($where)->order($sort, $order)->limit($offset, $limit)->select();
  54. $list = collection($list)->toArray();
  55. foreach ($list as $key => $value) {
  56. $list[$key]['message'] = htmlspecialchars_decode($value['message']);
  57. if ($list[$key]['sender_identity'] == 0) {
  58. $list[$key]['sender_id'] = Db::name('admin')
  59. ->where('id', $list[$key]['sender_id'])
  60. ->value('nickname');
  61. } else {
  62. $sender = Db::name('kefu_user')
  63. ->alias('u')
  64. ->field('u.nickname,fu.nickname as fu_nickname')
  65. ->join('user fu', 'u.user_id=fu.id', 'LEFT')
  66. ->where('u.id', $list[$key]['sender_id'])
  67. ->find();
  68. $list[$key]['sender_id'] = $sender['fu_nickname'] ? $sender['fu_nickname'] : $sender['nickname'];
  69. }
  70. }
  71. $result = ["total" => $total, "rows" => $list];
  72. return json($result);
  73. }
  74. return $this->view->fetch();
  75. }
  76. /**
  77. * 查看
  78. */
  79. public function index()
  80. {
  81. //设置过滤方法
  82. $this->request->filter(['strip_tags']);
  83. if ($this->request->isAjax()) {
  84. //如果发送的来源是Selectpage,则转发到Selectpage
  85. if ($this->request->request('keyField')) {
  86. return $this->selectpage();
  87. }
  88. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  89. $total = $this->model->where($where)->order($sort, $order)->count();
  90. $list = $this->model->where($where)->order($sort, $order)->limit($offset, $limit)->select();
  91. $list = collection($list)->toArray();
  92. foreach ($list as $key => $value) {
  93. $list[$key]['message'] = htmlspecialchars_decode($value['message']);
  94. // 把消息中的图片替换掉
  95. $list[$key]['message'] = preg_replace("/<img (.*?)\">/", "[图片]", $list[$key]['message']);
  96. if ($list[$key]['sender_identity'] == 0) {
  97. $list[$key]['sender_id'] = Db::name('admin')
  98. ->where('id', $list[$key]['sender_id'])
  99. ->value('nickname');
  100. } else {
  101. $sender = Db::name('kefu_user')
  102. ->alias('u')
  103. ->field('u.nickname,fu.nickname as fu_nickname')
  104. ->join('user fu', 'u.user_id=fu.id', 'LEFT')
  105. ->where('u.id', $list[$key]['sender_id'])
  106. ->find();
  107. $list[$key]['sender_id'] = $sender['fu_nickname'] ? $sender['fu_nickname'] : $sender['nickname'];
  108. }
  109. }
  110. $result = ["total" => $total, "rows" => $list];
  111. return json($result);
  112. }
  113. return $this->view->fetch();
  114. }
  115. /**
  116. * 编辑
  117. */
  118. public function edit($ids = null)
  119. {
  120. $row = $this->model->get($ids);
  121. if (!$row) {
  122. $this->error(__('No Results were found'));
  123. }
  124. $adminIds = $this->getDataLimitAdminIds();
  125. if (is_array($adminIds)) {
  126. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  127. $this->error(__('You have no permission'));
  128. }
  129. }
  130. if ($this->request->isPost()) {
  131. $params = $this->request->post("row/a");
  132. if ($params) {
  133. $params = method_exists($this, 'preExcludeFields') ? $this->preExcludeFields($params) : $params;
  134. $result = false;
  135. Db::startTrans();
  136. try {
  137. //是否采用模型验证
  138. if ($this->modelValidate) {
  139. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  140. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  141. $row->validateFailException(true)->validate($validate);
  142. }
  143. $result = $row->allowField(true)->save($params);
  144. Db::commit();
  145. } catch (ValidateException $e) {
  146. Db::rollback();
  147. $this->error($e->getMessage());
  148. } catch (PDOException $e) {
  149. Db::rollback();
  150. $this->error($e->getMessage());
  151. } catch (Exception $e) {
  152. Db::rollback();
  153. $this->error($e->getMessage());
  154. }
  155. if ($result !== false) {
  156. $this->success();
  157. } else {
  158. $this->error(__('No rows were updated'));
  159. }
  160. }
  161. $this->error(__('Parameter %s can not be empty', ''));
  162. }
  163. if ($row->message_type == 1) {
  164. $row->message = '<img height="200" src="' . $row->message . '" />';
  165. }
  166. if ($row->message_type == 0 || $row->message_type == 3) {
  167. $row->message = htmlspecialchars_decode($row->message);
  168. }
  169. $this->view->assign("row", $row);
  170. return $this->view->fetch();
  171. }
  172. }