shareAppMessage.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. const shareAppMessage = function(args) {
  6. args = args || {
  7. title: '这是一个分享页面',
  8. path: '/pages/index/index',
  9. params: {}
  10. };
  11. if (typeof args.params === 'undefined') {
  12. args.params = {};
  13. }
  14. // #ifdef MP-ALIPAY
  15. if (typeof args.imageUrl !== 'undefined') {
  16. args.bgImgUrl = args.imageUrl;
  17. }
  18. // #endif
  19. let user_id = 0;
  20. if (this.$user.isLogin() && this.$store.state.user.info) {
  21. user_id = this.$store.state.user.info.options.user_id;
  22. }
  23. if (typeof args.path === 'undefined' || (args.path === '/pages/index/index' && typeof args.params.page_id === 'undefined')) {
  24. args.path = `/pages/index/index?user_id=${user_id}`;
  25. if (Object.keys(args.params).length != 0) {
  26. args.path += `&` + objectToUrlParams(args.params);
  27. }
  28. } else {
  29. args.params.path = args.path;
  30. args.params.user_id = user_id;
  31. args.path = `/pages/index/index?scene=share&user_id=${user_id}&params=${JSON.stringify(args.params)}`;
  32. }
  33. setTimeout(() => {
  34. request({
  35. url: api.coupon.share_coupon,
  36. }).then(response => {
  37. if (response.code === 0) {
  38. let coupon = {
  39. list: response.data.list,
  40. type: 'share'
  41. };
  42. $store.dispatch('page/actionSetCoupon', coupon);
  43. }
  44. }).catch(e => {
  45. });
  46. },1000);
  47. return args;
  48. };
  49. export default shareAppMessage;