Admin.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace app\data\controller\user;
  3. use app\data\model\DataUser;
  4. use app\data\service\UserAdminService;
  5. use app\data\service\UserUpgradeService;
  6. use think\admin\Controller;
  7. /**
  8. * 普通用户管理
  9. * Class Admin
  10. * @package app\data\controller\user
  11. */
  12. class Admin extends Controller
  13. {
  14. /**
  15. * 普通用户管理
  16. * @auth true
  17. * @menu true
  18. * @throws \think\db\exception\DataNotFoundException
  19. * @throws \think\db\exception\DbException
  20. * @throws \think\db\exception\ModelNotFoundException
  21. */
  22. public function index()
  23. {
  24. // 用户等级分组
  25. [$ts, $ls] = [[], UserUpgradeService::instance()->levels()];
  26. $ts['ta'] = ['vip' => '', 'name' => '全部用户', 'count' => 0];
  27. foreach ($ls as $k => $v) $ts["t{$k}"] = ['vip' => $k, 'name' => $v['name'], 'count' => 0,];
  28. $ts['to'] = ['vip' => '', 'name' => '其他用户', 'count' => 0];
  29. // 等级分组统计
  30. foreach (DataUser::mk()->field('vip_code vip,count(1) count')->group('vip_code')->cursor() as $v) {
  31. [$name, $count] = ["t{$v['vip']}", $v['count'], $ts['ta']['count'] += $v['count']];
  32. isset($ts[$name]) ? $ts[$name]['count'] += $count : $ts['to']['count'] += $count;
  33. }
  34. if (empty($ts['to']['count'])) unset($ts['to']);
  35. $this->total = $ts;
  36. // 设置页面标题
  37. $this->title = '普通用户管理';
  38. // 创建查询对象
  39. $query = DataUser::mQuery()->order('id desc');
  40. // 数据筛选选项
  41. $this->type = ltrim(input('type', 'ta'), 't');
  42. if (is_numeric($this->type)) $query->where(['vip_code' => $this->type]);
  43. elseif ($this->type === 'o') $query->whereNotIn('vip_code', array_keys($ls));
  44. // 用户搜索查询
  45. $db = DataUser::mQuery()->equal('vip_code#from_vipcode')->like('phone#from_phone,username|nickname#from_username')->db();
  46. if ($db->getOptions('where')) $query->whereRaw("pid1 in {$db->field('id')->buildSql()}");
  47. // 数据查询分页
  48. $query->like('phone,username|nickname#username')->equal('status,vip_code')->dateBetween('create_at')->page();
  49. }
  50. /**
  51. * 数据列表处理
  52. * @param array $data
  53. */
  54. protected function _page_filter(array &$data)
  55. {
  56. $this->upgrades = UserUpgradeService::instance()->levels();
  57. UserAdminService::instance()->buildByUid($data, 'pid1', 'from');
  58. }
  59. /**
  60. * 用户团队关系
  61. * @auth true
  62. * @menu true
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. */
  67. public function teams()
  68. {
  69. $this->title = '用户团队关系';
  70. $map = ['pid1' => input('from', 0)];
  71. DataUser::mQuery()->where($map)->page(false);
  72. }
  73. /**
  74. * 数据列表处理
  75. * @param array $data
  76. */
  77. protected function _teams_page_filter(array &$data)
  78. {
  79. $uids = array_unique(array_column($data, 'id'));
  80. $subCount = DataUser::mk()->whereIn('pid1', $uids)->group('pid1')->column('count(1) count', 'pid1');
  81. foreach ($data as &$vo) $vo['subCount'] = $subCount[$vo['id']] ?? 0;
  82. }
  83. /**
  84. * 永久绑定代理
  85. * @auth true
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\DbException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. */
  90. public function forever()
  91. {
  92. $map = $this->_vali(['id.require' => '用户ID不能为空!']);
  93. $user = DataUser::mk()->where($map)->find();
  94. if (empty($user) || empty($user['pid0'])) $this->error('用户不符合操作要求!');
  95. [$status, $message] = UserUpgradeService::instance()->bindAgent($user['id'], $user['pid0']);
  96. $status && sysoplog('前端用户管理', "修改用户[{$map['id']}]的代理为永久状态");
  97. empty($status) ? $this->error($message) : $this->success($message);
  98. }
  99. /**
  100. * 设为总部用户
  101. * @auth true
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\DbException
  104. * @throws \think\db\exception\ModelNotFoundException
  105. */
  106. public function unbind()
  107. {
  108. $map = $this->_vali(['id.require' => '用户ID不能为空!']);
  109. $user = DataUser::mk()->where($map)->find();
  110. if (empty($user)) $this->error('用户不符合操作要求!');
  111. // 修改指定用户代理数据
  112. $user->save(['pid0' => 0, 'pid1' => 0, 'pid2' => 0, 'pids' => 1, 'path' => '-', 'layer' => 1]);
  113. // 刷新用户等级及上级等级
  114. UserUpgradeService::instance()->upgrade($user['id'], true);
  115. sysoplog('前端用户管理', "设置用户[{$map['id']}]为总部用户");
  116. $this->success('设为总部用户成功!');
  117. }
  118. /**
  119. * 绑定上级代理
  120. * @auth true
  121. * @throws \think\db\exception\DataNotFoundException
  122. * @throws \think\db\exception\DbException
  123. * @throws \think\db\exception\ModelNotFoundException
  124. */
  125. public function parent()
  126. {
  127. if ($this->request->isGet()) {
  128. $this->upgrades = UserUpgradeService::instance()->levels();
  129. $data = $this->_vali(['uuid.require' => '待操作UID不能为空!']);
  130. // 排除下级用户
  131. $path = DataUser::mk()->where(['id' => $data['uuid']])->value('path', '-');
  132. $subids = DataUser::mk()->whereLike('path', "{$path}{$data['uuid']}-%")->column('id');
  133. $query = DataUser::mQuery()->order('id desc')->whereNotIn('id', array_merge($subids, array_values($data)));
  134. // 用户搜索查询
  135. $db = DataUser::mQuery()->equal('vip_code#from_vipcode')->like('phone#from_phone,username|nickname#from_username')->db();
  136. if ($db->getOptions('where')) $query->whereRaw("pid1 in {$db->field('id')->buildSql()}");
  137. // 数据查询分页
  138. $query->like('phone,username|nickname#username')->whereRaw('vip_code>0')->equal('status,vip_code')->dateBetween('create_at')->page();
  139. } else {
  140. $data = $this->_vali(['pid.require' => '待绑定代理不能为空!', 'uuid.require' => '待操作用户不能为空!']);
  141. [$status, $message] = UserUpgradeService::instance()->bindAgent($data['uuid'], $data['pid'], 2);
  142. $status && sysoplog('前端用户管理', "修改用户[{$data['uuid']}]的代理为用户[{$data['pid']}]");
  143. empty($status) ? $this->error($message) : $this->success($message);
  144. }
  145. }
  146. /**
  147. * 重算用户余额返利
  148. * @auth true
  149. */
  150. public function sync()
  151. {
  152. $this->_queue('重新计算用户余额返利', 'xdata:UserAmount');
  153. }
  154. /**
  155. * 修改用户状态
  156. * @auth true
  157. */
  158. public function state()
  159. {
  160. DataUser::mSave($this->_vali([
  161. 'status.in:0,1' => '状态值范围异常!',
  162. 'status.require' => '状态值不能为空!',
  163. ]));
  164. }
  165. }