|
@@ -0,0 +1,194 @@
|
|
|
+<?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 object $user 当前用户对象
|
|
|
+ * @param array $params 分享参数
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public static function addShareLog($user, $params)
|
|
|
+ {
|
|
|
+ // 错误的分享参数
|
|
|
+ if (empty($params['spm'])) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析spm参数: shareId.page.query.platform.from
|
|
|
+ $spmParts = explode('.', $params['spm']);
|
|
|
+ if (count($spmParts) < 5) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ list($shareId, $pageType, $query, $platform, $from) = $spmParts;
|
|
|
+
|
|
|
+ // 分享用户为空
|
|
|
+ if ($shareId <= 0) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 不能分享给本人
|
|
|
+ if ($shareId == $user->id) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 新用户不能分享给老用户 按需打开
|
|
|
+ // if($user->id < $shareId) {
|
|
|
+ // return false;
|
|
|
+ // }
|
|
|
+
|
|
|
+ $shareUser = UserModel::where('id', $shareId)->find();
|
|
|
+ // 分享人不存在
|
|
|
+ if (!$shareUser) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 5分钟内相同的分享信息不保存,防止冗余数据
|
|
|
+ $lastShareLog = ShareModel::where([
|
|
|
+ 'user_id' => $user->id
|
|
|
+ ])->where('createtime', '>', time() - 300)->order('id desc')->find();
|
|
|
+
|
|
|
+ if ($lastShareLog && $lastShareLog->spm === $params['spm']) {
|
|
|
+ return $lastShareLog;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据页面类型构造页面路径和描述
|
|
|
+ $pageInfo = self::getPageInfo($pageType, $query);
|
|
|
+
|
|
|
+ $memoText = '通过' . ShareEnum::getFromText($params['from']) . '访问了' . $pageInfo['description'];
|
|
|
+
|
|
|
+ $ext = [
|
|
|
+ 'image' => $pageInfo['image'] ?? "",
|
|
|
+ 'memo' => $memoText
|
|
|
+ ];
|
|
|
+
|
|
|
+ $shareInfo = ShareModel::create([
|
|
|
+ 'user_id' => $user->id,
|
|
|
+ 'share_id' => $shareId,
|
|
|
+ 'spm' => $params['spm'],
|
|
|
+ 'page' => $pageInfo['path'],
|
|
|
+ 'query' => $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 查询参数
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ private static function getPageInfo($pageType, $query)
|
|
|
+ {
|
|
|
+ $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';
|
|
|
+ $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)
|
|
|
+ ];
|
|
|
+ }
|
|
|
+}
|