uniapp页面里面的登录注册模板

<!-- 账号密码登录页 -->
<template>
	<view class="page">
				<view class="uni-content">
			<view class="login-logo">
				<image :src="logo"></image>
			</view>
			<text class="title title-box">账号密码登录</text>
			<uni-forms>
				<uni-forms-item name="username">
					<uni-easyinput :focus="focusUsername" @blur="focusUsername = false" class="input-box"
						:inputBorder="false" v-model="username" placeholder="请输入手机号/用户名/邮箱" />
				</uni-forms-item>
				<uni-forms-item name="password">
					<uni-easyinput :focus="focusPassword" @blur="focusPassword = false" class="input-box" clearable
						type="password" :inputBorder="false" v-model="password" placeholder="请输入密码" />
				</uni-forms-item>
			</uni-forms>
			<uni-captcha v-if="needCaptcha" focus ref="captcha" scene="login-by-pwd" v-model="captcha" />
			<uni-id-pages-agreements scope="login" ref="agreements"></uni-id-pages-agreements>
			<button class="uni-btn" type="primary" @click="pwdLogin">登录</button>
			<view class="link-box">
				<view v-if="!config.isAdmin">
					<text class="forget">忘记了?</text>
					<text class="link" @click="toRetrievePwd">找回密码</text>
				</view>
				<text class="link" @click="toRegister">{{config.isAdmin ? '注册管理员账号': '注册账号'}}</text>
				<text class="link" @click="toRegister" v-if="config.isAdmin">注册账号</text>
			</view>
		</view>
		<uni-id-pages-fab-login ref="uniFabLogin"></uni-id-pages-fab-login>
	</view>
</template>

<script>
	import mixin from '@/uni_modules/uni-id-pages/common/login-page.mixin.js';
	const uniIdCo = uniCloud.importObject("uni-id-co", {
		errorOptions: {
			type: 'toast'
		}
	})
	export default {
		mixins: [mixin],
		data() {
			return {
				"password": "",
				"username": "",
				"captcha": "",
				"needCaptcha": false,
				"focusUsername": false,
				"focusPassword": false,
				"logo": "/static/logo.png"
			}
		},
		onShow() {
			// #ifdef H5
			document.onkeydown = event => {
				var e = event || window.event;
				if (e && e.keyCode == 13) { //回车键的键值为13
					this.pwdLogin()
				}
			};
			// #endif
		},
		methods: {
			// 页面跳转,找回密码
			toRetrievePwd() {
				let url = '/uni_modules/uni-id-pages/pages/retrieve/retrieve'
				//如果刚好用户名输入框的值为手机号码,就把它传到retrieve页面,根据该手机号找回密码
				if (/^1\d{10}$/.test(this.username)) {
					url += `?phoneNumber=${this.username}`
				}
				uni.navigateTo({
					url
				})
			},
			/**
			 * 密码登录
			 */
			pwdLogin() {
				if (!this.password.length) {
					this.focusPassword = true
					return uni.showToast({
						title: '请输入密码',
						icon: 'none',
						duration: 3000
					});
				}
				if (!this.username.length) {
					this.focusUsername = true
					return uni.showToast({
						title: '请输入手机号/用户名/邮箱',
						icon: 'none',
						duration: 3000
					});
				}
				if (this.needCaptcha && this.captcha.length != 4) {
					this.$refs.captcha.getImageCaptcha()
					return uni.showToast({
						title: '请输入验证码',
						icon: 'none',
						duration: 3000
					});
				}

				if (this.needAgreements && !this.agree) {
					return this.$refs.agreements.popup(this.pwdLogin)
				}

				let data = {
					"password": this.password,
					"captcha": this.captcha
				}

				if (/^1\d{10}$/.test(this.username)) {
					data.mobile = this.username
				} else if (/@/.test(this.username)) {
					data.email = this.username
				} else {
					data.username = this.username
				}

				uniIdCo.login(data).then(e => {
					this.loginSuccess(e)
				}).catch(e => {
					if (e.errCode == 'uni-id-captcha-required') {
						this.needCaptcha = true
					} else if (this.needCaptcha) {
						//登录失败,自动重新获取验证码
						this.$refs.captcha.getImageCaptcha()
					}
				})
			},
			/* 前往注册 */
			toRegister() {
				uni.navigateTo({
					// url: this.config.isAdmin ? '/uni_modules/uni-id-pages/pages/register/register-admin' :
					// 	'/uni_modules/uni-id-pages/pages/register/register',
					url: '/uni_modules/uni-id-pages/pages/register/register',
					fail(e) {
						console.error(e);
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import "@/uni_modules/uni-id-pages/common/login-page.scss";

	@media screen and (min-width: 690px) {
		.uni-content {
			height: auto;
		}
	}

	.forget {
		font-size: 12px;
		color: #8a8f8b;
	}

	.link-box {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: space-between;
		margin-top: 20px;
	}

	.link {
		font-size: 12px;
	}
</style>
  • 18
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UniApp 提供了内置的本地存储功能,如 Vuex(状态管理库)和 localStorage,方便在登录注册过程中进行数据缓存。以下是一个简单的 UniApp 登录注册本地缓存模板: 1. **使用 Vue.js 和 Vuex**: - 首先,安装 Vuex:`npm install vuex` - 创建一个 `store.js` 文件,用于存储用户信息: ```javascript import Vue from 'vue' import Vuex from 'vuex' export default new Vuex.Store({ state: { userInfo: null, // 存储登录信息 isLogin: false, // 用户是否已登录 }, mutations: { setUser(state, userInfo) { state.userInfo = userInfo; state.isLogin = true; }, logout(state) { state.userInfo = null; state.isLogin = false; }, }, actions: { login({ commit }, credentials) { // 这里模拟从接口获取数据 if (/* 登录成功 */) { commit('setUser', { username: 'John Doe', token: 'abc123' }); } else { alert('登录失败'); } }, logout({ commit }) { commit('logout'); }, }, }); ``` 2. **在登录组件中使用**: - 通过 `this.$store.state.userInfo` 获取或设置用户信息 - 使用 `this.$store.dispatch('login', credentials)` 登录 3. **注册组件中可能涉及**: - 注册后同样可以保存用户的注册信息到 store,然后在登录时检查是否存在。 4. **本地存储缓存**: - 对于敏感信息(如token),你可能还需要将它加密并存储在 localStorage 中,例如: ```javascript const encryptedToken = encrypt(credentials.token); // 使用加密函数 localStorage.setItem('token', encryptedToken); ``` - 登录时再从 localStorage 解密。 **相关问题:** 1. UniApp 中如何管理状态? 2. 为什么要使用 Vuex 而不是直接操作 localStorage? 3. 如何在 UniApp 中实现登录状态的持久化?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值