wujingjing
2025-04-02 6ca24301fd3edf76359240ae5272e75b7a4db77f
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<template>
    <div class="pc-login" v-if="isShowLogin">
        <div class="items-center justify-center flex">
            <div class="login_box">
                <div class="sign_in">
                    <i class="ywifont ywicon-guanbi closes" @click="handleClose"></i>
                    <h1><span>登录 WI水务智能</span></h1>
                    <el-tabs v-model="state.activeLoginName" class="w-full mt-[24px]" @tab-change="handleTabChange">
                        <el-tab-pane label="账户密码登录" name="accountUser">
                            <el-form
                                ref="loginFormRef"
                                :model="state.loginForm"
                                :rules="loginRules"
                                class="demo-ruleForm mt-[24px] min-h-[140px]"
                                size="large"
                            >
                                <el-form-item label="账号" prop="account">
                                    <el-input v-model="state.loginForm.account" clearable v-focus />
                                </el-form-item>
                                <el-form-item label="密码" prop="pwd">
                                    <el-input v-model="state.loginForm.pwd" type="password" autocomplete="off" clearable />
                                </el-form-item>
                            </el-form>
                        </el-tab-pane>
                        <el-tab-pane label="手机号登录" name="phoneUser">
                            <el-form
                                ref="formPhoneRef"
                                :rules="loginPhoneRules"
                                :model="state.loginPhoneForm"
                                size="large"
                                class="mt-[24px] min-h-[140px]"
                            >
                                <el-form-item label="手机号" prop="phoneUser">
                                    <el-input v-model="state.loginPhoneForm.phoneUser" placeholder="请输入手机号码" clearable>
                                        <template #prepend>+86</template>
                                    </el-input>
                                </el-form-item>
                                <el-form-item prop="verifyCode" label="验证码">
                                    <el-input v-model="state.loginPhoneForm.verifyCode" placeholder="请输入四位验证码" maxlength="6" clearable>
                                        <template #append>
                                            <el-button type="primary" @click="handleSendVerifyCode" :disabled="hasSended">{{ sendCodeMsg }}</el-button>
                                        </template>
                                    </el-input>
                                </el-form-item>
                            </el-form>
                        </el-tab-pane>
                        <el-tab-pane label="微信登录" name="wechat">
                            <!-- <div id="wechat-login"></div> -->
                            <div id="wechat-login" class="flex justify-center items-center">
                                <div class="flex flex-col items-center justify-center">
                                    <iframe
                                        ref="wechatQrRef"
                                        sandbox="allow-top-navigation allow-scripts"
                                        style="width: 300px; height: 213px; overflow: hidden"
                                        frameborder="0"
                                    ></iframe>
                                    <span class="relative top-[-10px]" :class="{ 'text-red-400': wechatTip.type === 'error' }">{{
                                        wechatTip.value
                                    }}</span>
                                </div>
                            </div>
                        </el-tab-pane>
                    </el-tabs>
 
                    <div class="mt-[24px]" v-if="state.activeLoginName !== 'wechat'">
                        <el-button type="primary" @click="onSubmit" class="set-login_btn">登录</el-button>
 
                        <!-- <el-button type="primary" @click="loginByWechat">微信登录</el-button> -->
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script setup lang="ts">
import type { FormInstance } from 'element-plus';
import { ElMessage } from 'element-plus';
import { computed, nextTick, reactive, ref, watch } from 'vue';
import { ACCOUNT_EXPIRE_DAY, handleAfterLogin, LOGIN_CLIENT, STORED_ACCOUNT_KEY } from './login';
import './wxLogin.js';
import { loginMessageUser, loginVerifyMessage, PostLogin } from '/@/api/ai/user';
import { SERVE_URL } from '/@/constants';
import { isShowLogin } from '/@/stores/chatRoom';
import { encrypt } from '/@/utils/cypto';
import { Local, LocalPlus } from '/@/utils/storage';
 
const handleClose = () => {
    isShowLogin.value = false;
};
 
const getWechatTipInfo = () => {
    const wechatLoginMsgInfo = Local.get('wechatLoginMsgInfo');
    if (wechatLoginMsgInfo) {
        Local.remove('wechatLoginMsgInfo');
        return wechatLoginMsgInfo;
    }
    return { type: 'info', value: '使用微信扫一扫登陆' };
};
 
