User.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\common\controller\Backend;
  4. use app\common\model\LedgerWalletModel;
  5. use app\common\model\TeamLevelModel;
  6. use app\common\model\UserModel;
  7. use Exception;
  8. use fast\Action;
  9. use fast\Asset;
  10. use fast\Common;
  11. use fast\MembershipLevel;
  12. use think\Db;
  13. use think\exception\DbException;
  14. /**
  15. * 用户管理
  16. *
  17. * @icon fa fa-user
  18. */
  19. class User extends Backend
  20. {
  21. /**
  22. * User模型对象
  23. * @var \app\admin\model\User
  24. */
  25. protected $model = null;
  26. protected $multiFields = ['is_login','is_withdraw'];
  27. public function _initialize()
  28. {
  29. parent::_initialize();
  30. $this->model = new \app\admin\model\User;
  31. $this->view->assign("statusList", $this->model->getStatusList());
  32. }
  33. /**
  34. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  35. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  36. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  37. */
  38. /**
  39. * 查看
  40. *
  41. * @return string|Json
  42. * @throws \think\Exception
  43. * @throws DbException
  44. */
  45. public function index()
  46. {
  47. //设置过滤方法
  48. $this->request->filter(['strip_tags', 'trim']);
  49. if (false === $this->request->isAjax()) {
  50. return $this->view->fetch();
  51. }
  52. //如果发送的来源是 Selectpage,则转发到 Selectpage
  53. if ($this->request->request('keyField')) {
  54. return $this->selectpage();
  55. }
  56. list(, $sort, $order, $offset, $limit) = $this->buildparams();
  57. //搜索条件
  58. $where = [];
  59. $filter = json_decode(urldecode(input('filter')), TRUE);
  60. list($where, $pid, $algebra) = $this->_where($filter);
  61. $list = $this->model
  62. ->with('ledgerWallet')
  63. ->alias('a');
  64. if ($pid > 0) {//上级大于0的时候,默认搜索伞下
  65. if($algebra > 0){
  66. $list = $list->where('a.id', 'in', function ($query) use($pid, $algebra) {
  67. $query->table('user_path')->where('parent_id', $pid)->where('distance', $algebra)->field('user_id');
  68. });
  69. }else{
  70. $list = $list->where('a.id', 'in', function ($query) use($pid, $algebra) {
  71. $query->table('user_path')->where('parent_id', $pid)->field('user_id');
  72. });
  73. }
  74. }
  75. $list = $list->where($where)->order($sort, $order)->paginate($limit);
  76. $result = ['total' => $list->total(), 'rows' => $list->items()];
  77. return json($result);
  78. }
  79. /**
  80. * 编辑
  81. *
  82. * @param $ids
  83. * @return string
  84. * @throws DbException
  85. * @throws \think\Exception
  86. */
  87. public function edit($ids = null)
  88. {
  89. $ids = intval($ids);
  90. $row = $this->model->get($ids);
  91. if (!$row) {
  92. $this->error(__('No Results were found'));
  93. }
  94. $adminIds = $this->getDataLimitAdminIds();
  95. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  96. $this->error(__('You have no permission'));
  97. }
  98. if (false === $this->request->isPost()) {
  99. $wallet = (new LedgerWalletModel())->get($ids);
  100. $row['power'] = $wallet['power'] ?? "-";
  101. $this->view->assign('row', $row);
  102. return $this->view->fetch();
  103. }
  104. $params = $this->request->post('row/a');
  105. if (empty($params)) {
  106. $this->error(__('Parameter %s can not be empty', ''));
  107. }
  108. $params = $this->preExcludeFields($params);
  109. $newLevelId = $params['team_level_id'];
  110. if($newLevelId > 0 && empty((new TeamLevelModel())->getTeamLevelName($newLevelId))){
  111. $this->error('会员等级错误');
  112. }
  113. //茶宝
  114. $newPower = bcadd($params['new_power'], 0, 6);
  115. // 启动事务
  116. Db::startTrans();
  117. try {
  118. // 更新会员等级
  119. if ($newLevelId != $row['team_level_id']) {
  120. $updated = (new UserModel())->save(['team_level_id' => $newLevelId], ['id' => $row['id']]);
  121. if (empty($updated)) {
  122. throw new Exception('更新会员等级影响行数为0');
  123. }
  124. }
  125. // 更新茶宝
  126. if (bccomp($newPower, 0, 6) !== 0) {
  127. (new LedgerWalletModel)->changeWalletAccount($ids, Asset::TOKEN, $newPower, LedgerWalletModel::System);
  128. }
  129. // 提交事务
  130. Db::commit();
  131. } catch (Exception $e) {
  132. // 回滚事务
  133. Db::rollback();
  134. $this->error('调整失败:' . $e->getMessage());
  135. }
  136. $this->success('调整成功');
  137. }
  138. //搜索条件
  139. private function _where(array $filter): array
  140. {
  141. $map = [];
  142. $pid = 0;
  143. $algebra = 0;
  144. if (isset($filter['id'])) $map['a.id'] = ['=', $filter['id']];
  145. if (isset($filter['address'])) $map['a.address'] = ['like', $filter['address']];
  146. //if (isset($filter['parent_id']) && !isset($filter['algebra'])) $map['a.parent_id'] = ['=', $filter['parent_id']];
  147. if (isset($filter['create_time'])) {
  148. $arr = explode(' - ', $filter['create_time']);
  149. $map['a.create_time']= ['between time',[$arr[0], $arr[1]]];
  150. }
  151. //团队等级
  152. if (isset($filter['team_level_id'])) $map['a.team_level_id'] = ['=', $filter['team_level_id']];
  153. //人数
  154. if (isset($filter['team_num'])) $map['a.team_num'] = ['=', $filter['team_num']];
  155. //直推
  156. if (isset($filter['direct_num'])) $map['a.direct_num'] = ['=', $filter['direct_num']];
  157. //代数
  158. if(isset($filter['parent_id'])) {
  159. if(isset($filter['algebra']) && $filter['algebra'] > 0){
  160. $pid = $filter['parent_id'];
  161. $algebra = $filter['algebra'];
  162. }else{
  163. $pid = $filter['parent_id'];
  164. $algebra = -1;
  165. }
  166. }
  167. return [$map, $pid, $algebra];
  168. }
  169. }