Category.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\admin\controller\shai;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. /**
  8. * 盲盒分类
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Category extends Backend
  13. {
  14. protected $modelValidate = true;
  15. protected $modelSceneValidate = true;
  16. protected $noNeedRight = ['*'];
  17. /**
  18. * Category模型对象
  19. * @var \app\admin\model\box\Category
  20. */
  21. protected $model = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\box\Category;
  26. }
  27. public function import()
  28. {
  29. parent::import();
  30. }
  31. /**
  32. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  33. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  34. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  35. */
  36. public function edit($ids = null)
  37. {
  38. $row = $this->model->get($ids);
  39. if (!$row) {
  40. $this->error(__('No Results were found'));
  41. }
  42. $adminIds = $this->getDataLimitAdminIds();
  43. if (is_array($adminIds)) {
  44. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  45. $this->error(__('You have no permission'));
  46. }
  47. }
  48. if ($this->request->isPost()) {
  49. $params = $this->request->post("row/a");
  50. if ($params) {
  51. $params = $this->preExcludeFields($params);
  52. $result = false;
  53. Db::startTrans();
  54. try {
  55. //是否采用模型验证
  56. if ($this->modelValidate) {
  57. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  58. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  59. $row->validateFailException(true)->validate($validate);
  60. }
  61. $checkExist = \app\admin\model\box\Category::where('name', $params['name'])->where('id', 'neq', $ids)->value('id');
  62. if ($checkExist) {
  63. $this->error('该分类名称已存在');
  64. }
  65. $result = $row->allowField(true)->save($params);
  66. Db::commit();
  67. } catch (ValidateException $e) {
  68. Db::rollback();
  69. $this->error($e->getMessage());
  70. } catch (PDOException $e) {
  71. Db::rollback();
  72. $this->error($e->getMessage());
  73. } catch (\Exception $e) {
  74. Db::rollback();
  75. $this->error($e->getMessage());
  76. }
  77. if ($result !== false) {
  78. $this->success();
  79. } else {
  80. $this->error(__('No rows were updated'));
  81. }
  82. }
  83. $this->error(__('Parameter %s can not be empty', ''));
  84. }
  85. $this->view->assign("row", $row);
  86. return $this->view->fetch();
  87. }
  88. public function del($ids = "")
  89. {
  90. if (!$this->request->isPost()) {
  91. $this->error(__("Invalid parameters"));
  92. }
  93. $ids = $ids ? $ids : $this->request->post("ids");
  94. if ($ids) {
  95. // 检查分类下是否有盲盒
  96. $boxExist = \app\admin\model\box\Box::whereIn('category_id', $ids)->count();
  97. if ($boxExist) {
  98. $this->error('分类下还有盲盒,目前不能删除');
  99. }
  100. $pk = $this->model->getPk();
  101. $adminIds = $this->getDataLimitAdminIds();
  102. if (is_array($adminIds)) {
  103. $this->model->where($this->dataLimitField, 'in', $adminIds);
  104. }
  105. $list = $this->model->where($pk, 'in', $ids)->select();
  106. $count = 0;
  107. Db::startTrans();
  108. try {
  109. foreach ($list as $k => $v) {
  110. $count += $v->delete();
  111. }
  112. Db::commit();
  113. } catch (PDOException $e) {
  114. Db::rollback();
  115. $this->error($e->getMessage());
  116. } catch (\Exception $e) {
  117. Db::rollback();
  118. $this->error($e->getMessage());
  119. }
  120. if ($count) {
  121. $this->success();
  122. } else {
  123. $this->error(__('No rows were deleted'));
  124. }
  125. }
  126. $this->error(__('Parameter %s can not be empty', 'ids'));
  127. }
  128. public function selectpage()
  129. {
  130. return parent::selectpage();
  131. }
  132. }