web
2025-03-17 56d13900e2d74eeb9e22a9d86dc929640a676f6f
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
<template>
    <div class="login">
        <div class="login-bcg1"></div>
        <div class="contain">
            <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form"
                     @keydown.enter="handleLogin">
                <h3 class="title">金川水电站生态流量监测系统</h3>
                <el-form-item label="账号" prop="userName">
                    <el-input
                        style="height: 45px;"
                        v-model="loginForm.userName"
                        type="text"
                        auto-complete="off"
                        placeholder="账号"
                    >
                        <!-- <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template> -->
                    </el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input
                        style="height: 45px;"
                        v-model="loginForm.password"
                        :type="isPassword"
                        size="large"
                        auto-complete="off"
                        placeholder="密码"
                    >
                        <template #suffix>
                            <div @click="showPassword">
                                <svg-icon :icon-class="eyeView"/>
                            </div>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item label="验证码" prop="code">
                    <div class="yzm-box">
                        <el-input
                            style="height: 45px;"
                            v-model="loginForm.code"
                            size="large"
                            auto-complete="off"
                            placeholder="验证码"
                        >
                        </el-input>
                        <div class="yzm" @click="resetCode">
                            <img v-if="codeUrl" :src="codeUrl" alt=""/>
                            <div v-else class="code-none">无法加载</div>
                        </div>
                    </div>
                </el-form-item>
                <el-checkbox class="check-box" v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">
                    记住密码
                </el-checkbox>
                <el-form-item>
                    <el-button
                        :loading="loading"
                        size="large"
                        type="primary"
                        style="width:100%;"
                        @click.prevent="handleLogin"
                    >
                        <span v-if="!loading">登 录</span>
                        <span v-else>登 录 中...</span>
                    </el-button>
                </el-form-item>
            </el-form>
            <img class="left-img" src="../assets/images/login/login_icon.png"/>
        </div>
        <!--  底部  -->
        <div class="footer">
            <span>苏州伦晗电子有限公司.</span>
        </div>
    </div>
</template>
 
<script setup>
import {onMounted} from 'vue';
import Cookies from "js-cookie";
import {encrypt, decrypt} from "@/utils/jsencrypt";
import useUserStore from '@/store/modules/user';
import usePermissionStore from '@/store/modules/permission';
import {addActiveRoute} from '@/utils/index';
import {setToken} from '@/utils/auth';
import {useRouter, useRoute} from 'vue-router';
import {login, register, getIp} from '@/api/login';
 
const userStore = useUserStore()
const permissionStore = usePermissionStore()
const route = useRoute();
const router = useRouter();
const {proxy} = getCurrentInstance();
const isPassword = ref('password');
const eyeView = ref('eye')
 
const showPassword = () => {
    if (eyeView.value === 'eye') {
        eyeView.value = 'eye-open'
        isPassword.value = 'text'
    } else if (eyeView.value === 'eye-open') {
        eyeView.value = 'eye'
        isPassword.value = 'password'
    }
}
const loginForm = reactive({
    userName: "",
    password: "",
    code: '',
    uuid: '',
    machineCode: '',
    rememberMe: false
});
const codeUrl = ref('');
const loginRules = {
    userName: [{required: true, trigger: "blur", message: "请输入您的账号"}],
    password: [{required: true, trigger: "blur", message: "请输入您的密码"}],
    code: [{required: true, trigger: "blur", message: "请输入您的验证码"}],
};
const loading = ref(false);
const redirect = ref(undefined);
 
watch(route, (newRoute) => {
    redirect.value = newRoute.query && newRoute.query.redirect;
}, {immediate: true});
 
/**
 * 生成随机字符串uuid
 * @param length 字符串的长度,默认11
 * @returns {string}
 */
function generateRandomString(length = 11) {
    let charset = 'abcdefghijklmnopqrstuvwxyz-_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    let values = new Uint32Array(length);
    window.crypto.getRandomValues(values);
    let str = '';
    for (let i = 0; i < length; i++) {
        str += charset[values[i] % charset.length];
    }
    return str;
}
 
/**
 * 获取验证码
 */
async function resetCode() {
    loginForm.uuid = generateRandomString(11);
    register(loginForm.uuid).then(res => {
        codeUrl.value = res.data
    })
}
 
/**
 * 获取电脑标识
 */
async function getConputerId() {
    const res = await getIp();
    loginForm.machineCode = res.data;
}
 
//根据moduleCode获取相对应菜单
const filterTreeMenus = (menus, userType) => {
    let filteredMenu = [];
    for (let item of menus) {
        filteredMenu.push(item);
        if (item.children && item.children.length > 0) {
            let filteredChildren = filterTreeMenus(item.children, userType);
            if (filteredChildren.length > 0) {
                item.children = filteredChildren;
            } else {
                delete item.children;
            }
        }
    }
    return filteredMenu;
}
 
