useCouponShare.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { useRequest } from 'alova/client'
  2. import { ref } from 'vue'
  3. import { getIssuerStatus } from '@/api/home'
  4. import { getCouponShareLink } from '@/api/receiveCoupon'
  5. import { useTokenStore } from '@/store/token'
  6. import { toLoginPage } from '@/utils/toLoginPage'
  7. export interface ICouponShareResult {
  8. shareLink?: string
  9. copySuccess?: boolean
  10. shareLoading?: boolean
  11. shareCoupon?: (couponId?: string) => Promise<string | null>
  12. copyShareLink?: () => void
  13. cleanShareLink?: () => void
  14. }
  15. export function useCouponShare() {
  16. // 分享短链
  17. const shareLink = ref('')
  18. // 复制成功提示
  19. const copySuccess = ref(false)
  20. const redirectPath = ref('pages/home/home')
  21. // 获取分享短链的请求
  22. const {
  23. send: getShareLink,
  24. loading: shareLoading,
  25. } = useRequest(getCouponShareLink, {
  26. immediate: false,
  27. })
  28. // 分享优惠券
  29. const shareCoupon = async (couponId?: string) => {
  30. try {
  31. // 检查用户是否已登录
  32. const tokenStore = useTokenStore()
  33. if (!tokenStore.hasLogin) {
  34. uni.showToast({
  35. title: '请先登录',
  36. icon: 'none',
  37. duration: 2000,
  38. })
  39. // 跳转到登录页
  40. toLoginPage()
  41. return null
  42. }
  43. console.log('分享链接')
  44. // 判断是否是发券人
  45. const IssuerResult = await getIssuerStatus()
  46. if (!IssuerResult) {
  47. uni.showModal({
  48. title: '提示',
  49. content: '您不是发券人,不能分享优惠券',
  50. showCancel: false, // 不显示取消按钮
  51. confirmText: '知道了',
  52. })
  53. return null
  54. }
  55. // // 请求短链
  56. const result = await getShareLink({
  57. pathUrl: redirectPath.value,
  58. templateId: couponId,
  59. })
  60. if (result) {
  61. // 保存短链
  62. console.log('分享短链:', result)
  63. shareLink.value = result as string
  64. return shareLink.value
  65. }
  66. return null
  67. }
  68. catch (error) {
  69. console.error('获取分享短链失败:', error)
  70. uni.showToast({
  71. title: '获取分享短链失败',
  72. icon: 'none',
  73. duration: 2000,
  74. })
  75. return null
  76. }
  77. }
  78. // 复制短链
  79. const copyShareLink = () => {
  80. // 使用uni-app的复制API
  81. uni.setClipboardData({
  82. data: shareLink.value,
  83. success: () => {
  84. // 显示复制成功提示
  85. copySuccess.value = true
  86. setTimeout(() => {
  87. copySuccess.value = false
  88. }, 2000)
  89. },
  90. fail: (error) => {
  91. console.log('复制的内容', shareLink.value)
  92. console.error('复制失败:', error)
  93. uni.showToast({
  94. title: '复制失败',
  95. icon: 'none',
  96. duration: 2000,
  97. })
  98. }
  99. })
  100. }
  101. const cleanShareLink = () => {
  102. shareLink.value = ''
  103. }
  104. return {
  105. shareLink,
  106. copySuccess,
  107. shareLoading,
  108. shareCoupon,
  109. copyShareLink,
  110. cleanShareLink
  111. }
  112. }