Leavemessage.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace app\admin\controller\kefu;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. /**
  8. * 用户留言记录
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Leavemessage extends Backend
  13. {
  14. /**
  15. * KeFuLeaveMessage模型对象
  16. * @var \app\admin\model\KeFuLeaveMessage
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\KeFuLeaveMessage;
  23. }
  24. /**
  25. * 查看
  26. */
  27. public function index()
  28. {
  29. //当前是否为关联查询
  30. $this->relationSearch = true;
  31. //设置过滤方法
  32. $this->request->filter(['strip_tags']);
  33. if ($this->request->isAjax()) {
  34. //如果发送的来源是Selectpage,则转发到Selectpage
  35. if ($this->request->request('keyField')) {
  36. return $this->selectpage();
  37. }
  38. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  39. $total = $this->model->with(['kefuuser'])->where($where)->order($sort, $order)->count();
  40. $list = $this->model->with(['kefuuser'])
  41. ->where($where)
  42. ->order($sort, $order)
  43. ->limit($offset, $limit)
  44. ->select();
  45. foreach ($list as $row) {
  46. // 查询关联用户的昵称
  47. if ($row->kefuuser->user_id) {
  48. $row->fu_user_nickname = \think\Db::name('user')
  49. ->where('id', $row->kefuuser->user_id)
  50. ->value('nickname');
  51. }
  52. }
  53. $list = collection($list)->toArray();
  54. $result = ["total" => $total, "rows" => $list];
  55. return json($result);
  56. }
  57. return $this->view->fetch();
  58. }
  59. /**
  60. * 编辑
  61. */
  62. public function edit($ids = null)
  63. {
  64. $row = $this->model->get($ids);
  65. if (!$row) {
  66. $this->error(__('No Results were found'));
  67. }
  68. $adminIds = $this->getDataLimitAdminIds();
  69. if (is_array($adminIds)) {
  70. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  71. $this->error(__('You have no permission'));
  72. }
  73. }
  74. if ($this->request->isPost()) {
  75. $params = $this->request->post("row/a");
  76. if ($params) {
  77. $params = $this->preExcludeFields($params);
  78. $result = false;
  79. Db::startTrans();
  80. try {
  81. //是否采用模型验证
  82. if ($this->modelValidate) {
  83. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  84. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  85. $row->validateFailException(true)->validate($validate);
  86. }
  87. $result = $row->allowField(true)->save($params);
  88. Db::commit();
  89. } catch (ValidateException $e) {
  90. Db::rollback();
  91. $this->error($e->getMessage());
  92. } catch (PDOException $e) {
  93. Db::rollback();
  94. $this->error($e->getMessage());
  95. } catch (Exception $e) {
  96. Db::rollback();
  97. $this->error($e->getMessage());
  98. }
  99. if ($result !== false) {
  100. $this->success();
  101. } else {
  102. $this->error(__('No rows were updated'));
  103. }
  104. }
  105. $this->error(__('Parameter %s can not be empty', ''));
  106. }
  107. // 获取用户访问轨迹
  108. $trajectory = Db::name('kefu_trajectory')
  109. ->where('user_id', $row->user_id)
  110. ->where('log_type', 0)
  111. ->order('createtime desc')
  112. ->select();
  113. foreach ($trajectory as $index => $item) {
  114. $trajectory[$index]['createtime'] = date('Y-m-d H:i:s', $trajectory[$index]['createtime']);
  115. }
  116. $this->view->assign("row", $row);
  117. $this->view->assign("trajectory", $trajectory);
  118. return $this->view->fetch();
  119. }
  120. }