You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.9 KiB

8 months ago
8 months ago
  1. import api from '@/plugins/api.js'
  2. function share() { //微信分享
  3. //获取签名
  4. let data = {
  5. url: import.meta.env.VITE_REDIRECT_URI + '/#/'
  6. }
  7. api('getVipShareSign', data, res => {
  8. if (res.code == 200) {
  9. let {
  10. appId,
  11. nonceStr,
  12. signature,
  13. timestamp
  14. } = res.result
  15. window.jWeixin.config({
  16. debug: false,
  17. appId: appId,
  18. nonceStr: nonceStr,
  19. signature: signature,
  20. timestamp: timestamp,
  21. jsApiList: [
  22. 'updateTimelineShareData',
  23. 'updateAppMessageShareData',
  24. 'onMenuShareWeibo',
  25. 'getLocation'
  26. ]
  27. });
  28. window.jWeixin.ready(function() {
  29. // 微信分享的数据
  30. var shareData = {
  31. "link": addQueryParams(data.url),
  32. "desc": "泰柔到家",
  33. "title": "泰柔到家,温柔呵护每一刻!",
  34. imgUrl : import.meta.env.VITE_REDIRECT_URI + '/static/share/logo.png',
  35. success: function() {
  36. //分享成功可以做相应的数据处理
  37. // uni.showToast({
  38. // mask: true,
  39. // duration: 1000,
  40. // title: '注册分享成功',
  41. // });
  42. }
  43. };
  44. //分享微信朋友圈内容设置
  45. window.jWeixin.updateTimelineShareData(shareData);
  46. //分享给朋友内容设置
  47. window.jWeixin.updateAppMessageShareData(shareData);
  48. //分享到微博内容设置
  49. window.jWeixin.onMenuShareWeibo(shareData);
  50. });
  51. window.jWeixin.error(function(err){
  52. console.error(err);
  53. // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
  54. })
  55. }
  56. })
  57. }
  58. function addQueryParams(url) {
  59. if (url) {
  60. //获取用户id
  61. let userInfo = localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')) : null
  62. if(userInfo){
  63. url += `?vid=${userInfo.id}`
  64. }
  65. }
  66. return url
  67. }
  68. export default share