123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- /**
- * 微信相关工具函数
- */
- // 微信SDK加载标志
- let isWXSDKReady = false
- /**
- * 加载微信JSSDK
- * @returns {Promise} 加载结果Promise
- */
- export function loadWXSDK() {
- return new Promise((resolve, reject) => {
- // #ifdef H5
- if (isWXSDKReady) {
- resolve(window.wx)
- return
- }
-
- // 如果已经存在script标签,直接返回
- if (document.getElementById('wx-sdk')) {
- resolve(window.wx)
- return
- }
-
- // 创建script标签加载微信SDK
- const script = document.createElement('script')
- script.id = 'wx-sdk'
- script.src = 'https://res.wx.qq.com/open/js/jweixin-1.6.0.js'
- script.async = true
- script.onload = () => {
- isWXSDKReady = true
- resolve(window.wx)
- }
- script.onerror = (err) => {
- console.error('微信JSSDK加载失败', err)
- reject(new Error('微信JSSDK加载失败'))
- }
- document.body.appendChild(script)
- // #endif
-
- // 非H5环境
- // #ifndef H5
- resolve(null)
- // #endif
- })
- }
- /**
- * 配置微信SDK
- * @param {Object} config 配置信息
- */
- export function configWX(config) {
- return new Promise((resolve, reject) => {
- // #ifdef H5
- loadWXSDK().then(wx => {
- if (!wx) {
- reject(new Error('微信JSSDK不可用'))
- return
- }
-
- wx.config({
- debug: false, // 生产环境不开启调试
- appId: config.appId,
- timestamp: config.timestamp,
- nonceStr: config.nonceStr,
- signature: config.signature,
- jsApiList: [
- 'updateAppMessageShareData',
- 'updateTimelineShareData',
- 'onMenuShareAppMessage',
- 'onMenuShareTimeline'
- ]
- })
-
- wx.ready(() => {
- resolve(wx)
- })
-
- wx.error(err => {
- console.error('微信JSSDK配置失败', err)
- reject(err)
- })
- }).catch(err => {
- reject(err)
- })
- // #endif
-
- // 非H5环境
- // #ifndef H5
- resolve(null)
- // #endif
- })
- }
- /**
- * 设置微信分享
- * @param {Object} shareData 分享数据
- */
- export function share(shareData = {}) {
- // #ifdef H5
- loadWXSDK().then(wx => {
- if (!wx) {
- console.warn('微信JSSDK不可用')
- return
- }
-
- const data = {
- title: shareData.title || 'MoeNova',
- desc: shareData.desc || '一站式多平台应用,随时随地满足您的需求',
- link: shareData.link || window.location.href,
- imgUrl: shareData.imgUrl || '/static/logo.png',
- success: function() {
- console.log('分享成功')
- if (typeof shareData.success === 'function') {
- shareData.success()
- }
- },
- cancel: function() {
- console.log('取消分享')
- if (typeof shareData.cancel === 'function') {
- shareData.cancel()
- }
- }
- }
-
- // 兼容新旧接口
- if (wx.updateAppMessageShareData) {
- // 新接口,分享给朋友
- wx.updateAppMessageShareData(data)
- } else {
- // 旧接口,分享给朋友
- wx.onMenuShareAppMessage(data)
- }
-
- if (wx.updateTimelineShareData) {
- // 新接口,分享到朋友圈
- wx.updateTimelineShareData(data)
- } else {
- // 旧接口,分享到朋友圈
- wx.onMenuShareTimeline(data)
- }
- }).catch(err => {
- console.error('微信分享设置失败', err)
- })
- // #endif
- }
- export default {
- loadWXSDK,
- configWX,
- share
- }
|