This commit is contained in:
mac·ufutx 2026-04-16 13:47:32 +08:00
parent d7ac260202
commit f0ceec5404
2 changed files with 174 additions and 87 deletions

View File

@ -1,67 +1,111 @@
import wx from 'weixin-js-sdk' import wx from 'weixin-js-sdk'
import store from '@/store' import store from '@/store'
// import { $toastText } from '@/config/toast'
const base = {} const base = {}
base.install = function(Vue, options) { base.install = function(Vue, options) {
// 初始化微信配置(只执行一次)
Vue.prototype.$initWechatConfig = function() {
return new Promise((resolve, reject) => {
const configData = store.state.app.configData
if (!configData) {
console.error('微信配置数据为空')
reject('微信配置数据为空')
return
}
wx.config(configData)
wx.ready(() => {
console.log('微信 JS-SDK 配置成功')
resolve()
})
wx.error((err) => {
console.error('微信 JS-SDK 配置失败:', err)
reject(err)
})
})
}
// 分享配置方法
Vue.prototype.$shareList = function(imgUrl, link, desc, title) { Vue.prototype.$shareList = function(imgUrl, link, desc, title) {
// 分享 // 检查 wx 是否已配置
let share_title = `` if (!wx) {
if (!localStorage.getItem('from_openid') || (localStorage.getItem('from_openid') && localStorage.getItem('from_openid').length < 20)) { console.error('微信 JS-SDK 未加载')
return
}
let share_title = ''
const fromOpenid = localStorage.getItem('from_openid')
const storeInfo = store.state.app.info
if (!fromOpenid || fromOpenid.length < 20) {
share_title = '' share_title = ''
} else if (store.state.app.info.nickname && store.state.app.info.nickname.length > 4) { } else if (storeInfo && storeInfo.nickname && storeInfo.nickname.length > 4) {
share_title = store.state.app.info.nickname.substring(0, 4) + '..推送' share_title = storeInfo.nickname.substring(0, 4) + '..推送'
} else if (storeInfo && storeInfo.nickname) {
share_title = storeInfo.nickname + '推送'
} else { } else {
share_title = store.state.app.info.nickname + '推送' share_title = ''
} }
wx.config(store.state.app.configData)
wx.ready(function() { // 确保在 wx.ready 中执行分享配置
if (wx && typeof wx.ready === 'function') {
wx.ready(() => {
console.log('开始配置分享参数', { title, desc, link, imgUrl })
// 隐藏菜单项(可选)
wx.hideMenuItems({ wx.hideMenuItems({
menuList: [] // 屏蔽复制链接 menuList: [] // 为空表示不隐藏任何菜单
}) })
// 分享给朋友
wx.updateAppMessageShareData({ wx.updateAppMessageShareData({
title: desc !== '欢迎和我预约' ? share_title + '《' + title + '》' : `${title}】商家预约`, // 分享标题 title: desc !== '欢迎和我预约' ? share_title + '《' + title + '》' : `${title}】商家预约`,
desc: desc, // 分享描述 desc: desc,
link: link, // 分享链接 link: link,
imgUrl: imgUrl, // 分享图标 imgUrl: imgUrl,
success: function() { success: function() {
console.log('分享成功') console.log('分享给朋友成功')
}, },
cancel: function() { cancel: function() {
console.log('分享失败') console.log('分享给朋友取消')
} }
}) })
// 微信分享菜单测试
// 分享到朋友圈
wx.updateTimelineShareData({ wx.updateTimelineShareData({
title: share_title + '《' + title + '》', // 分享标题 title: share_title + '《' + title + '》',
desc: desc, // 分享描述 link: link,
link: link, // 分享链接 imgUrl: imgUrl,
imgUrl: imgUrl, // 分享图标
success: function() { success: function() {
console.log('分享成功') console.log('分享到朋友圈成功')
}, },
cancel: function() { cancel: function() {
console.log('分享失败') console.log('分享到朋友圈取消')
} }
}) })
// 兼容旧版本(可选)
if (wx.onMenuShareAppMessage) {
wx.onMenuShareAppMessage({ wx.onMenuShareAppMessage({
title: share_title + '《' + title + '》', // 分享标题 title: share_title + '《' + title + '》',
desc: desc, // 分享描述 desc: desc,
link: link, // 分享链接;在微信上分享时,该链接的域名必须与企业某个应用的可信域名一致 link: link,
imgUrl: imgUrl, // 分享图标 imgUrl: imgUrl,
success: function() { success: function() {
console.log('分享成功-企业') // 用户确认分享后执行的回调函数 console.log('分享成功-旧版')
}, },
cancel: function() { cancel: function() {
// 用户取消分享后执行的回调函数 console.log('分享取消-旧版')
} }
}) })
}
}) })
wx.error(function(err) { } else {
// $toastText('网络异常,请稍后重试') console.error('wx.ready 不可用,请确保微信 JS-SDK 已正确加载')
console.log(JSON.stringify(err), '7777777777777') }
// alert(JSON.stringify(err))
})
} }
} }

