CultureCard.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 CultureCard extends Backend
  11. {
  12. /**
  13. * CultureCard模型对象
  14. * @var \app\admin\model\wwh\CultureCard
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\common\model\WwhCultureCard;
  21. $this->view->assign("cardTypeList", [
  22. 'normal' => '图片在上',
  23. 'reverse' => '内容在上'
  24. ]);
  25. $this->view->assign("listTypeList", [
  26. 'single' => '单列布局',
  27. 'double' => '双列布局'
  28. ]);
  29. $this->view->assign("statusList", [
  30. '0' => '禁用',
  31. '1' => '启用'
  32. ]);
  33. $this->view->assign("langList", [
  34. '1' => '简体中文',
  35. '2' => 'English'
  36. ]);
  37. }
  38. /**
  39. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  40. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自定义
  41. * 如果需要自定义,请复制对应的方法至此文件中
  42. */
  43. public function index()
  44. {
  45. //当前是否为关联查询
  46. $this->relationSearch = false;
  47. //设置过滤方法
  48. $this->request->filter(['strip_tags', 'trim']);
  49. if ($this->request->isAjax()) {
  50. //如果发送的来源是Selectpage,则转发到Selectpage
  51. if ($this->request->request('keyField')) {
  52. return $this->selectpage();
  53. }
  54. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  55. $list = $this->model
  56. ->where($where)
  57. ->order($sort, $order)
  58. ->paginate($limit);
  59. foreach ($list as $row) {
  60. $row->visible(['id','title','subtitle','image','card_type','list_type','weigh','status','lang','createtime']);
  61. $row->visible(['id','title','subtitle','image','card_type','list_type','weigh','status','lang','createtime']);
  62. $row->getRelation('admin')->visible(['username']);
  63. }
  64. $result = array("total" => $list->total(), "rows" => $list->items());
  65. return json($result);
  66. }
  67. return $this->view->fetch();
  68. }
  69. /**
  70. * 添加
  71. */
  72. public function add()
  73. {
  74. if ($this->request->isPost()) {
  75. $params = $this->request->post("row/a");
  76. if ($params) {
  77. $params = $this->preExcludeFields($params);
  78. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  79. $params[$this->dataLimitField] = $this->auth->id;
  80. }
  81. $result = false;
  82. Db::startTrans();
  83. try {
  84. //是否采用模型验证
  85. if ($this->modelValidate) {
  86. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  87. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  88. $this->model->validateFailException(true)->validate($validate);
  89. }
  90. // 处理内容项目JSON格式
  91. if (isset($params['content_items'])) {
  92. if (is_string($params['content_items'])) {
  93. // 如果是字符串,按换行符分割
  94. $items = array_filter(explode("\n", $params['content_items']));
  95. $params['content_items'] = json_encode($items, JSON_UNESCAPED_UNICODE);
  96. }
  97. }
  98. $params['createtime'] = time();
  99. $params['updatetime'] = time();
  100. $result = $this->model->allowField(true)->save($params);
  101. Db::commit();
  102. } catch (ValidateException $e) {
  103. Db::rollback();
  104. $this->error($e->getMessage());
  105. } catch (PDOException $e) {
  106. Db::rollback();
  107. $this->error($e->getMessage());
  108. } catch (Exception $e) {
  109. Db::rollback();
  110. $this->error($e->getMessage());
  111. }
  112. if ($result !== false) {
  113. $this->success();
  114. } else {
  115. $this->error(__('No rows were inserted'));
  116. }
  117. }
  118. $this->error(__('Parameter %s can not be empty', ''));
  119. }
  120. return $this->view->fetch();
  121. }
  122. /**
  123. * 编辑
  124. */
  125. public function edit($ids = null)
  126. {
  127. $row = $this->model->get($ids);
  128. if (!$row) {
  129. $this->error(__('No Results were found'));
  130. }
  131. $adminIds = $this->getDataLimitAdminIds();
  132. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  133. $this->error(__('You have no permission'));
  134. }
  135. if ($this->request->isPost()) {
  136. $params = $this->request->post("row/a");
  137. if ($params) {
  138. $params = $this->preExcludeFields($params);
  139. $result = false;
  140. Db::startTrans();
  141. try {
  142. //是否采用模型验证
  143. if ($this->modelValidate) {
  144. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  145. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  146. $row->validateFailException(true)->validate($validate);
  147. }
  148. // 处理内容项目JSON格式
  149. if (isset($params['content_items'])) {
  150. if (is_string($params['content_items'])) {
  151. // 如果是字符串,按换行符分割
  152. $items = array_filter(explode("\n", $params['content_items']));
  153. $params['content_items'] = json_encode($items, JSON_UNESCAPED_UNICODE);
  154. }
  155. }
  156. $params['updatetime'] = time();
  157. $result = $row->allowField(true)->save($params);
  158. Db::commit();
  159. } catch (ValidateException $e) {
  160. Db::rollback();
  161. $this->error($e->getMessage());
  162. } catch (PDOException $e) {
  163. Db::rollback();
  164. $this->error($e->getMessage());
  165. } catch (Exception $e) {
  166. Db::rollback();
  167. $this->error($e->getMessage());
  168. }
  169. if ($result !== false) {
  170. $this->success();
  171. } else {
  172. $this->error(__('No rows were updated'));
  173. }
  174. }
  175. $this->error(__('Parameter %s can not be empty', ''));
  176. }
  177. // 处理内容项目显示
  178. if ($row['content_items']) {
  179. $items = json_decode($row['content_items'], true);
  180. if (is_array($items)) {
  181. $row['content_items'] = implode("\n", $items);
  182. }
  183. }
  184. $this->view->assign("row", $row);
  185. return $this->view->fetch();
  186. }
  187. }