Goods.php 7.0 KB

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