123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. let APPID = '';
  2. const isDev = false
  3. const API_BASE="http://127.0.0.1:9087"
  4. /**
  5. * 获取 code
  6. * @returns
  7. */
  8. export function getCode () {
  9. if (isDev) return '123';
  10. const matched = /[?&]*code=([^&]+)/.exec(location.search)
  11. if (matched) {
  12. return decodeURIComponent(matched[1])
  13. }
  14. }
  15. /**
  16. * 跳转授权页面
  17. */
  18. export function redirect () {
  19. if (isDev) return;
  20. const appid = APPID || 'wxd3bab568bc42d1de';
  21. const local = encodeURIComponent(location.origin + location.pathname)
  22. const scope = 'snsapi_userinfo'; // snsapi_base
  23. const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${local}&response_type=code&scope=${scope}&state=123#wechat_redirect`
  24. window.location.href = url;
  25. }
  26. function wxsdk() {
  27. // API_BASE 来源 public/config.js
  28. const apiBase = API_BASE // import.meta.env.VITE_APP_API_BASE
  29. // 分享接口
  30. const shareAPIs = [
  31. // 自定义“分享给朋友”及“分享到QQ”按钮的分享内容(1.4.0)
  32. 'updateAppMessageShareData',
  33. // 自定义“分享到朋友圈”及“分享到 QQ 空间”按钮的分享内容(1.4.0)
  34. 'updateTimelineShareData',
  35. // 获取“分享到朋友圈”按钮点击状态及自定义分享内容接口(即将废弃)
  36. 'onMenuShareTimeline',
  37. // 获取“分享给朋友”按钮点击状态及自定义分享内容接口(即将废弃)
  38. 'onMenuShareAppMessage',
  39. // 获取“分享到QQ”按钮点击状态及自定义分享内容接口(即将废弃)
  40. 'onMenuShareQQ',
  41. // 获取“分享到腾讯微博”按钮点击状态及自定义分享内容接口
  42. 'onMenuShareWeibo',
  43. // 获取“分享到 QQ 空间”按钮点击状态及自定义分享内容接口(即将废弃)
  44. 'onMenuShareQZone',
  45. ]
  46. let inited = false;
  47. function init() {
  48. if (isDev) return;
  49. const url = window.location.href;
  50. // 请求后台接口
  51. fetch(`${apiBase}/api/wx/jsapi?url=${encodeURIComponent(url)}`).then(res => res.json()).then((res) => {
  52. if (res.code === 1000) {
  53. const data = res.data;
  54. APPID = data.appId;
  55. wx.config({
  56. debug: isDev, // 开启调试模式,调用的所有 api 的返回值会在客户端 alert 出来,若要查看传入的参数,可以在 pc 端打开,参数信息会通过 log 打出,仅在 pc 端时才会打印。
  57. appId: data.appId, // 必填,公众号的唯一标识
  58. timestamp: data.timestamp, // 必填,生成签名的时间戳
  59. nonceStr: data.nonceStr, // 必填,生成签名的随机串
  60. signature: data.signature,// 必填,签名
  61. jsApiList: [
  62. ...shareAPIs,
  63. 'openLocation',
  64. ] // 必填,需要使用的 JS 接口列表
  65. });
  66. }
  67. })
  68. }
  69. function share(params = {}) {
  70. if (isDev) return;
  71. if (!inited) {
  72. init();
  73. }
  74. wx.ready(() => {
  75. const shareData = {
  76. ...shareOptTpl,
  77. ...params
  78. }
  79. console.log('---分享参数-->', shareData);
  80. for (let api of shareAPIs) {
  81. wx[api]({
  82. ...shareData,
  83. success: () => {
  84. console.log('分享成功: ', api)
  85. },
  86. });
  87. }
  88. });
  89. wx.error(function(res){
  90. console.error('wx js-sdk error');
  91. console.error(res);
  92. console.error('<==============');
  93. });
  94. }
  95. return {
  96. share,
  97. getCode,
  98. redirect,
  99. }
  100. }
  101. export default wxsdk;