GoodsLabelGroup.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace app\admin\controller\shop;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\Exception;
  7. use think\exception\ValidateException;
  8. class GoodsLabelGroup extends Backend
  9. {
  10. /**
  11. * GoodsLabelGroup模型对象
  12. * @var \app\admin\model\shop\GoodsLabelGroup
  13. */
  14. protected $model = null;
  15. public function _initialize()
  16. {
  17. parent::_initialize();
  18. $this->model = new \app\admin\model\shop\GoodsLabelGroup;
  19. $this->view->assign("statusList", $this->model->getStatusList());
  20. $this->assignconfig("statusList", json_encode($this->model->getStatusList()));
  21. }
  22. public function getList()
  23. {
  24. $list = $this->model->field('id,name')->select();
  25. return json($list);
  26. }
  27. public function index()
  28. {
  29. $this->relationSearch = false;
  30. $this->request->filter(['strip_tags', 'trim']);
  31. if ($this->request->isAjax()) {
  32. if ($this->request->request('keyField')) {
  33. return $this->selectpage();
  34. }
  35. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  36. $list = $this->model
  37. ->where($where)
  38. ->order($sort, $order)
  39. ->paginate($limit);
  40. foreach ($list as $row) {
  41. $row->visible(['id', 'name', 'sort', 'status', 'createtime', 'updatetime']);
  42. $row->visible(['status_text']);
  43. $row->hidden(['deletetime']);
  44. }
  45. $result = array("total" => $list->total(), "rows" => $list->items());
  46. return json($result);
  47. }
  48. return $this->view->fetch();
  49. }
  50. /**
  51. * 添加
  52. */
  53. public function add()
  54. {
  55. if ($this->request->isPost()) {
  56. $params = $this->request->post('row/a');
  57. if ($params) {
  58. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  59. $params[$this->dataLimitField] = $this->auth->id;
  60. }
  61. // 检查标签名称是否唯一
  62. $label = $this->model->where('name', $params['name'])->find();
  63. if ($label) {
  64. $this->error('分组名称已存在');
  65. }
  66. $result = false;
  67. Db::startTrans();
  68. try {
  69. //是否采用模型验证
  70. if ($this->modelValidate) {
  71. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  72. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  73. $this->model->validateFailException(true)->validate($validate);
  74. }
  75. $result = $this->model->allowField(true)->save($params);
  76. Db::commit();
  77. } catch (ValidateException $e) {
  78. Db::rollback();
  79. $this->error($e->getMessage());
  80. } catch (PDOException $e) {
  81. Db::rollback();
  82. $this->error($e->getMessage());
  83. } catch (Exception $e) {
  84. Db::rollback();
  85. $this->error($e->getMessage());
  86. }
  87. if ($result !== false) {
  88. $this->success('添加成功');
  89. } else {
  90. $this->error(__('No rows were inserted'));
  91. }
  92. }
  93. $this->error(__('Parameter %s can not be empty', ''));
  94. }
  95. return $this->view->fetch();
  96. }
  97. /**
  98. * 编辑
  99. */
  100. public function edit($ids = null)
  101. {
  102. $row = $this->model->get($ids);
  103. if (!$row) {
  104. $this->error(__('No Results were found'));
  105. }
  106. $adminIds = $this->getDataLimitAdminIds();
  107. if (is_array($adminIds)) {
  108. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  109. $this->error(__('You have no permission'));
  110. }
  111. }
  112. if ($this->request->isPost()) {
  113. $params = $this->request->post('row/a');
  114. if ($params) {
  115. // 检查标签名称是否唯一
  116. $label = $this->model->where('name', $params['name'])->where('id', 'neq', $row->id)->find();
  117. if ($label) {
  118. $this->error('分组名称已存在');
  119. }
  120. $params = $this->preExcludeFields($params);
  121. $result = false;
  122. Db::startTrans();
  123. try {
  124. //是否采用模型验证
  125. if ($this->modelValidate) {
  126. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  127. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  128. $row->validateFailException(true)->validate($validate);
  129. }
  130. $result = $row->allowField(true)->save($params);
  131. Db::commit();
  132. } catch (ValidateException $e) {
  133. Db::rollback();
  134. $this->error($e->getMessage());
  135. } catch (PDOException $e) {
  136. Db::rollback();
  137. $this->error($e->getMessage());
  138. } catch (Exception $e) {
  139. Db::rollback();
  140. $this->error($e->getMessage());
  141. }
  142. if ($result !== false) {
  143. $this->success('编辑成功');
  144. } else {
  145. $this->error(__('No rows were updated'));
  146. }
  147. }
  148. $this->error(__('Parameter %s can not be empty', ''));
  149. }
  150. $this->view->assign("row", $row);
  151. return $this->view->fetch();
  152. }
  153. }