Admin.php 6.9 KB

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