Goods.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace app\cmgadm\controller\shop;
  3. use app\common\controller\Backend;
  4. use app\cmgadm\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\cmgadm\model\Goods
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\cmgadm\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. $this->model->where(['id'=> $result])->update(['description'=> 'G'.$result]);
  91. } catch (ValidateException $e) {
  92. Db::rollback();
  93. $this->error($e->getMessage());
  94. } catch (PDOException $e) {
  95. Db::rollback();
  96. $this->error($e->getMessage());
  97. } catch (Exception $e) {
  98. Db::rollback();
  99. $this->error($e->getMessage());
  100. }
  101. if ($result !== false) {
  102. $this->success();
  103. } else {
  104. $this->error(__('No rows were inserted'));
  105. }
  106. }
  107. $this->error(__('Parameter %s can not be empty', ''));
  108. }
  109. return parent::add();
  110. }
  111. public function edit($ids = NULL)
  112. {
  113. $row = $this->model->get($ids);
  114. if (!$row)
  115. {
  116. $this->error(__('No Results were found'));
  117. }
  118. if ($this->request->isPost()) {
  119. $params = $this->request->post("row/a");
  120. if ($params) {
  121. $params['price1']=$params['price'];
  122. $params = $this->preExcludeFields($params);
  123. $result = false;
  124. Db::startTrans();
  125. try {
  126. //是否采用模型验证
  127. if ($this->modelValidate) {
  128. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  129. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  130. $row->validateFailException(true)->validate($validate);
  131. }
  132. $result = $row->allowField(true)->save($params);
  133. Db::commit();
  134. } catch (ValidateException $e) {
  135. Db::rollback();
  136. $this->error($e->getMessage());
  137. } catch (PDOException $e) {
  138. Db::rollback();
  139. $this->error($e->getMessage());
  140. } catch (Exception $e) {
  141. Db::rollback();
  142. $this->error($e->getMessage());
  143. }
  144. if ($result !== false) {
  145. $this->success();
  146. } else {
  147. $this->error(__('No rows were updated'));
  148. }
  149. }
  150. $this->error(__('Parameter %s can not be empty', ''));
  151. }
  152. return parent::edit($ids);
  153. }
  154. function multi($ids = '')
  155. {
  156. $ids = $ids ? $ids : $this->request->param("ids");
  157. if ($ids) {
  158. $params=input('params');
  159. $arrs=explode('=', $params);
  160. $data[$arrs[0]]=$arrs[1];
  161. $res=$this->model->where(['id'=>$ids])->update ($data);
  162. if($res)
  163. {
  164. $this->success('操作成功');
  165. }else{
  166. $this->error('操作失败');
  167. }
  168. }else{
  169. $this->error('请选择要操作的数据');
  170. }
  171. return parent::multi($ids);
  172. }
  173. }