Goods.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. //如果发送的来源是Selectpage,则转发到Selectpage
  45. if ($this->request->request('keyField')) {
  46. return $this->selectpage();
  47. }
  48. $filter = $this->request->get("filter", '');
  49. $filter = $filter ? json_decode($filter, true) : [];
  50. if (isset($filter['stuid']) && $filter['stuid']) {
  51. $filter['stuid'] = Studio::where(['title' => $filter['stuid']])->value('id');
  52. }
  53. list($where, $sort, $order, $offset, $limit) = $this->buildparamsFilter('', '', $filter);
  54. $total = $this->model
  55. ->where($where)
  56. ->order($sort, $order)
  57. ->count();
  58. //var_dump($this->model->getLastSql());
  59. $list = $this->model
  60. ->where($where)
  61. ->order($sort, $order)
  62. ->limit($offset, $limit)
  63. ->select();
  64. foreach ($list as &$row) {
  65. $row['catid'] = get_table_column('goods_cats', $row['catid'], 'name');
  66. $row['stuid'] = get_table_column('studio', $row['stuid'], 'title');
  67. $row['userid'] = get_user_info($row['userid'], 'username', 'mobile');
  68. }
  69. $result = array("total" => $total, "rows" => $list);
  70. return json($result);
  71. }
  72. return $this->view->fetch();
  73. }
  74. function add()
  75. {
  76. if ($this->request->isPost()) {
  77. $params = $this->request->post("row/a");
  78. if ($params) {
  79. $params = $this->preExcludeFields($params);
  80. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  81. $params[$this->dataLimitField] = $this->auth->id;
  82. }
  83. $params['price1'] = $params['price'];
  84. $result = false;
  85. Db::startTrans();
  86. try {
  87. //是否采用模型验证
  88. if ($this->modelValidate) {
  89. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  90. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  91. $this->model->validateFailException(true)->validate($validate);
  92. }
  93. $result = $this->model->allowField(true)->insertGetId($params);
  94. Db::commit();
  95. $this->model->where(['id' => $result])->update(['description' => 'G' . $result]);
  96. } catch (ValidateException $e) {
  97. Db::rollback();
  98. $this->error($e->getMessage());
  99. } catch (PDOException $e) {
  100. Db::rollback();
  101. $this->error($e->getMessage());
  102. } catch (Exception $e) {
  103. Db::rollback();
  104. $this->error($e->getMessage());
  105. }
  106. if ($result !== false) {
  107. $this->success();
  108. } else {
  109. $this->error(__('No rows were inserted'));
  110. }
  111. }
  112. $this->error(__('Parameter %s can not be empty', ''));
  113. }
  114. return parent::add();
  115. }
  116. public function edit($ids = NULL)
  117. {
  118. $row = $this->model->get($ids);
  119. if (!$row) {
  120. $this->error(__('No Results were found'));
  121. }
  122. if ($this->request->isPost()) {
  123. $params = $this->request->post("row/a");
  124. if ($params) {
  125. $params['price1'] = $params['price'];
  126. $params = $this->preExcludeFields($params);
  127. $result = false;
  128. Db::startTrans();
  129. try {
  130. //是否采用模型验证
  131. if ($this->modelValidate) {
  132. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  133. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  134. $row->validateFailException(true)->validate($validate);
  135. }
  136. $result = $row->allowField(true)->save($params);
  137. Db::commit();
  138. } catch (ValidateException $e) {
  139. Db::rollback();
  140. $this->error($e->getMessage());
  141. } catch (PDOException $e) {
  142. Db::rollback();
  143. $this->error($e->getMessage());
  144. } catch (Exception $e) {
  145. Db::rollback();
  146. $this->error($e->getMessage());
  147. }
  148. if ($result !== false) {
  149. $this->success();
  150. } else {
  151. $this->error(__('No rows were updated'));
  152. }
  153. }
  154. $this->error(__('Parameter %s can not be empty', ''));
  155. }
  156. return parent::edit($ids);
  157. }
  158. function multi($ids = '')
  159. {
  160. $ids = $ids ? $ids : $this->request->param("ids");
  161. if ($ids) {
  162. $params = input('params');
  163. $arrs = explode('=', $params);
  164. $data[$arrs[0]] = $arrs[1];
  165. $res = $this->model->where(['id' => $ids])->update($data);
  166. if ($res) {
  167. $this->success('操作成功');
  168. } else {
  169. $this->error('操作失败');
  170. }
  171. } else {
  172. $this->error('请选择要操作的数据');
  173. }
  174. return parent::multi($ids);
  175. }
  176. }