| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- import { useRequest } from 'alova/client'
- import { ref } from 'vue'
- import { getIssuerStatus } from '@/api/home'
- import { getCouponShareLink } from '@/api/receiveCoupon'
- import { useTokenStore } from '@/store/token'
- import { toLoginPage } from '@/utils/toLoginPage'
- export interface ICouponShareResult {
- shareLink?: string
- copySuccess?: boolean
- shareLoading?: boolean
- shareCoupon?: (couponId?: string) => Promise<string | null>
- copyShareLink?: () => void
- cleanShareLink?: () => void
- }
- export function useCouponShare() {
- // 分享短链
- const shareLink = ref('')
- // 复制成功提示
- const copySuccess = ref(false)
- const redirectPath = ref('pages/home/home')
- // 获取分享短链的请求
- const {
- send: getShareLink,
- loading: shareLoading,
- } = useRequest(getCouponShareLink, {
- immediate: false,
- })
- // 分享优惠券
- const shareCoupon = async (couponId?: string) => {
- try {
- // 检查用户是否已登录
- const tokenStore = useTokenStore()
- if (!tokenStore.hasLogin) {
- uni.showToast({
- title: '请先登录',
- icon: 'none',
- duration: 2000,
- })
- // 跳转到登录页
- toLoginPage()
- return null
- }
- console.log('分享链接')
- // 判断是否是发券人
- const IssuerResult = await getIssuerStatus()
- if (!IssuerResult) {
- uni.showModal({
- title: '提示',
- content: '您不是发券人,不能分享优惠券',
- showCancel: false, // 不显示取消按钮
- confirmText: '知道了',
- })
- return null
- }
- // // 请求短链
- const result = await getShareLink({
- pathUrl: redirectPath.value,
- templateId: couponId,
- })
- if (result) {
- // 保存短链
- console.log('分享短链:', result)
- shareLink.value = result as string
- return shareLink.value
- }
- return null
- }
- catch (error) {
- console.error('获取分享短链失败:', error)
- uni.showToast({
- title: '获取分享短链失败',
- icon: 'none',
- duration: 2000,
- })
- return null
- }
- }
- // 复制短链
- const copyShareLink = () => {
- // 使用uni-app的复制API
- uni.setClipboardData({
- data: shareLink.value,
- success: () => {
- // 显示复制成功提示
- copySuccess.value = true
- setTimeout(() => {
- copySuccess.value = false
- }, 2000)
- },
- fail: (error) => {
- console.log('复制的内容', shareLink.value)
- console.error('复制失败:', error)
- uni.showToast({
- title: '复制失败',
- icon: 'none',
- duration: 2000,
- })
- }
- })
- }
- const cleanShareLink = () => {
- shareLink.value = ''
- }
- return {
- shareLink,
- copySuccess,
- shareLoading,
- shareCoupon,
- copyShareLink,
- cleanShareLink
- }
- }
|