CultureBanner.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace app\admin\controller\wwh;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. /**
  8. * 企业文化横幅管理
  9. */
  10. class CultureBanner extends Backend
  11. {
  12. /**
  13. * CultureBanner模型对象
  14. * @var \app\admin\model\wwh\CultureBanner
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\common\model\WwhCultureBanner;
  21. $this->view->assign("contentTypeList", [
  22. 'text' => '纯文本',
  23. 'grid_single' => '单列网格',
  24. 'grid_double' => '双列网格'
  25. ]);
  26. $this->view->assign("statusList", [
  27. '0' => '禁用',
  28. '1' => '启用'
  29. ]);
  30. $this->view->assign("langList", [
  31. '1' => '简体中文',
  32. '2' => 'English'
  33. ]);
  34. }
  35. /**
  36. * 查看
  37. */
  38. public function index()
  39. {
  40. //当前是否为关联查询
  41. $this->relationSearch = false;
  42. //设置过滤方法
  43. $this->request->filter(['strip_tags', 'trim']);
  44. if ($this->request->isAjax()) {
  45. //如果发送的来源是Selectpage,则转发到Selectpage
  46. if ($this->request->request('keyField')) {
  47. return $this->selectpage();
  48. }
  49. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  50. $list = $this->model
  51. ->where($where)
  52. ->order($sort, $order)
  53. ->paginate($limit);
  54. foreach ($list as $row) {
  55. $row->visible(['id','title','subtitle','image','content_type','weigh','status','lang','createtime']);
  56. }
  57. $result = array("total" => $list->total(), "rows" => $list->items());
  58. return json($result);
  59. }
  60. return $this->view->fetch();
  61. }
  62. /**
  63. * 添加
  64. */
  65. public function add()
  66. {
  67. if ($this->request->isPost()) {
  68. $params = $this->request->post("row/a");
  69. if ($params) {
  70. $params = $this->preExcludeFields($params);
  71. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  72. $params[$this->dataLimitField] = $this->auth->id;
  73. }
  74. $result = false;
  75. Db::startTrans();
  76. try {
  77. //是否采用模型验证
  78. if ($this->modelValidate) {
  79. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  80. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  81. $this->model->validateFailException(true)->validate($validate);
  82. }
  83. // 处理内容数据JSON格式
  84. if (isset($params['content_data'])) {
  85. $contentData = $this->parseContentData($params);
  86. $params['content_data'] = json_encode($contentData, JSON_UNESCAPED_UNICODE);
  87. }
  88. $params['createtime'] = time();
  89. $params['updatetime'] = time();
  90. $result = $this->model->allowField(true)->save($params);
  91. Db::commit();
  92. } catch (ValidateException $e) {
  93. Db::rollback();
  94. $this->error($e->getMessage());
  95. } catch (PDOException $e) {
  96. Db::rollback();
  97. $this->error($e->getMessage());
  98. } catch (Exception $e) {
  99. Db::rollback();
  100. $this->error($e->getMessage());
  101. }
  102. if ($result !== false) {
  103. $this->success();
  104. } else {
  105. $this->error(__('No rows were inserted'));
  106. }
  107. }
  108. $this->error(__('Parameter %s can not be empty', ''));
  109. }
  110. return $this->view->fetch();
  111. }
  112. /**
  113. * 编辑
  114. */
  115. public function edit($ids = null)
  116. {
  117. $row = $this->model->get($ids);
  118. if (!$row) {
  119. $this->error(__('No Results were found'));
  120. }
  121. $adminIds = $this->getDataLimitAdminIds();
  122. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  123. $this->error(__('You have no permission'));
  124. }
  125. if ($this->request->isPost()) {
  126. $params = $this->request->post("row/a");
  127. if ($params) {
  128. $params = $this->preExcludeFields($params);
  129. $result = false;
  130. Db::startTrans();
  131. try {
  132. //是否采用模型验证
  133. if ($this->modelValidate) {
  134. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  135. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  136. $row->validateFailException(true)->validate($validate);
  137. }
  138. // 处理内容数据JSON格式
  139. if (isset($params['content_data'])) {
  140. $contentData = $this->parseContentData($params);
  141. $params['content_data'] = json_encode($contentData, JSON_UNESCAPED_UNICODE);
  142. }
  143. $params['updatetime'] = time();
  144. $result = $row->allowField(true)->save($params);
  145. Db::commit();
  146. } catch (ValidateException $e) {
  147. Db::rollback();
  148. $this->error($e->getMessage());
  149. } catch (PDOException $e) {
  150. Db::rollback();
  151. $this->error($e->getMessage());
  152. } catch (Exception $e) {
  153. Db::rollback();
  154. $this->error($e->getMessage());
  155. }
  156. if ($result !== false) {
  157. $this->success();
  158. } else {
  159. $this->error(__('No rows were updated'));
  160. }
  161. }
  162. $this->error(__('Parameter %s can not be empty', ''));
  163. }
  164. // 处理内容数据显示
  165. if ($row['content_data']) {
  166. $contentData = json_decode($row['content_data'], true);
  167. if (is_array($contentData)) {
  168. if (isset($contentData['text'])) {
  169. $row['content_text'] = $contentData['text'];
  170. }
  171. if (isset($contentData['items']) && is_array($contentData['items'])) {
  172. $row['content_items'] = implode("\n", $contentData['items']);
  173. }
  174. }
  175. }
  176. $this->view->assign("row", $row);
  177. return $this->view->fetch();
  178. }
  179. /**
  180. * 解析内容数据
  181. */
  182. private function parseContentData($params)
  183. {
  184. $contentData = [];
  185. if ($params['content_type'] == 'text') {
  186. $contentData['text'] = $params['content_text'] ?? '';
  187. } else {
  188. // grid_single 或 grid_double
  189. if (isset($params['content_items'])) {
  190. $items = array_filter(explode("\n", $params['content_items']));
  191. $contentData['items'] = $items;
  192. }
  193. }
  194. return $contentData;
  195. }
  196. }