Product.php 11 KB

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