Commit 1ff825f6 authored by 刘小敏's avatar 刘小敏

feat(share): 添加分享记录流程日志

parent efc59e39
......@@ -13,15 +13,30 @@ class Share extends Common
public function add()
{
custom_log('[share.add] 收到分享记录请求, method=' . $this->request->method(), 0, 'share');
if (!$this->request->isPost()) {
custom_log('[share.add] 请求方法错误,非POST请求', 0, 'share', 'error');
$this->error('');
}
custom_log('[share.add] 请求方法校验通过 (POST)', 0, 'share');
$params = $this->request->only(['shareId', 'spm', 'page', 'query', 'from', 'platform']);
custom_log('[share.add] 解析请求参数: ' . json_encode($params, JSON_UNESCAPED_UNICODE), 0, 'share');
$user = auth_user();
$userId = $user->id ?? 0;
custom_log('[share.add] 用户认证通过, user_id=' . $userId, $userId, 'share');
$shareInfo = ShareModel::log($user, $params);
if ($shareInfo) {
$logData = method_exists($shareInfo, 'toArray') ? $shareInfo->toArray() : json_decode(json_encode($shareInfo), true);
custom_log('[share.add] 分享记录写入成功, 记录详情: ' . json_encode($logData, JSON_UNESCAPED_UNICODE), $userId, 'share');
} else {
custom_log('[share.add] 分享记录写入失败(返回false/空,可能原因: spm为空/shareId无效/自己点自己/5分钟内重复/sharing不存在)', $userId, 'share', 'warning');
}
custom_log('[share.add] 返回成功响应', $userId, 'share');
$this->success("");
}
......
......@@ -41,22 +41,30 @@ class Share extends Common
public static function log(Object $user, $params)
{
$userId = $user->id ?? 0;
custom_log('[share.log] 进入方法, user_id=' . $userId . ', params=' . json_encode($params, JSON_UNESCAPED_UNICODE), $userId, 'share');
// 错误的分享参数
if (empty($params['spm'])) {
custom_log('[share.log] spm为空,拒绝写入', $userId, 'share', 'warning');
return false;
}
custom_log('[share.log] spm校验通过: ' . $params['spm'], $userId, 'share');
$shareId = $params['shareId'];
// 分享用户为空
if ($shareId <= 0) {
custom_log('[share.log] shareId无效(<=0): ' . $shareId, $userId, 'share', 'warning');
return false;
}
custom_log('[share.log] shareId校验通过: ' . $shareId, $userId, 'share');
// 不能分享给本人
if ($shareId == $user->id) {
custom_log('[share.log] 自己点自己链接,拒绝写入, shareId=' . $shareId . ' == user_id=' . $user->id, $userId, 'share', 'warning');
return false;
}
custom_log('[share.log] 非本人分享校验通过', $userId, 'share');
// 新用户不能分享给老用户 按需打开
// if($user->id < $shareId) {
......@@ -66,8 +74,10 @@ class Share extends Common
$shareUser = UserModel::where('id', $shareId)->find();
// 分享人不存在
if (!$shareUser) {
custom_log('[share.log] 分享人不存在, shareId=' . $shareId, $userId, 'share', 'warning');
return false;
}
custom_log('[share.log] 分享人存在, shareId=' . $shareId, $userId, 'share');
// 5分钟内相同的分享信息不保存,防止冗余数据
$lastShareLog = self::where([
......@@ -75,8 +85,10 @@ class Share extends Common
])->where('createtime', '>', time() - 300)->order('id desc')->find();
if ($lastShareLog && $lastShareLog->spm === $params['spm']) {
custom_log('[share.log] 5分钟内重复分享记录,返回已有记录, last_id=' . $lastShareLog->id . ', spm=' . $params['spm'], $userId, 'share', 'warning');
return $lastShareLog;
}
custom_log('[share.log] 重复检查通过', $userId, 'share');
$memoText = '通过' . (self::FROM)[$params['from']] . '访问了';
if ($params['page'] == '/pages/index/index') {
......@@ -110,6 +122,8 @@ class Share extends Common
'memo' => $memoText
];
custom_log('[share.log] 准备写入shopro_share, user_id=' . $user->id . ', share_id=' . $shareId . ', page=' . $params['page'], $userId, 'share');
$shareInfo = self::create([
'user_id' => $user->id,
'share_id' => $shareId,
......@@ -121,9 +135,13 @@ class Share extends Common
'ext' => $ext
]);
custom_log('[share.log] 写入shopro_share成功, record_id=' . ($shareInfo->id ?? '') . ', memo=' . $memoText, $userId, 'share');
$data = ['shareInfo' => $shareInfo];
custom_log('[share.log] 触发 user_share_after Hook', $userId, 'share');
\think\Hook::listen('user_share_after', $data);
custom_log('[share.log] 方法执行完毕,返回分享记录, record_id=' . ($shareInfo->id ?? ''), $userId, 'share');
return $shareInfo;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment