Commit 3ff6ee1d authored by 刘小敏's avatar 刘小敏

fix(share): 阻止自己点击自己链接并优化日志

parent e41ccba9
...@@ -19,18 +19,25 @@ class Share extends Common ...@@ -19,18 +19,25 @@ class Share extends Common
} }
$params = $this->request->only(['shareId', 'spm', 'page', 'query', 'from', 'platform']); $params = $this->request->only(['shareId', 'spm', 'page', 'query', 'from', 'platform']);
custom_log('[share.add] 解析请求参数: ' . json_encode($params, JSON_UNESCAPED_UNICODE), 0, 'share'); // custom_log('[share.add] 解析请求参数: ' . json_encode($params, JSON_UNESCAPED_UNICODE), 0, 'share');
$user = auth_user(); $user = auth_user();
$userId = $user->id ?? 0; $userId = $user->id ?? 0;
custom_log('[share.add] 用户认证通过, user_id=' . $userId, $userId, 'share'); // custom_log('[share.add] 用户认证通过, user_id=' . $userId, $userId, 'share');
$shareId = $params['shareId'];
if ($shareId == $userId) {
custom_log('[share.add] 自己点自己链接,拒绝写入, shareId=' . $shareId . ' == user_id=' .$userId, $userId, 'share', 'warning');
return false;
}
$shareInfo = ShareModel::log($user, $params); $shareInfo = ShareModel::log($user, $params);
if ($shareInfo) { if ($shareInfo) {
$logData = method_exists($shareInfo, 'toArray') ? $shareInfo->toArray() : json_decode(json_encode($shareInfo), true); $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'); custom_log('[share.add] 分享记录写入成功, 解析请求参数:'.json_encode($params, JSON_UNESCAPED_UNICODE).' 记录详情: ' . json_encode($logData, JSON_UNESCAPED_UNICODE), $userId, 'share');
} else { } else {
custom_log('[share.add] 分享记录写入失败(返回false/空,可能原因: spm为空/shareId无效/自己点自己/5分钟内重复/sharing不存在)', $userId, 'share', 'warning'); custom_log('[share.add] 分享记录写入失败(返回false/空,可能原因: spm为空/shareId无效/自己点自己/5分钟内重复/sharing不存在) 解析请求参数:'.json_encode($params, JSON_UNESCAPED_UNICODE), $userId, 'share', 'warning');
} }
$this->success(""); $this->success("");
......
...@@ -42,29 +42,29 @@ class Share extends Common ...@@ -42,29 +42,29 @@ class Share extends Common
public static function log(Object $user, $params) public static function log(Object $user, $params)
{ {
$userId = $user->id ?? 0; $userId = $user->id ?? 0;
custom_log('[share.log] 进入方法, user_id=' . $userId . ', params=' . json_encode($params, JSON_UNESCAPED_UNICODE), $userId, 'share'); custom_log('[share.log] 进入写日志方法, user_id=' . $userId . ', params=' . json_encode($params, JSON_UNESCAPED_UNICODE), $userId, 'share');
// 错误的分享参数 // 错误的分享参数
if (empty($params['spm'])) { if (empty($params['spm'])) {
custom_log('[share.log] spm为空,拒绝写入', $userId, 'share', 'warning'); // custom_log('[share.log] spm为空,拒绝写入', $userId, 'share', 'warning');
return false; return false;
} }
custom_log('[share.log] spm校验通过: ' . $params['spm'], $userId, 'share'); // custom_log('[share.log] spm校验通过: ' . $params['spm'], $userId, 'share');
$shareId = $params['shareId']; $shareId = $params['shareId'];
// 分享用户为空 // 分享用户为空
if ($shareId <= 0) { if ($shareId <= 0) {
custom_log('[share.log] shareId无效(<=0): ' . $shareId, $userId, 'share', 'warning'); // custom_log('[share.log] shareId无效(<=0): ' . $shareId, $userId, 'share', 'warning');
return false; return false;
} }
custom_log('[share.log] shareId校验通过: ' . $shareId, $userId, 'share'); // custom_log('[share.log] shareId校验通过: ' . $shareId, $userId, 'share');
// 不能分享给本人 // 不能分享给本人
if ($shareId == $user->id) { if ($shareId == $user->id) {
custom_log('[share.log] 自己点自己链接,拒绝写入, shareId=' . $shareId . ' == user_id=' . $user->id, $userId, 'share', 'warning'); // custom_log('[share.log] 自己点自己链接,拒绝写入, shareId=' . $shareId . ' == user_id=' . $user->id, $userId, 'share', 'warning');
return false; return false;
} }
custom_log('[share.log] 非本人分享校验通过', $userId, 'share'); // custom_log('[share.log] 非本人分享校验通过', $userId, 'share');
// 新用户不能分享给老用户 按需打开 // 新用户不能分享给老用户 按需打开
// if($user->id < $shareId) { // if($user->id < $shareId) {
...@@ -74,10 +74,10 @@ class Share extends Common ...@@ -74,10 +74,10 @@ class Share extends Common
$shareUser = UserModel::where('id', $shareId)->find(); $shareUser = UserModel::where('id', $shareId)->find();
// 分享人不存在 // 分享人不存在
if (!$shareUser) { if (!$shareUser) {
custom_log('[share.log] 分享人不存在, shareId=' . $shareId, $userId, 'share', 'warning'); // custom_log('[share.log] 分享人不存在, shareId=' . $shareId, $userId, 'share', 'warning');
return false; return false;
} }
custom_log('[share.log] 分享人存在, shareId=' . $shareId, $userId, 'share'); // custom_log('[share.log] 分享人存在, shareId=' . $shareId, $userId, 'share');
// 5分钟内相同的分享信息不保存,防止冗余数据 // 5分钟内相同的分享信息不保存,防止冗余数据
$lastShareLog = self::where([ $lastShareLog = self::where([
...@@ -88,7 +88,7 @@ class Share extends Common ...@@ -88,7 +88,7 @@ class Share extends Common
custom_log('[share.log] 5分钟内重复分享记录,返回已有记录, last_id=' . $lastShareLog->id . ', spm=' . $params['spm'], $userId, 'share', 'warning'); custom_log('[share.log] 5分钟内重复分享记录,返回已有记录, last_id=' . $lastShareLog->id . ', spm=' . $params['spm'], $userId, 'share', 'warning');
return $lastShareLog; return $lastShareLog;
} }
custom_log('[share.log] 重复检查通过', $userId, 'share'); // custom_log('[share.log] 重复检查通过', $userId, 'share');
$memoText = '通过' . (self::FROM)[$params['from']] . '访问了'; $memoText = '通过' . (self::FROM)[$params['from']] . '访问了';
if ($params['page'] == '/pages/index/index') { if ($params['page'] == '/pages/index/index') {
...@@ -135,10 +135,10 @@ class Share extends Common ...@@ -135,10 +135,10 @@ class Share extends Common
'ext' => $ext 'ext' => $ext
]); ]);
custom_log('[share.log] 写入shopro_share成功, record_id=' . ($shareInfo->id ?? '') . ', memo=' . $memoText, $userId, 'share'); // custom_log('[share.log] 写入shopro_share成功, record_id=' . ($shareInfo->id ?? '') . ', memo=' . $memoText, $userId, 'share');
$data = ['shareInfo' => $shareInfo]; $data = ['shareInfo' => $shareInfo];
custom_log('[share.log] 触发 user_share_after Hook', $userId, 'share'); // custom_log('[share.log] 触发 user_share_after Hook', $userId, 'share');
\think\Hook::listen('user_share_after', $data); \think\Hook::listen('user_share_after', $data);
custom_log('[share.log] 方法执行完毕,返回分享记录, record_id=' . ($shareInfo->id ?? ''), $userId, 'share'); custom_log('[share.log] 方法执行完毕,返回分享记录, record_id=' . ($shareInfo->id ?? ''), $userId, 'share');
......
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