wx.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import request from "./request"
  2. const jsApiList = [
  3. 'updateAppMessageShareData',
  4. 'updateTimelineShareData',
  5. 'onMenuShareTimeline',
  6. 'onMenuShareAppMessage',
  7. 'onMenuShareQQ',
  8. 'onMenuShareWeibo',
  9. 'onMenuShareQZone'
  10. ]
  11. function initSDK (url) {
  12. request(`https://api.h5.njyunzhi.com/mp/jssdk?url=${encodeURIComponent(url)}`).then((res) => {
  13. window.wx.config({
  14. debug: process.env.NODE_ENV === 'development', // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  15. appId: res.appId, // 必填,公众号的唯一标识
  16. timestamp: res.timestamp, // 必填,生成签名的时间戳
  17. nonceStr: res.nonceStr, // 必填,生成签名的随机串
  18. signature: res.signature, // 必填,签名
  19. jsApiList // 必填,需要使用的JS接口列表
  20. })
  21. })
  22. }
  23. /**
  24. * 分享
  25. * @param {*} opt
  26. */
  27. export function share (opt) {
  28. const { origin, pathname } = window.location
  29. const defaultLink = origin + pathname
  30. const defaultImg = `${origin}${pathname}images/share.jpg`
  31. const link = window.location.href // opt.link || defaultLink
  32. const imgUrl = opt.imgUrl || defaultImg
  33. initSDK(link)
  34. window.wx.ready(function () {
  35. jsApiList.map((apiName) => {
  36. const api = window.wx[apiName]
  37. api({
  38. title: opt.title || '', // 分享标题
  39. link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  40. imgUrl, // 分享图标
  41. desc: opt.desc || ''
  42. })
  43. })
  44. })
  45. }
  46. /**
  47. * 获取 code
  48. * @returns
  49. */
  50. function getCode () {
  51. const matched = /[?&]*code=([^&]+)/.exec(location.search)
  52. if (matched) {
  53. return decodeURIComponent(matched[1])
  54. }
  55. }
  56. /**
  57. * 跳转授权页面
  58. */
  59. export function redirect (force) {
  60. if (process.env.NODE_ENV === 'development') return;
  61. const originCode = localStorage.getItem('wxcode');
  62. const queryCode = getCode();
  63. localStorage.setItem('wxcode', queryCode)
  64. if (!queryCode || queryCode === originCode || force) {
  65. const local = encodeURIComponent(location.origin + location.pathname)
  66. const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd3bab568bc42d1de&redirect_uri=${local}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`
  67. window.location.href = url
  68. }
  69. }
  70. /**
  71. * 获取 openid
  72. * @returns
  73. */
  74. export function getOpenId () {
  75. if (process.env.NODE_ENV === 'development') return Promise.resolve('123');
  76. const code = getCode()
  77. if (!code) {
  78. return Promise.reject("获取用户信息失败, 请刷新重试")
  79. }
  80. return request(`https://api.h5.njyunzhi.com/mp/openid?code=${encodeURIComponent(code)}`)
  81. }
  82. /**
  83. * 获取 openid
  84. * @returns
  85. */
  86. export function getUserInfo () {
  87. if (process.env.NODE_ENV === 'development') return Promise.resolve({ openid: '123' });
  88. const code = getCode()
  89. if (!code) {
  90. return Promise.reject("获取用户信息失败, 请刷新重试")
  91. }
  92. return request(`https://api.h5.njyunzhi.com/mp/userinfo?code=${encodeURIComponent(code)}`)
  93. }