wx.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import wx from "weixin-js-sdk";
  2. import axios from 'axios';
  3. export function getWxCode(title, image, url, desc) {
  4. let params = {
  5. link: location.href,
  6. url: location.href
  7. }
  8. let postApi = '/test/face/wxapi/wx/signature'
  9. if (process.env.NODE_ENV !== 'development') {
  10. postApi = 'https://operationapi.mige.tv/wxapi/wx/signature'
  11. }
  12. axios.post(postApi, params)
  13. .then(res => {
  14. console.log(res.data.data)
  15. wx.config({
  16. debug: false, // 开启调试模式
  17. appId: res.data.data.appId, // 必填,公众号的唯一标识
  18. timestamp: res.data.data.timestamp, // 必填,生成签名的时间戳
  19. nonceStr: res.data.data.nonceStr, // 必填,生成签名的随机串
  20. signature: res.data.data.signature, // 必填,签名
  21. jsApiList: [
  22. "updateTimelineShareData",
  23. "updateAppMessageShareData",
  24. "onMenuShareAppMessage",
  25. "onMenuShareTimeline"
  26. ] // 必填,需要使用的JS接口列表
  27. });
  28. wx.ready(function () { //需在用户可能点击分享按钮前就先调用
  29. wx.updateAppMessageShareData({
  30. title: title, // 分享标题
  31. desc: desc, // 分享描述
  32. link: url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  33. imgUrl: image, // 分享图标
  34. success: function () {
  35. console.log("成功")
  36. },
  37. fail: function () {
  38. console.log('取消')
  39. }
  40. })
  41. wx.onMenuShareTimeline({
  42. title: title, // 分享标题
  43. desc: desc, // 分享描述
  44. link: url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  45. imgUrl: image, // 分享图标
  46. success: function () {
  47. console.log("成功")
  48. },
  49. fail: function () {
  50. console.log('取消')
  51. }
  52. });
  53. });
  54. })
  55. .catch(err => {
  56. console.error(err);
  57. })
  58. }