You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Product.php 11 KiB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. namespace app\admin\controller\unishop;
  3. use app\common\controller\Backend;
  4. use fast\Tree;
  5. use think\Db;
  6. use think\Exception;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. /**
  10. * 产品管理
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Product extends Backend
  15. {
  16. /**
  17. * 快速搜索时执行查找的字段
  18. */
  19. protected $searchFields = 'title';
  20. /**
  21. * Multi方法可批量修改的字段
  22. */
  23. protected $multiFields = 'switch';
  24. /**
  25. * product模型对象
  26. * @var \app\admin\model\unishop\Product
  27. */
  28. protected $model = null;
  29. public function _initialize()
  30. {
  31. parent::_initialize();
  32. $this->model = new \app\admin\model\unishop\Product;
  33. $servers = \app\admin\model\unishop\Config::getByName('server');
  34. $this->assign('servers',json_decode($servers['value']));
  35. }
  36. /**
  37. * 添加
  38. */
  39. public function add()
  40. {
  41. if ($this->request->isPost()) {
  42. $params = $this->request->post("row/a");
  43. if ($params) {
  44. $params = $this->preExcludeFields($params);
  45. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  46. $params[$this->dataLimitField] = $this->auth->id;
  47. }
  48. $result = false;
  49. Db::startTrans();
  50. try {
  51. //是否采用模型验证
  52. if ($this->modelValidate) {
  53. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  54. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  55. $this->model->validateFailException(true)->validate($validate);
  56. }
  57. // 查看规格有没有添加
  58. if (!empty($params['use_spec']) && $params['use_spec'] == 1) {
  59. if (empty($params['specList']) || empty($params['specTableList']) || $params['specList'] == '""' || $params['specTableList'] == '""' || $params['specList'] == '[]' || $params['specTableList'] == '[]') {
  60. throw new Exception('规格不能为空');
  61. }
  62. }
  63. $params['server'] = implode(',',$params['server']);
  64. $result = $this->model->allowField(true)->save($params);
  65. Db::commit();
  66. } catch (ValidateException $e) {
  67. Db::rollback();
  68. $this->error($e->getMessage());
  69. } catch (PDOException $e) {
  70. Db::rollback();
  71. $this->error($e->getMessage());
  72. } catch (Exception $e) {
  73. Db::rollback();
  74. $this->error($e->getMessage());
  75. }
  76. if ($result !== false) {
  77. $this->success();
  78. } else {
  79. $this->error(__('No rows were inserted'));
  80. }
  81. }
  82. $this->error(__('Parameter %s can not be empty', ''));
  83. }
  84. return $this->view->fetch();
  85. }
  86. /**
  87. * 查看
  88. */
  89. public function index()
  90. {
  91. //设置过滤方法
  92. $this->request->filter(['strip_tags']);
  93. if ($this->request->isAjax()) {
  94. //如果发送的来源是Selectpage,则转发到Selectpage
  95. if ($this->request->request('keyField')) {
  96. return $this->selectpage();
  97. }
  98. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  99. $total = $this->model
  100. ->where($where)
  101. ->count();
  102. $list = $this->model
  103. ->with([
  104. 'category' => function($query) {
  105. $query->with('parent');
  106. },
  107. 'delivery'
  108. ])
  109. ->where($where)
  110. ->order($sort, $order)
  111. ->limit($offset, $limit)
  112. ->select();
  113. $list = collection($list)->toArray();
  114. $result = array("total" => $total, "rows" => $list);
  115. return json($result);
  116. }
  117. return $this->view->fetch();
  118. }
  119. public function selectpage(){
  120. return parent::selectpage();
  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)) {
  133. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  134. $this->error(__('You have no permission'));
  135. }
  136. }
  137. if ($this->request->isPost()) {
  138. $params = $this->request->post("row/a");
  139. if ($params) {
  140. $params = $this->preExcludeFields($params);
  141. $result = false;
  142. Db::startTrans();
  143. try {
  144. //是否采用模型验证
  145. if ($this->modelValidate) {
  146. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  147. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  148. $row->validateFailException(true)->validate($validate);
  149. }
  150. // 查看规格有没有添加
  151. if (!empty($params['use_spec']) && $params['use_spec'] == 1) {
  152. if (empty($params['specList']) || empty($params['specTableList']) || $params['specList'] == '""' || $params['specTableList'] == '""' || $params['specList'] == '[]' || $params['specTableList'] == '[]') {
  153. throw new Exception('规格不能为空');
  154. }
  155. }
  156. $params['server'] = implode(',',$params['server']);
  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. $this->view->assign("row", $row);
  178. $this->view->assign('categoryList', $this->build_category_select('row[category_id]', 'product' ,$row->category_id));
  179. return $this->view->fetch();
  180. }
  181. /**
  182. * 生成分类下拉列表框
  183. * @param string $name
  184. * @param string $type
  185. * @param mixed $selected
  186. * @param array $attr
  187. * @param array $header
  188. * @return string
  189. */
  190. protected function build_category_select($name, $type, $selected = null, $attr = [], $header = [])
  191. {
  192. $tree = Tree::instance();
  193. $tree->init(\app\admin\model\unishop\Category::getCategoryArray($type), 'pid');
  194. $categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
  195. $categorydata = $header ? $header : [];
  196. foreach ($categorylist as $k => $v) {
  197. $categorydata[$v['id']] = $v['name'];
  198. }
  199. $attr = array_merge(['id' => "c-{$name}", 'class' => 'form-control selectpicker'], $attr);
  200. return build_select($name, $categorydata, $selected, $attr);
  201. }
  202. /**
  203. * 选择附件
  204. */
  205. public function select()
  206. {
  207. if ($this->request->isAjax()) {
  208. return $this->index();
  209. }
  210. return $this->view->fetch();
  211. }
  212. /**
  213. * 编辑
  214. */
  215. public function copy($ids = null)
  216. {
  217. $row = $this->model->get($ids);
  218. if (!$row) {
  219. $this->error(__('No Results were found'));
  220. }
  221. $adminIds = $this->getDataLimitAdminIds();
  222. if (is_array($adminIds)) {
  223. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  224. $this->error(__('You have no permission'));
  225. }
  226. }
  227. if ($this->request->isPost()) {
  228. $params = $this->request->post("row/a");
  229. if ($params) {
  230. $params = $this->preExcludeFields($params);
  231. $result = false;
  232. Db::startTrans();
  233. try {
  234. //是否采用模型验证
  235. if ($this->modelValidate) {
  236. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  237. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  238. $row->validateFailException(true)->validate($validate);
  239. }
  240. // 查看规格有没有添加
  241. if (!empty($params['use_spec']) && $params['use_spec'] == 1) {
  242. if (empty($params['specList']) || empty($params['specTableList']) || $params['specList'] == '""' || $params['specTableList'] == '""' || $params['specList'] == '[]' || $params['specTableList'] == '[]') {
  243. throw new Exception('规格不能为空');
  244. }
  245. }
  246. $params['server'] = implode(',',$params['server']);
  247. $result = $this->model->allowField(true)->save($params);
  248. Db::commit();
  249. } catch (ValidateException $e) {
  250. Db::rollback();
  251. $this->error($e->getMessage());
  252. } catch (PDOException $e) {
  253. Db::rollback();
  254. $this->error($e->getMessage());
  255. } catch (Exception $e) {
  256. Db::rollback();
  257. $this->error($e->getMessage());
  258. }
  259. if ($result !== false) {
  260. $this->success();
  261. } else {
  262. $this->error(__('No rows were updated'));
  263. }
  264. }
  265. $this->error(__('Parameter %s can not be empty', ''));
  266. }
  267. $this->view->assign("row", $row);
  268. $this->view->assign('categoryList', $this->build_category_select('row[category_id]', 'product' ,$row->category_id));
  269. return $this->view->fetch();
  270. }
  271. }