123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- <?php
- namespace app\common\Service\Share;
- use think\Model;
- use app\common\model\user\Share as ShareModel;
- use app\common\model\User as UserModel;
- use app\common\model\Goods as GoodsModel;
- use app\common\Enum\ShareEnum;
- use app\common\Enum\PageTypeEnum;
- class ShareService
- {
- /**
- * 添加分享记录
- * @param int $userId 当前用户ID
- * @param array $params 分享参数
- * @return mixed
- */
- public static function addShareLog($userId, $params)
- {
- // 错误的分享参数
- if (empty($params['spm'])) {
- return false;
- }
- $shareId = $params['shareId'];
- // 分享用户为空
- if ($shareId <= 0) {
- return false;
- }
- // 不能分享给本人
- if ($shareId == $userId) {
- return false;
- }
- // 新用户不能分享给老用户 按需打开
- // if($user->id < $shareId) {
- // return false;
- // }
- $shareUser = UserModel::where('id', $shareId)->find();
- // 分享人不存在
- if (!$shareUser) {
- return false;
- }
- // 5分钟内相同的分享信息不保存,防止冗余数据
- $lastShareLog = ShareModel::where([
- 'user_id' => $userId
- ])->where('createtime', '>', time() - 300)->order('id desc')->find();
- if ($lastShareLog && $lastShareLog->spm === $params['spm']) {
- return $lastShareLog;
- }
- // 根据页面类型构造页面路径和描述
- $pageInfo = self::getPageInfo($params['page'], $params['query'], $shareUser);
-
- $memoText = '通过' . ShareEnum::getFromText($params['from']) . '访问了' . $pageInfo['description'];
- $ext = [
- 'image' => $pageInfo['image'] ?? "",
- 'memo' => $memoText
- ];
- $shareInfo = ShareModel::create([
- 'user_id' => $userId,
- 'share_id' => $shareId,
- 'spm' => $params['spm'],
- 'page' => $pageInfo['path'],
- 'query' => $params['query'],
- 'platform' => $params['platform'],
- 'from' => $params['from'],
- 'ext' => $ext
- ]);
- $data = ['shareInfo' => $shareInfo];
-
- \think\Hook::listen('user_share_after', $data);
- return $shareInfo;
- }
- /**
- * 根据页面类型获取页面信息
- * @param int $pageType 页面类型
- * @param string $query 查询参数
- * @param object $shareUser 分享者用户对象(可选)
- * @return array
- */
- private static function getPageInfo($pageType, $query, $shareUser = null)
- {
- $pageInfo = [
- 'path' => '',
- 'description' => '',
- 'image' => ''
- ];
- switch ($pageType) {
- case PageTypeEnum::HOME:
- $pageInfo['path'] = '/pages/index/index';
- $pageInfo['description'] = '首页';
- break;
-
- case PageTypeEnum::GOODS:
- $pageInfo['path'] = '/pages/goods/index';
- $pageInfo['description'] = '商品';
- $pageInfo = self::appendGoodsInfo($pageInfo, $query);
- break;
-
- case PageTypeEnum::GROUP_GOODS:
- $pageInfo['path'] = '/pages/goods/groupon';
- $pageInfo['description'] = '拼团商品';
- $pageInfo = self::appendGoodsInfo($pageInfo, $query);
- break;
-
- case PageTypeEnum::SECKILL_GOODS:
- $pageInfo['path'] = '/pages/goods/seckill';
- $pageInfo['description'] = '秒杀商品';
- $pageInfo = self::appendGoodsInfo($pageInfo, $query);
- break;
-
- case PageTypeEnum::INVITE_GROUP:
- $pageInfo['path'] = '/pages/activity/groupon/detail';
- $pageInfo['description'] = '拼团活动';
- break;
-
- case PageTypeEnum::AGENT_POSTER:
- $pageInfo['path'] = '/pages/agent/poster';
- if ($shareUser) {
- $pageInfo['description'] = $shareUser->nickname . '的分销海报页';
- } else {
- $pageInfo['description'] = '分销海报页';
- }
- break;
-
- default:
- $pageInfo['path'] = '/pages/index/index';
- $pageInfo['description'] = '未知页面';
- break;
- }
- return $pageInfo;
- }
- /**
- * 为商品页面添加商品信息
- * @param array $pageInfo 页面信息
- * @param string $goodsId 商品ID
- * @return array
- */
- private static function appendGoodsInfo($pageInfo, $goodsId)
- {
- if (!empty($goodsId)) {
- $goods = GoodsModel::find($goodsId);
- if ($goods) {
- $pageInfo['description'] .= "[{$goods->title}]";
- $pageInfo['image'] = $goods->image;
- }
- }
- return $pageInfo;
- }
- /**
- * 解析spm参数
- * @param string $spm smp参数
- * @return array|false
- */
- public static function parseSpm($spm)
- {
- if (empty($spm)) {
- return false;
- }
- $spmParts = explode('.', $spm);
- if (count($spmParts) < 5) {
- return false;
- }
- list($shareId, $pageType, $query, $platform, $from) = $spmParts;
- return [
- 'shareId' => intval($shareId),
- 'pageType' => intval($pageType),
- 'query' => $query,
- 'platform' => intval($platform),
- 'from' => intval($from)
- ];
- }
- /**
- * 获取用户的分享记录列表
- * @param int $userId 用户ID
- * @param int $pageSize 每页数量
- * @param string $type 记录类型:shared(我分享的) | received(我收到的分享)
- * @return mixed
- */
- public static function getUserShareLogs($userId, $pageSize = 8, $type = 'received')
- {
- $query = ShareModel::with(['user' => function ($query) {
- return $query->field(['id', 'nickname', 'avatar']);
- }]);
- // 根据类型设置查询条件
- if ($type === 'shared') {
- // 查看我分享的记录(我作为分享者)
- $query->where('share_id', $userId);
- } else {
- // 查看我收到的分享记录(我作为被分享者)
- $query->where('user_id', $userId);
- }
- $logs = $query->order('createtime desc')
- ->paginate($pageSize);
- return $logs;
- }
- /**
- * 获取用户分享统计数据
- * @param int $userId 用户ID
- * @return array
- */
- public static function getUserShareStats($userId)
- {
- return [
- 'shared_count' => ShareModel::where('share_id', $userId)->count(), // 我分享的次数
- 'received_count' => ShareModel::where('user_id', $userId)->count(), // 收到的分享次数
- 'today_shared' => ShareModel::where('share_id', $userId)
- ->whereTime('createtime', 'today')
- ->count(), // 今日分享次数
- 'today_received' => ShareModel::where('user_id', $userId)
- ->whereTime('createtime', 'today')
- ->count(), // 今日收到分享次数
- ];
- }
- /**
- * 获取分享记录详情
- * @param int $shareLogId 分享记录ID
- * @param int $userId 当前用户ID(用于权限验证)
- * @return mixed
- */
- public static function getShareLogDetail($shareLogId, $userId = 0)
- {
- $query = ShareModel::with(['user']);
-
- // 如果指定了用户ID,则验证权限(只能查看自己相关的记录)
- if ($userId > 0) {
- $query->where(function($query) use ($userId) {
- $query->whereOr('user_id', $userId)
- ->whereOr('share_id', $userId);
- });
- }
- $shareLog = $query->where('id', $shareLogId)->find();
- if ($shareLog) {
- // 解析SPM参数
- $spmInfo = self::parseSpm($shareLog->spm);
- if ($spmInfo) {
- $shareLog->spm_info = $spmInfo;
- $shareLog->page_type_text = PageTypeEnum::getPageTypeText($spmInfo['pageType']);
- }
- }
- return $shareLog;
- }
- }
|