Goods.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace app\admin\controller\shop;
  3. use app\common\controller\Backend;
  4. use app\admin\model\Goodscats;
  5. use Think\Db;
  6. use think\Session;
  7. /**
  8. * 商品管理
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Goods extends Backend
  13. {
  14. /**
  15. * Goods模型对象
  16. * @var \app\admin\model\Goods
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\Goods;
  23. $this->categoryList=Goodscats::getTreeList();
  24. array_unshift($this->categoryList, ['id' => 0, 'name' => '无','type'=>0]);
  25. $this->assign('categoryList', $this->categoryList);
  26. $this->assign('shoptype',config('shopType'));
  27. }
  28. /**
  29. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  30. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  31. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32. */
  33. /**
  34. * 查看
  35. */
  36. public function index()
  37. {
  38. //当前是否为关联查询
  39. $this->relationSearch = false;
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags']);
  42. if ($this->request->isAjax())
  43. {
  44. //如果发送的来源是Selectpage,则转发到Selectpage
  45. if ($this->request->request('keyField'))
  46. {
  47. return $this->selectpage();
  48. }
  49. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  50. $total = $this->model
  51. ->where($where)
  52. ->order($sort, $order)
  53. ->count();
  54. $list = $this->model
  55. ->where($where)
  56. ->order($sort, $order)
  57. ->limit($offset, $limit)
  58. ->select();
  59. foreach ($list as &$row) {
  60. $row['catid']=get_table_column('goods_cats',$row['catid'],'name');
  61. $row['stuid']=get_table_column('studio',$row['stuid'],'title');
  62. $row['userid']=get_user_info($row['userid'],'username','mobile');
  63. }
  64. $result = array("total" => $total, "rows" => $list);
  65. return json($result);
  66. }
  67. return $this->view->fetch();
  68. }
  69. function add()
  70. {
  71. if ($this->request->isPost()) {
  72. $params = $this->request->post("row/a");
  73. if ($params) {
  74. $params = $this->preExcludeFields($params);
  75. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  76. $params[$this->dataLimitField] = $this->auth->id;
  77. }
  78. $params['price1']=$params['price'];
  79. $result = false;
  80. Db::startTrans();
  81. try {
  82. //是否采用模型验证
  83. if ($this->modelValidate) {
  84. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  85. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  86. $this->model->validateFailException(true)->validate($validate);
  87. }
  88. $result = $this->model->allowField(true)->insertGetId($params);
  89. Db::commit();
  90. } catch (ValidateException $e) {
  91. Db::rollback();
  92. $this->error($e->getMessage());
  93. } catch (PDOException $e) {
  94. Db::rollback();
  95. $this->error($e->getMessage());
  96. } catch (Exception $e) {
  97. Db::rollback();
  98. $this->error($e->getMessage());
  99. }
  100. if ($result !== false) {
  101. $this->success();
  102. } else {
  103. $this->error(__('No rows were inserted'));
  104. }
  105. }
  106. $this->error(__('Parameter %s can not be empty', ''));
  107. }
  108. return parent::add();
  109. }
  110. public function edit($ids = NULL)
  111. {
  112. $row = $this->model->get($ids);
  113. if (!$row)
  114. {
  115. $this->error(__('No Results were found'));
  116. }
  117. if ($this->request->isPost()) {
  118. $params = $this->request->post("row/a");
  119. if ($params) {
  120. $params['price1']=$params['price'];
  121. $params = $this->preExcludeFields($params);
  122. $result = false;
  123. Db::startTrans();
  124. try {
  125. //是否采用模型验证
  126. if ($this->modelValidate) {
  127. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  128. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  129. $row->validateFailException(true)->validate($validate);
  130. }
  131. $result = $row->allowField(true)->save($params);
  132. Db::commit();
  133. } catch (ValidateException $e) {
  134. Db::rollback();
  135. $this->error($e->getMessage());
  136. } catch (PDOException $e) {
  137. Db::rollback();
  138. $this->error($e->getMessage());
  139. } catch (Exception $e) {
  140. Db::rollback();
  141. $this->error($e->getMessage());
  142. }
  143. if ($result !== false) {
  144. $this->success();
  145. } else {
  146. $this->error(__('No rows were updated'));
  147. }
  148. }
  149. $this->error(__('Parameter %s can not be empty', ''));
  150. }
  151. return parent::edit($ids);
  152. }
  153. function multi($ids = '')
  154. {
  155. $ids = $ids ? $ids : $this->request->param("ids");
  156. if ($ids) {
  157. $params=input('params');
  158. $arrs=explode('=', $params);
  159. $data[$arrs[0]]=$arrs[1];
  160. $res=$this->model->where(['id'=>$ids])->update ($data);
  161. if($res)
  162. {
  163. $this->success('操作成功');
  164. }else{
  165. $this->error('操作失败');
  166. }
  167. }else{
  168. $this->error('请选择要操作的数据');
  169. }
  170. return parent::multi($ids);
  171. }
  172. }