开始

十分适合做分享功能。

global.js

  1. // 分享首页
  2. const shareHomepage = () => {
  3. const link = `${window.location.origin}/course`
  4. const shareConfig = {
  5. title: '跟小帮规划学财商知识',
  6. desc: '经济行情不好?小帮规划带你了解财商知识',
  7. link: link,
  8. imgUrl: 'https://course-static.xiaobangguihua.com/upload/9898c361-10c9-4b1e-8141-4d84ceae450d.png',
  9. }
  10. setWeixinShare(shareConfig)
  11. }
  12. // 分享笔记
  13. const shareNote = ({ noteId, uniqueId, productTitle, shareCredits = false }) => {
  14. const link = `${window.location.origin}/course/note/landing/${uniqueId}/${noteId}?sourceType=h5wxshare`
  15. const desc = `【小帮规划】${productTitle}精选笔记`
  16. const shareConfig = {
  17. title: '我又完成一条投资笔记,专门分享给你',
  18. desc: desc,
  19. link: link,
  20. imgUrl: 'https://course-static.xiaobangguihua.com/upload/9898c361-10c9-4b1e-8141-4d84ceae450d.png',
  21. success: async function() {
  22. growingService.pageShare(this.link, this.title)
  23. if (shareCredits) {
  24. const { score = 0 } = await creditService.shareNote(uniqueId)
  25. if (score > 0) {
  26. Toast.show(`分享成功 +${score}积分`)
  27. }
  28. }
  29. },
  30. complete: function() {
  31. GlobalModalShare.hide()
  32. },
  33. }
  34. setWeixinShare(shareConfig)
  35. }
  36. window.addEventListener('xiaobang-wechat-share/homepage', e => {
  37. shareHomepage(e.detail)
  38. })
  39. window.addEventListener('xiaobang-wechat-share/note', e => {
  40. shareNote(e.detail)
  41. })

页面的js

  1. if (// 判断分享什么) {
  2. const event = new CustomEvent('xiaobang-wechat-share/note', {
  3. detail: {
  4. noteId: this.noteId,
  5. uniqueId: this.uniqueId,
  6. productTitle: this.productTitle,
  7. shareCredits: this.showShareButton,
  8. },
  9. })
  10. window.dispatchEvent(event)
  11. } else {
  12. const event = new CustomEvent('xiaobang-wechat-share/homepage')
  13. window.dispatchEvent(event)
  14. }

weixin.js

  1. import weixinService from '@@/services/weixin'
  2. import { device } from '@@/utils/userAgent'
  3. import { getShareImageUrl } from '@@/utils/imageCrop'
  4. import growingService from '@@/services/growing'
  5. // 默认微信分享参数
  6. const defaultShareConfig = {
  7. title: '小帮规划',
  8. desc: '小帮规划',
  9. link: `${window.location.origin}/course`,
  10. imgUrl: 'https://oss.xiaobangtouzi.com/[xiaobangzouti]2019-06-19:5d0a0a594a3c5.png',
  11. success: function() {
  12. growingService.pageShare(this.link, this.title)
  13. },
  14. }
  15. /**
  16. * 设置微信分享
  17. *
  18. * @param {object} shareConfig 分享参数
  19. */
  20. export const setWeixinShare = async shareConfig => {
  21. if (device.isWeixin) {
  22. const signature = await weixinService.getWeixinSignature()
  23. const config = Object.assign({}, defaultShareConfig, shareConfig)
  24. config.imgUrl = getShareImageUrl(config.imgUrl)
  25. window.wx.config(signature)
  26. window.wx.ready(() => {
  27. window.wx.onMenuShareAppMessage(config)
  28. window.wx.onMenuShareTimeline(config)
  29. })
  30. }
  31. }
  32. // 预览图片
  33. export const previewImage = (current, imageList) => {
  34. window.wx.previewImage({
  35. current: current, // 当前显示图片的http链接
  36. urls: imageList, // 需要预览的图片http链接列表
  37. })
  38. }