login.vue 3.1 KB
<template>
	<view class="login">
		<view class="icon">
			<image class="bgimg" src="../../static/ic_logo.png" mode=""></image>

			<view class="bar flexwrap">
				<view class="name">
					用户名:
				</view>
				<view class="last name">
					<input v-model="account" maxlength="16" type="text" value="" placeholder="请输入用户名" />
				</view>
			</view>
			<view class="bar flexwrap">
				<view class="name">
					密码:
				</view>
				<view class="last name">
					<input v-model="password"  maxlength="16"  type="password" value="" placeholder="请输入密码" />
				</view>
			</view>
			<view class="loginbtn" @click="doLogin">
				登录
			</view>
			<view class="passway">
				<text @click="forget">忘记密码</text>
				<text @click="register">没有账号,去注册</text>
			</view>
		</view>
	</view>
</template>

<script>
	import { login } from '@/api/login.js'
	import {toa } from '@/utils/toast.js'
	export default {
		data() {
			return {
				account:'',
				password:''
			};
		},
		methods: {
			forget() {
				uni.navigateTo({
					url:'/pages/login/password'
				})
			},
			register(){
				uni.navigateTo({
					url:'/pages/login/register'
				})
			},
			// 登录
			doLogin(){
				if(!this.account) return toa.toast('请输入账号')
				if(!this.password) return toa.toast('请输入密码')
				this.login()
			},
			async login(){
			  try {
			    const res = await login(this.account,this.password)
			    console.log('login', res)
				uni.setStorageSync('userInfo',res.userinfo)
				uni.setStorageSync('token',res.userinfo.token)
				setTimeout(()=>{
					toa.success('登录成功')
				},200)
				uni.reLaunch({
					url:'/pages/index/index'
				})
			    // 保存数据
			  } catch (err) {
			 	uni.showToast({ title:err,icon:'none' })
			    console.log('login', err)
			  }
			},
		},
	}
</script>

<style lang="scss">
	.login {
		padding: 0 48rpx;
		box-sizing: border-box;

		.icon {
			.bgimg {
				margin-top: 100rpx;
				margin-left: 50%;
				transform: translate(-50%);
				margin-bottom: 24rpx;
				width: 184rpx;
				height: 184rpx;
			}

			.bar {
				border-bottom: 1px solid #f0f0f0;
				padding: 40rpx 0;
				color: rgba(0, 0, 0, 1);
				font-size: 32rpx;
				font-weight: 500;
				font-family: "PingFang SC";

				.name {
					width: 180rpx;
				}

				.last {
					flex: 1;
					// width: 300rpx;
				}
			}

			.loginbtn {
				margin-top: 100rpx;
				width: 654rpx;
				height: 88rpx;
				border-radius: 28rpx;
				opacity: 1;
				color: rgba(0, 0, 0, 0.9);
				font-size: 32rpx;
				font-weight: 600;
				font-family: "PingFang SC";
				text-align: center;
				line-height: 88rpx;
				background: rgba(254, 208, 0, 1);
			}

			.passway {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-top: 16rpx;

				text {
					&:first-child {
						color: rgba(0, 0, 0, 0.26);
						font-size: 24rpx;
						font-weight: 400;
						font-family: "PingFang SC";
					}

					&:last-child {
						color: rgba(254, 208, 0, 1);
						font-size: 26rpx;
						font-weight: 400;
						font-family: "PingFang SC";
					}
				}
			}
		}
	}
</style>