View File

@ -1,6 +1,7 @@
<template> <template>
<div class="poster-generator-container"> <div class="poster-generator-container">
<div class="input-card"> <div class="input-card">
<div class="card-header"> <div class="card-header">
<span class="header-icon">📋</span> <span class="header-icon">📋</span>
@ -106,7 +107,7 @@
import html2canvas from 'html2canvas' import html2canvas from 'html2canvas'
import { ImagePreview } from 'vant' import { ImagePreview } from 'vant'
import { $toastText, $toastSuccess, $toastLoading, $toastClear } from '@/config/toast' import { $toastClear, $toastLoading, $toastSuccess, $toastText } from '@/config/toast'
import requestApp from '@/utils/request' import requestApp from '@/utils/request'
import QRCode from 'qrcode' import QRCode from 'qrcode'
@ -125,14 +126,20 @@ export default {
showActivityPicker: false, showActivityPicker: false,
listLoading: false, listLoading: false,
listFinished: true, listFinished: true,
openId: '' openId: '',
wxConfigInitialized: false
// qrcodeCanvas DOM mounted this.$refs 访 // qrcodeCanvas DOM mounted this.$refs 访
} }
}, },
mounted() { async mounted() {
// onMounted
this.detectWeChatEnv() this.detectWeChatEnv()
this.getData()
//
if (this.isWeChatEnv) {
await this.initWechatShare()
}
await this.getData()
console.log('33') console.log('33')
}, },
computed: { computed: {
@ -146,17 +153,29 @@ export default {
} }
}, },
methods: { methods: {
// · //
// formatTitle(title) { async initWechatShare() {
// if (!title) return '' try {
// // · // store configData
// debugger if (!this.$store.state.app.configData) {
// const parts = title.split('·') console.log('等待微信配置数据加载...')
// console.log(parts) // store
// debugger await new Promise(resolve => setTimeout(resolve, 500))
// const lastPart = parts[parts.length - 1].trim() }
// return lastPart
// }, //
if (this.$initWechatConfig) {
await this.$initWechatConfig()
this.wxConfigInitialized = true
console.log('微信分享配置初始化成功')
} else {
console.error('$initWechatConfig 方法不存在')
}
} catch (err) {
console.error('微信分享配置初始化失败:', err)
}
},
// script setup // script setup
async getData() { async getData() {
@ -164,20 +183,27 @@ export default {
const vm = this const vm = this
this.listLoading = true this.listLoading = true
//
if (this.isWeChatEnv && !this.wxConfigInitialized) {
await this.initWechatShare()
}
//
this.setupWechatShare()
// weXinShare Vue 2 // weXinShare Vue 2
vm.$nextTick(() => { // vm.$nextTick(() => {
setTimeout(() => { // setTimeout(() => {
// if (vm.$store.state.app.configData) { // // if (vm.$store.state.app.configData) {
const urls = `${vm.$shareCallback}/api/official/live/wechat/FamilyAuth?from_openid=${localStorage.getItem('openid')}&merchant_id=${vm.$store.state.app.merchant_id}&spread_merchant_id=${vm.$store.state.app.spread_merchant_id}&url=` + encodeURIComponent(`${vm.$shareCallback}/pu/#/activityPoster`) // const urls = `${vm.$shareCallback}/api/official/live/wechat/FamilyAuth?from_openid=${localStorage.getItem('openid')}&merchant_id=${vm.$store.state.app.merchant_id}&spread_merchant_id=${vm.$store.state.app.spread_merchant_id}&url=` + encodeURIComponent(`${vm.$shareCallback}/pu/#/activityPoster`)
vm.$shareList( // vm.$shareList(
'https://image.fulllinkai.com/202310/28/88e931a50ec0a8094fb46191b389457e.png?x-oss-process=image/resize,w_200,h_200', // 'https://image.fulllinkai.com/202310/28/88e931a50ec0a8094fb46191b389457e.png?x-oss-process=image/resize,w_200,h_200',
urls, // urls,
'查看详情', // '',
'活动海报生成「saas」' // 'saas'
) // )
// } // // }
}, 300) // }, 300)
}) // })
// vm.$shareList('https://image.fulllinkai.com/202310/28/88e931a50ec0a8094fb46191b389457e.png', // vm.$shareList('https://image.fulllinkai.com/202310/28/88e931a50ec0a8094fb46191b389457e.png',
// `https://health.ufutx.cn/go_html/role_apply#/activityPoster`, // `https://health.ufutx.cn/go_html/role_apply#/activityPoster`,
// '', 'saas') // '', 'saas')
@ -204,6 +230,22 @@ export default {
$toastClear() $toastClear()
} }
}, },
//
setupWechatShare() {
if (!this.isWeChatEnv) return
const shareUrl = `${this.$shareCallback}/api/official/live/wechat/FamilyAuth?from_openid=${localStorage.getItem('openid')}&merchant_id=${this.$store.state.app.merchant_id}&spread_merchant_id=${this.$store.state.app.spread_merchant_id}&url=` + encodeURIComponent(`${this.$shareCallback}/pu/#/activityPoster`)
// wx
setTimeout(() => {
this.$shareList(
'https://image.fulllinkai.com/202310/28/88e931a50ec0a8094fb46191b389457e.png?x-oss-process=image/resize,w_200,h_200',
shareUrl,
'查看详情',
'活动海报生成「saas」'
)
}, 300)
},
detectWeChatEnv() { detectWeChatEnv() {
const userAgent = navigator.userAgent.toLowerCase() const userAgent = navigator.userAgent.toLowerCase()
@ -413,6 +455,7 @@ img {
overflow: hidden; overflow: hidden;
text-overflow: ellipsis; text-overflow: ellipsis;
} }
.poster-subtitle { .poster-subtitle {
position: absolute; position: absolute;
left: 0; left: 0;
@ -439,12 +482,12 @@ img {
overflow: hidden; overflow: hidden;
text-overflow: ellipsis; text-overflow: ellipsis;
} }
.sponsor-name { .sponsor-name {
position: absolute; position: absolute;
left: 0; left: 0;
top: 446px; top: 446px;
width: 100%; width: 100%;
letter-spacing: 0.8px;
display: flex; display: flex;
justify-content: center; justify-content: center;
padding: 0; padding: 0;
@ -453,6 +496,8 @@ img {
.sponsor-name .name { .sponsor-name .name {
font-size: 14px; font-size: 14px;
color: #0e2965; color: #0e2965;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
} }
.poster-qrcode { .poster-qrcode {
@ -489,7 +534,6 @@ img {
font-weight: 500; font-weight: 500;
color: #0e2866; color: #0e2866;
} }
.poster-time { .poster-time {
font-weight: 400; font-weight: 400;
font-size: 22px; font-size: 22px;
@ -548,7 +592,6 @@ img {
flex-direction: column; flex-direction: column;
align-items: center; align-items: center;
gap: 24px; gap: 24px;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
} }
/* 输入卡片 */ /* 输入卡片 */