123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- import { getShareConfig } from '@/api/context/context'
- import { DFindParams, DScrollTab, ChangeTabEmit } from '@/types/views/database.type'
- import wx from 'weixin-js-sdk'
- import { IShareConfig } from '../types/api/context.type'
- import { showToast } from 'vant'
- type shareOptions = {
- type: ChangeTabEmit
- imageUrl: string
- text: `您的好友给你分享了${DFindParams<DScrollTab, 'text'>}链接`
- }
- const config: {
- appId: DFindParams<IShareConfig, 'appId'>
- jsApiList: wx.ApiMethod[]
- } = {
- appId: 'wx91c4152b60ca91a3', // 微信公众号appId
- jsApiList: ['updateAppMessageShareData', 'onMenuShareAppMessage', 'updateTimelineShareData', 'onMenuShareTimeline'], // 微信配置可用权限
- }
- export type shareParams = {
- type: ChangeTabEmit | string
- id: string
- spId?: number
- userId?: number
- suid?: number
- link?: string
- imageUrl?: string
- isShowToast?: boolean
- }
- export const shareOptions: shareOptions[] = [
- {
- type: '1',
- imageUrl: '',
- text: '您的好友给你分享了图片链接',
- },
- {
- type: '2',
- imageUrl: '',
- text: '您的好友给你分享了视频链接',
- },
- {
- type: '3',
- imageUrl: '',
- text: '您的好友给你分享了文件链接',
- },
- {
- type: '4',
- imageUrl: '',
- text: '您的好友给你分享了文本链接',
- },
- {
- type: '6',
- imageUrl: '',
- text: '您的好友给你分享了文章链接',
- },
- {
- type: '7',
- imageUrl: '',
- text: '您的好友给你分享了百科链接',
- },
- ]
- const useWeChatShare = async (params: shareParams) => {
- const isWeChat = () => /MicroMessenger/i.test(navigator.userAgent)
- const linkParams =
- import.meta.env.VITE_HTTP_URL +
- (Number(params.type) === 7 ? '/encyclopedia/detail-' : '/info/detail-') +
- params.id +
- (Number(params.type) === 6 ? "-1.html" : ".html");
- if (sessionStorage.getItem('isDetailRefresh')) {
- sessionStorage.removeItem('isDetailRefresh')
- }
- if (!isWeChat()) return params.isShowToast && showToast('请在微信浏览器中分享')
- const { data } = await getShareConfig({
- appId: 'wx91c4152b60ca91a3',
- url: location.href,
- })
- console.log(data)
- wx.config({
- debug: false,
- appId: config.appId,
- nonceStr: data?.noncestr!,
- timestamp: data?.timestamp!,
- signature: data?.signature!,
- jsApiList: config.jsApiList,
- })
- const obj = shareOptions.filter((e) => e.type === params.type)[0] || {}
- const link =
- Number(params.type) < 5
- ? import.meta.env.VITE_BASE_HTTP +
- `/preview?t=${params.type}&id=${params.id}&uid=${params.userId || ''}&suid=${params.suid || ''}&spId=${params.spId || ''
- }`
- : linkParams
- const shareConfig: wx.IonMenuShareAppMessage = {
- title: obj.text,
- desc: obj.text,
- link,
- imgUrl: params.imageUrl || '',
- success: () => {
- console.log(obj, '成功')
- },
- fail: () => {
- window.location.reload()
- },
- cancel: () => {
- console.log('取消了')
- },
- }
- params.isShowToast && showToast('请点击右上角...进行分享')
- wx.ready(() => {
- wx.onMenuShareAppMessage(shareConfig)
- wx.onMenuShareTimeline(shareConfig)
- })
- wx.error(function (err) {
- console.warn(err)
- })
- }
- export default useWeChatShare
|