Banner.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace app\admin\controller\setting;
  3. use app\admin\model\box\Box;
  4. use app\common\controller\Backend;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 轮播图管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Banner extends Backend
  14. {
  15. protected $modelValidate = true;
  16. /**
  17. * Banner模型对象
  18. * @var \app\admin\model\setting\Banner
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\setting\Banner;
  25. $this->view->assign("placeList", $this->model->getPlaceList());
  26. $this->view->assign("typeList", $this->model->getTypeList());
  27. $this->view->assign("statusList", $this->model->getStatusList());
  28. }
  29. public function import()
  30. {
  31. parent::import();
  32. }
  33. /**
  34. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  35. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  36. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  37. */
  38. public function index()
  39. {
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. if ($this->request->isAjax()) {
  43. //如果发送的来源是Selectpage,则转发到Selectpage
  44. if ($this->request->request('keyField')) {
  45. return $this->selectpage();
  46. }
  47. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  48. // 默认查看首页轮播图
  49. $where_place = ['place' => 'index'];
  50. $filter = json_decode(input('filter'), true);
  51. if ($filter) {
  52. $where_place = [];
  53. }
  54. $list = $this->model
  55. ->field($this->indexField, $this->indexFieldExcept)
  56. ->where($where)
  57. ->where($where_place)
  58. ->order($sort, $order)
  59. ->paginate($limit)
  60. ->each(function ($item) {
  61. $item->hidden(['value']);
  62. });
  63. $result = array("total" => $list->total(), "rows" => $list->items());
  64. return json($result);
  65. }
  66. $this->assignconfig('tab_default', '#tab-place-index');
  67. return $this->view->fetch();
  68. }
  69. public function add()
  70. {
  71. if ($this->request->isPost()) {
  72. $params = $this->request->post("row/a");
  73. if ($params) {
  74. $params = $this->preExcludeFields($params);
  75. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  76. $params[$this->dataLimitField] = $this->auth->id;
  77. }
  78. $result = false;
  79. Db::startTrans();
  80. try {
  81. //是否采用模型验证
  82. if ($this->modelValidate) {
  83. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  84. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  85. $this->model->validateFailException(true)->validate($validate);
  86. }
  87. if ('normal' == $params['status']) {
  88. switch ($params['type']) {
  89. case 'box':
  90. // 检查盲盒
  91. if (empty($params['box']) || !is_numeric($params['box'])) {
  92. $this->error('盲盒选择有误');
  93. }
  94. $checkBox = Box::where('id', $params['box'])->value('id');
  95. if (empty($checkBox)) {
  96. $this->error('盲盒不存在');
  97. }
  98. $params['value'] = $params['box'];
  99. break;
  100. case 'link':
  101. if (empty($params['link']) || 0 !== strpos($params['link'], 'http')) {
  102. $this->error('链接设置有误');
  103. }
  104. $params['value'] = $params['link'];
  105. break;
  106. case 'word':
  107. $params['value'] = $params['word'];
  108. }
  109. }
  110. $result = $this->model->allowField(true)->save($params);
  111. Db::commit();
  112. } catch (ValidateException $e) {
  113. Db::rollback();
  114. $this->error($e->getMessage());
  115. } catch (PDOException $e) {
  116. Db::rollback();
  117. $this->error($e->getMessage());
  118. } catch (\Exception $e) {
  119. Db::rollback();
  120. $this->error($e->getMessage());
  121. }
  122. if ($result !== false) {
  123. $this->success();
  124. } else {
  125. $this->error(__('No rows were inserted'));
  126. }
  127. }
  128. $this->error(__('Parameter %s can not be empty', ''));
  129. }
  130. return $this->view->fetch();
  131. }
  132. public function edit($ids = null)
  133. {
  134. $row = $this->model->get($ids);
  135. if (!$row) {
  136. $this->error(__('No Results were found'));
  137. }
  138. $adminIds = $this->getDataLimitAdminIds();
  139. if (is_array($adminIds)) {
  140. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  141. $this->error(__('You have no permission'));
  142. }
  143. }
  144. if ($this->request->isPost()) {
  145. $params = $this->request->post("row/a");
  146. if ($params) {
  147. $params = $this->preExcludeFields($params);
  148. $result = false;
  149. Db::startTrans();
  150. try {
  151. //是否采用模型验证
  152. if ($this->modelValidate) {
  153. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  154. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  155. $row->validateFailException(true)->validate($validate);
  156. }
  157. if ('normal' == $params['status']) {
  158. switch ($params['type']) {
  159. case 'box':
  160. // 检查盲盒
  161. if (empty($params['box']) || !is_numeric($params['box'])) {
  162. $this->error('盲盒选择有误');
  163. }
  164. $checkBox = Box::where('id', $params['box'])->value('id');
  165. if (empty($checkBox)) {
  166. $this->error('盲盒不存在');
  167. }
  168. $params['value'] = $params['box'];
  169. break;
  170. case 'link':
  171. if (empty($params['link']) || 0 !== strpos($params['link'], 'http')) {
  172. $this->error('链接设置有误');
  173. }
  174. $params['value'] = $params['link'];
  175. break;
  176. case 'word':
  177. $params['value'] = $params['word'];
  178. }
  179. }
  180. $result = $row->allowField(true)->save($params);
  181. Db::commit();
  182. } catch (ValidateException $e) {
  183. Db::rollback();
  184. $this->error($e->getMessage());
  185. } catch (PDOException $e) {
  186. Db::rollback();
  187. $this->error($e->getMessage());
  188. } catch (\Exception $e) {
  189. Db::rollback();
  190. $this->error($e->getMessage());
  191. }
  192. if ($result !== false) {
  193. $this->success();
  194. } else {
  195. $this->error(__('No rows were updated'));
  196. }
  197. }
  198. $this->error(__('Parameter %s can not be empty', ''));
  199. }
  200. $this->view->assign("row", $row);
  201. return $this->view->fetch();
  202. }
  203. }