index.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  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. if (!!search.stationId) {
  12. $nuxt.$store.commit("authen/setStationId", search.stationId)
  13. } else {
  14. alert('你没有传递站点参数');
  15. }
  16. if (!!search.appId) {
  17. $nuxt.$store.commit("authen/setAppId", search.appId)
  18. appIdPromise = Promise.resolve();
  19. } else {
  20. appIdPromise = $nuxt.$store.dispatch('authen/getAppId', search.stationId).then((res) => {
  21. $nuxt.$store.commit("authen/setStationId", res.stationId)
  22. })
  23. }
  24. const path = window.location.pathname === "/authen" ? "/" : window.location.pathname
  25. const searchStr = window.location.search.replace("?", ".").replace(/&/g, "-").replace(/=/g, "_")
  26. appIdPromise.then(() => {
  27. window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + $nuxt.$store.getters["authen/appId"] + "&redirect_uri=" + settings.url + "%2fauthen&response_type=code&scope=snsapi_userinfo&state=" + path + searchStr + "#wechat_redirect"
  28. })
  29. } else {
  30. alert("请传递站点参数")
  31. }
  32. }