Richtext.php 3.6 KB

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