wujingjing
2025-04-09 dd58c1d3a27ba48a5df050aab7c586bb9b988914
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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';
import emitter from '../utils/mitt';
/**
 * 连接消息同步服务
 * @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;
    Local.set('isWechatLogin', true);
    const loadingInstance = ElLoadingService({
        // text: '加载中...',
        target: '.layout-parent',
    });
    const res = await PostLogin({
        weixin_code: wxcode,
    }).finally(() => {
        loadingInstance.close();
    });
    if (res?.json_ok) {
        handleAfterLogin(res, false);
        Local.remove('isWechatLogin');
 
        // 使用新地址替换当前页面,移除微信登录参数
        // const newUrl = window.location.href.split('?')[0];
        // window.history.replaceState({}, '', SERVE_URL);
        window.location.href = SERVE_URL;
 
        // window.location.reload();
    } else {
        // isShowLogin.value = true;
        Local.set('wechatLoginMsgInfo', {
            type: 'error',
            value: res?.json_msg ?? '登录失败,请检查是否已绑定微信',
        });
        window.location.href = SERVE_URL;
    }
};
 
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('绑定成功');
        const userInfo = Local.get(userInfoKey);
        Local.set(userInfoKey, {
            ...userInfo,
            isBindWechat: true,
            wechatNickname:res.json_url
        });
        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);
    }
};
 
 
/**
 * 返回一个对象,给 parent 调用
 * 此对象包含多个函数
 * @returns
 */
const childCallObj = {
    /** @description 测试函数 */
    test: (msg: string) => {
        console.log('test 函数来自 child', msg);
    },
    execute: (obj: any) => {
        console.log('execute 函数来自 child', obj);
    },
};
export class ParentRegister {
    static notify = null;
    static setRegisterFunc() {
        if (!window.parent) return;
        (window as any).registerNotifyFunction = (notifyFunc: any) => {
            ParentRegister.notify = notifyFunc;
            return childCallObj;
        };
    }
 
    static updateChildCallObj(key:string,callback:Function) {
        childCallObj[key] = callback;
    }
}
 
 
window.callObj = childCallObj;