云函数uni-id用户体验

<template>
	<view class="content">
		<input type="text" value="" placeholder="请输入用户名" v-model="username" />
		<input type="password" value="" placeholder="请输入密码" v-model="password" />
		<input type="text" value="" placeholder="请输入验证码" v-model="code" />
		<input type="text" value="" placeholder="请输入手机号码" v-model="phoneNumber" />
		<image @click="refresh" id="codeimgid" :src="codeImg" v-if="codeImg"></image>
		<button type="primary" size="mini" @click="register">注册</button>
		<button type="primary" size="mini" @click="login">登录</button>
		<button type="primary" size="mini" @click="loginOut">退出登录</button>
		<button type="primary" size="mini" @click="getUserInfo">获取用户信息</button>
		<button type="primary" size="mini" @click="verify">校验验证码</button>
		<button type="primary" size="mini" @click="phoneCode">获取手机验证码</button>
	</view>
</template>

<script>
	var   uniIdToken=uni.getStorageSync("uni_id_token");
	console.log(uniIdToken,"缓存的token");
	export default {
		data() {
			return {
				username: '',
				password: '',
				code:'',
				action:"register",
				codeImg:'',
				phoneNumber:""
			}
		},
		created() {
			this.createCaptcha();
		},
		onLoad() {
            
		},
		methods: {
			phoneCode(){
				let  that=this;
				uniCloud.callFunction({
					name:"use-center",
					data:{
						action:"sendSmsCode",
						mobile:that.phoneNumber
					},
					success:res=>{
						console.log(res.result,"手机验证码");
						if(res.result.code===0){
 							 uni.showToast({
 							 	title:"获取成功",
								icon:"success",
 							 })
						}else{
							uni.showToast({
								title:res.result.msg,
								icon:"none"
							})
						}
					}
				})
			},
			 
			refresh(){
				let  that=this;
				uniCloud.callFunction({
					name:"use-center",
					data:{
						action:"refresh"
					},
					success:res=>{
						console.log(res.result,"刷新验证码");
						if(res.result.code===0){
							uni.showToast({
								title:"刷新成功",
								icon:"success"
							})
							that.codeImg=res.result.captchaBase64;
						}else{
							uni.showToast({
								title:res.result.msg,
								icon:"none"
							})
						}
					}
				})
			},
			verify(){
				let  that=this;
				let  captcha=this.code;
				console.log(captcha);
				uniCloud.callFunction({
					name:"use-center",
					data:{
						action:"verify",
						captcha  
					},
					success:res=>{
						console.log(res.result,"校验验证码");
						if(res.result.code===0){
							uni.showToast({
								title:"校验成功",
								icon:"success"
							})
						}else{
							uni.showToast({
								title:res.result.message,
								icon:"none"
							})
						}
					}
				})
			},
			createCaptcha(){
				let  that=this;
				uniCloud.callFunction({
					name:"use-center",
					data:{
						action:"createCaptcha"
					},
					success:res=>{
						console.log(res.result,"获取验证码");
						if(res.result.code===0){
							uni.showToast({
								title:"获取成功",
								icon:"success"
							})
							that.codeImg=res.result.captchaBase64;
						}else{
							uni.showToast({
								title:res.result.msg,
								icon:"none"
							})
						}
					}
				})
			},
			getUserInfo(){
				uniCloud.callFunction({
					name:"use-center",
					data:{
						action:"getUserInfo",
						uniIdToken
					},
					success:res=>{
						console.log(res,"获取用户信息");
						if(res.result.code===0){
							uni.showToast({
								title:"获取成功",
								icon:"success"
							})
						}else{
							uni.showToast({
								title:res.result.msg,
								icon:"none"
							})
						}
					}
				})
			},
			loginOut(){
				 
				uniCloud.callFunction({
					name:"use-center",
					data:{
 						action:"logout",
						uniIdToken
					},
					success:res=>{
						console.log(res,"退出信息");
						if(res.result.code===0){
							uni.showToast({
								title:"退出成功",
								icon:"success"
							})
							uni.removeStorageSync("uni_id_token");
							uni.removeStorageSync("uni_id_token_expired");
						}else{
							uni.showToast({
								title:res.result.msg,
								icon:"none"
							})
						}
					}
				})
			},
			req(){
				let {
					username,
					password,
					action
				} = this;
				
				return  new  Promise((resolve,reject)=>{
					uniCloud.callFunction({
						name:"use-center",
						data:{
							username,
							password,
							action
						},
						success:res=>{
							resolve(res);
						},
						fail:err=>{
							reject(err);
						}
					})
				})
			},
			register(){
				this.req().then(res=>{
					console.log(res.result,"注册信息");
					if(res.result.code==0){
						uni.showToast({
							title:"注册成功",
							icon:"success"
						})
						const {
						    token,
						    tokenExpired
						  } = res.result;
						  console.log(token,"token");
						  if(token) {
						    uni.setStorageSync('uni_id_token', token)
						    uni.setStorageSync('uni_id_token_expired', tokenExpired)
						  }
					}else{
						uni.showToast({
							title:res.result.msg,
							icon:"none"
						})
					}
					 
				}).catch(err=>{
					console.log(err,"错误信息");
				})
			},
			login() {
				 
				let {
					username,
					password,
					action
				} = this;
				uniCloud.callFunction({
					name: "use-center",
					data: {
						action:"login",
						username,
						password
					},
					success: res => {
						console.log(res.result);
						if(res.result.code===0){
						   uni.showToast({
						   	title:"登录成功",
						   	icon:"none"
						   })
						   const {
						       token,
						       tokenExpired
						     } = res.result
						     if(token) {
						       uni.setStorageSync('uni_id_token', token)
						       uni.setStorageSync('uni_id_token_expired', tokenExpired)
						     }
						}else{
							uni.showToast({
								title:res.result.msg,
								icon:"none"
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
  button{
	  margin-top: 20px;
	  width: 200px;
	  letter-spacing: 1px;
  }
	input {
		width: 70%;
		height: 30px;
		background-color: #ddd;
		padding-left: 10px;
		margin: 5px;
		border-radius: 1px;
		font-size: 12px;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
	#codeimgid{
		width: 100px;
		height: 30px;
		background-size: cover;
		padding: 20rpx;
	}
</style>

云函数
‘use strict’;
const uniID=require(“uni-id”);
const uniCaptcha=require(“uni-captcha”);
exports.main = async (event, context) => {

//event为客户端上传的参数
console.log('event : ', event)
 const {
        username,
        password
    } = event;
	console.log(username,password,"信息");
   
  let res ={};
  switch(event.action){
	  // 注册
	 case   "register":{
		 //自己额外增加的校验密码规范的逻辑(可选)
		 //强弱密码校验,密码至少包含大写字母,小写字母,数字,且不少于6位
		 if(!/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{6,16}$/.test(password)){
		   return {
		     code: 401,
		     msg: '密码至少包含大写字母,小写字母,数字,且不少于6位'
		   }
		 }
		 // 自动验证用户名是否与已经注册的用户名重复,如果重复会直接返回错误。否则会自动生成token并加密password存储username、password、token到数据表uni-id-users,并返回如上响应参数
		res = await uniID.register({ //支持传入任何值,比如可以直接传入mobile即可设置手机号码,切勿直接传入event否则这是一个极大的安全问题
		    username,
		    password
		}) 
		break  ;
	 }
	 // 登录
	 case  "login":{
		 res = await uniID.login({
		 		username,
		 		password 
		 	})
		 break;
	 }
	 // 退出登录
	 case  "logout":{
		 res = await uniID.logout(event.uniIdToken);
		 break;
	 }
	 // 获取用户信息
	 case  "getUserInfo":{
		 
		let  payload = await uniID.checkToken(event.uniIdToken);
		  console.log(payload,"payload");
		   if (payload.code) {
		   	return payload
		   }
		  res = await uniID.getUserInfo({
		     uid: payload.uid,
		     field: ['mobile']
		   })
		   break;
	 }
	 
	 case  "createCaptcha":{
		res = await uniCaptcha.create({
			scene:"login",
			size:4
		});
		 break;
	 }
	 case  "refresh":{
	 			res = await uniCaptcha.refresh({
	 				scene:"login",
	 				size:4
	 			});
	 			 break;
	 }
	 case  "verify":{
		res = await uniCaptcha.verify({
			scene:"login",
			captcha:event.captcha
		});
		 break;
	 }
	 case   "sendSmsCode":{
		 const {
		 		mobile
		 	} = event
		   // 生成验证码可以按自己的需求来,这里以生成6位数字为例
		   const randomStr = '00000' + Math.floor(Math.random() * 1000000)
		   const code = randomStr.substring(randomStr.length - 6)
		 console.log(code);
		  res = await uniID.sendSmsCode({
		 	 mobile,
		     code,
			 templateId:"",
		     type: 'login'
		 	})
			
			break;
	 }
	 default: {
		 res={
			 code:774,
			 msg:"请求非法"
		 }
	 }
	 
	 
  }
     
       
//返回数据给客户端
return res

};

ui:

在这里插入图片描述

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
如果你使用了uni-id插件来实现登录功能,可以按照以下步骤来实现TabBar登录拦截: 1. 在App.vue中引入uni-id插件,并初始化uni-id实例: ```vue <script> import uniID from 'uni-id' export default { async onLaunch() { // 初始化uni-id实例 uniID.init({ // 这里填写你的uni-id配置信息,如api地址、公共请求头等 }) // 判断用户登录状态,此处使用uni-id提供的登录状态判断方法 const res = await uniID.checkToken() if (res.code !== 0) { // 未登录,跳转到登录页面 uni.redirectTo({ url: '/pages/login/login' }) } } } </script> ``` 2. 在每个TabBar页面的onLoad生命周期函数中,判断用户是否已经登录: ```vue <script> export default { onLoad() { // 判断用户登录状态,使用uni-id提供的登录状态判断方法 uniID.checkToken().then(res => { if (res.code !== 0) { // 未登录,跳转到登录页面 uni.redirectTo({ url: '/pages/login/login' }) } }) } } </script> ``` 3. 在登录页面的处理逻辑中,使用uni-id提供的登录方法完成登录操作: ```vue <script> import uniID from 'uni-id' export default { methods: { login() { // 调用uni-id的登录方法进行登录操作 uniID.login({ // 这里填写你的登录参数,比如用户名、密码等 }).then(res => { if (res.code === 0) { // 登录成功,跳转回之前点击的TabBar页面 uni.navigateBack() } else { // 登录失败,处理错误逻辑 } }) } } } </script> ``` 以上代码示例基于uni-id插件来实现登录功能,具体的实现方式可能会根据你的项目需求和uni-id插件的使用方式有所不同。请根据实际情况进行相应的调整和修改。同时,请确保在登录成功后,将用户的登录信息保存到本地缓存中,以便下次启动应用时能够正确判断用户的登录状态。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你的美,让我痴迷

你的好,我会永远记住你的。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值