import type { Ref } from 'vue';
|
import { ref } from 'vue';
|
import { GetSystemGlobalConfig } from '../api/system';
|
import { SERVE_URL, SSE_URL } from '../constants';
|
import { accessSessionKey, userInfoKey } from '../utils/request';
|
import { SSEClient } from '../utils/sse/SSEClient';
|
import { Local } from '../utils/storage';
|
import type { SystemGlobalConfig } from '../views/types';
|
import { ElLoadingService, ElMessage } from 'element-plus';
|
import { handleAfterLogin } from '../layout/component/login/login';
|
import { isShowLogin } from './chatRoom';
|
import { PostLogin, userBindingWechat } from '../api/ai/user';
|
/**
|
* 连接消息同步服务
|
* @returns
|
*/
|
const connectMsgSyncService = () => {
|
if (!Local.get(accessSessionKey)) return;
|
if (!SSE_URL) return;
|
// 创建实例
|
const sseClient = new SSEClient(
|
// `https://widev.cpolar.top/sse/chat/connect_broadcast_chat`,
|
// `${MAIN_URL}events`
|
SSE_URL
|
);
|
sseClient.connect({
|
websessionid: Local.get(accessSessionKey),
|
});
|
return sseClient;
|
};
|
|
export const sseClient = connectMsgSyncService();
|
|
/** @description 系统全局配置 */
|
export const systemGlobalConfig: Ref<SystemGlobalConfig> = ref({} as SystemGlobalConfig);
|
|
/**
|
* 获取系统全局配置
|
* @returns
|
*/
|
export const getSystemGlobalConfig = async () => {
|
const res = await GetSystemGlobalConfig({
|
config_key_list: ['ui.project_city'].join(','),
|
});
|
systemGlobalConfig.value = res.values ?? {};
|
};
|
|
const resolveWechatInfo = () => {
|
const url = window.location.href;
|
const hashParams = url.split('#')[1];
|
if (!hashParams) return;
|
|
const [path, queryString] = hashParams.split('?');
|
if (!queryString) return;
|
|
const params = new URLSearchParams(queryString);
|
const wxcode = params.get('wxcode');
|
const isLogin = params.get('isWxLogin');
|
if (!wxcode) return;
|
|
return { wxcode, isLogin };
|
};
|
|
export const checkWechatLogin = async (wxcode: string) => {
|
isShowLogin.value = false;
|
const loadingInstance = ElLoadingService({
|
// text: '加载中...',
|
target: '.layout-parent',
|
});
|
const res = await PostLogin({
|
weixin_code: wxcode,
|
}).finally(() => {
|
loadingInstance.close();
|
});
|
if (res?.json_ok) {
|
handleAfterLogin(res, false);
|
|
// 使用新地址替换当前页面,移除微信登录参数
|
// const newUrl = window.location.href.split('?')[0];
|
// window.history.replaceState({}, '', SERVE_URL);
|
window.location.href = SERVE_URL;
|
|
// window.location.reload();
|
} else {
|
ElMessage.error(res?.json_msg ?? '登录失败,请检查是否已绑定微信');
|
isShowLogin.value = true;
|
setTimeout(() => {
|
window.location.href = SERVE_URL;
|
}, 2500);
|
}
|
};
|
|
export const handleBindWechat = async (wxcode: string) => {
|
const userInfo = Local.get(userInfoKey);
|
const loadingInstance = ElLoadingService({
|
// text: '加载中...',
|
target: '.layout-parent',
|
});
|
const res = await userBindingWechat({
|
weixin_code: wxcode,
|
user_name: userInfo.userName,
|
}).finally(() => {
|
loadingInstance.close();
|
});
|
if (res?.json_ok) {
|
ElMessage.success('绑定成功');
|
|
setTimeout(() => {
|
window.location.href = SERVE_URL;
|
}, 700);
|
} else {
|
ElMessage.error(res?.json_msg ?? '绑定失败');
|
setTimeout(() => {
|
window.location.href = SERVE_URL;
|
}, 2000);
|
}
|
};
|
|
export const checkWxOperate = () => {
|
const result = resolveWechatInfo();
|
if (!result) return;
|
|
if (result.isLogin === 'Y') {
|
checkWechatLogin(result.wxcode);
|
} else {
|
handleBindWechat(result.wxcode);
|
}
|
};
|