From 3febde6ab88fba1d57cbded0d7f024ace33a1a80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=AF=E7=AB=8B?= Date: Wed, 19 May 2021 19:22:50 +0800 Subject: [PATCH] Update UserUpgradeService.php --- app/data/service/UserUpgradeService.php | 44 +++++++++++++------------ 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/app/data/service/UserUpgradeService.php b/app/data/service/UserUpgradeService.php index e80fc2a14..6fa1982db 100644 --- a/app/data/service/UserUpgradeService.php +++ b/app/data/service/UserUpgradeService.php @@ -46,29 +46,31 @@ class UserUpgradeService extends Service if (empty($agent['vip_code'])) return [0, '代理无推荐资格']; if (stripos($agent['path'], "-{$uid}-") !== false) return [0, '不能绑定下属']; // 组装代理数据 - $result = [0, '绑定代理失败']; - $this->app->db->transaction(function () use ($user, $agent, $mod, &$result) { - // 更新用户代理 - $path1 = rtrim($agent['path'] ?: '-', '-') . "-{$agent['id']}-"; - $this->app->db->name('DataUser')->where(['id' => $user['id']])->update([ - '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) { - foreach ($this->app->db->name('DataUser')->whereLike('path', "{$path2}%")->order('layer desc')->select() as $vo) { - $attr = array_reverse(str2arr($path3 = preg_replace("#^{$path2}#", "{$path1}{$user['id']}-", $vo['path']), '-')); - $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, '-'), - ]); + + 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([ + '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) { + foreach ($this->app->db->name('DataUser')->whereLike('path', "{$path2}%")->order('layer desc')->select() as $vo) { + $attr = array_reverse(str2arr($path3 = preg_replace("#^{$path2}#", "{$path1}{$user['id']}-", $vo['path']), '-')); + $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']); - $result = [1, '绑定代理成功']; - }); - return $result; + return [1, '绑定代理成功']; + } catch (\Exception $exception) { + return [0, "绑定代理失败, {$exception->getMessage()}"]; + } } /**