ShareServer.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. </template>
  3. <script>
  4. import * as API_Common from '$project/apis/common'
  5. import wx from 'weixin-js-sdk'
  6. import * as WxJsApi from '$project/utils/wxJsApi'
  7. export default {
  8. name: 'Share',
  9. props: {
  10. id: {
  11. require: true,
  12. default: '0',
  13. },
  14. serverList: {
  15. require: true,
  16. type: Array,
  17. default: () => {
  18. return [];
  19. }
  20. }
  21. },
  22. data() {
  23. return {
  24. shareName:null,
  25. isLoading: false,
  26. nowItem: {
  27. shareTitle: '',
  28. shareImg: '',
  29. shareDescription: ''
  30. }
  31. }
  32. },
  33. created() {
  34. },
  35. methods: {
  36. //父组件调用子组件的方法在获取相应的数据后调用
  37. init() {
  38. for (var i = 0; i < this.serverList.length; i++) {
  39. if (this.serverList[i]['dictionaryId'] == this.id) {
  40. this.nowItem = this.serverList[i];
  41. break;
  42. }
  43. }
  44. //定义微信分享
  45. this.wxShare();
  46. },
  47. get_wx_config() {
  48. // this.isLoading = true;
  49. var sz=['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareQZone']
  50. WxJsApi.getWxConfig(sz)
  51. },
  52. //第二种方法,直接通过调用该方法来处理
  53. setShare() {
  54. this.isLoading = true;
  55. API_Common.getSchoolServeList().then(response => {
  56. this.isLoading = false;
  57. for (var i = 0; i < response.length; i++) {
  58. if (response[i]['dictionaryId'] == this.id) {
  59. this.nowItem = response[i];
  60. break;
  61. }
  62. }
  63. //定义微信分享
  64. this.wxShare();
  65. }).catch(error => {
  66. this.isLoading = false;
  67. //this.mui.toast(error);
  68. })
  69. },
  70. setName(name) {
  71. this.shareName=name;
  72. },
  73. //获取微信jssdk配置信息
  74. //微信分享自定义
  75. wxShare() {
  76. console.log(this.nowItem);
  77. if(this.shareName){
  78. this.nowItem.shareTitle=this.shareName;
  79. }
  80. let joinUrl = window.location.href
  81. if(joinUrl.indexOf("&share=1")>0){
  82. joinUrl= joinUrl.replace("&share=1","")
  83. }
  84. var _this = this;
  85. wx.ready(function() {
  86. wx.onMenuShareAppMessage({
  87. title: '【小鹏管家】'+_this.nowItem.shareTitle,
  88. desc: _this.nowItem.shareDescription,
  89. link: joinUrl,
  90. imgUrl: _this.nowItem.shareImg,
  91. success: function() {
  92. // 设置成功
  93. }
  94. })
  95. wx.onMenuShareTimeline({
  96. title: _this.nowItem.shareTitle,
  97. desc: _this.nowItem.shareDescription,
  98. link: joinUrl,
  99. imgUrl: _this.nowItem.shareImg,
  100. success: function() {
  101. // 设置成功
  102. }
  103. })
  104. });
  105. }
  106. },
  107. mounted() {
  108. this.get_wx_config();
  109. },
  110. }
  111. </script>
  112. <style>
  113. </style>