Lists.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace app\admin\controller\product;
  3. use app\common\controller\Backend;
  4. use Exception;
  5. use think\Db;
  6. use app\common\model\ProductOrder;
  7. use app\common\model\ProductArea;
  8. use think\exception\DbException;
  9. use think\exception\PDOException;
  10. use think\exception\ValidateException;
  11. /**
  12. * 商品列管理
  13. *
  14. * @icon fa fa-circle-o
  15. */
  16. class Lists extends Backend
  17. {
  18. protected $multiFields = 'status,is_transfer,is_gift,is_freight,is_teac' ;
  19. protected $model = null;
  20. protected $productArea = null;
  21. protected $areaCode = null;
  22. protected $relationSearch = true;
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->productArea = new ProductArea;
  27. $this->model = new \app\common\model\ProductLists;
  28. }
  29. /**
  30. * 查看
  31. *
  32. * @return string|Json
  33. * @throws \think\Exception
  34. * @throws DbException
  35. */
  36. public function index()
  37. {
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags', 'trim']);
  40. if (false === $this->request->isAjax()) {
  41. return $this->view->fetch();
  42. }
  43. //如果发送的来源是 Selectpage,则转发到 Selectpage
  44. if ($this->request->request('keyField')) {
  45. return $this->selectpage();
  46. }
  47. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  48. $list = $this->model->with('products')->with('productarea')->with('productareastop')//->with('productorder')
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->paginate($limit);
  52. foreach ($list as &$item) {
  53. $item['total_num']= $item->productarea? count($item->productarea): 0;
  54. $item['sell_num'] = $item->productareastop? count($item->productareastop): 0;
  55. }
  56. $result = ['total' => $list->total(), 'rows' => $list->items()];
  57. return json($result);
  58. }
  59. /**
  60. * 添加
  61. *
  62. * @return string
  63. * @throws \think\Exception
  64. */
  65. public function add()
  66. {
  67. if (false === $this->request->isPost()) {
  68. return $this->view->fetch();
  69. }
  70. $params = $this->request->post('row/a');
  71. if (empty($params)) {
  72. $this->error(__('Parameter %s can not be empty', ''));
  73. }
  74. $params = $this->preExcludeFields($params);
  75. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  76. $params[$this->dataLimitField] = $this->auth->id;
  77. }
  78. $result = false;
  79. Db::startTrans();
  80. try {
  81. //是否采用模型验证
  82. if ($this->modelValidate) {
  83. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  84. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  85. $this->model->validateFailException()->validate($validate);
  86. }
  87. //商品
  88. $result = $this->model->create($params);
  89. //if(!empty($params['is_area'])) $result= self::setEqualArea($result->id, $areaArr, $areaArrTxt, 0);
  90. Db::commit();
  91. } catch (ValidateException|PDOException|Exception $e) {
  92. Db::rollback();
  93. $this->error($e->getMessage());
  94. }
  95. if ($result === false) {
  96. $this->error(__('No rows were inserted'));
  97. }
  98. $this->success();
  99. }
  100. /**
  101. * 编辑
  102. * @param $ids
  103. * @return string
  104. * @throws DbException
  105. * @throws \think\Exception
  106. */
  107. public function edit($ids = null)
  108. {
  109. $row = $this->model->get($ids);
  110. if (!$row) {
  111. $this->error(__('No Results were found'));
  112. }
  113. $adminIds = $this->getDataLimitAdminIds();
  114. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  115. $this->error(__('You have no permission'));
  116. }
  117. if (false === $this->request->isPost()) {
  118. $this->view->assign('row', $row);
  119. return $this->view->fetch();
  120. }
  121. $params = $this->request->post('row/a');
  122. if (empty($params)) {
  123. $this->error(__('Parameter %s can not be empty', ''));
  124. }
  125. $params = $this->preExcludeFields($params);
  126. $result = false;
  127. Db::startTrans();
  128. try {
  129. //是否采用模型验证
  130. if ($this->modelValidate) {
  131. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  132. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  133. $row->validateFailException()->validate($validate);
  134. }
  135. $result=$row->allowField(true)->save($params);
  136. Db::commit();
  137. } catch (ValidateException|PDOException|Exception $e) {
  138. Db::rollback();
  139. $this->error($e->getMessage());
  140. }
  141. if (false === $result) {
  142. $this->error(__('No rows were updated'));
  143. }
  144. $this->success();
  145. }
  146. }