share.vue 2.6 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. 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. console.log(this.$router);
  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 = `${this.host}${process.env.VUE_APP_ROUTER}${uri}`;
  35. const res = await this.jsAuth({ url });
  36. if (this.$checkRes(res)) {
  37. const { data } = res;
  38. wx.config({
  39. debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  40. appId: data.appid, // 必填,公众号的唯一标识
  41. timestamp: data.timestamp, // 必填,生成签名的时间戳
  42. nonceStr: data.noncestr, // 必填,生成签名的随机串
  43. signature: data.sign, // 必填,签名
  44. jsApiList: [
  45. 'updateAppMessageShareData', //朋友及QQ
  46. 'updateTimelineShareData', // 朋友圈及QQ空间
  47. ],
  48. });
  49. wx.ready(() => {
  50. console.log('in ready');
  51. this.$set(this, 'wx', wx);
  52. });
  53. wx.error(res => {
  54. console.log(res);
  55. });
  56. }
  57. },
  58. toShare() {
  59. this.wx.updateAppMessageShareData({
  60. title: '测试分享',
  61. desc: '测试',
  62. // imgUrl:''
  63. link: 'http://free.waityou24.cn/',
  64. success: res => {
  65. console.log('in function:share success');
  66. console.log(res);
  67. },
  68. error: err => {
  69. alert('share is fail');
  70. alert(JSON.stringify(err));
  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>