function handleLogin() {
    proxy.$refs.loginRef.validate(valid => {
        if (valid) {
            loading.value = true;
 
            //记住密码
            if (loginForm.rememberMe) {
                Cookies.set("userName", loginForm.userName, {expires: 30});
                Cookies.set("password", encrypt(loginForm.password), {expires: 30});
                Cookies.set("rememberMe", loginForm.rememberMe, {expires: 30});
            } else {
                //移除
                Cookies.remove("userName");
                Cookies.remove("password");
                Cookies.remove("rememberMe");
            }
            const userInfo = {
                userName: loginForm.userName.trim(),
                password: loginForm.password,
                code: loginForm.code,
                uuid: loginForm.uuid,
                machineCode: loginForm.machineCode,
            }
            login(userInfo).then(async (res) => {
                loading.value = false;
                setToken(res.data.token)
 
                //普通管理员筛除菜单、权限、数据字典,普通用户筛除系统管理
                if (res.data.userType == 2) {
                    res.data.menus.forEach((item, index) => {
                        if (item.id == '10001') {
                            item.children.forEach((itemChild, childIndex) => {
                                if (itemChild.id == '10101' || itemChild.id == '10102' || itemChild.id == '1822894922704416770') {
                                    item.children.splice(childIndex, 1)
                                }
                            })
                        }
                    })
                } else if (res.data.userType == 3) {
                    res.data.menus.forEach((item, index) => {
                        if (item.id == '10001') {
                            res.data.menus.splice(index, 1)
                        }
                    })
                }
                console.log(123123, res.data.menus)
                let filteredMenus = filterTreeMenus(res.data.menus, 1)
                console.log('filterTreeMenus菜单权限过滤', filteredMenus)
 
                permissionStore.setSidebarRouters(filteredMenus);
                console.log('setSidebarRouters配置菜单路由项数据', permissionStore.sidebarRouters)
 
                addActiveRoute(permissionStore, router)
 
                localStorage.setItem('listPermission', JSON.stringify(res.data.listPermission))
                localStorage.setItem('id', JSON.stringify(res.data.id))
                localStorage.setItem('userType', JSON.stringify(res.data.userType))
 
                router.push("/screen");//overview
            }).catch(error => {
                loading.value = false;
                proxy.$modal.msgError('登录失败!')
            })
        }
    });
}
 
function getCookie() {
    const userName = Cookies.get("userName");
    const password = Cookies.get("password");
    const rememberMe = Cookies.get("rememberMe");
    loginForm.userName = userName === undefined ? loginForm.userName : userName;
    loginForm.password = password === undefined ? loginForm.password : password;
    loginForm.rememberMe = rememberMe === undefined ? loginForm.rememberMe : rememberMe;
}
 
onMounted(() => {
    getCookie();
    resetCode();
    getConputerId()
})
</script>
 
<style lang='scss' scoped>
 
.login {
    position: relative;
    // justify-content: center;
    // align-items: center;
    width: 100%;
    height: 100%;
    background-image: url("../assets/images/login/login_bcg.png");
    background-size: 100% 100%;
 
    .login-bcg1 {
        position: absolute;
        height: 100%;
        width: 100%;
        background-image: url("../assets/images/login/login_bcg1.png");
        background-size: 100% 100%;
    }
 
    .contain {
        position: absolute;
        background-color: rgba(239, 245, 254, 0.75);
        border-radius: 26px;
        width: 76%;
        height: 85%;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        margin: auto;
    }
 
    .left-img {
        position: absolute;
        width: 44%;
        height: 62%;
        top: 16%;
        left: -4%;
    }
 
    .login-form {
        position: absolute;
        width: 42%;
        height: 80%;
        right: 5%;
        top: 10%;
        border-radius: 10px;
        background: #ffffff;
        padding: 25px;
        box-shadow: 0 0 10px 4px #c4d4f7;
 
        .title {
            font-size: 40px;
            margin: 10px 20px 70px;
            text-align: center;
            color: #000000;
        }
        .el-form-item {
            margin: 30px auto;
            width: 85%;
 
            .yzm-box{
                width: 100%;
                display: flex;
                justify-content: space-between;
                align-items: center;
                :deep(.el-input){
                    width: 60%;
                }
                .yzm{
                    width: 35%;
                    display: flex;
                    align-items: center;
                    img{
                        width: 100%;
                    }
                }
            }
 
            :deep(.el-form-item__label) {
                line-height: 45px;
                font-size: large;
                font-weight: 500;
                width: 80px;
            }
 
            .el-button {
                height: 50px;
            }
        }
 
        .check-box {
            width: 85%;
            left: 50%;
            transform: translate(-50%);
        }
    }
}
 
.footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
}
 
</style>