Update UserUpgradeService.php

This commit is contained in:
邹景立 2021-05-19 19:22:50 +08:00
parent 4452bfd77a
commit 3febde6ab8

View File

@ -46,29 +46,31 @@ class UserUpgradeService extends Service
if (empty($agent['vip_code'])) return [0, '代理无推荐资格']; if (empty($agent['vip_code'])) return [0, '代理无推荐资格'];
if (stripos($agent['path'], "-{$uid}-") !== false) return [0, '不能绑定下属']; if (stripos($agent['path'], "-{$uid}-") !== false) return [0, '不能绑定下属'];
// 组装代理数据 // 组装代理数据
$result = [0, '绑定代理失败'];
$this->app->db->transaction(function () use ($user, $agent, $mod, &$result) { try {
// 更新用户代理 $this->app->db->transaction(function () use ($user, $agent, $mod) {
$path1 = rtrim($agent['path'] ?: '-', '-') . "-{$agent['id']}-"; // 更新用户代理
$this->app->db->name('DataUser')->where(['id' => $user['id']])->update([ $path1 = rtrim($agent['path'] ?: '-', '-') . "-{$agent['id']}-";
'pid0' => $agent['id'], 'pid1' => $agent['id'], 'pid2' => $agent['pid1'], $this->app->db->name('DataUser')->where(['id' => $user['id']])->update([
'pids' => $mod > 0 ? 1 : 0, 'path' => $path1, 'layer' => substr_count($path1, '-'), 'pid0' => $agent['id'], 'pid1' => $agent['id'], 'pid2' => $agent['pid1'],
]); 'pids' => $mod > 0 ? 1 : 0, 'path' => $path1, 'layer' => substr_count($path1, '-'),
// 更新下级代理 ]);
$path2 = "{$user['path']}{$user['id']}-"; // 更新下级代理
if ($this->app->db->name('DataUser')->whereLike('path', "{$path2}%")->count() > 0) { $path2 = "{$user['path']}{$user['id']}-";
foreach ($this->app->db->name('DataUser')->whereLike('path', "{$path2}%")->order('layer desc')->select() as $vo) { if ($this->app->db->name('DataUser')->whereLike('path', "{$path2}%")->count() > 0) {
$attr = array_reverse(str2arr($path3 = preg_replace("#^{$path2}#", "{$path1}{$user['id']}-", $vo['path']), '-')); foreach ($this->app->db->name('DataUser')->whereLike('path', "{$path2}%")->order('layer desc')->select() as $vo) {
$this->app->db->name('DataUser')->where(['id' => $vo['id']])->update([ $attr = array_reverse(str2arr($path3 = preg_replace("#^{$path2}#", "{$path1}{$user['id']}-", $vo['path']), '-'));
'pid0' => $attr[0] ?? 0, 'pid1' => $attr[0] ?? 0, 'pid2' => $attr[1] ?? 0, 'path' => $path3, 'layer' => substr_count($path3, '-'), $this->app->db->name('DataUser')->where(['id' => $vo['id']])->update([
]); 'pid0' => $attr[0] ?? 0, 'pid1' => $attr[0] ?? 0, 'pid2' => $attr[1] ?? 0, 'path' => $path3, 'layer' => substr_count($path3, '-'),
]);
}
} }
} });
// 更新用户等级
$this->upgrade($user['id']); $this->upgrade($user['id']);
$result = [1, '绑定代理成功']; return [1, '绑定代理成功'];
}); } catch (\Exception $exception) {
return $result; return [0, "绑定代理失败, {$exception->getMessage()}"];
}
} }
/** /**