import querystring from "querystring";
import settings from "@/settings"

export const getAuthen = function () {
  
  const search = window.location.search
 /**
 * 这一段作用是先挑战网页后再跳转
 **/
  if (search !== '') {
    const search = querystring.parse(window.location.search.replace("?", ''))
    let appIdPromise
    if (!!search.stationId) {
      $nuxt.$store.commit("authen/setStationId", search.stationId)
    } else {
      alert('你没有传递站点参数');
    }

    if (!!search.appId) {
      $nuxt.$store.commit("authen/setAppId", search.appId)
      appIdPromise = Promise.resolve();
    } else {
      appIdPromise = $nuxt.$store.dispatch('authen/getAppId', search.stationId).then((res) => {
        $nuxt.$store.commit("authen/setStationId", res.stationId)
      })
    }

    const path = window.location.pathname === "/authen" ? "/" : window.location.pathname
    const searchStr = window.location.search.replace("?", ".").replace(/&/g, "-").replace(/=/g, "_")
    appIdPromise.then(() => {
      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"
    })
  } else {
    alert("请传递站点参数")
  }
}
