Files
yshop-pro-uniapp/hooks/useShare.js
2023-11-22 18:55:55 +08:00

148 lines
3.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @name: useShare
* @author: kahu4
* @date: 2023-11-10 09:45
* @descriptionuseShare
* @update: 2023-11-10 09:45
* */
import { ref, unref } from "vue";
import { PARAMS_KEY, useRouter } from "@/hooks/useRouter";
import { VUE_SHARE_TITLE } from "@/config";
// 分享页面
export const SharePathMap = {
GOODS_DETAIL:'/pages/goodsDetail/goodsDetail'
}
export function useShare(url='pages/share/index'){
const shareInfo = ref({
title: VUE_SHARE_TITLE,
path: 'pages/share/index',
imageUrl: ''
})
const shareUrl = ref(url)
/**
* 设置路径
* @param packageParameter
*/
const handleSetShareData = (packageParameter) => {
//views/dynamic/detail/index
shareInfo.value.path = `/pages/share/index?${packageParameter}`
}
const shareAppMessage = ()=>Promise.resolve({
title: shareInfo.value.title,
path: shareInfo.value.path,
imageUrl: shareInfo.value.imageUrl
})
const shareTimeline = ()=>({
title: shareInfo.value.title,
path: shareInfo.value.path,
imageUrl: shareInfo.value.imageUrl
})
/**
* 封装参数
* @param data
*/
const packageParameter = (data)=>{
const shareData = {
url,
data
}
shareData.url = shareUrl.value
return `${PARAMS_KEY}=${encodeURIComponent(JSON.stringify(shareData))}`
}
/**
* 默认分享
*/
const defaultShare = ()=>{
unref(shareInfo).title = VUE_SHARE_TITLE
unref(shareInfo).imageUrl = ''
handleSetShareData(packageParameter({
url:'/pages/index/index',
data:{}
}))
}
/**
* 商品分享
* @param goods
*/
const goodsDetailShare = (goods)=>{
unref(shareInfo).title = goods.storeName
unref(shareInfo).imageUrl = goods.image
handleSetShareData(packageParameter({
id:goods.id
}))
}
return {
shareUrl,
shareInfo,
handleSetShareData,
shareAppMessage,
shareTimeline,
packageParameter,
defaultShare,
goodsDetailShare,
}
}
export const useShareInner = () => {
const {push, getParams, pushToTab} = useRouter()
const params = ref({
data: undefined, url: ""
})
/**
* 处理share参数
* @param options
*/
async function analysisParams(options) {
params.value = getParams(options);
await analysisParameter()
}
/**
* 解析参数
*/
async function analysisParameter() {
switch (unref(params).url) {
case SharePathMap.GOODS_DETAIL:
toSkip()
break;
default:
pushToTab({url: '/pages/index/index'})
}
}
/**
* 跳转
*/
function toSkip() {
if (unref(params).url) {
push({
url: unref(params).url,
}, {
data: unref(params).data,
type: 'redirectTo'
})
}
}
return {
params,
analysisParams
}
}