123456789101112131415161718192021222324252627282930
  1. /**
  2. * 获取 code
  3. * @returns
  4. */
  5. export function getCode () {
  6. const matched = /[?&]*code=([^&]+)/.exec(location.search)
  7. if (matched) {
  8. return decodeURIComponent(matched[1])
  9. }
  10. }
  11. /**
  12. * 跳转授权页面
  13. */
  14. export function getRedirectURL (force, code) {
  15. if (import.meta.env.DEV) return;
  16. const originCode = localStorage.getItem('wxcode');
  17. const wxfirst = localStorage.getItem('wxfirst') === null;
  18. localStorage.setItem('wxcode', code)
  19. localStorage.setItem('wxfirst', 'not');
  20. if (force || wxfirst || !code || code === originCode) {
  21. const origin = location.origin + location.pathname
  22. const searchStr = location.search.replace(/code\=[^&]+/, '').replace(/state\=[^&]+/, '').replace(/^&+/, '').replace(/\?+$/, '')
  23. const local = encodeURIComponent(origin + searchStr + location.hash)
  24. return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd3bab568bc42d1de&redirect_uri=${local}&response_type=code&scope=snsapi_base&state=123#wechat_redirect`
  25. }
  26. }