wechat.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * 微信相关工具函数
  3. */
  4. // 微信SDK加载标志
  5. let isWXSDKReady = false
  6. /**
  7. * 加载微信JSSDK
  8. * @returns {Promise} 加载结果Promise
  9. */
  10. export function loadWXSDK() {
  11. return new Promise((resolve, reject) => {
  12. // #ifdef H5
  13. if (isWXSDKReady) {
  14. resolve(window.wx)
  15. return
  16. }
  17. // 如果已经存在script标签,直接返回
  18. if (document.getElementById('wx-sdk')) {
  19. resolve(window.wx)
  20. return
  21. }
  22. // 创建script标签加载微信SDK
  23. const script = document.createElement('script')
  24. script.id = 'wx-sdk'
  25. script.src = 'https://res.wx.qq.com/open/js/jweixin-1.6.0.js'
  26. script.async = true
  27. script.onload = () => {
  28. isWXSDKReady = true
  29. resolve(window.wx)
  30. }
  31. script.onerror = (err) => {
  32. console.error('微信JSSDK加载失败', err)
  33. reject(new Error('微信JSSDK加载失败'))
  34. }
  35. document.body.appendChild(script)
  36. // #endif
  37. // 非H5环境
  38. // #ifndef H5
  39. resolve(null)
  40. // #endif
  41. })
  42. }
  43. /**
  44. * 配置微信SDK
  45. * @param {Object} config 配置信息
  46. */
  47. export function configWX(config) {
  48. return new Promise((resolve, reject) => {
  49. // #ifdef H5
  50. loadWXSDK().then(wx => {
  51. if (!wx) {
  52. reject(new Error('微信JSSDK不可用'))
  53. return
  54. }
  55. wx.config({
  56. debug: false, // 生产环境不开启调试
  57. appId: config.appId,
  58. timestamp: config.timestamp,
  59. nonceStr: config.nonceStr,
  60. signature: config.signature,
  61. jsApiList: [
  62. 'updateAppMessageShareData',
  63. 'updateTimelineShareData',
  64. 'onMenuShareAppMessage',
  65. 'onMenuShareTimeline'
  66. ]
  67. })
  68. wx.ready(() => {
  69. resolve(wx)
  70. })
  71. wx.error(err => {
  72. console.error('微信JSSDK配置失败', err)
  73. reject(err)
  74. })
  75. }).catch(err => {
  76. reject(err)
  77. })
  78. // #endif
  79. // 非H5环境
  80. // #ifndef H5
  81. resolve(null)
  82. // #endif
  83. })
  84. }
  85. /**
  86. * 设置微信分享
  87. * @param {Object} shareData 分享数据
  88. */
  89. export function share(shareData = {}) {
  90. // #ifdef H5
  91. loadWXSDK().then(wx => {
  92. if (!wx) {
  93. console.warn('微信JSSDK不可用')
  94. return
  95. }
  96. const data = {
  97. title: shareData.title || 'MoeNova',
  98. desc: shareData.desc || '一站式多平台应用,随时随地满足您的需求',
  99. link: shareData.link || window.location.href,
  100. imgUrl: shareData.imgUrl || '/static/logo.png',
  101. success: function() {
  102. console.log('分享成功')
  103. if (typeof shareData.success === 'function') {
  104. shareData.success()
  105. }
  106. },
  107. cancel: function() {
  108. console.log('取消分享')
  109. if (typeof shareData.cancel === 'function') {
  110. shareData.cancel()
  111. }
  112. }
  113. }
  114. // 兼容新旧接口
  115. if (wx.updateAppMessageShareData) {
  116. // 新接口,分享给朋友
  117. wx.updateAppMessageShareData(data)
  118. } else {
  119. // 旧接口,分享给朋友
  120. wx.onMenuShareAppMessage(data)
  121. }
  122. if (wx.updateTimelineShareData) {
  123. // 新接口,分享到朋友圈
  124. wx.updateTimelineShareData(data)
  125. } else {
  126. // 旧接口,分享到朋友圈
  127. wx.onMenuShareTimeline(data)
  128. }
  129. }).catch(err => {
  130. console.error('微信分享设置失败', err)
  131. })
  132. // #endif
  133. }
  134. export default {
  135. loadWXSDK,
  136. configWX,
  137. share
  138. }