/** * 优匠优服师傅端 · 小程序分享统一引擎 */ import { getCurrentInstance } from 'vue'; import { onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'; import { getUser } from './request.js'; export const DEFAULT_SHARE_TITLE = '优匠优服师傅端 · 实名认证接同城好单'; export const DEFAULT_SHARE_PATH = '/pages/master/hall'; export const DEFAULT_SHARE_IMAGE = '/static/brand-logo.png'; const PUBLIC_SHARE_ROUTES = [ 'pages/master/hall', 'pages/master/detail', 'pages/master/apply' ]; const ROUTE_SHARE_TITLES = { 'pages/master/hall': '优匠优服师傅端 · 海量同城安装维修工单实时派发', 'pages/master/detail': '【同城工单派发】优匠优服新工单,认证师傅快来接单', 'pages/master/apply': '优匠优服师傅招募 · 实名技能认证,海量同城好单随时接' }; export function stringifyShareQuery(params = {}) { if (!params || typeof params !== 'object') return ''; const parts = []; Object.keys(params).forEach((key) => { const val = params[key]; if (val !== undefined && val !== null && val !== '') { parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(val))}`); } }); return parts.join('&'); } export function parseSharePath(fullPath = '') { const clean = String(fullPath || '').trim() || DEFAULT_SHARE_PATH; const normalized = clean.startsWith('/') ? clean : '/' + clean; const qIndex = normalized.indexOf('?'); if (qIndex === -1) { return { route: normalized.replace(/^\/+/, ''), path: normalized, queryStr: '' }; } const pathOnly = normalized.slice(0, qIndex); const queryStr = normalized.slice(qIndex + 1); return { route: pathOnly.replace(/^\/+/, ''), path: pathOnly, queryStr }; } function getCurrentPageMeta() { try { const pages = typeof getCurrentPages === 'function' ? getCurrentPages() : []; if (pages && pages.length) { const cur = pages[pages.length - 1]; const route = cur.route || (cur.$page && cur.$page.route) || 'pages/master/hall'; const options = cur.options || (cur.$page && cur.$page.options) || {}; return { route: route.replace(/^\/+/, ''), options, pageInstance: cur }; } } catch (e) {} return { route: 'pages/master/hall', options: {}, pageInstance: null }; } function appendSharerTrack(queryStr = '') { try { const master = getUser(); if (master && master.id) { if (!/(^|&)share_mid=/.test(queryStr)) { return queryStr ? `${queryStr}&share_mid=${master.id}` : `share_mid=${master.id}`; } } } catch (e) {} return queryStr; } export function buildDefaultShareData(vm) { const { route, options } = getCurrentPageMeta(); const isPublic = PUBLIC_SHARE_ROUTES.includes(route); let title = ROUTE_SHARE_TITLES[route] || DEFAULT_SHARE_TITLE; let targetRoute = isPublic ? `/${route}` : DEFAULT_SHARE_PATH; let queryStr = isPublic ? stringifyShareQuery(options) : ''; let imageUrl = ''; const customGetter = vm && vm.$ && typeof vm.$.__yjyfShareGetter === 'function' ? vm.$.__yjyfShareGetter : null; if (customGetter) { try { const custom = customGetter() || {}; if (custom.title) title = custom.title; if (custom.imageUrl) imageUrl = custom.imageUrl; if (custom.path) { const parsed = parseSharePath(custom.path); targetRoute = parsed.path; queryStr = parsed.queryStr; } if (custom.query !== undefined) { queryStr = typeof custom.query === 'object' ? stringifyShareQuery(custom.query) : String(custom.query || '').replace(/^\?/, ''); } } catch (e) {} } const trackedQuery = appendSharerTrack(queryStr); const finalPath = trackedQuery ? `${targetRoute}?${trackedQuery}` : targetRoute; return { title: title || DEFAULT_SHARE_TITLE, path: finalPath || DEFAULT_SHARE_PATH, query: trackedQuery, imageUrl: imageUrl || '' }; } export function useShare(getterOrConfig) { const instance = getCurrentInstance(); const getter = typeof getterOrConfig === 'function' ? getterOrConfig : () => (getterOrConfig || {}); if (instance) { instance.__yjyfShareGetter = getter; } onShareAppMessage(() => { const proxy = instance && instance.proxy ? instance.proxy : null; const data = buildDefaultShareData(proxy); const out = { title: data.title, path: data.path }; if (data.imageUrl) out.imageUrl = data.imageUrl; return out; }); onShareTimeline(() => { const proxy = instance && instance.proxy ? instance.proxy : null; const data = buildDefaultShareData(proxy); const out = { title: data.title, query: data.query || '' }; if (data.imageUrl) out.imageUrl = data.imageUrl; return out; }); } export function triggerCustomShare(customInfo = {}) { const title = customInfo.title || DEFAULT_SHARE_TITLE; const path = customInfo.path || DEFAULT_SHARE_PATH; // #ifdef H5 const fullUrl = typeof window !== 'undefined' && window.location ? `${window.location.origin}/#${path.startsWith('/') ? path : '/' + path}` : path; if (typeof navigator !== 'undefined' && typeof navigator.share === 'function') { navigator.share({ title, text: title, url: fullUrl }).catch(() => {}); return; } uni.setClipboardData({ data: `${title} ${fullUrl}`, success: () => uni.showToast({ title: '已复制分享链接', icon: 'none' }) }); // #endif // #ifndef H5 uni.setClipboardData({ data: `${title}`, success: () => uni.showToast({ title: '请点击右上角“...”转发给好友或分享朋友圈', icon: 'none' }) }); // #endif } export default { onLoad() { // #ifdef MP-WEIXIN if (typeof uni.showShareMenu === 'function') { uni.showShareMenu({ withShareTicket: true, menus: ['shareAppMessage', 'shareTimeline'], fail: () => {} }); } // #endif }, onShow() { // #ifdef MP-WEIXIN if (typeof uni.showShareMenu === 'function') { uni.showShareMenu({ withShareTicket: true, menus: ['shareAppMessage', 'shareTimeline'], fail: () => {} }); } // #endif }, onShareAppMessage() { const data = buildDefaultShareData(this); const out = { title: data.title, path: data.path }; if (data.imageUrl) out.imageUrl = data.imageUrl; return out; }, onShareTimeline() { const data = buildDefaultShareData(this); const out = { title: data.title, query: data.query || '' }; if (data.imageUrl) out.imageUrl = data.imageUrl; return out; } };