useShare.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { getIssuerDetail, getShareInfo } from '@/api/home'
  2. import { useTokenStore } from '@/store/token'
  3. import { getImageUrl } from '@/utils/imageUtil'
  4. /**
  5. * 分享配置选项
  6. */
  7. export interface ShareOptions {
  8. title?: string
  9. path?: string
  10. imageUrl?: string
  11. shareType?: 'INVITE' | 'COUPON' | string
  12. showLoading?: boolean
  13. imageSource?: 'LOCAL' | 'REMOTE'
  14. pathParamKey?: string
  15. }
  16. interface ShareInfo {
  17. shareType: 'INVITE' | 'COUPON' | string
  18. shareContentId?: string
  19. sharePath?: string
  20. }
  21. /**
  22. * 默认分享配置
  23. */
  24. const defaultOptions: Required<ShareOptions> = {
  25. title: '领券省心,消费超值,优质生活轻松开启!',
  26. path: '/pages/home/home',
  27. imageUrl: `${getImageUrl('@img/share.png')}`,
  28. shareType: 'INVITE',
  29. showLoading: true,
  30. imageSource: 'LOCAL',
  31. pathParamKey: 'shareRecordId',
  32. }
  33. /**
  34. * 分享hook - 用于页面生命周期中使用
  35. * @param initOptions 自定义分享配置
  36. * @returns 可以在onShareAppMessage中使用的异步函数
  37. */
  38. export function useShare(initOptions: ShareOptions = {}) {
  39. /**
  40. * 获取分享配置 - 可以直接在onShareAppMessage中调用
  41. * @returns 完整的分享配置
  42. */
  43. const getShareConfig = async (customOption?: ShareOptions, extraParams?): Promise<WechatMiniprogram.Page.ICustomShareContent | {}> => {
  44. // 合并默认配置和自定义配置
  45. const options = { ...defaultOptions, ...initOptions, ...customOption }
  46. // 检查用户是否已登录
  47. const tokenStore = useTokenStore()
  48. if (!tokenStore.hasLogin) {
  49. uni.showToast({
  50. title: '请先登录后再分享',
  51. icon: 'none',
  52. })
  53. }
  54. try {
  55. if (options.showLoading) {
  56. uni.showLoading({
  57. title: '获取分享信息中...',
  58. mask: true,
  59. })
  60. }
  61. const IssuerResult = await getIssuerDetail()
  62. console.log(IssuerResult, 'IssuerResult')
  63. if (!IssuerResult || (IssuerResult?.status && IssuerResult?.status !== '1')) {
  64. throw new Error('当前用户不是发券人,无法发放优惠券')
  65. }
  66. const params: ShareInfo = {
  67. shareType: options.shareType,
  68. }
  69. if (options.shareType === 'COUPON') {
  70. if (!extraParams?.shareContentId) {
  71. throw new Error('分享优惠券时需要提供优惠券ID')
  72. }
  73. params.shareContentId = extraParams?.shareContentId || ''
  74. }
  75. else {
  76. params.sharePath = options.path
  77. }
  78. // 请求分享信息接口
  79. const res = await getShareInfo(params)
  80. if (options.showLoading) {
  81. uni.hideLoading()
  82. }
  83. if (res !== '' && res !== null && res !== 'null') {
  84. // 拼接分享路径
  85. const sharePath = `${options.path}${options.path.includes('?') ? '&' : '?'}${options.pathParamKey}=${res}`
  86. let imageUrl = ''
  87. if (options.imageSource === 'LOCAL') {
  88. imageUrl = options.imageUrl
  89. }
  90. else {
  91. imageUrl = `${import.meta.env.VITE_SERVER_BASEURL}${import.meta.env.VITE_SERVER_RESOURCE_BASEURL}/${options.imageUrl}`
  92. }
  93. console.log(sharePath)
  94. return {
  95. title: options.title,
  96. path: sharePath,
  97. imageUrl,
  98. }
  99. }
  100. else {
  101. uni.showToast({
  102. title: '分享信息获取失败',
  103. icon: 'none',
  104. duration: 2000,
  105. })
  106. }
  107. }
  108. catch (error) {
  109. if (options.showLoading) {
  110. uni.hideLoading()
  111. }
  112. uni.showToast({
  113. title: error.message || '获取分享信息失败,请稍后重试',
  114. icon: 'none',
  115. })
  116. }
  117. }
  118. /**
  119. * 获取朋友圈分享配置 - 可以直接在onShareTimeline中调用
  120. * @returns 完整的朋友圈分享配置
  121. */
  122. const getTimelineShareConfig = async (): Promise<WechatMiniprogram.Page.ICustomShareTimelineContent | {}> => {
  123. const config = await getShareConfig() as WechatMiniprogram.Page.ICustomShareContent
  124. return {
  125. title: config.title,
  126. path: config.path,
  127. imageUrl: config.imageUrl,
  128. }
  129. }
  130. return {
  131. getShareConfig,
  132. getTimelineShareConfig
  133. }
  134. }