app.js 3.9 KB
import { Http } from "glsx-common";
import { Notify } from "vant";
import { ssoApi } from "@/api";
import reportTrackFunMixin from "@/mixins/reportTrackFunMixin";
import { util } from "@/utils";

const state = {
	showWindow: false,
	lastPath: "",
};
const IS_PROD = ["production", "prod"].includes(process.env.VUE_APP_ENV);

const mutations = {
	SET_PAGE_META(state, meta) {
		state.pageMeta = meta;
	},
	SET_LAST_PATH(state, path) {
		state.lastPath = path;
	},
};
const actions = {
	setPageMeta({ commit }, meta) {
		document.title = meta.title || "团队工作台";
		commit("SET_PAGE_META", meta);
	},
	registerHttp({}, sessionId = "") {
		const { scrmRequest, djRequest } = window.$fetchConfig;
		window.$SCRM_HTTP = new Http({ ...scrmRequest, alert: Notify });
		window.$DJ_HTTP = new Http({ ...djRequest, alert: Notify, headers: { token: sessionId } });
	},
	async login({ dispatch }) {
		console.log("login");
		// return
		try {
			const wwCode = getParams("code");
			const { corpId, agentId } = util.getLocalStorage("ww_storage_info") || {};
			// const paramsPhone = util.getLocalStorage('ww_params_phone')
			const { loginUrl } = window.$fetchConfig;
			if (wwCode) {
				const { code, data, message } = await ssoApi.login({
					code: wwCode,
					corpId,
					getUserInfoFlag: false,
				});
				if (code == 0) {
					if (data.loginList && data.loginList.length) {
						const [item] = data.loginList;
						if (item.loginList && item.loginList.length) {
							const [userInfo] = item.loginList;
							// const { phone } = userInfo
							// if (paramsPhone == phone) {
							// const res = await ssoApi.loginByPhone({
							//   phone,
							// })
							// if (res) {
							let route = state.lastPath || "";
							const [jumpUrl] = window.location.href.split("/#/");
							route = route.replace(".", "/");
							util.setLocalStorage("ww_userInfo", userInfo);
							// util.setSessionId(res.sessionId)
							// dispatch('registerHttp', res.sessionId)
							reportTrackFunMixin.methods.setReportTrackData(0, "supervisor_h5_login");
							state.showWindow = true;
							console.log(jumpUrl);
							window.location.replace(`${jumpUrl}/#/${route}`);
							// } else throw ''
							// } else throw '手机号不一致'
						} else {
							throw "";
						}
					} else {
						throw "";
					}
				} else {
					throw message;
				}
			} else {
				window.location.href = `${loginUrl}?redirect_uri=${encodeURIComponent(window.location.href)}&corpId=${corpId}&agentId=${agentId}&backLoginCode=BACKCODE`;
			}
		} catch (error) {
			if (!error)
				error = "登录失败或无权限";
			if (error)
				Notify(typeof error === "string" ? error : error.toString());
		}
	},
	navigation({ dispatch }, to) {
		const { corpId, agentId, phone, name } = getParams();
		const userInfo = util.getLocalStorage("ww_userInfo");
		if (phone)
			util.setLocalStorage("ww_params_phone", phone);
		if (name)
			util.setLocalStorage("ww_params_name", name);
		dispatch("registerHttp");
		/**
		 * 开发环境,跳过企微登陆
		 */
		if (!IS_PROD) {
			util.setLocalStorage("ww_userInfo", { phone: "13012345678" || "" });
			// reportTrackFunMixin.methods.setReportTrackData(0, 'supervisor_h5_login')
			state.showWindow = true;
			return;
		}
		if (corpId) {
			const ww_storage_info = {};
			ww_storage_info.corpId = corpId;
			if (agentId && agentId != "undefined")
				ww_storage_info.agentId = agentId;
			util.setLocalStorage("ww_storage_info", ww_storage_info);
		}
		if (userInfo) {
			state.showWindow = true;
		} else {
			util.showLoading();
			dispatch("login");
		}
	},
};
function getParams(name) {
	const [url, paramsList] = window.location.href.split("?");
	if (!paramsList)
		return name ? null : {};
	const param = paramsList.split("&");
	const obj = {};
	param.map((e) => {
		const [key, value] = e.split("=");
		obj[key] = value ? decodeURIComponent(value) : value;
	});
	return name ? obj[name] : obj;
}
export default {
	state,
	mutations,
	actions,
};