Box.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace app\admin\controller\box;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. /**
  7. * 盲盒管理
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Box extends Backend
  12. {
  13. protected $modelValidate = true;
  14. protected $multiFields = "switch";
  15. protected $noNeedRight = ['selectpage'];
  16. /**
  17. * Box模型对象
  18. * @var \app\admin\model\box\Box
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\box\Box;
  25. }
  26. public function import()
  27. {
  28. parent::import();
  29. }
  30. /**
  31. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  32. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  33. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  34. */
  35. public function index()
  36. {
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags', 'trim']);
  39. if ($this->request->isAjax()) {
  40. //如果发送的来源是Selectpage,则转发到Selectpage
  41. if ($this->request->request('keyField')) {
  42. return $this->selectpage();
  43. }
  44. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  45. $list = $this->model->alias('box')
  46. ->field('box.box_name,box.id,box.box_banner_images,box_foot_images,box.coin_price,box.update_time,box.is_hot,box.is_cheap,box.is_try,box.sort,box.switch')
  47. ->field('category.name category_name')
  48. ->join('category category', 'category.id = box.category_id', 'left')
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->paginate($limit);
  52. $result = array("total" => $list->total(), "rows" => $list->items());
  53. return json($result);
  54. }
  55. return $this->view->fetch();
  56. }
  57. public function selectpage()
  58. {
  59. return parent::selectpage();
  60. }
  61. public function multi($ids = "")
  62. {
  63. if (!$this->request->isPost()) {
  64. $this->error(__("Invalid parameters"));
  65. }
  66. $ids = $ids ? $ids : $this->request->post("ids");
  67. if ($ids) {
  68. if ($this->request->has('params')) {
  69. parse_str($this->request->post("params"), $values);
  70. $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  71. if ($values) {
  72. $adminIds = $this->getDataLimitAdminIds();
  73. if (is_array($adminIds)) {
  74. $this->model->where($this->dataLimitField, 'in', $adminIds);
  75. }
  76. $count = 0;
  77. Db::startTrans();
  78. try {
  79. if (isset($values['is_try'])) {
  80. if (strpos($ids, ',')) {
  81. throw new \Exception('只能设置一个盲盒为试一试');
  82. }
  83. // 所有盲盒取消试一试
  84. \app\admin\model\box\Box::update(['is_try' => 0], ['is_try' => 1]);
  85. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  86. foreach ($list as $index => $item) {
  87. $count += $item->allowField(true)->isUpdate(true)->save($values);
  88. }
  89. }
  90. if (isset($values['switch'])) {
  91. \app\admin\model\box\Box::where(['id'=>$ids])->update($values);
  92. $count++;
  93. }
  94. Db::commit();
  95. } catch (PDOException $e) {
  96. Db::rollback();
  97. $this->error($e->getMessage());
  98. } catch (\Exception $e) {
  99. Db::rollback();
  100. $this->error($e->getMessage());
  101. }
  102. if ($count) {
  103. $this->success();
  104. } else {
  105. $this->error(__('No rows were updated'));
  106. }
  107. } else {
  108. $this->error(__('You have no permission'));
  109. }
  110. }
  111. }
  112. $this->error(__('Parameter %s can not be empty', 'ids'));
  113. }
  114. }