share.vue 2.9 KB

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