Rule.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\common\controller\Backend;
  4. use fast\Tree;
  5. /**
  6. * 会员规则管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Rule extends Backend
  11. {
  12. /**
  13. * @var \app\admin\model\UserRule
  14. */
  15. protected $model = null;
  16. protected $rulelist = [];
  17. protected $multiFields = 'ismenu,status';
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = model('UserRule');
  22. $this->view->assign("statusList", $this->model->getStatusList());
  23. // 必须将结果集转换为数组
  24. $ruleList = collection($this->model->order('weigh', 'desc')->select())->toArray();
  25. foreach ($ruleList as $k => &$v) {
  26. $v['title'] = __($v['title']);
  27. $v['remark'] = __($v['remark']);
  28. }
  29. unset($v);
  30. Tree::instance()->init($ruleList);
  31. $this->rulelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
  32. $ruledata = [0 => __('None')];
  33. foreach ($this->rulelist as $k => &$v) {
  34. if (!$v['ismenu']) {
  35. continue;
  36. }
  37. $ruledata[$v['id']] = $v['title'];
  38. }
  39. $this->view->assign('ruledata', $ruledata);
  40. }
  41. /**
  42. * 查看
  43. */
  44. public function index()
  45. {
  46. if ($this->request->isAjax()) {
  47. $list = $this->rulelist;
  48. $total = count($this->rulelist);
  49. $result = array("total" => $total, "rows" => $list);
  50. return json($result);
  51. }
  52. return $this->view->fetch();
  53. }
  54. /**
  55. * 添加
  56. */
  57. public function add()
  58. {
  59. if ($this->request->isPost()) {
  60. $this->token();
  61. }
  62. return parent::add();
  63. }
  64. /**
  65. * 编辑
  66. */
  67. public function edit($ids = null)
  68. {
  69. if ($this->request->isPost()) {
  70. $this->token();
  71. }
  72. return parent::edit($ids);
  73. }
  74. /**
  75. * 删除
  76. */
  77. public function del($ids = "")
  78. {
  79. if (!$this->request->isPost()) {
  80. $this->error(__("Invalid parameters"));
  81. }
  82. $ids = $ids ? $ids : $this->request->post("ids");
  83. if ($ids) {
  84. $delIds = [];
  85. foreach (explode(',', $ids) as $k => $v) {
  86. $delIds = array_merge($delIds, Tree::instance()->getChildrenIds($v, true));
  87. }
  88. $delIds = array_unique($delIds);
  89. $count = $this->model->where('id', 'in', $delIds)->delete();
  90. if ($count) {
  91. $this->success();
  92. }
  93. }
  94. $this->error();
  95. }
  96. }