Compare commits
2 Commits
39ac674871
...
bf6106184d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf6106184d | ||
|
|
0ec3e570af |
@ -17,7 +17,7 @@ import { triggerPromise } from '@/utils';
|
|||||||
|
|
||||||
export default function useGuard() {
|
export default function useGuard() {
|
||||||
const { account } = storeToRefs(useAccount());
|
const { account } = storeToRefs(useAccount());
|
||||||
const { login } = useAccount();
|
const { login, hasValidMiniappSession } = useAccount();
|
||||||
const { resolve, promise } = triggerPromise();
|
const { resolve, promise } = triggerPromise();
|
||||||
|
|
||||||
const onShowEvents = ref([]);
|
const onShowEvents = ref([]);
|
||||||
@ -54,7 +54,7 @@ export default function useGuard() {
|
|||||||
const page = pages[pages.length - 1];
|
const page = pages[pages.length - 1];
|
||||||
const route = routes.find(i => page && i.path === page.route);
|
const route = routes.find(i => page && i.path === page.route);
|
||||||
const requireLogin = route && route.meta && route.meta.login;
|
const requireLogin = route && route.meta && route.meta.login;
|
||||||
if (requireLogin && !account.value) {
|
if (requireLogin && (!account.value || !hasValidMiniappSession())) {
|
||||||
const res = await login()
|
const res = await login()
|
||||||
if (res) {
|
if (res) {
|
||||||
resolve()
|
resolve()
|
||||||
|
|||||||
@ -11,7 +11,7 @@ import api from "@/utils/api";
|
|||||||
import { set } from "@/utils/cache";
|
import { set } from "@/utils/cache";
|
||||||
import { toast } from "@/utils/widget";
|
import { toast } from "@/utils/widget";
|
||||||
import useAccountStore from "@/store/account";
|
import useAccountStore from "@/store/account";
|
||||||
import { normalizeCorpId, normalizeCorpIdFields } from "@/utils/api-base-config";
|
import { isSh6CorpId, normalizeCorpId, normalizeCorpIdFields, resolveApiContext } from "@/utils/api-base-config";
|
||||||
|
|
||||||
const env = __VITE_ENV__;
|
const env = __VITE_ENV__;
|
||||||
const appid = env.MP_WX_APP_ID;
|
const appid = env.MP_WX_APP_ID;
|
||||||
@ -81,7 +81,12 @@ function getAiConsultEntryUrl({ teamId, corpId, qrid }) {
|
|||||||
|
|
||||||
async function changeTeam({ teamId, corpId, corpUserId, externalUserId, qrid, referenceCustomerId, type }) {
|
async function changeTeam({ teamId, corpId, corpUserId, externalUserId, qrid, referenceCustomerId, type }) {
|
||||||
const normalizedCorpId = normalizeCorpId(corpId);
|
const normalizedCorpId = normalizeCorpId(corpId);
|
||||||
|
const requiresSession = isSh6CorpId(normalizedCorpId);
|
||||||
const redirectUrl = type === 'aiConsult' ? getAiConsultEntryUrl({ teamId, corpId: normalizedCorpId, qrid }) : '';
|
const redirectUrl = type === 'aiConsult' ? getAiConsultEntryUrl({ teamId, corpId: normalizedCorpId, qrid }) : '';
|
||||||
|
if (requiresSession) {
|
||||||
|
resolveApiContext(normalizedCorpId);
|
||||||
|
await login('', { forceVerify: true });
|
||||||
|
}
|
||||||
loading.value = true;
|
loading.value = true;
|
||||||
const res = await api("getTeamData", { teamId, corpId: normalizedCorpId, withCorpName: true });
|
const res = await api("getTeamData", { teamId, corpId: normalizedCorpId, withCorpName: true });
|
||||||
loading.value = false;
|
loading.value = false;
|
||||||
@ -99,7 +104,7 @@ async function changeTeam({ teamId, corpId, corpUserId, externalUserId, qrid, re
|
|||||||
qrid,
|
qrid,
|
||||||
referenceCustomerId: referenceCustomerId || ''
|
referenceCustomerId: referenceCustomerId || ''
|
||||||
});
|
});
|
||||||
await login('', { forceVerify: true })
|
if (!requiresSession) await login('', { forceVerify: true })
|
||||||
if (account.value && account.value.mobile) {
|
if (account.value && account.value.mobile) {
|
||||||
bindTeam({ corpUserId, externalUserId, redirectUrl })
|
bindTeam({ corpUserId, externalUserId, redirectUrl })
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -4,7 +4,8 @@ import api from '@/utils/api';
|
|||||||
import { toast } from '@/utils/widget';
|
import { toast } from '@/utils/widget';
|
||||||
import { initGlobalTIM, globalTimChatManager } from "@/utils/tim-chat.js";
|
import { initGlobalTIM, globalTimChatManager } from "@/utils/tim-chat.js";
|
||||||
import { globalUnreadListenerManager } from "@/utils/global-unread-listener.js";
|
import { globalUnreadListenerManager } from "@/utils/global-unread-listener.js";
|
||||||
import { normalizeCorpId, normalizeCorpIdFields } from "@/utils/api-base-config";
|
import { isSh6CorpId, normalizeCorpId, normalizeCorpIdFields, resolveApiContext } from "@/utils/api-base-config";
|
||||||
|
import { setMiniappSessionRefresher } from "@/utils/http";
|
||||||
const env = __VITE_ENV__;
|
const env = __VITE_ENV__;
|
||||||
|
|
||||||
export default defineStore("accountStore", () => {
|
export default defineStore("accountStore", () => {
|
||||||
@ -18,9 +19,16 @@ export default defineStore("accountStore", () => {
|
|||||||
const teams = ref([]);
|
const teams = ref([]);
|
||||||
const teamsPromise = ref(null);
|
const teamsPromise = ref(null);
|
||||||
|
|
||||||
|
function hasValidMiniappSession() {
|
||||||
|
if (!isSh6CorpId(resolveApiContext().corpId)) return true;
|
||||||
|
const token = uni.getStorageSync("miniappSessionToken");
|
||||||
|
const expiresAt = Number(uni.getStorageSync("miniappSessionExpiresAt"));
|
||||||
|
return Boolean(token && (!expiresAt || expiresAt > Date.now() + 30 * 1000));
|
||||||
|
}
|
||||||
|
|
||||||
async function login(phoneCode = '', options = {}) {
|
async function login(phoneCode = '', options = {}) {
|
||||||
if (loading.value) return;
|
if (loading.value) return;
|
||||||
if (account.value && account.value.mobile && !options.forceVerify) {
|
if (account.value && account.value.mobile && !options.forceVerify && hasValidMiniappSession()) {
|
||||||
return account.value
|
return account.value
|
||||||
}
|
}
|
||||||
loading.value = true;
|
loading.value = true;
|
||||||
@ -38,7 +46,12 @@ export default defineStore("accountStore", () => {
|
|||||||
});
|
});
|
||||||
loading.value = false
|
loading.value = false
|
||||||
if (res.success && res.data) {
|
if (res.success && res.data) {
|
||||||
const normalizedAccount = normalizeCorpIdFields(res.data);
|
const { sessionToken, sessionExpiresAt, sessionKey, ...safeAccount } = res.data;
|
||||||
|
if (sessionToken) {
|
||||||
|
uni.setStorageSync("miniappSessionToken", sessionToken);
|
||||||
|
uni.setStorageSync("miniappSessionExpiresAt", sessionExpiresAt || 0);
|
||||||
|
}
|
||||||
|
const normalizedAccount = normalizeCorpIdFields(safeAccount);
|
||||||
account.value = normalizedAccount;
|
account.value = normalizedAccount;
|
||||||
openid.value = normalizedAccount.openid;
|
openid.value = normalizedAccount.openid;
|
||||||
|
|
||||||
@ -121,6 +134,8 @@ export default defineStore("accountStore", () => {
|
|||||||
|
|
||||||
// 清空账户信息
|
// 清空账户信息
|
||||||
account.value = null;
|
account.value = null;
|
||||||
|
uni.removeStorageSync("miniappSessionToken");
|
||||||
|
uni.removeStorageSync("miniappSessionExpiresAt");
|
||||||
openid.value = "";
|
openid.value = "";
|
||||||
isIMInitialized.value = false;
|
isIMInitialized.value = false;
|
||||||
|
|
||||||
@ -177,5 +192,7 @@ export default defineStore("accountStore", () => {
|
|||||||
}
|
}
|
||||||
}, { immediate: true })
|
}, { immediate: true })
|
||||||
|
|
||||||
return { account, teams, login, initIMAfterLogin, logout, openid, isIMInitialized, externalUserId, getExternalUserId, getTeams }
|
setMiniappSessionRefresher(() => login('', { forceVerify: true, silent: true }));
|
||||||
|
|
||||||
|
return { account, teams, login, hasValidMiniappSession, initIMAfterLogin, logout, openid, isIMInitialized, externalUserId, getExternalUserId, getTeams }
|
||||||
})
|
})
|
||||||
|
|||||||
@ -12,6 +12,10 @@ export const CORP_ID_ALIAS_MAP = {
|
|||||||
"wpLgjyawAAeRkCPQMp9-z5q-xEzK64nA": "wwa54dfba0b5441ef1",
|
"wpLgjyawAAeRkCPQMp9-z5q-xEzK64nA": "wwa54dfba0b5441ef1",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const SH6_CORP_IDS = new Set([
|
||||||
|
"wpLgjyawAAxKtBH5GVT5DVhLefg246Ag",
|
||||||
|
]);
|
||||||
|
|
||||||
function normalizeBaseUrl(url) {
|
function normalizeBaseUrl(url) {
|
||||||
return String(url || "").replace(/\/+$/, "");
|
return String(url || "").replace(/\/+$/, "");
|
||||||
}
|
}
|
||||||
@ -25,6 +29,10 @@ export function normalizeCorpId(corpId) {
|
|||||||
return CORP_ID_ALIAS_MAP[normalizedCorpId] || normalizedCorpId;
|
return CORP_ID_ALIAS_MAP[normalizedCorpId] || normalizedCorpId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isSh6CorpId(corpId) {
|
||||||
|
return SH6_CORP_IDS.has(normalizeCorpId(corpId));
|
||||||
|
}
|
||||||
|
|
||||||
export function normalizeCorpIdFields(value) {
|
export function normalizeCorpIdFields(value) {
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
return value.map((item) => normalizeCorpIdFields(item));
|
return value.map((item) => normalizeCorpIdFields(item));
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { loading, hideLoading } from "./widget";
|
import { loading, hideLoading } from "./widget";
|
||||||
import { buildApiUrl, getDefaultUploadUrl, normalizeCorpId } from "./api-base-config";
|
import { buildApiUrl, getDefaultUploadUrl, isSh6CorpId, normalizeCorpId, resolveApiContext } from "./api-base-config";
|
||||||
|
|
||||||
const defaultOptions = {
|
const defaultOptions = {
|
||||||
header: {
|
header: {
|
||||||
@ -9,6 +9,25 @@ const defaultOptions = {
|
|||||||
timeout: 1000 * 60, // 请求超时设置为60秒
|
timeout: 1000 * 60, // 请求超时设置为60秒
|
||||||
};
|
};
|
||||||
const tasks = [];
|
const tasks = [];
|
||||||
|
let miniappSessionRefresher = null;
|
||||||
|
let miniappSessionRefreshPromise = null;
|
||||||
|
|
||||||
|
export function setMiniappSessionRefresher(refresher) {
|
||||||
|
miniappSessionRefresher = typeof refresher === "function" ? refresher : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function refreshMiniappSession() {
|
||||||
|
if (!miniappSessionRefresher) return false;
|
||||||
|
if (!miniappSessionRefreshPromise) {
|
||||||
|
miniappSessionRefreshPromise = Promise.resolve()
|
||||||
|
.then(() => miniappSessionRefresher())
|
||||||
|
.finally(() => {
|
||||||
|
miniappSessionRefreshPromise = null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
await miniappSessionRefreshPromise;
|
||||||
|
return Boolean(uni.getStorageSync("miniappSessionToken"));
|
||||||
|
}
|
||||||
|
|
||||||
// 是否正在刷新 token
|
// 是否正在刷新 token
|
||||||
let isRefreshing = false;
|
let isRefreshing = false;
|
||||||
@ -94,27 +113,29 @@ async function refreshAccessToken() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const request = async (options = {}, showLoading = true) => {
|
const request = async (options = {}, showLoading = true) => {
|
||||||
|
const sessionRetried = options.__miniappSessionRetried === true;
|
||||||
|
const requestOptions = { ...options };
|
||||||
|
delete requestOptions.__miniappSessionRetried;
|
||||||
// 合并用户传入的配置和默认配置
|
// 合并用户传入的配置和默认配置
|
||||||
if (!options.data) options.data = {};
|
if (!requestOptions.data) requestOptions.data = {};
|
||||||
if (options.data.corpId) {
|
if (requestOptions.data.corpId) {
|
||||||
options.data = {
|
requestOptions.data = {
|
||||||
...options.data,
|
...requestOptions.data,
|
||||||
corpId: normalizeCorpId(options.data.corpId),
|
corpId: normalizeCorpId(requestOptions.data.corpId),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
const requestCorpId = requestOptions.data.corpId || resolveApiContext().corpId;
|
||||||
|
const requiresMiniappSession = isSh6CorpId(requestCorpId);
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
...defaultOptions,
|
...defaultOptions,
|
||||||
...options,
|
...requestOptions,
|
||||||
url: buildApiUrl(options.url, options.data.corpId),
|
url: buildApiUrl(requestOptions.url, requestOptions.data.corpId),
|
||||||
};
|
};
|
||||||
|
const sessionToken = uni.getStorageSync("miniappSessionToken");
|
||||||
// 添加 token 到请求头
|
if (requiresMiniappSession && sessionToken) {
|
||||||
// const accessToken = uni.getStorageSync("accessToken");
|
config.header = { ...(config.header || {}), "X-Miniapp-Session": sessionToken };
|
||||||
// if (accessToken) {
|
}
|
||||||
// if (!config.header) config.header = {};
|
|
||||||
// config.header.Authorization = `Bearer ${accessToken}`;
|
|
||||||
// }
|
|
||||||
|
|
||||||
const key = `${JSON.stringify(config)}_${Date.now()}`;
|
const key = `${JSON.stringify(config)}_${Date.now()}`;
|
||||||
if (showLoading) {
|
if (showLoading) {
|
||||||
@ -128,6 +149,17 @@ const request = async (options = {}, showLoading = true) => {
|
|||||||
removeTask(key)
|
removeTask(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (requiresMiniappSession && res.statusCode === 401) {
|
||||||
|
uni.removeStorageSync("miniappSessionToken");
|
||||||
|
uni.removeStorageSync("miniappSessionExpiresAt");
|
||||||
|
if (
|
||||||
|
!sessionRetried &&
|
||||||
|
requestOptions.data.type !== "wxAppLogin" &&
|
||||||
|
await refreshMiniappSession()
|
||||||
|
) {
|
||||||
|
return request({ ...requestOptions, __miniappSessionRetried: true }, showLoading);
|
||||||
|
}
|
||||||
|
}
|
||||||
// 如果返回 401,尝试刷新 token
|
// 如果返回 401,尝试刷新 token
|
||||||
// if (res.statusCode === 401) {
|
// if (res.statusCode === 401) {
|
||||||
// if (showLoading) {
|
// if (showLoading) {
|
||||||
@ -214,10 +246,15 @@ export function getFullPath(path, corpId) {
|
|||||||
|
|
||||||
export function upload(path, corpId) {
|
export function upload(path, corpId) {
|
||||||
const normalizedCorpId = normalizeCorpId(corpId);
|
const normalizedCorpId = normalizeCorpId(corpId);
|
||||||
|
const uploadCorpId = normalizedCorpId || resolveApiContext().corpId;
|
||||||
|
const sessionToken = uni.getStorageSync("miniappSessionToken");
|
||||||
const currentUploadUrl = buildApiUrl("/upload", normalizedCorpId);
|
const currentUploadUrl = buildApiUrl("/upload", normalizedCorpId);
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
uni.uploadFile({
|
uni.uploadFile({
|
||||||
url: currentUploadUrl,
|
url: currentUploadUrl,
|
||||||
|
header: isSh6CorpId(uploadCorpId) && sessionToken
|
||||||
|
? { "X-Miniapp-Session": sessionToken }
|
||||||
|
: {},
|
||||||
filePath: path,
|
filePath: path,
|
||||||
name: 'file',
|
name: 'file',
|
||||||
fileType: 'image',
|
fileType: 'image',
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user