Group.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace app\admin\controller\shopro\user;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. use Exception;
  8. /**
  9. * 会员组管理
  10. *
  11. * @icon fa fa-users
  12. */
  13. class Group extends Backend
  14. {
  15. /**
  16. * @var \app\admin\model\shopro\user\Group
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\shopro\user\Group;
  23. $this->view->assign("statusList", $this->model->getStatusList());
  24. }
  25. /**
  26. * 查看
  27. */
  28. public function index()
  29. {
  30. //设置过滤方法
  31. $this->request->filter(['strip_tags']);
  32. if ($this->request->isAjax()) {
  33. //如果发送的来源是Selectpage,则转发到Selectpage
  34. if ($this->request->request('keyField')) {
  35. return $this->selectpage();
  36. }
  37. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  38. $total = $this->model
  39. ->where($where)
  40. ->order($sort, $order)
  41. ->count();
  42. $list = $this->model
  43. ->where($where)
  44. ->order($sort, $order)
  45. ->limit($offset, $limit)
  46. ->select();
  47. $list = collection($list)->toArray();
  48. $result = array("total" => $total, "rows" => $list);
  49. $this->success('用户组', null, $result);
  50. }
  51. return $this->view->fetch();
  52. }
  53. /**
  54. * 添加
  55. */
  56. public function add()
  57. {
  58. if ($this->request->isPost()) {
  59. $params = $this->request->post();
  60. if ($params) {
  61. $params = json_decode($params['data'], true);
  62. $result = false;
  63. Db::startTrans();
  64. try {
  65. $result = $this->model->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 inserted'));
  81. }
  82. }
  83. $this->error(__('Parameter %s can not be empty', ''));
  84. }
  85. $nodeList = \app\admin\model\UserRule::getTreeList();
  86. $this->assignconfig("nodeList", $nodeList);
  87. return $this->view->fetch();
  88. }
  89. /**
  90. * 编辑
  91. */
  92. public function edit($ids = null)
  93. {
  94. $row = $this->model->get($ids);
  95. if (!$row) {
  96. $this->error(__('No Results were found'));
  97. }
  98. if ($this->request->isPost()) {
  99. $params = $this->request->post();
  100. if ($params) {
  101. $params = json_decode($params['data'], true);
  102. $result = false;
  103. Db::startTrans();
  104. try {
  105. $row->allowField(true)->save($params);
  106. $result = true;
  107. Db::commit();
  108. } catch (ValidateException $e) {
  109. Db::rollback();
  110. $this->error($e->getMessage());
  111. } catch (PDOException $e) {
  112. Db::rollback();
  113. $this->error($e->getMessage());
  114. } catch (Exception $e) {
  115. Db::rollback();
  116. $this->error($e->getMessage());
  117. }
  118. if ($result !== false) {
  119. $this->success();
  120. } else {
  121. $this->error(__('No rows were updated'));
  122. }
  123. }
  124. $this->error(__('Parameter %s can not be empty', ''));
  125. }
  126. $rules = explode(',', $row['rules']);
  127. $nodeList = \app\admin\model\UserRule::getTreeList($rules);
  128. $this->assignconfig("nodeList", $nodeList);
  129. $this->assignconfig('row', $row);
  130. return $this->view->fetch();
  131. }
  132. }