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.
 
 
 
 
 
 

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