Admin.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace app\data\controller\user;
  3. use app\data\service\UserAdminService;
  4. use app\data\service\UserUpgradeService;
  5. use think\admin\Controller;
  6. /**
  7. * 普通用户管理
  8. * Class Admin
  9. * @package app\data\controller\user
  10. */
  11. class Admin extends Controller
  12. {
  13. /**
  14. * 绑定数据表
  15. * @var string
  16. */
  17. private $table = 'DataUser';
  18. /**
  19. * 普通用户管理
  20. * @auth true
  21. * @menu true
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\DbException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. */
  26. public function index()
  27. {
  28. // 用户等级分组
  29. $levels = UserUpgradeService::instance()->levels();
  30. $totals = ['ta' => ['name' => '全部用户', 'count' => 0, 'vips' => '']];
  31. foreach ($levels as $k => $v) $totals["t{$k}"] = ['name' => $v['name'], 'count' => 0, 'vips' => $k];
  32. $totals['to'] = ['name' => '其他用户', 'count' => 0, 'vips' => ''];
  33. foreach ($this->app->db->name($this->table)->field('vip_code vip,count(1) count')->group('vip_code')->cursor() as $v) {
  34. [$name, $count, $totals['ta']['count']] = ["t{$v['vip']}", $v['count'], $v['count']];
  35. isset($totals[$name]) ? $totals[$name]['count'] += $count : $totals['to']['count'] += $count;
  36. }
  37. if (empty($totals['to']['count'])) unset($totals['to']);
  38. $this->total = $totals;
  39. // 设置页面标题
  40. $this->title = '普通用户管理';
  41. // 创建查询对象
  42. $query = $this->_query($this->table)->order('id desc');
  43. // 数据筛选选项
  44. $this->type = ltrim(input('type', 'ta'), 't');
  45. if (is_numeric($this->type)) $query->where(['vip_code' => $this->type]);
  46. elseif ($this->type === 'o') $query->whereNotIn('vip_code', array_keys($levels));
  47. // 用户搜索查询
  48. $db = $this->_query($this->table)->equal('vip_code#from_vipcode')->like('phone#from_phone,username|nickname#from_username')->db();
  49. if ($db->getOptions('where')) $query->whereRaw("pid1 in {$db->field('id')->buildSql()}");
  50. // 数据查询分页
  51. $query->like('phone,username|nickname#username')->equal('status,vip_code')->dateBetween('create_at')->page();
  52. }
  53. /**
  54. * 数据列表处理
  55. * @param array $data
  56. */
  57. protected function _page_filter(array &$data)
  58. {
  59. $this->upgrades = UserUpgradeService::instance()->levels();
  60. UserAdminService::instance()->buildByUid($data, 'pid1', 'from');
  61. }
  62. /**
  63. * 修改用户上传
  64. * @auth true
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\DbException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. */
  69. public function parent()
  70. {
  71. $data = $this->_vali(['pid.default' => '', 'uid.require' => '待操作UID不能为空']);
  72. if ($data['uid'] === $data['pid']) $this->error('代理不能是自己');
  73. if (empty($data['pid'])) {
  74. $map = [['id', '<>', $data['uid']], ['deleted', '=', 0]];
  75. $query = $this->_query($this->table)->where($map)->equal('status,vip_code');
  76. $query->like('phone,username|nickname#username')->dateBetween('create_at')->order('id desc')->page();
  77. } else try {
  78. $user = $this->app->db->name('DataUser')->where(['id' => $data['uid']])->find();
  79. $parent = $this->app->db->name('DataUser')->where(['id' => $data['pid']])->find();
  80. if (empty($user)) $this->error('读取用户数据失败!');
  81. if (empty($parent)) $this->error('读取代理数据失败!');
  82. $this->app->db->transaction(function () use ($data, $user, $parent) {
  83. if (empty($parent['vip_code'])) $this->error('代理无推荐资格');
  84. if (is_numeric(strpos($parent['path'], "-{$data['uid']}-"))) $this->error('代理不能绑下属');
  85. // 组装当前用户上级数据
  86. $path = rtrim($parent['path'] ?: '-', '-') . "-{$parent['id']}-";
  87. // $this->app->db->name('DataUser')->where(['id' => $data['uid']])->update([
  88. // 'pid0' => $parent['id'], 'pid1' => $parent['id'], 'pid2' => $parent['pid1'],
  89. // 'path' => $path, 'layer' => substr_count($path, '-'),
  90. // ]);
  91. // 替换原来用户的下级用户
  92. $newPath = rtrim($path, '-') . "-{$user['id']}-";
  93. $oldPath = rtrim($user['path'], '-') . "-{$user['id']}-";
  94. foreach ($this->app->db->name('DataUser')->whereLike('path', "{$oldPath}%")->cursor() as $vo) {
  95. dump($vo);
  96. }
  97. // $this->app->db->name('DataUser')->whereLike('path', "{$oldPath}%")->update([
  98. // 'path' => $this->app->db->raw("replace(path,'{$oldPath}','{$newPath}')"),
  99. // ]);
  100. // foreach (array_reverse(array_unique(array_merge(str2arr($newPath), str2arr($oldPath)))) as $uid) {
  101. // UserUpgradeService::instance()->upgrade($uid);
  102. // }
  103. });
  104. exit;
  105. $this->success('修改代理成功!');
  106. } catch (\think\exception\HttpResponseException $exception) {
  107. throw $exception;
  108. } catch (\Exception $exception) {
  109. $this->error($exception->getMessage());
  110. }
  111. }
  112. /**
  113. * 重算用户余额返利
  114. * @auth true
  115. */
  116. public function sync()
  117. {
  118. $this->_queue('重新计算用户余额返利', 'xdata:UserAmount');
  119. }
  120. /**
  121. * 修改用户状态
  122. * @auth true
  123. * @throws \think\db\exception\DbException
  124. */
  125. public function state()
  126. {
  127. $this->_save($this->table, $this->_vali([
  128. 'status.in:0,1' => '状态值范围异常!',
  129. 'status.require' => '状态值不能为空!',
  130. ]));
  131. }
  132. }