const wechatTip = ref(getWechatTipInfo());
 
const resetWechatTip = () => {
    wechatTip.value = {
        type: 'info',
        value: '使用微信扫一扫登陆',
    };
};
 
const getWechartQrCode = () => {
    const state = (new Date().getTime() / 1000).toString();
    // JJJHHH 表示#号
    const url = `${SERVE_URL}JJJHHH/home?isWxLogin=Y`;
 
    const base64 =
        'LmltcG93ZXJCb3ggLnRpdGxlIHtkaXNwbGF5Om5vbmU7fQoKLmltcG93ZXJCb3ggLnN0YXR1cy5zdGF0dXNfYnJvd3NlciAgcDpudGgtb2YtdHlwZSgyKXsKICAgIGRpc3BsYXk6IG5vbmU7Cn0=';
    const obj = new window.WxLogin({
        id: 'wechat-login',
        appid: 'wx4ea2dca37170074c',
        scope: 'snsapi_login',
        redirect_uri: encodeURIComponent(`http://apiv3.xpump.net/User/wxUserLoginCB.html?from=wi&url=${url}`),
        state: state,
        href: `data:text/css;base64,${base64}`,
    });
};
 
const wechatQrRef = ref<HTMLIFrameElement>();
 
const openWechatLogin = () => {
    if (!wechatQrRef.value) return;
    const url = `${SERVE_URL}JJJHHH/home?isWxLogin=Y`;
    const appid = 'wx4ea2dca37170074c';
    const state = (new Date().getTime() / 1000).toString();
    const base64 = btoa(`
    .impowerBox .title {display:none;}
 
    .impowerBox .status.status_browser  p:nth-of-type(2){
        display: none;
    }
 
    .impowerBox .qrcode {
    width: 170px;
    margin-top:0;
 
    }
    .impowerBox .status{
    }
        .info{
        display: none;
    }
    #tpl_for_iframe{
        height:100%;
        overflow: hidden;
 
            display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }
    
    `);
    const wechatAuthUrl = `https://open.weixin.qq.com/connect/qrconnect?appid=${appid}&redirect_uri=${encodeURIComponent(
        `http://apiv3.xpump.net/User/wxUserLoginCB.html?from=wi&url=${url}`
    )}&response_type=code&scope=snsapi_login&state=${state}&href=data:text/css;base64,${base64}#wechat_redirect`;
    wechatQrRef.value.src = wechatAuthUrl;
};
 
//切换用户登录页面
const handleTabChange = (item) => {
    state.activeLoginName = item;
    resetWechatTip();
    switch (item) {
        case 'wechat':
            // getWechartQrCode();
            openWechatLogin();
            break;
        case 'accountUser':
            break;
        case 'phoneUser':
            break;
    }
};
 
watch(
    () => isShowLogin.value,
    (val) => {
        if (val && state.activeLoginName === 'wechat') {
            nextTick(() => {
                openWechatLogin();
            });
        }
    }
);
 
const getActiveLoginName = () => {
    const isWechatLogin = Local.get('isWechatLogin');
    if (isWechatLogin) {
        Local.remove('isWechatLogin');
        return 'wechat';
    }
    return 'accountUser';
};
 
