shareAppMessage.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import {objectToUrlParams} from "./utils";
  2. import request from "../core/request.js";
  3. import api from "../core/appOnLaunch.js";
  4. import $store from "../store/index.js";
  5. import user from './user.js';
  6. import Vue from "vue";
  7. // #ifdef H5
  8. import jwx from '../core/jweixin.js';
  9. // #endif
  10. const shareAppMessage = function (args, success) {
  11. args = args || {
  12. title: '这是一个分享页面',
  13. path: '/pages/index/index',
  14. params: {}
  15. };
  16. if (typeof args.params === 'undefined') {
  17. args.params = {};
  18. }
  19. // #ifdef MP-ALIPAY
  20. if (typeof args.imageUrl !== 'undefined') {
  21. args.bgImgUrl = args.imageUrl;
  22. }
  23. // #endif
  24. let user_id = 0;
  25. if (user.isLogin() && Vue.prototype.$store.state.user.info) {
  26. user_id = Vue.prototype.$store.state.user.info.options.user_id;
  27. }
  28. if (typeof args.path === 'undefined' || (args.path === '/pages/index/index' && typeof args.params.page_id === 'undefined')) {
  29. args.path = `/pages/index/index?user_id=${user_id}`;
  30. if (Object.keys(args.params).length != 0) {
  31. args.path += `&` + objectToUrlParams(args.params);
  32. }
  33. } else {
  34. args.params.path = args.path;
  35. args.params.user_id = user_id;
  36. // #ifdef MP
  37. args.path = `/pages/index/index?scene=share&user_id=${user_id}&params=${JSON.stringify(args.params)}`;
  38. // #endif
  39. // #ifdef H5
  40. args.path = `/pages/index/index?scene=share&user_id=${user_id}&params=${btoa(JSON.stringify(args.params))}`;
  41. // #endif
  42. }
  43. // #ifdef H5
  44. let reg = /^(\S+\?\#)\S+$/;
  45. let h = window.location.href.match(reg);
  46. if (h) {
  47. let link = h[1] + args.path;
  48. if (success) {
  49. if (jwx.isWechat()) {
  50. Vue.prototype.$store.commit('share/status', true);
  51. } else {
  52. Vue.prototype.$utils.uniCopy({
  53. data: link,
  54. success() {
  55. uni.showToast({
  56. icon: 'none',
  57. title: '链接已复制'
  58. });
  59. }
  60. });
  61. }
  62. }
  63. async function we() {
  64. await new Promise(function (resolve, reject) {
  65. jwx.updateAppMessageShareData({
  66. data: {
  67. title: args.title,
  68. imgUrl: args.imgUrl ? args.imgUrl : args.imageUrl,
  69. link,
  70. desc: args.desc ? args.desc : ' '
  71. },
  72. success: function (res) {
  73. success && success(res);
  74. resolve(res);
  75. },
  76. cancel: function (res) {
  77. reject(res);
  78. }
  79. });
  80. });
  81. }
  82. we();
  83. }
  84. // #endif
  85. // #ifdef MP
  86. // 无法监听分享 故去掉
  87. setTimeout(() => {
  88. request({
  89. url: api.coupon.share_coupon,
  90. }).then(response => {
  91. if (response.code === 0) {
  92. let coupon = {
  93. list: response.data.list,
  94. type: 'share'
  95. };
  96. $store.dispatch('page/actionSetCoupon', coupon);
  97. }
  98. }).catch(() => {
  99. });
  100. }, 1000);
  101. // #endif
  102. return args;
  103. };
  104. export default shareAppMessage;