Slide.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/Apache-2.0
  5. * @link https://www.gougucms.com
  6. */
  7. declare (strict_types = 1);
  8. namespace app\admin\controller;
  9. use app\admin\BaseController;
  10. use app\admin\model\Slide as SlideList;
  11. use app\admin\model\SlideInfo;
  12. use app\admin\validate\SlideCheck;
  13. use think\exception\ValidateException;
  14. use think\facade\Db;
  15. use think\facade\View;
  16. class Slide extends BaseController
  17. {
  18. public function index()
  19. {
  20. if (request()->isAjax()) {
  21. $param = get_params();
  22. $where = array();
  23. if (!empty($param['keywords'])) {
  24. $where[] = ['id|name|title|desc', 'like', '%' . $param['keywords'] . '%'];
  25. }
  26. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  27. $slide = SlideList::where($where)
  28. ->order('create_time asc')
  29. ->paginate($rows, false, ['query' => $param]);
  30. return table_assign(0, '', $slide);
  31. } else {
  32. return view();
  33. }
  34. }
  35. //添加
  36. public function add()
  37. {
  38. $param = get_params();
  39. if (request()->isAjax()) {
  40. if (!empty($param['id']) && $param['id'] > 0) {
  41. try {
  42. validate(SlideCheck::class)->scene('edit')->check($param);
  43. } catch (ValidateException $e) {
  44. // 验证失败 输出错误信息
  45. return to_assign(1, $e->getError());
  46. }
  47. $param['update_time'] = time();
  48. $res = SlideList::where('id', $param['id'])->strict(false)->field(true)->update($param);
  49. if ($res) {
  50. add_log('edit', $param['id'], $param);
  51. }
  52. clear_cache('homeSlide');
  53. return to_assign();
  54. } else {
  55. try {
  56. validate(SlideCheck::class)->scene('add')->check($param);
  57. } catch (ValidateException $e) {
  58. // 验证失败 输出错误信息
  59. return to_assign(1, $e->getError());
  60. }
  61. $param['create_time'] = time();
  62. $sid = SlideList::strict(false)->field(true)->insertGetId($param);
  63. if ($sid) {
  64. add_log('add', $sid, $param);
  65. }
  66. // 删除banner缓存
  67. clear_cache('homeSlide');
  68. return to_assign();
  69. }
  70. }
  71. else{
  72. $id = isset($param['id']) ? $param['id'] : 0;
  73. if ($id > 0) {
  74. $slide = Db::name('Slide')->where(['id' => $id])->find();
  75. View::assign('slide', $slide);
  76. }
  77. View::assign('id', $id);
  78. return view();
  79. }
  80. }
  81. //删除
  82. public function delete()
  83. {
  84. $id = get_params("id");
  85. $count = Db::name('SlideInfo')->where([
  86. 'slide_id' => $id,
  87. ])->count();
  88. if ($count > 0) {
  89. return to_assign(1, '该组下还有Banner,无法删除');
  90. }
  91. if (Db::name('Slide')->delete($id) !== false) {
  92. add_log('delete', $id);
  93. clear_cache('homeSlide');
  94. return to_assign(0, "删除成功");
  95. } else {
  96. return to_assign(1, "删除失败");
  97. }
  98. }
  99. //管理幻灯片
  100. public function slide_info()
  101. {
  102. $param = get_params();
  103. if (request()->isAjax()) {
  104. $where = array();
  105. $where[] = ['s.slide_id', '=', $param['id']];
  106. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  107. $slideInfoList = SlideInfo::where($where)
  108. ->alias('s')
  109. ->join('File f', 's.img=f.id', 'LEFT')
  110. ->field('s.*,f.filepath')
  111. ->order('s.sort desc, s.id desc')
  112. ->paginate($rows, false, ['query' => $param]);
  113. return table_assign(0, '', $slideInfoList);
  114. } else {
  115. return view('', [
  116. 'slide_id' => $param['id'],
  117. ]);
  118. }
  119. }
  120. //幻灯片列表
  121. public function slide_info_list()
  122. {
  123. $param = get_params();
  124. $where = array();
  125. $where[] = ['s.slide_id', '=', $param['id']];
  126. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  127. $slideInfoList = SlideInfo::where($where)
  128. ->alias('s')
  129. ->join('File f', 's.img=f.id', 'LEFT')
  130. ->field('s.*,f.filepath')
  131. ->order('s.sort desc, s.id desc')
  132. ->paginate($rows, false, ['query' => $param]);
  133. return table_assign(0, '', $slideInfoList);
  134. }
  135. //添加幻灯片
  136. public function slide_info_add()
  137. {
  138. $param = get_params();
  139. if (request()->isAjax()) {
  140. if (!empty($param['id']) && $param['id'] > 0) {
  141. try {
  142. validate(SlideCheck::class)->scene('editInfo')->check($param);
  143. } catch (ValidateException $e) {
  144. // 验证失败 输出错误信息
  145. return to_assign(1, $e->getError());
  146. }
  147. $param['update_time'] = time();
  148. $res = SlideInfo::where(['id' => $param['id']])->strict(false)->field(true)->update($param);
  149. if ($res) {
  150. add_log('edit', $param['id'], $param);
  151. }
  152. // 删除缓存
  153. clear_cache('homeSlide');
  154. return to_assign();
  155. } else {
  156. try {
  157. validate(SlideCheck::class)->scene('addInfo')->check($param);
  158. } catch (ValidateException $e) {
  159. // 验证失败 输出错误信息
  160. return to_assign(1, $e->getError());
  161. }
  162. $param['create_time'] = time();
  163. $sid = SlideInfo::strict(false)->field(true)->insertGetId($param);
  164. if ($sid) {
  165. add_log('add', $sid, $param);
  166. }
  167. // 删除缓存
  168. clear_cache('homeSlide');
  169. return to_assign();
  170. }
  171. }
  172. else{
  173. $id = isset($param['id']) ? $param['id'] : 0;
  174. $slide_id = isset($param['sid']) ? $param['sid'] : 0;
  175. if ($id > 0) {
  176. $slide_info = Db::name('SlideInfo')->where(['id' => $id])->find();
  177. View::assign('slide_info', $slide_info);
  178. $slide_id = $slide_info['slide_id'];
  179. }
  180. View::assign('id', $id);
  181. View::assign('slide_id', $slide_id);
  182. return view();
  183. }
  184. }
  185. //删除幻灯片
  186. public function slide_info_delete()
  187. {
  188. $id = get_params("id");
  189. if (Db::name('SlideInfo')->delete($id) !== false) {
  190. add_log('delete', $id);
  191. clear_cache('homeSlide');
  192. return to_assign(0, "删除成功");
  193. } else {
  194. return to_assign(1, "删除失败");
  195. }
  196. }
  197. }