Files
shanghaiganxiangtuzhuangwor…/pages/login/login.vue
2024-07-15 15:29:38 +08:00

208 lines
5.4 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<view class="common-container">
<!-- 登录图片 -->
<view class="login-banner-box">
<u--image :showLoading="true" src="/static/images/ui/login.png" width="360px" height="240px"></u--image>
</view>
<!-- 登录框 -->
<view class="login-form-box">
<!-- 登录表单 -->
<u--form labelPosition="left" :model="loginForm" ref="uForm">
<u-form-item prop="username" borderBottom>
<u--input v-model="loginForm.username" border="none" placeholder="账号" prefixIcon="account"
prefixIconStyle="font-size: 22px;color: #909399"></u--input>
</u-form-item>
<u-form-item prop="password" borderBottom>
<u--input v-model="loginForm.password" border="none" placeholder="密码" prefixIcon="lock"
prefixIconStyle="font-size: 22px;color: #909399"></u--input>
</u-form-item>
<u-form-item prop="BaseUrl" borderBottom>
<u-input v-model="BaseUrl" border="none" placeholder="连接地址,例:127.0.0.1:8888" prefixIcon="ie"
prefixIconStyle="font-size: 22px;color: #909399">
<template slot="suffix">
<u-button type="primary" size="mini" text="修改地址" @click="handlerBaseUrlConfirm"
shape="circle"></u-button>
</template>
</u-input>
</u-form-item>
<u-form-item prop="button" style="margin-top: 60px">
<u-button @click="handleLogin" type="primary" :color="buttonColor" text="登录"
shape="circle"></u-button>
</u-form-item>
</u--form>
</view>
<!-- 无网络处理 -->
<u-no-network></u-no-network>
<view class="bangben">版本号{{ version }}</view>
</view>
</template>
<script>
import {
getCodeImg
} from '@/api/login';
import {
getToken,
setToken,
removeToken
} from '@/utils/auth';
import {
getBaseUrl,
setBaseUrl,
removeBaseUrl
} from '@/utils/baseUrl';
import {
getLoginInfo,
setLoginInfo,
removeLoginInfo
} from '@/utils/loginInfo';
import config from '@/config';
export default {
onLoad() {
const systemInfo = uni.getSystemInfoSync();
this.version = systemInfo.appVersion;
},
data() {
return {
version: '',
codeUrl: '',
captchaEnabled: false,
// 用户注册开关
register: false,
globalConfig: getApp().globalData.config,
BaseUrl: '127.0.0.1:8888',
buttonColor: 'linear-gradient(to right, #3296F9, #235FF5)',
loginForm: {
username: '',
password: '',
code: '',
uuid: ''
}
};
},
created() {
this.init();
// this.getCode();
},
methods: {
init() {
// 初始化链接地址设计
let baseUrl = getBaseUrl();
if (!baseUrl) {
baseUrl = '192.168.60.251:8888';
setBaseUrl(baseUrl);
}
this.BaseUrl = baseUrl;
// 初始化用户登录存储
try {
let loginInfoStr = getLoginInfo();
let loginInfo = JSON.parse(loginInfoStr);
if (loginInfo) {
this.loginForm.username = loginInfo.username;
this.loginForm.password = loginInfo.password;
}
} catch (e) {
// console.log(e);
//TODO handle the exception
}
},
// 获取图形验证码
getCode() {
getCodeImg().then((res) => {
if (res.code !== 200) {
uni.showToast({
title: '获取验证码失败!请检查网络!',
icon: 'error'
});
return;
}
const result = res.data;
if (result.captchaOff === 'off') {
this.captchaEnabled = false;
return;
} else {
this.captchaEnabled = true;
}
this.codeUrl = 'data:image/gif;base64,' + result.img;
this.loginForm.uuid = result.uuid;
});
},
// 登录方法
async handleLogin() {
// debug快速登录
if (this.loginForm.username === 'debug') {
setToken('123');
this.$tab.reLaunch('/pages/index/index');
return;
}
if (this.loginForm.username === '') {
this.$modal.msgError('请输入您的账号');
} else if (this.loginForm.password === '') {
this.$modal.msgError('请输入您的密码');
} else if (this.loginForm.code === '' && this.captchaEnabled) {
this.$modal.msgError('请输入验证码');
} else {
this.$modal.loading('登录中,请耐心等待...');
setTimeout(() => {
this.$modal.closeLoading();
}, 30000);
this.pwdLogin();
}
},
// 密码登录
async pwdLogin() {
this.$store
.dispatch('Login', this.loginForm)
.then(() => {
try {
const loginInfo = {
username: this.loginForm.username,
password: this.loginForm.password
}
const loginInfoStr = JSON.stringify(loginInfo);
setLoginInfo(loginInfoStr);
} catch (e) {
// console.log(e);
}
this.$modal.closeLoading();
this.loginSuccess();
})
.catch(() => {
this.$modal.closeLoading();
if (this.captchaEnabled) {
this.getCode();
}
});
},
// 登录成功后,处理函数
loginSuccess(result) {
// 获取户信息
this.$store.dispatch('GetInfo').then((res) => {
this.$tab.reLaunch('/pages/index/index');
});
},
// 确认修改链接地址
handlerBaseUrlConfirm() {
const baseUrl = this.BaseUrl;
// console.log('changeBaseUrl', baseUrl);
uni.showToast({
title: '链接地址已修改:' + baseUrl,
icon: 'none'
});
setBaseUrl(baseUrl);
}
}
};
</script>
<style lang="scss">
.bangben {
margin-top: 110px;
margin-left: 35%;
font-size: 0.8rem;
}
@import url('login.scss');
</style>