login.vue 4.48 KB
<template>
	<view class="login-page">
		<!-- 公共组件-每个页面必须引入 -->
		<public-module></public-module>
		<view class="u-flex login-main">
			<view class="content">
				<view class="hello">您好,</view>
				<view class="hello-welcome">欢迎来到ThingsKit!</view>
				<view class="circleStyle"></view>
			</view>
		</view>
		<view class="f__login">
			<view class="loginPhone">
				<view class="form-row u-flex">
					<u-icon name="account-fill"></u-icon>
					<view class="v-input"><input type="text" v-model="loginForm.username" maxlength="32" placeholder="请输入登录账号" /></view>
					<u-icon></u-icon>
				</view>
				<view class="form-row u-flex">
					<u-icon name="lock-fill"></u-icon>
					<view class="v-input"><input type="text" v-model="loginForm.password" maxlength="32" placeholder="请输入登录密码" :password="!showPassword" /></view>
					<view class="v-password"><u-icon @click="showPasswordMode" :name="showPassword ? 'eye-fill' : 'eye-off'"></u-icon></view>
					<u-icon></u-icon>
				</view>
				<button class="submit" size="default" @click="onSubmitFunc"><text class="text">登录</text></button>
				<view class="u-flex row-item">
					<view class="row-phone" @click="openCodeFunc">手机验证码登录</view>
					<view class="row-reset" @click="findPassrordFunc">忘记密码</view>
				</view>
				<view @click="onAuthorization" class="u-flex link-login">
					<view class="link-text">第三方账号登录</view>
					<view style="height:20rpx"></view>
					<view class="link-image"><image class="image" src="../../static/weixin.png" mode="aspectFill"></image></view>
					<view class="circleStyleBottom"></view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import { mapMutations } from 'vuex';
import { getUserInfo } from '@/components/module/f-login/f-login.js';

export default {
	data() {
		return {
			loginForm: {
				username: '',
				password: ''
			},
			showPassword: false
		};
	},
	methods: {
		...mapMutations(['setUserInfo']),
		onAuthorization(e) {
			getUserInfo(
				info => {
					console.log(info, '授权信息');
					let httpData = {
						code: this.code,
						nickName: info.nickName || '', //昵称
						avatarUrl: info.avatarUrl || '', //头像
						gender: info.gender || '' //性别 0:未知 1:男 2:女
					};
					// uni.$u.http.post('您的接口', httpData).then(res => {
					let userInfo = {
						// ...res,
						token: true //token用于判断是否登录
					};
					// this.setUserInfo(userInfo)
					// setTimeout(()=>{
					//     uni.showToast({
					//     	title: '登录成功',
					//     	icon: 'none'
					//     });
					//     this.closeLogin();
					// },100)
					// })
				},
				err => {
					// this.closeLogin();
				}
			);
		},
		onSubmitFunc() {
			if (this.loginForm.username == '') {
				return uni.$u.toast('请输入登录账号~');
			}
			const passReg = /^(?=.*?[A-Z])(?=(.*[a-z]){1,})(?=(.*[\d]){1,})(?=(.*[\W]){1,})(?!.*\s).{8,}$/;
			if (this.loginForm.password == '') {
				uni.showToast({
					title: '请输入登录密码~',
					icon: 'none'
				});
				return;
			} else if (!passReg.test(this.loginForm.password)) {
				uni.showToast({
					title: '密码格式不正确(至少一个大写英文字母、至少一个小写英文字母、至少一位数字、至少一个特殊字符、最少八个字符)~',
					icon: 'none',
					duration: 3000
				});
				return;
			}
			uni.$u.http
				.post('/auth/login', this.loginForm)
				.then(res => {
					if (res) {
						// 储存登录信息
						let resObj = {
							refreshToken: res.refreshToken,
							isToken: res.token
						};
						let userInfo = {
							...resObj,
							token: true //token用于判断是否登录
						};
						if (userInfo.token) {
							this.setUserInfo(userInfo);
						}
						uni.showToast({
							title: '登录成功~',
							icon: 'none'
						}).then(res => {
							uni.reLaunch({	
							url: '/pages/personal/personal'	
						});
						});
						this.saveUserInfo();
					}
				})
				.catch(e => {
					uni.$u.toast(e.data?.message);
				});
		},
		saveUserInfo() {
			//储存个人信息
			uni.$u.http.get('/yt/user/me/info').then(res => {
				if (res) {
					this.setUserInfo(res);
				}
			});
		},
		openCodeFunc() {
			uni.navigateTo({
				url: '../other/code'
			});
		},
		findPassrordFunc() {
			uni.navigateTo({
				url: '../other/findPassword'
			});
		},
		showPasswordMode() {
			this.showPassword = !this.showPassword;
		}
	}
};
</script>

<style lang="scss" scoped>
@import './static/login.scss';
</style>