Box.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. namespace app\admin\controller\shai;
  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 = ['*'];
  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\shai\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. //print_r($where);
  46. //exit;
  47. $list = db::name('shai')
  48. // ->field('box.*')
  49. ->where($where)
  50. ->order('switch DESC')
  51. ->paginate($limit);
  52. //->select()
  53. ;
  54. //print_r($list);
  55. //exit;
  56. $result = array("total" => $list->total(), "rows" => $list->items());
  57. return json($result);
  58. }
  59. return $this->view->fetch();
  60. }
  61. public function selectpage()
  62. {
  63. return parent::selectpage();
  64. }
  65. public function multi($ids = "")
  66. {
  67. if (!$this->request->isPost()) {
  68. $this->error(__("Invalid parameters"));
  69. }
  70. $ids = $ids ? $ids : $this->request->post("ids");
  71. if ($ids) {
  72. if ($this->request->has('params')) {
  73. parse_str($this->request->post("params"), $values);
  74. $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  75. if ($values) {
  76. $adminIds = $this->getDataLimitAdminIds();
  77. if (is_array($adminIds)) {
  78. $this->model->where($this->dataLimitField, 'in', $adminIds);
  79. }
  80. $count = 0;
  81. // file_put_contents("20220319.txt", var_export($ids, true));
  82. Db::startTrans();
  83. try {
  84. // if (isset($values['is_try'])) {
  85. // if (strpos($ids, ',')) {
  86. // throw new \Exception('只能设置一个盲盒为试一试');
  87. // }
  88. // // 所有盲盒取消试一试
  89. // \app\admin\model\box\Box::update(['is_try' => 0], ['is_try' => 1]);
  90. // }
  91. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  92. foreach ($list as $index => $item) {
  93. $count += $item->allowField(true)->isUpdate(true)->save($values);
  94. }
  95. if (isset($values['switch'])) {
  96. // file_put_contents("20220319.txt", var_export($values, true));
  97. // \app\admin\model\shai\Box::where(['id'=>$ids])->update($values);
  98. $res = db('shai')->where('id',$ids)->update($values);
  99. // file_put_contents("20220319.txt", var_export($res, true));
  100. $count++;
  101. }
  102. // file_put_contents("20220319.txt", var_export(1, true));
  103. Db::commit();
  104. // file_put_contents("20220319.txt", var_export(2, true));
  105. } catch (PDOException $e) {
  106. Db::rollback();
  107. $this->error($e->getMessage());
  108. } catch (\Exception $e) {
  109. Db::rollback();
  110. $this->error($e->getMessage());
  111. }
  112. if ($count) {
  113. $this->success();
  114. } else {
  115. $this->error(__('No rows were updated'));
  116. }
  117. } else {
  118. $this->error(__('You have no permission'));
  119. }
  120. }
  121. }
  122. $this->error(__('Parameter %s can not be empty', 'ids'));
  123. }
  124. public function add()
  125. {
  126. if ($this->request->isPost()) {
  127. $params = $this->request->post("row/a");
  128. // print_r($params);
  129. $params['create_time']=time();
  130. Db::table('box_shai')->insertGetId($params);
  131. $this->success();
  132. // $cr=DB::name('box_shai')->allowField(true)->isUpdate(true)->save($params);
  133. }
  134. else{
  135. return $this->view->fetch();
  136. }}
  137. public function del($ids = "")
  138. {
  139. if (!$this->request->isPost()) {
  140. $this->error(__("Invalid parameters"));
  141. }
  142. $ids = $ids ? $ids : $this->request->post("ids");
  143. if ($ids) {
  144. $pk = $this->model->getPk();
  145. // print_r($pk);
  146. $adminIds = $this->getDataLimitAdminIds();
  147. if (is_array($adminIds)) {
  148. $this->model->where($this->dataLimitField, 'in', $adminIds);
  149. }
  150. $list = db::name('shai')->where($pk, 'in', $ids)->select();
  151. //print_r($list);
  152. $count = 0;
  153. Db::startTrans();
  154. try {
  155. foreach ($list as $k => $v) {
  156. $count += db::name('shai')->where('id='.$v['id'])->delete();
  157. // print_r($v);
  158. }
  159. Db::commit();
  160. } catch (PDOException $e) {
  161. Db::rollback();
  162. $this->error($e->getMessage());
  163. } catch (Exception $e) {
  164. Db::rollback();
  165. $this->error($e->getMessage());
  166. }
  167. if ($count) {
  168. $this->success();
  169. } else {
  170. $this->error(__('No rows were deleted'));
  171. }
  172. }
  173. $this->error(__('Parameter %s can not be empty', 'ids'));
  174. }
  175. public function edit($ids = null)
  176. {
  177. $row = $this->request->route('ids');
  178. if (!$row) {
  179. $this->error(__('No Results were found'));
  180. }
  181. $adminIds = $this->getDataLimitAdminIds();
  182. if (is_array($adminIds)) {
  183. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  184. $this->error(__('You have no permission'));
  185. }
  186. }
  187. if ($this->request->isPost()) {
  188. $params = $this->request->post("row/a");
  189. $params['create_time']=time();
  190. Db::table('box_shai')->where(['id'=>$row])->setField($params);
  191. // Db::table('box_shai')->insertGetId($params);
  192. $this->success();
  193. }
  194. // print_r($row);
  195. $row2=db::name('shai')->where('id='.$row)->find();
  196. $this->view->assign("row", $row2);
  197. return $this->view->fetch();
  198. }
  199. }