Django+uniapp 前后端【登录、注册】功能

登录注册 + 还有一个随时可被前端调用的看看此时jwt是否过期、被修改的视图函数

复制粘贴大概率是跑不了,因为main.js和settings.py还有一些文件夹、文件的创建什么的,可能都需要有相关的配置。

①前端(登录):

F:\uniapp_project\Farm\————\pages\auth\login.vue

<template>
	<view class="wrap">
		<view class="top"></view>
		<view class="content">
			<view class="title">欢迎登录</view>
			<input class="u-border-bottom" v-model="email" placeholder="请输入邮箱" />
			<input class="u-border-bottom" type="password" v-model="password" placeholder="请输入密码" />
			<button @tap="submit" :style="[inputStyle]" class="getCaptcha">登录</button>
			<view class="alternative">
				<view class="password">找回密码</view>
				<view class="issue" @click="regist">注册</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				email: '',
				password: ''
			}
		},
		computed: {
			inputStyle() {
				let style = {};
				if (this.$u.test.email(this.email) && this.password) { //当邮箱和密码都输入之后,按钮才会变色可点击
					style.color = "#fff";
					style.backgroundColor = this.$u.color['warning'];
				}
				return style;
			}
		},
		methods: {
			async submit() {
				//登录所需的账号密码
				const params = {
					email: this.email,
					password: this.password
				}
				//请求API,执行登录
				const loginRes = await this.$u.api.authLogin(params)

				//如果登陆成功,缓存token到vuex(在浏览器F12的Application里可以看到)
				if (loginRes.code == 1000) {         //登陆失败的话,不论什么原因,后端都返回1000
					this.$u.toast('邮箱或者密码错误!')
					return;
				}
				this.$u.vuex("vuex_token", loginRes.token)
				this.$u.toast('登录成功!')

				// //缓存用户信息到vuex
				// const userInfo = await this.$u.api.userInfo()
				// this.$u.vuex("vuex_user", userInfo)

				//登陆之后,返回来源页(为了防止报错,多个或者跳转到主页)
				const backUrl = uni.getStorageSync('back_url') || 'pages/index/index'
				setTimeout(() => {
					this.$u.route({
						type: "reLaunch",
						url: backUrl
					})
				}, 1500)
				
			},

			// 点击注册,进行注册页面跳转
			regist() {
				uni.showLoading({
					title: '',
					mask: false
				});

				setTimeout(() => {
					this.$u.route({
						type: "redirectTo",
						url: 'pages/auth/register'
					})
				}, 700)
			}
		}
	};
</script>

<style lang="scss" scoped>
	.u-border-bottom {
		margin-bottom: 40rpx !important; //还要写个!
  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值