useWeChatShare.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { getShareConfig } from '@/api/context/context'
  2. import { DFindParams, DScrollTab, ChangeTabEmit } from '@/types/views/database.type'
  3. import wx from 'weixin-js-sdk'
  4. import { IShareConfig } from '../types/api/context.type'
  5. import { showToast } from 'vant'
  6. type shareOptions = {
  7. type: ChangeTabEmit
  8. imageUrl: string
  9. text: `您的好友给你分享了${DFindParams<DScrollTab, 'text'>}链接`
  10. }
  11. const config: {
  12. appId: DFindParams<IShareConfig, 'appId'>
  13. jsApiList: wx.ApiMethod[]
  14. } = {
  15. appId: 'wx91c4152b60ca91a3', // 微信公众号appId
  16. jsApiList: ['updateAppMessageShareData', 'onMenuShareAppMessage', 'updateTimelineShareData', 'onMenuShareTimeline'], // 微信配置可用权限
  17. }
  18. export type shareParams = {
  19. type: ChangeTabEmit | string
  20. id: string
  21. spId?: number
  22. userId?: number
  23. suid?: number
  24. link?: string
  25. imageUrl?: string
  26. isShowToast?: boolean
  27. }
  28. export const shareOptions: shareOptions[] = [
  29. {
  30. type: '1',
  31. imageUrl: '',
  32. text: '您的好友给你分享了图片链接',
  33. },
  34. {
  35. type: '2',
  36. imageUrl: '',
  37. text: '您的好友给你分享了视频链接',
  38. },
  39. {
  40. type: '3',
  41. imageUrl: '',
  42. text: '您的好友给你分享了文件链接',
  43. },
  44. {
  45. type: '4',
  46. imageUrl: '',
  47. text: '您的好友给你分享了文本链接',
  48. },
  49. {
  50. type: '6',
  51. imageUrl: '',
  52. text: '您的好友给你分享了文章链接',
  53. },
  54. {
  55. type: '7',
  56. imageUrl: '',
  57. text: '您的好友给你分享了百科链接',
  58. },
  59. ]
  60. const useWeChatShare = async (params: shareParams) => {
  61. const isWeChat = () => /MicroMessenger/i.test(navigator.userAgent)
  62. const linkParams =
  63. import.meta.env.VITE_HTTP_URL +
  64. (Number(params.type) === 7 ? '/encyclopedia/detail-' : '/info/detail-') +
  65. params.id +
  66. (Number(params.type) === 6 ? "-1.html" : ".html");
  67. if (sessionStorage.getItem('isDetailRefresh')) {
  68. sessionStorage.removeItem('isDetailRefresh')
  69. }
  70. if (!isWeChat()) return params.isShowToast && showToast('请在微信浏览器中分享')
  71. const { data } = await getShareConfig({
  72. appId: 'wx91c4152b60ca91a3',
  73. url: location.href,
  74. })
  75. console.log(data)
  76. wx.config({
  77. debug: false,
  78. appId: config.appId,
  79. nonceStr: data?.noncestr!,
  80. timestamp: data?.timestamp!,
  81. signature: data?.signature!,
  82. jsApiList: config.jsApiList,
  83. })
  84. const obj = shareOptions.filter((e) => e.type === params.type)[0] || {}
  85. const link =
  86. Number(params.type) < 5
  87. ? import.meta.env.VITE_BASE_HTTP +
  88. `/preview?t=${params.type}&id=${params.id}&uid=${params.userId || ''}&suid=${params.suid || ''}&spId=${params.spId || ''
  89. }`
  90. : linkParams
  91. const shareConfig: wx.IonMenuShareAppMessage = {
  92. title: obj.text,
  93. desc: obj.text,
  94. link,
  95. imgUrl: params.imageUrl || '',
  96. success: () => {
  97. console.log(obj, '成功')
  98. },
  99. fail: () => {
  100. window.location.reload()
  101. },
  102. cancel: () => {
  103. console.log('取消了')
  104. },
  105. }
  106. params.isShowToast && showToast('请点击右上角...进行分享')
  107. wx.ready(() => {
  108. wx.onMenuShareAppMessage(shareConfig)
  109. wx.onMenuShareTimeline(shareConfig)
  110. })
  111. wx.error(function (err) {
  112. console.warn(err)
  113. })
  114. }
  115. export default useWeChatShare