GoodsLabel.php 6.3 KB

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