share.vue 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <template>
  2. <div id="share">
  3. <van-button type="info" @click="toShare">分享</van-button>
  4. </div>
  5. </template>
  6. <script>
  7. let wx = require('weixin-js-sdk');
  8. import { mapState, createNamespacedHelpers } from 'vuex';
  9. const { mapActions: weixin } = createNamespacedHelpers('weixin');
  10. export default {
  11. name: 'share',
  12. props: {},
  13. components: {},
  14. data: function() {
  15. return {
  16. wx: {},
  17. };
  18. },
  19. created() {
  20. this.init();
  21. },
  22. methods: {
  23. ...weixin(['jsAuth']),
  24. async init() {
  25. // 处理参数
  26. const { path, query } = this.$route;
  27. const { openid, ...others } = query;
  28. let uri = `${path}`;
  29. const keys = Object.keys(others);
  30. if (keys.length > 0) {
  31. const arr = keys.map(key => `${key}=${query[key]}`);
  32. uri = `${uri}?${arr.join('&')}`;
  33. }
  34. // const url = encodeURIComponent(`${this.host}${process.env.VUE_APP_ROUTER}${uri}`);
  35. const url = encodeURIComponent(location.href.split('#')[0]);
  36. console.log(url);
  37. console.log(encodeURIComponent(`${this.host}${process.env.VUE_APP_ROUTER}${uri}`));
  38. const res = await this.jsAuth({ url });
  39. if (this.$checkRes(res)) {
  40. const { data } = res;
  41. wx.config({
  42. debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  43. appId: data.appid, // 必填,公众号的唯一标识
  44. timestamp: data.timestamp, // 必填,生成签名的时间戳
  45. nonceStr: data.noncestr, // 必填,生成签名的随机串
  46. signature: data.sign, // 必填,签名
  47. jsApiList: [
  48. 'updateAppMessageShareData', //朋友及QQ
  49. 'updateTimelineShareData', // 朋友圈及QQ空间
  50. ],
  51. });
  52. wx.ready(() => {
  53. console.log('in ready');
  54. this.$set(this, 'wx', wx);
  55. });
  56. wx.error(res => {
  57. console.log(res);
  58. });
  59. }
  60. },
  61. toShare() {
  62. this.wx.updateAppMessageShareData({
  63. title: '测试分享',
  64. desc: '测试',
  65. // imgUrl:''
  66. link: `${this.host}/articlemobile/login`,
  67. success: res => {
  68. console.log('in function:share success');
  69. console.log(res);
  70. },
  71. error: err => {
  72. alert('share is fail');
  73. alert(JSON.stringify(err));
  74. },
  75. });
  76. },
  77. },
  78. computed: {
  79. ...mapState(['user', 'menuParams']),
  80. pageTitle() {
  81. return `${this.$route.meta.title}`;
  82. },
  83. host() {
  84. return 'http://broadcast.waityou24.cn';
  85. },
  86. authUrl() {
  87. return `${host}/api/article/auth`;
  88. },
  89. },
  90. metaInfo() {
  91. return { title: this.$route.meta.title };
  92. },
  93. };
  94. </script>
  95. <style lang="less" scoped></style>