index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import querystring from "querystring";
  2. import settings from "@/settings"
  3. export const getAuthen = function () {
  4. const search = window.location.search
  5. /**
  6. * 这一段作用是先跳转网页后再跳转
  7. **/
  8. if (search !== '') {
  9. const search = querystring.parse(window.location.search.replace("?", ''))
  10. let appIdPromise
  11. // 有stationId
  12. if (!!search.stationId) {
  13. $nuxt.$store.commit("authen/setStationId", search.stationId)
  14. } else {
  15. alert('你没有传递站点参数');
  16. }
  17. // 有appId
  18. if (!!search.appId) {
  19. $nuxt.$store.commit("authen/setAppId", search.appId)
  20. appIdPromise = Promise.resolve();
  21. } else {
  22. appIdPromise = $nuxt.$store.dispatch('authen/getAppId', search.stationId).then((res) => {
  23. $nuxt.$store.commit("authen/setStationId", res.stationId)
  24. })
  25. }
  26. // 拿到当前地址
  27. const path = window.location.pathname === "/authen" ? "/" : window.location.pathname
  28. // 替换为自己的标识符号
  29. const searchStr = window.location.search.replace("?", ".").replace(/&/g, "-").replace(/=/g, "_")
  30. appIdPromise.then(() => {
  31. window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + $nuxt.$store.getters["authen/appId"] + "&redirect_uri=" + settings.gotoUrl + "%2fauthen&response_type=code&scope=snsapi_userinfo&state=" + path + searchStr + "#wechat_redirect"
  32. })
  33. } else {
  34. alert("请传递站点参数")
  35. }
  36. }