SkuTemplate.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace app\admin\controller\shop;
  3. use app\common\controller\Backend;
  4. use Exception;
  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 SkuTemplate extends Backend
  14. {
  15. /**
  16. * SkuTemplate模型对象
  17. * @var \app\admin\model\shop\SkuTemplate
  18. */
  19. protected $model = null;
  20. protected $selectpageFields = "id,name,spec_names,spec_values";
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\shop\SkuTemplate;
  25. }
  26. protected function resetSpec()
  27. {
  28. $spec = $this->request->post("spec/a");
  29. $name = $this->request->post("name");
  30. if ($spec) {
  31. $spec_names = [];
  32. $spec_values = [];
  33. foreach ($spec as $item) {
  34. if (empty($item['name'])) {
  35. $this->error('规格名称不能为空');
  36. }
  37. $spec_names[] = $item['name'];
  38. if (empty($item['value']) || !is_array($item['value'])) {
  39. $this->error('规格属性不能为空');
  40. }
  41. foreach ($item['value'] as $value) {
  42. if (empty($value)) {
  43. $this->error('请填写' . $item['name'] . '的属性值');
  44. }
  45. if (stripos($value, ',') !== false) {
  46. $this->error('属性值中不能包含,');
  47. }
  48. }
  49. if (count($item['value']) != count(array_unique($item['value']))) {
  50. $this->error($item['name'] . '的属性值不允许重复');
  51. }
  52. $spec_values[] = implode(',', $item['value']);
  53. }
  54. return [
  55. 'name' => $name,
  56. 'spec_names' => implode(';', $spec_names),
  57. 'spec_values' => implode(';', $spec_values)
  58. ];
  59. }
  60. }
  61. /**
  62. * 添加
  63. */
  64. public function add()
  65. {
  66. if ($this->request->isPost()) {
  67. $params = $this->resetSpec();
  68. if ($params) {
  69. $params = $this->preExcludeFields($params);
  70. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  71. $params[$this->dataLimitField] = $this->auth->id;
  72. }
  73. $result = false;
  74. Db::startTrans();
  75. try {
  76. //是否采用模型验证
  77. if ($this->modelValidate) {
  78. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  79. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  80. $this->model->validateFailException(true)->validate($validate);
  81. }
  82. $result = $this->model->allowField(true)->save($params);
  83. Db::commit();
  84. } catch (ValidateException $e) {
  85. Db::rollback();
  86. $this->error($e->getMessage());
  87. } catch (PDOException $e) {
  88. Db::rollback();
  89. $this->error($e->getMessage());
  90. } catch (Exception $e) {
  91. Db::rollback();
  92. $this->error($e->getMessage());
  93. }
  94. if ($result !== false) {
  95. $this->success('添加成功');
  96. } else {
  97. $this->error(__('No rows were inserted'));
  98. }
  99. }
  100. $this->error(__('Parameter %s can not be empty', ''));
  101. }
  102. return $this->view->fetch();
  103. }
  104. /**
  105. * 编辑
  106. */
  107. public function edit($ids = null)
  108. {
  109. $row = $this->model->get($ids);
  110. if (!$row) {
  111. $this->error(__('No Results were found'));
  112. }
  113. $adminIds = $this->getDataLimitAdminIds();
  114. if (is_array($adminIds)) {
  115. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  116. $this->error(__('You have no permission'));
  117. }
  118. }
  119. if ($this->request->isPost()) {
  120. $params = $this->resetSpec();
  121. if ($params) {
  122. $params = $this->preExcludeFields($params);
  123. $result = false;
  124. Db::startTrans();
  125. try {
  126. //是否采用模型验证
  127. if ($this->modelValidate) {
  128. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  129. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  130. $row->validateFailException(true)->validate($validate);
  131. }
  132. $result = $row->allowField(true)->save($params);
  133. Db::commit();
  134. } catch (ValidateException $e) {
  135. Db::rollback();
  136. $this->error($e->getMessage());
  137. } catch (PDOException $e) {
  138. Db::rollback();
  139. $this->error($e->getMessage());
  140. } catch (Exception $e) {
  141. Db::rollback();
  142. $this->error($e->getMessage());
  143. }
  144. if ($result !== false) {
  145. $this->success('编辑成功');
  146. } else {
  147. $this->error(__('No rows were updated'));
  148. }
  149. }
  150. $this->error(__('Parameter %s can not be empty', ''));
  151. }
  152. $this->assignconfig('row', $row);
  153. return $this->view->fetch();
  154. }
  155. }