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.
 
 
 
 
 
 

306 lines
11 KiB

  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. $params['category_id']=78;
  49. $result = false;
  50. Db::startTrans();
  51. try {
  52. //是否采用模型验证
  53. if ($this->modelValidate) {
  54. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  55. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  56. $this->model->validateFailException(true)->validate($validate);
  57. }
  58. // 查看规格有没有添加
  59. if (!empty($params['use_spec']) && $params['use_spec'] == 1) {
  60. if (empty($params['specList']) || empty($params['specTableList']) || $params['specList'] == '""' || $params['specTableList'] == '""' || $params['specList'] == '[]' || $params['specTableList'] == '[]') {
  61. throw new Exception('规格不能为空');
  62. }
  63. }
  64. $params['server'] = implode(',',$params['server']);
  65. $result = $this->model->allowField(true)->save($params);
  66. Db::commit();
  67. } catch (ValidateException $e) {
  68. Db::rollback();
  69. $this->error($e->getMessage());
  70. } catch (PDOException $e) {
  71. Db::rollback();
  72. $this->error($e->getMessage());
  73. } catch (Exception $e) {
  74. Db::rollback();
  75. $this->error($e->getMessage());
  76. }
  77. if ($result !== false) {
  78. $this->success();
  79. } else {
  80. $this->error(__('No rows were inserted'));
  81. }
  82. }
  83. $this->error(__('Parameter %s can not be empty', ''));
  84. }
  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. $params['category_id']=78;
  144. Db::startTrans();
  145. try {
  146. //是否采用模型验证
  147. if ($this->modelValidate) {
  148. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  149. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  150. $row->validateFailException(true)->validate($validate);
  151. }
  152. // 查看规格有没有添加
  153. if (!empty($params['use_spec']) && $params['use_spec'] == 1) {
  154. if (empty($params['specList']) || empty($params['specTableList']) || $params['specList'] == '""' || $params['specTableList'] == '""' || $params['specList'] == '[]' || $params['specTableList'] == '[]') {
  155. throw new Exception('规格不能为空');
  156. }
  157. }
  158. $params['server'] = implode(',',$params['server']);
  159. $result = $row->allowField(true)->save($params);
  160. Db::commit();
  161. } catch (ValidateException $e) {
  162. Db::rollback();
  163. $this->error($e->getMessage());
  164. } catch (PDOException $e) {
  165. Db::rollback();
  166. $this->error($e->getMessage());
  167. } catch (Exception $e) {
  168. Db::rollback();
  169. $this->error($e->getMessage());
  170. }
  171. if ($result !== false) {
  172. $this->success();
  173. } else {
  174. $this->error(__('No rows were updated'));
  175. }
  176. }
  177. $this->error(__('Parameter %s can not be empty', ''));
  178. }
  179. $this->view->assign("row", $row);
  180. $this->view->assign('categoryList', $this->build_category_select('row[category_id]', 'product' ,$row->category_id));
  181. return $this->view->fetch();
  182. }
  183. /**
  184. * 生成分类下拉列表框
  185. * @param string $name
  186. * @param string $type
  187. * @param mixed $selected
  188. * @param array $attr
  189. * @param array $header
  190. * @return string
  191. */
  192. protected function build_category_select($name, $type, $selected = null, $attr = [], $header = [])
  193. {
  194. $tree = Tree::instance();
  195. $tree->init(\app\admin\model\unishop\Category::getCategoryArray($type), 'pid');
  196. $categorylist = $tree->getTreeList($tree->getTreeArray(0), 'name');
  197. $categorydata = $header ? $header : [];
  198. foreach ($categorylist as $k => $v) {
  199. $categorydata[$v['id']] = $v['name'];
  200. }
  201. $attr = array_merge(['id' => "c-{$name}", 'class' => 'form-control selectpicker'], $attr);
  202. return build_select($name, $categorydata, $selected, $attr);
  203. }
  204. /**
  205. * 选择附件
  206. */
  207. public function select()
  208. {
  209. if ($this->request->isAjax()) {
  210. return $this->index();
  211. }
  212. return $this->view->fetch();
  213. }
  214. /**
  215. * 编辑
  216. */
  217. public function copy($ids = null)
  218. {
  219. $row = $this->model->get($ids);
  220. if (!$row) {
  221. $this->error(__('No Results were found'));
  222. }
  223. $adminIds = $this->getDataLimitAdminIds();
  224. if (is_array($adminIds)) {
  225. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  226. $this->error(__('You have no permission'));
  227. }
  228. }
  229. if ($this->request->isPost()) {
  230. $params = $this->request->post("row/a");
  231. if ($params) {
  232. $params = $this->preExcludeFields($params);
  233. $result = false;
  234. Db::startTrans();
  235. try {
  236. //是否采用模型验证
  237. if ($this->modelValidate) {
  238. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  239. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  240. $row->validateFailException(true)->validate($validate);
  241. }
  242. // 查看规格有没有添加
  243. if (!empty($params['use_spec']) && $params['use_spec'] == 1) {
  244. if (empty($params['specList']) || empty($params['specTableList']) || $params['specList'] == '""' || $params['specTableList'] == '""' || $params['specList'] == '[]' || $params['specTableList'] == '[]') {
  245. throw new Exception('规格不能为空');
  246. }
  247. }
  248. $params['server'] = implode(',',$params['server']);
  249. $result = $this->model->allowField(true)->save($params);
  250. Db::commit();
  251. } catch (ValidateException $e) {
  252. Db::rollback();
  253. $this->error($e->getMessage());
  254. } catch (PDOException $e) {
  255. Db::rollback();
  256. $this->error($e->getMessage());
  257. } catch (Exception $e) {
  258. Db::rollback();
  259. $this->error($e->getMessage());
  260. }
  261. if ($result !== false) {
  262. $this->success();
  263. } else {
  264. $this->error(__('No rows were updated'));
  265. }
  266. }
  267. $this->error(__('Parameter %s can not be empty', ''));
  268. }
  269. $this->view->assign("row", $row);
  270. $this->view->assign('categoryList', $this->build_category_select('row[category_id]', 'product' ,$row->category_id));
  271. return $this->view->fetch();
  272. }
  273. }