Reply.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace app\admin\controller\shopro\wechat;
  3. use app\admin\controller\shopro\Common;
  4. use think\Db;
  5. /**
  6. * 常见问题
  7. */
  8. class Reply extends Common
  9. {
  10. /**
  11. * Faq模型对象
  12. * @var \app\admin\model\shopro\wechat\Reply
  13. */
  14. protected $model = null;
  15. public function _initialize()
  16. {
  17. parent::_initialize();
  18. $this->model = new \app\admin\model\shopro\wechat\Reply;
  19. }
  20. /**
  21. * 查看
  22. *
  23. * @return string|Json
  24. * @throws \think\Exception
  25. * @throws DbException
  26. */
  27. public function index()
  28. {
  29. if (!$this->request->isAjax()) {
  30. return $this->view->fetch();
  31. }
  32. $group = $this->request->param('group', 'keywords');
  33. $data = $this->model->sheepFilter()->where('group', $group)->select();
  34. $this->success('操作成功', null, $data);
  35. }
  36. /**
  37. * 添加
  38. */
  39. public function add()
  40. {
  41. if (!$this->request->isAjax()) {
  42. return $this->view->fetch();
  43. }
  44. $params = $this->request->only(['group', 'keywords', 'type', 'status', 'content']);
  45. Db::transaction(function () use ($params) {
  46. $result = $this->model->save($params);
  47. if($result) {
  48. $reply = $this->model;
  49. if($reply->group !== 'keywords' && $reply->status === 'enable') {
  50. $this->model->where('group', $reply->group)->where('id', '<>', $reply->id)->enable()->update(['status' => 'disabled']);
  51. }
  52. }
  53. return $result;
  54. });
  55. $this->success('保存成功');
  56. }
  57. /**
  58. * 详情
  59. *
  60. * @param $id
  61. * @return \think\Response
  62. */
  63. public function detail($id)
  64. {
  65. $detail = $this->model->get($id);
  66. if (!$detail) {
  67. $this->error(__('No Results were found'));
  68. }
  69. $this->success('获取成功', null, $detail);
  70. }
  71. /**
  72. * 编辑(支持批量)
  73. */
  74. public function edit($id = null)
  75. {
  76. if (!$this->request->isAjax()) {
  77. return $this->view->fetch('add');
  78. }
  79. $reply = $this->model->get($id);
  80. $params = $this->request->only(['keywords', 'type', 'status', 'content']);
  81. // 参数校验
  82. // $this->svalidate($params);
  83. $result = Db::transaction(function () use ($params, $reply) {
  84. $result = $reply->save($params);
  85. if($result) {
  86. if($reply->group !== 'keywords' && $reply->status === 'enable') {
  87. $this->model->where('group', $reply->group)->where('id', '<>', $reply->id)->enable()->update(['status' => 'disabled']);
  88. }
  89. }
  90. return $result;
  91. });
  92. if ($result) {
  93. $this->success('更新成功', null, $result);
  94. } else {
  95. $this->error('更新失败');
  96. }
  97. }
  98. /**
  99. * 删除(支持批量)
  100. *
  101. * @param $id
  102. * @return \think\Response
  103. */
  104. public function delete($id)
  105. {
  106. if (empty($id)) {
  107. $this->error(__('Parameter %s can not be empty', 'id'));
  108. }
  109. $list = $this->model->where('id', 'in', $id)->select();
  110. $result = Db::transaction(function () use ($list) {
  111. $count = 0;
  112. foreach ($list as $item) {
  113. $count += $item->delete();
  114. }
  115. return $count;
  116. });
  117. if ($result) {
  118. $this->success('删除成功', null, $result);
  119. } else {
  120. $this->error(__('No rows were deleted'));
  121. }
  122. }
  123. }