/**
 * dcuc,dids,dssoac登录
 * @Author: huangjq
 * @Date:   2019-10-16
 * @Project jz
 */
import store from "@/store";

const NO_LOGIN_STATUS = {
    "9": "登出成功",
    "10": "未登录",
    "11": "登录超时",
    "13": "登出"
};
class DCISLogin  {
   
    judgeIsLogin(response) {}

    logout(pageObj) {
        pageObj.$store.dispatch("Logout").then(() => {
            pageObj.$router.push(`/login?redirect=${pageObj.$route.fullPath}`);
            location.reload(); // In order to re-instantiate the vue-router object to avoid bugs
        });
    }

    beforeRequest(config) {
        return config;
    }

    afterResponseSuccess(response) {
        const res = response.data;
        if (res.statusCode === "401") {
            window.location.href = res.link;
        }
        if (NO_LOGIN_STATUS[res.statusCode]) {
            if (res.link) {
                let link = this._getReplacedLink(res);
                window.location.href = link;
                return Promise.reject({ link });
            } else {
                //dssoac登录方式
                store.dispatch("FedLogout").then(() => {
                    if (!response.config.url.endsWith("/api/user/info")) {
                        location.reload(); // 为了重新实例化vue-router对象 避免bug
                    }
                });
            }
        } else {
            return response;
        }
    }

    afterResponseFailed(error) {
        return error;
    }

    //获取替换关键字后的字符串
    _getReplacedLink(res) {
        if (res.loginType === "dids") {
            res.link = res.link.replace(
                "forwardUrl=",
                "forwardUrl=" + encodeURIComponent(encodeURIComponent(window.location.href))
            );
        } else if (res.loginType === "dcuc") {
            res.link = res.link.replace(
                "forwardUrl=",
                "forwardUrl=" + encodeURIComponent(window.location.href)
            );
        }
        return res.link;
    }
}

export default DCISLogin;
