Product.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. $this->view->assign('categoryList', $this->build_category_select('row[category_id]', 'product' , 0));
  85. return $this->view->fetch();
  86. }
  87. /**
  88. * 查看
  89. */
  90. public function index()
  91. {
  92. //设置过滤方法
  93. $this->request->filter(['strip_tags']);
  94. if ($this->request->isAjax()) {
  95. //如果发送的来源是Selectpage,则转发到Selectpage
  96. if ($this->request->request('keyField')) {
  97. return $this->selectpage();
  98. }
  99. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  100. $total = $this->model
  101. ->where($where)
  102. ->count();
  103. $list = $this->model
  104. ->with([
  105. 'category' => function($query) {
  106. $query->with('parent');
  107. },
  108. 'delivery'
  109. ])
  110. ->where($where)
  111. ->order($sort, $order)
  112. ->limit($offset, $limit)
  113. ->select();
  114. $list = collection($list)->toArray();
  115. $result = array("total" => $total, "rows" => $list);
  116. return json($result);
  117. }
  118. return $this->view->fetch();
  119. }
  120. public function selectpage(){
  121. return parent::selectpage();
  122. }
  123. /**
  124. * 编辑
  125. */
  126. public function edit($ids = null)
  127. {
  128. $row = $this->model->get($ids);
  129. if (!$row) {
  130. $this->error(__('No Results were found'));
  131. }
  132. $adminIds = $this->getDataLimitAdminIds();
  133. if (is_array($adminIds)) {
  134. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  135. $this->error(__('You have no permission'));
  136. }
  137. }
  138. if ($this->request->isPost()) {
  139. $params = $this->request->post("row/a");
  140. if ($params) {
  141. $params = $this->preExcludeFields($params);
  142. $result = false;
  143. Db::startTrans();
  144. try {
  145. //是否采用模型验证
  146. if ($this->modelValidate) {
  147. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  148. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  149. $row->validateFailException(true)->validate($validate);
  150. }
  151. // 查看规格有没有添加
  152. if (!empty($params['use_spec']) && $params['use_spec'] == 1) {
  153. if (empty($params['specList']) || empty($params['specTableList']) || $params['specList'] == '""' || $params['specTableList'] == '""' || $params['specList'] == '[]' || $params['specTableList'] == '[]') {
  154. throw new Exception('规格不能为空');
  155. }
  156. }
  157. // $params['server'] = implode(',',$params['server']);
  158. $result = $row->allowField(true)->save($params);
  159. Db::commit();
  160. } catch (ValidateException $e) {
  161. Db::rollback();
  162. $this->error($e->getMessage());
  163. } catch (PDOException $e) {
  164. Db::rollback();
  165. $this->error($e->getMessage());
  166. } catch (Exception $e) {
  167. Db::rollback();
  168. $this->error($e->getMessage());
  169. }
  170. if ($result !== false) {
  171. $this->success();
  172. } else {
  173. $this->error(__('No rows were updated'));
  174. }
  175. }
  176. $this->error(__('Parameter %s can not be empty', ''));
  177. }
  178. $this->view->assign("row", $row);
  179. // $this->view->assign('categoryList', $this->build_category_select('row[category_id]', 'product' ,$row->category_id));
  180. return $this->view->fetch();
  181. }
  182. /**
  183. * 生成分类下拉列表框
  184. * @param string $name
  185. * @param string $type
  186. * @param mixed $selected
  187. * @param array $attr
  188. * @param array $header
  189. * @return string
  190. */
  191. protected function build_category_select($name, $type, $selected = null, $attr = [], $header = [])
  192. {
  193. $tree = Tree::instance();
  194. $tree->init(\app\admin\model\unishop\Category::getCategoryArray($type), 'pid');
  195. $categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
  196. $categorydata = $header ? $header : [];
  197. foreach ($categorylist as $k => $v) {
  198. $categorydata[$v['id']] = $v['name'];
  199. }
  200. $attr = array_merge(['id' => "c-{$name}", 'class' => 'form-control selectpicker'], $attr);
  201. return build_select($name, $categorydata, $selected, $attr);
  202. }
  203. /**
  204. * 选择附件
  205. */
  206. public function select()
  207. {
  208. if ($this->request->isAjax()) {
  209. return $this->index();
  210. }
  211. return $this->view->fetch();
  212. }
  213. /**
  214. * 编辑
  215. */
  216. public function copy($ids = null)
  217. {
  218. $row = $this->model->get($ids);
  219. if (!$row) {
  220. $this->error(__('No Results were found'));
  221. }
  222. $adminIds = $this->getDataLimitAdminIds();
  223. if (is_array($adminIds)) {
  224. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  225. $this->error(__('You have no permission'));
  226. }
  227. }
  228. if ($this->request->isPost()) {
  229. $params = $this->request->post("row/a");
  230. if ($params) {
  231. $params = $this->preExcludeFields($params);
  232. $result = false;
  233. Db::startTrans();
  234. try {
  235. //是否采用模型验证
  236. if ($this->modelValidate) {
  237. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  238. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  239. $row->validateFailException(true)->validate($validate);
  240. }
  241. // 查看规格有没有添加
  242. if (!empty($params['use_spec']) && $params['use_spec'] == 1) {
  243. if (empty($params['specList']) || empty($params['specTableList']) || $params['specList'] == '""' || $params['specTableList'] == '""' || $params['specList'] == '[]' || $params['specTableList'] == '[]') {
  244. throw new Exception('规格不能为空');
  245. }
  246. }
  247. // $params['server'] = implode(',',$params['server']);
  248. $result = $this->model->allowField(true)->save($params);
  249. Db::commit();
  250. } catch (ValidateException $e) {
  251. Db::rollback();
  252. $this->error($e->getMessage());
  253. } catch (PDOException $e) {
  254. Db::rollback();
  255. $this->error($e->getMessage());
  256. } catch (Exception $e) {
  257. Db::rollback();
  258. $this->error($e->getMessage());
  259. }
  260. if ($result !== false) {
  261. $this->success();
  262. } else {
  263. $this->error(__('No rows were updated'));
  264. }
  265. }
  266. $this->error(__('Parameter %s can not be empty', ''));
  267. }
  268. $this->view->assign("row", $row);
  269. $this->view->assign('categoryList', $this->build_category_select('row[category_id]', 'product' ,$row->category_id));
  270. return $this->view->fetch();
  271. }
  272. }