wxshare.mixin.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // 微信分享配置
  2. import haloConfig from '@/config/halo.config.js'
  3. import HaloTokenConfig from '@/config/token.config.js'
  4. import { jsonToUrlParams2 } from '@/utils/url.params.js'
  5. export const haloWxShareMixin = {
  6. data() {
  7. return {
  8. haloWxShareData: {
  9. ...haloConfig.wxShareConfig
  10. },
  11. }
  12. },
  13. //#ifdef MP-WEIXIN
  14. onShareAppMessage(res) {
  15. return {
  16. ...this.haloWxShareData,
  17. success: res => {}
  18. }
  19. },
  20. //#endif
  21. /* 分享到微信好友 */
  22. onShareAppMessage(res) {
  23. const promise = new Promise(resolve => {
  24. setTimeout(() => {
  25. resolve({
  26. title: this.haloWxShareData.title,
  27. path: this.haloWxShareData.path,
  28. })
  29. }, 2000)
  30. })
  31. return {
  32. title: this.haloWxShareData.title,
  33. path: '',
  34. promise
  35. }
  36. },
  37. // 分享到朋友圈-这里封装不够,在页面还要声明一次,否则是拿不到参数的,被分享者在朋友圈打开链接是空的
  38. onShareTimeline: function() {
  39. return {
  40. title: this.haloWxShareData.title,
  41. query: {},
  42. imageUrl: this.haloWxShareData.imageUrl,
  43. }
  44. },
  45. methods: {
  46. // 设置分享信息(需要在页面调用)
  47. fnSetWxShareConfig(config = {}) {
  48. let currentRoutes = getCurrentPages(); // 获取当前打开过的页面路由数组
  49. let currentRoute = currentRoutes[currentRoutes.length - 1].route; //获取当前页面路由(分销思路,分享者点开使用的小程序将获取到分享者的id)
  50. let sharePath = currentRoutes
  51. if (config.params) {
  52. const URLParams = config.params ? jsonToUrlParams2(config.params) : {};
  53. sharePath += URLParams
  54. }
  55. let _config = Object.assign({}, {
  56. path: sharePath,
  57. copyLink: HaloTokenConfig.BASE_API,
  58. query: {}
  59. }, config)
  60. uni.$tm.vx.commit('setWxShare', _config);
  61. this.haloWxShareData = _config;
  62. }
  63. }
  64. }