封装分享功能

This commit is contained in:
2026-01-21 09:40:31 +08:00
parent a125995808
commit c60a2115ea
7 changed files with 1423 additions and 945 deletions

View File

@@ -1,7 +1,15 @@
import {
autoBindInviteUser
autoBindInviteUser,
autoGetInviteCode
} from '@/common/api/market/distribution.js'
import {
APIshopUserInfo,
APIusershopInfodetail
} from '@/common/api/member.js'
import {
productStore
} from '@/stores/user.js';
const accountStore = productStore();
const accountInfo = wx.getAccountInfoSync();
export const envVersion = accountInfo.miniProgram.envVersion;
let type = 3;
@@ -32,7 +40,7 @@ export function bindInvite(args) {
shopUserId,
shopId,
inviteCode
} = args;
} = args;
// 校验邀请码有效性若邀请码为空、null、undefined则直接返回不执行后续绑定逻辑
if (!inviteCode || inviteCode === null || inviteCode === undefined) {
@@ -52,4 +60,238 @@ export function wxShare(par) {
...par,
type
}
}
}
export async function returnQuery(query) {
const shopId = uni.cache.get('shopId')
const shopUserInfo = uni.cache.get('shopUserInfo')
const shopInfo = uni.cache.get('shopInfo')
const inviteCode = await autoGetInviteCode({
shopId: shopId,
shopUserId: shopUserInfo.id
})
const pages = getCurrentPages();
const currentPage = pages[pages.length - 1];
const currentPath = currentPage.route;
const currentOptions = currentPage.options;
let sharePath = `/${currentPath}`;
const queryJson = {
inviteCode: (inviteCode && inviteCode !== true) ? inviteCode : null,
shopId,
shopUserId: shopUserInfo.id,
...query
}
for (const key in currentOptions) {
if (currentOptions.hasOwnProperty(key)) {
queryJson[key] = encodeURIComponent(currentOptions[key])
}
}
queryJson.shopId = shopId
queryJson.inviteCode = (inviteCode && inviteCode !== true) ? inviteCode : null
queryJson.shopUserId = shopUserInfo.id
let result = ''
for (let key in queryJson) {
if (queryJson[key]) {
if (result === '') {
result += `${key}=${queryJson[key]}`
} else {
result += `&${key}=${queryJson[key]}`
}
}
}
return result
}
function parseQueryString(queryString) {
const queryParams = queryString.split("&").map((param) => param.split("="));
const params = {};
for (const [key, value] of queryParams) {
params[key] = value;
}
return params;
}
/**
* 混入的 onLoad 核心逻辑(抽成独立函数)
* @param {Object} opt - 页面 onLoad 接收的参数
* @param {Object} vm - 组件实例this
*/
export async function handleMixinOnLoad(opt, vm) {
console.log('onLoad');
const options = {}
if (opt.q) {
const q = decodeURIComponent(opt.q);
const params = parseQueryString(q.split("?")[1]);
Object.assign(options, params);
} else {
Object.assign(options, opt);
}
console.log('options', options);
if (options.shopId) {
uni.cache.set('shopId', options.shopId)
accountStore.shopId = options.shopId
await accountStore.pageOnload()
}
uni.setStorageSync('loadFinsh', true)
// const shopId = uni.cache.get('shopId')
// const shopUserInfo = uni.cache.get('shopUserInfo')
// const shopInfo = uni.cache.get('shopInfo')
// const inviteCode = await autoGetInviteCode({
// shopId: shopId,
// shopUserId: shopUserInfo.id
// })
if (options.inviteCode) {
bindInvite(options)
}
}
// utils/share.js
export const shareMixin = {
// async onLoad(opt) {
// console.log('onLoad');
// const options = {}
// if (opt.q) {
// const q = decodeURIComponent(opt.q);
// const params = parseQueryString(q.split("?")[1]);
// Object.assign(options, params);
// } else {
// Object.assign(options, opt);
// }
// console.log('options', options);
// if (options.shopId) {
// uni.cache.set('shopId', options.shopId)
// }
// const shopId = uni.cache.get('shopId')
// const shopUserInfo = uni.cache.get('shopUserInfo')
// const shopInfo = uni.cache.get('shopInfo')
// const inviteCode = await autoGetInviteCode({
// shopId: shopId,
// shopUserId: shopUserInfo.id
// })
// uni.setStorageSync('loadFinsh',true)
// if (options.inviteCode) {
// bindInvite(options)
// }
// },
async onShareAppMessage(res) {
// const shopId = uni.cache.get('shopId')
// const shopUserInfo = uni.cache.get('shopUserInfo')
const shopInfo = uni.cache.get('shopInfo')
// const inviteCode = await autoGetInviteCode({
// shopId: shopId,
// shopUserId: shopUserInfo.id
// })
const pages = getCurrentPages();
const currentPage = pages[pages.length - 1];
const currentPath = currentPage.route;
const currentOptions = currentPage.options;
let sharePath = `/${currentPath}`;
// const queryJson = {
// inviteCode: (inviteCode && inviteCode !== true) ? inviteCode : null,
// shopId,
// shopUserId: shopUserInfo.id,
// }
// for (const key in currentOptions) {
// if (currentOptions.hasOwnProperty(key)) {
// queryJson[key] = encodeURIComponent(currentOptions[key])
// }
// }
// queryJson.shopId = shopId
// queryJson.inviteCode = (inviteCode && inviteCode !== true) ? inviteCode : null
// queryJson.shopUserId = shopUserInfo.id
const query = returnQuery()
// 全局默认配置(可被页面覆盖)
const defaultShareConfig = {
title: shopInfo.shopName,
path: sharePath + '?' + query,
imageUrl: shopInfo.logo,
query,
// desc: '描述',
success: (res) => {
console.log('分享好友成功', res);
// 可加埋点等统一逻辑
},
fail: (err) => {
console.error('分享好友失败', err);
}
};
// 页面自定义配置覆盖全局
const pageShareConfig = this.$options.shareConfig || {};
console.log('mixIn onShareAppMessage', {
...defaultShareConfig,
...pageShareConfig
})
return {
...defaultShareConfig,
...pageShareConfig
};
},
async onShareTimeline() {
// const shopId = uni.cache.get('shopId')
// const shopUserInfo = uni.cache.get('shopUserInfo')
const shopInfo = uni.cache.get('shopInfo')
// const inviteCode = await autoGetInviteCode({
// shopId: shopId,
// shopUserId: shopUserInfo.id
// })
const pages = getCurrentPages();
const currentPage = pages[pages.length - 1];
const currentPath = currentPage.route;
const currentOptions = currentPage.options;
let sharePath = `/${currentPath}`;
// const queryJson = {
// inviteCode: (inviteCode && inviteCode !== true) ? inviteCode : null,
// shopId,
// shopUserId: shopUserInfo.id,
// }
// for (const key in currentOptions) {
// if (currentOptions.hasOwnProperty(key)) {
// queryJson[key] = encodeURIComponent(currentOptions[key])
// }
// }
// queryJson.shopId = shopId
// queryJson.inviteCode = (inviteCode && inviteCode !== true) ? inviteCode : null
// queryJson.shopUserId = shopUserInfo.id
const query = returnQuery()
const defaultTimelineConfig = {
title: shopInfo.shopName,
path: sharePath + '?' + query,
imageUrl: shopInfo.logo,
query,
};
const pageTimelineConfig = this.$options.shareTimelineConfig || {};
return {
...defaultTimelineConfig,
...pageTimelineConfig
};
}
};