uni-app 登录成功后自动跳转至登录前页面(H5\微信小程序)

uni-app 登录成功后自动跳转至登录前页面

一、新建配置文件,位于根目录下,config.js

javascript 复制代码
// 应用全局配置
module.exports = {
  // 应用信息
  appInfo: {
	loginPage: "/pages/login",
	cacheRouteKey: "unLoginTargetRoute"
	// 更多配置项...
  }
  // 更多配置项...
}

二、新建文件:router.js,位于:utils文件夹下

javascript 复制代码
import { appInfo } from '@/config.js'

export function cacheRouter(_router) {
	
	let router = _router
	
	// 获取当前页面栈信息
	const pages = getCurrentPages()
	if(pages && pages.length) {
		
		// 获取当前页面路由信息
		const currentPage = pages[pages.length - 1]
		router = '/' + currentPage.route
		
		if(router !== appInfo.loginPage) {
			
			let options = undefined
			// #ifdef MP-WEIXIN
			options = currentPage.options
			// #endif
			// #ifdef H5
			if(currentPage.__page__) {
				options = currentPage.__page__.options
			}
			// #endif
			if(options) {
				let params = ''
				for(let key in options) {
					params += '&' + key + '=' + options[key]
				}
				if(params && params.length) {
					router += '?' + params.substring(1)
				}
			} else {
				// #ifdef MP-WEIXIN
				if(currentPage.$page && currentPage.$page.fullPath) {
					router = currentPage.$page.fullPath
				}
				// #endif
				// #ifdef H5
				if(currentPage.__page__ && currentPage.__page__.fullPath) {
					router = currentPage.__page__.fullPath
				}
				// #endif
			}
		}
	}
	
	if(router) {
		uni.setStorageSync(appInfo.cacheRouteKey, router)
	}
}

三、H5兼容适配

在App.vue中添加以下代码

javascript 复制代码
import config from '@/config.js'
import { cacheRouter } from '@/utils/router.js'

onLaunch: function() {
	//#ifdef H5
	this.checkLogin()
	//#endif
},
methods: {
	checkLogin() {
		if(// TODO 未登录判断) {
			const _uri = window.location.href
			let _route = ''
			// 默认【域名+#+页面】分割值
			if (_uri.indexOf('#') !== -1) {
				_route = _uri.substring(_uri.indexOf('#') + 1)
			}
			// h5/:为manifest.json下【Web配置 -> 运行的基础路径】
			if (_uri.indexOf('h5/') !== -1) {
				_route = _uri.substring(_uri.indexOf('h5/') + 3)
			}
	
			cacheRouter(_route)
			uni.reLaunch({
				url: config.appInfo.loginPage
		    })
		}
	}
}

四、通配

在拦截器或后端请求方法中,引入方法,合适位置添加使用方法代码即可。

例如:

1、拦截器

javascript 复制代码
// 页面跳转验证拦截器
import { appInfo } from '@/config.js'
// 登录页面
const loginPage = appInfo.loginPage

// 页面白名单
const whiteList = [
	loginPage, '/pages/register', '/pages/common/webview/index'
]

// 检查地址白名单
function checkWhite(url) {
	const path = url.split('?')[0]
	return whiteList.indexOf(path) !== -1
}

let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"]
list.forEach(item => {
	uni.addInterceptor(item, {
		invoke(to) {
			if (// TODO 验证是否登录) {
				if (to.url === loginPage) {
					uni.reLaunch({
						url: "/"
					})
				}
				return true
			} else {
				if (to.url !== loginPage) {
					// 缓存当前路由信息
					cacheRouter(to.url)
				}
				// 验证是否放行
				if (checkWhite(to.url)) {
					return true
				}
				uni.reLaunch({
					url: loginPage
				})
				return false
			}
		},
		fail(err) {
			console.log(err)
		}
	})
})

2、后端请求方法

javascript 复制代码
import config from '@/config'
import { cacheRouter } from '@/utils/router.js'
const loginPage = config.appInfo.loginPage

// TODO 判断接口返回状态码code是否正常,通常code=401时,代表未登录
if (code === 401) {
	cacheRouter()
	showConfirm('登录状态已过期,您可以继续留在该页面,或者重新登录?').then(res => {
		if (res.confirm) {
			// TODO 跳转登录页面
		}
	})
}

五、登录页面

javascript 复制代码
import { appInfo } from '@/config.js'

// 登录成功后调用,处理函数
loginSuccess(result) {
	let unLoginTargetRoute = uni.getStorageSync(this.appInfo.cacheRouteKey)
	if (unLoginTargetRoute) {
		uni.removeStorageSync(this.appInfo.cacheRouteKey)
	} else {
		unLoginTargetRoute = '/pages/index'
	}
	uni.reLaunch({
		url: unLoginTargetRoute
	})
}

六、注销登录,移除缓存

import { appInfo } from '@/config.js'

uni.removeStorageSync(this.appInfo.cacheRouteKey)

至此,大功告成

个人博客:紫琪软件工作室

相关推荐
前端小万3 天前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序
码兄科技3 天前
24小时自助健身房系统软件开发实战指南:功能设计与部署方案
java·spring boot·uni-app
拖孩3 天前
代码我能全交给 AI,流量主这 500 个访客它一个都替不了我
前端·后端·微信小程序
2501_916007473 天前
苹果商店iOS应用上架费用全面解析:开发者计划与审核费用计算
android·ios·小程序·https·uni-app·iphone·webview
2501_916008893 天前
如何实现iOS App代码混淆:SwiftShield使用指南
android·ios·小程序·https·uni-app·iphone·webview
甜到心里的蛋糕3 天前
如何用企业微信实现让微信收到通知实时通知
服务器·python·微信小程序·fastapi
2501_916008893 天前
怎么用 Godot 导出 iOS 应用并上架 App Store?签名字段该填什么?
android·ios·小程序·https·uni-app·iphone·webview
2601_963870203 天前
基于springboot的综合网上购物商场系统
微信小程序·小程序·课程设计
2601_963869954 天前
力序健身房管理系统
微信小程序·小程序
郑州光合科技余经理4 天前
同城外卖小程序开发:下单成功后,后台导出能不能对上用户端状态
开发语言·前端·git·后端·uni-app·php·ai编程