Group.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace app\admin\controller\user;
  3. use think\Db;
  4. use Exception;
  5. use think\exception\DbException;
  6. use think\exception\PDOException;
  7. use app\common\controller\Backend;
  8. use think\exception\ValidateException;
  9. /**
  10. * 会员代理管理
  11. *
  12. * @icon fa fa-users
  13. */
  14. class Group extends Backend
  15. {
  16. /**
  17. * @var \app\admin\model\UserGroup
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = model('Users');
  24. $this->view->assign("statusList", $this->model->getStatusList());
  25. }
  26. /**
  27. * 查看
  28. */
  29. public function index()
  30. {
  31. //设置过滤方法
  32. $this->request->filter(['strip_tags', 'trim']);
  33. if ($this->request->isAjax()) {
  34. //如果发送的来源是Selectpage,则转发到Selectpage
  35. if ($this->request->request('keyField')) {
  36. return $this->selectpage();
  37. }
  38. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  39. $list = $this->model
  40. ->where($where)->where('is_agent', 1)->where('is_delete', 0)
  41. ->order($sort, $order)
  42. ->paginate($limit);
  43. $result = array("total" => $list->total(), "rows" => $list->items());
  44. return json($result);
  45. }
  46. return $this->view->fetch();
  47. }
  48. /**
  49. * 编辑
  50. *
  51. * @param $ids
  52. * @return string
  53. * @throws DbException
  54. * @throws \think\Exception
  55. */
  56. public function edit($ids = null)
  57. {
  58. $row = $this->model->get($ids);
  59. if (!$row) {
  60. $this->error(__('No Results were found'));
  61. }
  62. $adminIds = $this->getDataLimitAdminIds();
  63. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  64. $this->error(__('You have no permission'));
  65. }
  66. if (false === $this->request->isPost()) {
  67. $bank = json_decode($row->agent_bank_info, true);
  68. $row->account_name = $bank['account_name']??'';
  69. $row->bank_name = $bank['bank_name']??'';
  70. $row->bank_card = $bank['bank_card']??'';
  71. $this->view->assign('row', $row);
  72. return $this->view->fetch();
  73. }
  74. $params = $this->request->post('row/a');
  75. if (empty($params)) {
  76. $this->error(__('Parameter %s can not be empty', ''));
  77. }
  78. $params = $this->preExcludeFields($params);
  79. $result = false;
  80. Db::startTrans();
  81. try {
  82. //是否采用模型验证
  83. if ($this->modelValidate) {
  84. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  85. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  86. $row->validateFailException()->validate($validate);
  87. }
  88. $params['agent_bank_info'] = json_encode(array_slice($params, 6, 3));
  89. unset($params['account_name'],$params['bank_name'],$params['bank_card']);
  90. $result = $row->allowField(true)->save($params);
  91. Db::commit();
  92. } catch (ValidateException|PDOException|Exception $e) {
  93. Db::rollback();
  94. $this->error($e->getMessage());
  95. }
  96. if (false === $result) {
  97. $this->error(__('No rows were updated'));
  98. }
  99. $this->success();
  100. }
  101. /**
  102. * 禁用
  103. * @param $ids
  104. * @return string
  105. * @throws DbException
  106. * @throws \think\Exception
  107. */
  108. public function disable($ids = null, $is_lock=0)
  109. {
  110. $row = $this->model->get($ids);
  111. if (!$row) {
  112. $this->error(__('No Results were found'));
  113. }
  114. $result = false;
  115. try {
  116. $result = $row->allowField(true)->save(['is_lock'=>$is_lock]);
  117. } catch (ValidateException|PDOException|Exception $e) {
  118. $this->error($e->getMessage());
  119. }
  120. if (false === $result) {
  121. $this->error(__('No rows were updated'));
  122. }
  123. $this->success();
  124. }
  125. }