const state = reactive({
    activeLoginName: getActiveLoginName(),
    loginForm: {
        account: '',
        pwd: '',
    },
    loginPhoneForm: {
        phoneUser: '',
        verifyCode: '',
    },
});
const loginRules = reactive({
    account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
    pwd: [{ required: true, message: '请输入密码', trigger: 'blur' }],
});
const loginPhoneRules = {
    phoneUser: [
        { required: true, message: '请输入手机号码', trigger: 'blur' },
        { pattern: /^1\d{10}$/, message: '请输入正确的手机号码', trigger: 'blur' },
    ],
    verifyCode: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
};
const loginFormRef = ref<FormInstance>(null); //账户密码登录
const formPhoneRef = ref(); //手机号登录
const hasSended = computed(() => {
    return countdown.value !== null;
});
const sendCodeMsg = computed(() => {
    return !hasSended.value ? '获取验证码' : `${countdown.value} 秒后重试`;
});
//登录
const onSubmit = async () => {
    let res;
    if (state.activeLoginName === 'accountUser') {
        //账户密码登录
        const isValid = await loginFormRef.value.validate().catch(() => {});
        if (!isValid) return;
        res = await PostLogin({
            user: state.loginForm.account,
            pass: state.loginForm.pwd,
            client: LOGIN_CLIENT,
        });
        if (!res.json_ok) {
            return ElMessage.error(res.json_msg);
        }
    } else if (state.activeLoginName === 'phoneUser') {
        //手机登录
        const isValid = await formPhoneRef.value.validate().catch(() => {});
        if (!isValid) return;
        res = await loginMessageUser({
            phone: state.loginPhoneForm.phoneUser,
            code: state.loginPhoneForm.verifyCode,
            client: LOGIN_CLIENT,
        });
        if (!res.json_ok) {
            return ElMessage.error(res.json_msg);
        }
    }
 
    if (state.activeLoginName === 'accountUser') {
        const accountEncryptStr = encrypt({
            username: state.loginForm.account,
            password: state.loginForm.pwd,
        });
        LocalPlus.set(STORED_ACCOUNT_KEY, accountEncryptStr, ACCOUNT_EXPIRE_DAY);
    }
    handleAfterLogin(res);
};
const countdown = ref(null);
 
//获取验证码
const handleSendVerifyCode = async () => {
    formPhoneRef.value.validateField('phoneUser', async (valid: boolean) => {
        if (valid) {
            const res = await loginVerifyMessage({
                phone: state.loginPhoneForm.phoneUser,
            });
            if (res.json_ok) {
                countdown.value = 60; //开启倒计时
                // 倒计时逻辑
                const intervalId = setInterval(() => {
                    if (countdown.value > 0) {
                        countdown.value--;
                    } else {
                        clearInterval(intervalId);
                    }
                }, 1000);
            }
        }
    });
};
</script>
 
<style lang="scss" scoped>
.pc-login {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    z-index: 2000;
    background-color: rgba(0, 0, 0, 0.6);
    .login_box {
        position: relative;
        width: 450px;
        margin: 15vh auto;
        .sign_in {
            padding: 39px;
            position: relative;
            width: 100%;
            height: 100%;
            border-radius: 12px;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            display: flex;
            -webkit-box-orient: vertical;
            -webkit-box-direction: normal;
            -ms-flex-direction: column;
            flex-direction: column;
            -webkit-box-align: center;
            -ms-flex-align: center;
            align-items: center;
            background-color: #fff;
            -webkit-box-shadow: 0 0 16px 0 rgba(20, 29, 53, 0.21);
            box-shadow: 0 0 16px 0 rgba(20, 29, 53, 0.21);
            .closes {
                position: absolute;
                top: -28px;
                right: -38px;
                font-size: 30px;
                cursor: pointer;
                color: #eee;
                -o-transition: color 0.1s;
                transition: color 0.1s;
            }
            h1 {
                box-sizing: content-box;
                width: 100%;
                font-size: 30px;
                font-weight: 500;
                color: #1c153a;
                text-align: left !important;
            }
            .demo-ruleForm {
                :deep(.el-input__wrapper) {
                    position: relative;
                    margin-bottom: 12px;
                    display: flex;
                    -webkit-box-align: center;
                    -ms-flex-align: center;
                    align-items: center;
                    width: 346px;
                    // height: 44px;
                    background-color: #fff;
                    border-radius: 5px;
                }
                :deep(.el-form-item--large .el-form-item__error) {
                    padding: unset !important;
                }
            }
            .set-pwd {
                text-align: right;
                font-size: 14px;
                font-weight: 300;
                color: #999;
                width: 100%;
                padding: 0px 32px;
            }
            .set-login_btn {
                width: 366px;
                height: 44px;
                font-size: 16px;
                font-weight: 500;
                color: #fff;
                background: #0a58ed;
                border-radius: 12px;
                -webkit-box-shadow: 4px 6px 15px rgba(10, 88, 237, 0.2);
                box-shadow: 4px 6px 15px rgba(10, 88, 237, 0.2);
            }
        }
    }
}
</style>