Session.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 Session extends Backend
  14. {
  15. /**
  16. * KeFuSession模型对象
  17. * @var \app\admin\model\KeFuSession
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\KeFuSession;
  24. }
  25. /**
  26. * 删除-同时删除该会话的聊天记录
  27. */
  28. public function del($ids = "")
  29. {
  30. if ($ids) {
  31. $pk = $this->model->getPk();
  32. $adminIds = $this->getDataLimitAdminIds();
  33. if (is_array($adminIds)) {
  34. $this->model->where($this->dataLimitField, 'in', $adminIds);
  35. }
  36. $list = $this->model->where($pk, 'in', $ids)->select();
  37. $count = 0;
  38. Db::startTrans();
  39. try {
  40. foreach ($list as $k => $v) {
  41. $count += $res = $v->delete();
  42. if ($res) {
  43. Db::name('kefu_record')->where('session_id', $v->id)->delete();
  44. }
  45. }
  46. Db::commit();
  47. } catch (PDOException $e) {
  48. Db::rollback();
  49. $this->error($e->getMessage());
  50. } catch (Exception $e) {
  51. Db::rollback();
  52. $this->error($e->getMessage());
  53. }
  54. if ($count) {
  55. $this->success();
  56. } else {
  57. $this->error(__('No rows were deleted'));
  58. }
  59. }
  60. $this->error(__('Parameter %s can not be empty', 'ids'));
  61. }
  62. /**
  63. * 查看
  64. */
  65. public function index()
  66. {
  67. //当前是否为关联查询
  68. $this->relationSearch = true;
  69. //设置过滤方法
  70. $this->request->filter(['strip_tags']);
  71. if ($this->request->isAjax()) {
  72. //如果发送的来源是Selectpage,则转发到Selectpage
  73. if ($this->request->request('keyField')) {
  74. return $this->selectpage();
  75. }
  76. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  77. $total = $this->model->with(['kefuuser', 'csr'])->where($where)->order($sort, $order)->count();
  78. $list = $this->model->with(['kefuuser', 'csr'])
  79. ->where($where)
  80. ->order($sort, $order)
  81. ->limit($offset, $limit)
  82. ->select();
  83. foreach ($list as $row) {
  84. // 查询关联用户的昵称
  85. if ($row->kefuuser->user_id) {
  86. $row->fu_user_nickname = \think\Db::name('user')
  87. ->where('id', $row->kefuuser->user_id)
  88. ->value('nickname');
  89. }
  90. }
  91. $list = collection($list)->toArray();
  92. $result = ["total" => $total, "rows" => $list];
  93. return json($result);
  94. }
  95. return $this->view->fetch();
  96. }
  97. }