UserAgent.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace app\data\command;
  3. use app\data\service\UserUpgradeService;
  4. use think\admin\Command;
  5. use think\console\Input;
  6. use think\console\input\Argument;
  7. use think\console\Output;
  8. /**
  9. * 更新用户代理关系
  10. * Class UserAgent
  11. * @package app\data\command
  12. */
  13. class UserAgent extends Command
  14. {
  15. protected function configure()
  16. {
  17. $this->setName('xdata:UserAgent');
  18. $this->addArgument('uuid', Argument::OPTIONAL, '目标用户', '');
  19. $this->addArgument('puid', Argument::OPTIONAL, '上级代理', '');
  20. $this->setDescription('重新设置用户上级代理, 参数:UUID PUID');
  21. }
  22. /**
  23. * @param Input $input
  24. * @param Output $output
  25. * @return void
  26. * @throws \think\admin\Exception
  27. * @throws \think\db\exception\DataNotFoundException
  28. * @throws \think\db\exception\DbException
  29. * @throws \think\db\exception\ModelNotFoundException
  30. */
  31. protected function execute(Input $input, Output $output)
  32. {
  33. [$uuid, $puid] = [$input->getArgument('uuid'), $input->getArgument('puid')];
  34. if (empty($uuid)) $this->setQueueError("参数UID无效,请传入正确的参数!");
  35. if (empty($puid)) $this->setQueueError("参数PID无效,请传入正确的参数!");
  36. // 检查当前用户资料
  37. $user = $this->app->db->name('DataUser')->where(['id' => $uuid])->find();
  38. if (empty($user)) $this->setQueueError("读取用户数据失败!");
  39. // 检查上级代理用户
  40. $parant = $this->app->db->name('DataUser')->where(['id' => $puid])->find();
  41. if (empty($parant)) $this->setQueueError('读取代理数据失败!');
  42. // 检查异常关系处理
  43. if (stripos($parant['path'], "-{$user['id']}-")) {
  44. $this->setQueueError('不能把下级设置为代理!');
  45. }
  46. // 更新自己的代理关系
  47. $path1 = rtrim($parant['path'] ?: '-', '-') . "-{$parant['id']}-";
  48. $this->app->db->name('DataUser')->where(['id' => $user['id']])->update([
  49. 'path' => $path1, 'layer' => substr_count($path1, '-'),
  50. 'pid0' => $parant['id'], 'pid1' => $parant['id'], 'pid2' => $parant['pid1'],
  51. ]);
  52. UserUpgradeService::instance()->upgrade($user['id'], true);
  53. $this->setQueueMessage(1, 1, "更新指定用户[{$user['id']}]代理绑定成功!");
  54. // 更新下级的代理关系
  55. $path2 = "{$user['path']}{$user['id']}-";
  56. [$total, $count] = [$this->app->db->name('DataUser')->whereLike('path', "{$path2}%")->count(), 0];
  57. foreach ($this->app->db->name('DataUser')->whereLike('path', "{$path2}%")->order('layer desc')->select() as $vo) {
  58. $this->setQueueMessage($total, ++$count, "开始更新下级用户[{$vo['id']}]代理绑定!");
  59. // 更新下级用户代理数据
  60. $path3 = preg_replace("#^{$path2}#", "{$path1}{$user['id']}-", $vo['path']);
  61. $attrs = array_reverse(str2arr($path3, '-'));
  62. $this->app->db->name('DataUser')->where(['id' => $vo['id']])->update([
  63. 'path' => $path3, 'layer' => substr_count($path3, '-'),
  64. 'pid0' => $attrs[0] ?? 0, 'pid1' => $attrs[0] ?? 0, 'pid2' => $attrs[1] ?? 0,
  65. ]);
  66. $this->setQueueMessage($total, $count, "完成更新下级用户[{$vo['id']}]代理绑定!", 1);
  67. }
  68. }
  69. }