Popular.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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\ProductArea;
  7. use think\exception\DbException;
  8. use think\exception\PDOException;
  9. use think\exception\ValidateException;
  10. /**
  11. * 热门抢购
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class Popular extends Backend
  16. {
  17. /**
  18. * Popular模型对象
  19. * @var \app\admin\model\product\Popular
  20. */
  21. protected $model = null;
  22. protected $productArea = null;
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = new \app\common\model\ProductPopular();
  27. $this->productArea = new ProductArea;
  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
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->paginate($limit);
  52. $result = ['total' => $list->total(), 'rows' => $list->items()];
  53. return json($result);
  54. }
  55. /**
  56. * 添加
  57. * @return string
  58. * @throws \think\Exception
  59. */
  60. public function add()
  61. {
  62. if (false === $this->request->isPost()) {
  63. return $this->view->fetch();
  64. }
  65. $params = $this->request->post('row/a');
  66. if (empty($params)) {
  67. $this->error(__('Parameter %s can not be empty', ''));
  68. }
  69. $params = $this->preExcludeFields($params);
  70. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  71. $params[$this->dataLimitField] = $this->auth->id;
  72. }
  73. $result = false;
  74. Db::startTrans();
  75. try {
  76. //是否采用模型验证
  77. if ($this->modelValidate) {
  78. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  79. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  80. $this->model->validateFailException()->validate($validate);
  81. }
  82. $params['stock'] = ProductArea::getProductStock($params['product_id']);
  83. $result = $this->model->allowField(true)->save($params);
  84. Db::commit();
  85. } catch (ValidateException|PDOException|Exception $e) {
  86. Db::rollback();
  87. $this->error($e->getMessage());
  88. }
  89. if ($result === false) {
  90. $this->error(__('No rows were inserted'));
  91. }
  92. $this->success();
  93. }
  94. /**
  95. * 编辑
  96. * @param $ids
  97. * @return string
  98. * @throws DbException
  99. * @throws \think\Exception
  100. */
  101. public function edit($ids = null)
  102. {
  103. $row = $this->model->get($ids);
  104. if (!$row) {
  105. $this->error(__('No Results were found'));
  106. }
  107. $adminIds = $this->getDataLimitAdminIds();
  108. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  109. $this->error(__('You have no permission'));
  110. }
  111. if (false === $this->request->isPost()) {
  112. $this->view->assign('row', $row);
  113. return $this->view->fetch();
  114. }
  115. $params = $this->request->post('row/a');
  116. if (empty($params)) {
  117. $this->error(__('Parameter %s can not be empty', ''));
  118. }
  119. $params = $this->preExcludeFields($params);
  120. $result = false;
  121. Db::startTrans();
  122. try {
  123. //是否采用模型验证
  124. if ($this->modelValidate) {
  125. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  126. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  127. $row->validateFailException()->validate($validate);
  128. }
  129. $params['stock'] = ProductArea::getProductStock($params['product_id']);
  130. $result = $row->allowField(true)->save($params);
  131. Db::commit();
  132. } catch (ValidateException|PDOException|Exception $e) {
  133. Db::rollback();
  134. $this->error($e->getMessage());
  135. }
  136. if (false === $result) {
  137. $this->error(__('No rows were updated'));
  138. }
  139. $this->success();
  140. }
  141. }