CommonWord.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace app\admin\controller\shopro\chat;
  3. use think\Db;
  4. use app\admin\controller\shopro\Common;
  5. use app\admin\model\shopro\chat\CommonWord as ChatCommonWord;
  6. class CommonWord extends Common
  7. {
  8. public function _initialize()
  9. {
  10. parent::_initialize();
  11. $this->model = new ChatCommonWord;
  12. }
  13. /**
  14. * 常用语列表
  15. */
  16. public function index()
  17. {
  18. if (!$this->request->isAjax()) {
  19. return $this->view->fetch();
  20. }
  21. $commonWords = $this->model->sheepFilter()->paginate($this->request->param('list_rows', 10));
  22. $this->success('获取成功', null, $commonWords);
  23. }
  24. /**
  25. * 常用语添加
  26. */
  27. public function add()
  28. {
  29. if (!$this->request->isAjax()) {
  30. return $this->view->fetch();
  31. }
  32. $params = $this->request->only(['room_id', 'name', 'status', 'weigh']);
  33. $params['content'] = $this->request->param('content', '', null); // content 不经过全局过滤
  34. $this->svalidate($params, ".add");
  35. $this->model->save($params);
  36. $this->success('保存成功', null, $this->model);
  37. }
  38. /**
  39. * 常用语详情
  40. *
  41. * @param $id
  42. */
  43. public function detail($id)
  44. {
  45. $commonWord = $this->model->where('id', $id)->find();
  46. if (!$commonWord) {
  47. $this->error(__('No Results were found'));
  48. }
  49. $this->success('获取成功', null, $commonWord);
  50. }
  51. /**
  52. * 常用语编辑
  53. *
  54. * @param $id
  55. */
  56. public function edit($id = null)
  57. {
  58. if (!$this->request->isAjax()) {
  59. return $this->view->fetch('add');
  60. }
  61. $params = $this->request->only(['room_id', 'name', 'status', 'weigh']);
  62. $this->request->has('content') && $params['content'] = $this->request->param('content', '', null); // content 不经过全局过滤
  63. $this->svalidate($params);
  64. $id = explode(',', $id);
  65. $list = $this->model->where('id', 'in', $id)->select();
  66. $result = Db::transaction(function () use ($list, $params) {
  67. $count = 0;
  68. foreach ($list as $item) {
  69. $params['id'] = $item->id;
  70. $count += $item->save($params);
  71. }
  72. return $count;
  73. });
  74. if ($result) {
  75. $this->success('更新成功', null, $result);
  76. } else {
  77. $this->error('更新失败,未改变任何记录');
  78. }
  79. }
  80. /**
  81. * 删除(支持批量)
  82. *
  83. * @param $id
  84. */
  85. public function delete($id)
  86. {
  87. if (empty($id)) {
  88. $this->error(__('Parameter %s can not be empty', 'id'));
  89. }
  90. $id = explode(',', $id);
  91. $list = $this->model->where('id', 'in', $id)->select();
  92. $result = Db::transaction(function () use ($list) {
  93. $count = 0;
  94. foreach ($list as $item) {
  95. $count += $item->delete();
  96. }
  97. return $count;
  98. });
  99. if ($result) {
  100. $this->success('删除成功', null, $result);
  101. } else {
  102. $this->error(__('No rows were deleted'));
  103. }
  104. }
  105. }