Admin.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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|username|nickname#from_keys')->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. dump($data);die;
  58. $this->upgrades = BaseUserUpgrade::items();
  59. UserAdminService::buildByUid($data, 'pid1', 'from');
  60. }
  61. /**
  62. * 用户团队关系
  63. * @auth true
  64. * @menu true
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\DbException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. */
  69. public function teams()
  70. {
  71. $this->title = '用户团队关系';
  72. $map = ['pid1' => input('from', 0)];
  73. DataUser::mQuery()->where($map)->page(false);
  74. }
  75. /**
  76. * 数据列表处理
  77. * @param array $data
  78. */
  79. protected function _teams_page_filter(array &$data)
  80. {
  81. $uids = array_unique(array_column($data, 'id'));
  82. $subCount = DataUser::mk()->whereIn('pid1', $uids)->group('pid1')->column('count(1) count', 'pid1');
  83. foreach ($data as &$vo) $vo['subCount'] = $subCount[$vo['id']] ?? 0;
  84. }
  85. /**
  86. * 永久绑定代理
  87. * @auth true
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\DbException
  90. * @throws \think\db\exception\ModelNotFoundException
  91. */
  92. public function forever()
  93. {
  94. $map = $this->_vali(['id.require' => '用户ID不能为空!']);
  95. $user = DataUser::mk()->where($map)->find();
  96. if (empty($user) || empty($user['pid0'])) $this->error('用户不符合操作要求!');
  97. [$status, $message] = UserUpgradeService::bindAgent($user['id'], $user['pid0']);
  98. $status && sysoplog('前端用户管理', "修改用户[{$map['id']}]的代理为永久状态");
  99. empty($status) ? $this->error($message) : $this->success($message);
  100. }
  101. /**
  102. * 设为总部用户
  103. * @auth true
  104. * @throws \think\db\exception\DataNotFoundException
  105. * @throws \think\db\exception\DbException
  106. * @throws \think\db\exception\ModelNotFoundException
  107. */
  108. public function unbind()
  109. {
  110. $map = $this->_vali(['id.require' => '用户ID不能为空!']);
  111. $user = DataUser::mk()->where($map)->findOrEmpty();
  112. if ($user->isEmpty()) $this->error('用户不符合操作要求!');
  113. // 修改指定用户代理数据
  114. $user->save(['pid0' => 0, 'pid1' => 0, 'pid2' => 0, 'pids' => 1, 'path' => '-', 'layer' => 1]);
  115. // 刷新用户等级及上级等级
  116. UserUpgradeService::upgrade($user['id'], true);
  117. sysoplog('前端用户管理', "设置用户[{$map['id']}]为总部用户");
  118. $this->success('设为总部用户成功!');
  119. }
  120. /**
  121. * 绑定上级代理
  122. * @auth true
  123. * @throws \think\db\exception\DataNotFoundException
  124. * @throws \think\db\exception\DbException
  125. * @throws \think\db\exception\ModelNotFoundException
  126. */
  127. public function parent()
  128. {
  129. if ($this->request->isGet()) {
  130. $this->upgrades = BaseUserUpgrade::items();
  131. $data = $this->_vali(['uuid.require' => '待操作UID不能为空!']);
  132. // 排除下级用户
  133. $path = DataUser::mk()->where(['id' => $data['uuid']])->value('path', '-');
  134. $subids = DataUser::mk()->whereLike('path', "{$path}{$data['uuid']}-%")->column('id');
  135. $query = DataUser::mQuery()->order('id desc')->whereNotIn('id', array_merge($subids, array_values($data)));
  136. // 用户搜索查询
  137. $db = DataUser::mQuery()->equal('vip_code#from_vipcode')->like('phone#from_phone,username|nickname#from_username')->db();
  138. if ($db->getOptions('where')) $query->whereRaw("pid1 in {$db->field('id')->buildSql()}");
  139. // 数据查询分页
  140. $query->like('phone,username|nickname#username')->whereRaw('vip_code>0')->equal('status,vip_code')->dateBetween('create_at')->page();
  141. } else {
  142. $data = $this->_vali(['pid.require' => '待绑定代理不能为空!', 'uuid.require' => '待操作用户不能为空!']);
  143. [$status, $message] = UserUpgradeService::bindAgent($data['uuid'], $data['pid'], 2);
  144. $status && sysoplog('前端用户管理', "修改用户[{$data['uuid']}]的代理为用户[{$data['pid']}]");
  145. empty($status) ? $this->error($message) : $this->success($message);
  146. }
  147. }
  148. /**
  149. * 重算用户余额返利
  150. * @auth true
  151. */
  152. public function sync()
  153. {
  154. $this->_queue('重新计算用户余额返利', 'xdata:UserAmount');
  155. }
  156. /**
  157. * 修改用户状态
  158. * @auth true
  159. */
  160. public function state()
  161. {
  162. DataUser::mSave($this->_vali([
  163. 'status.in:0,1' => '状态值范围异常!',
  164. 'status.require' => '状态值不能为空!',
  165. ]));
  166. }
  167. }