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)

至此,大功告成

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

相关推荐
m0_462803883 小时前
【无标题】
微信小程序
2501_916007476 小时前
iOS和macOS应用程序性能分析和优化工具使用综合指南
android·macos·ios·小程序·uni-app·iphone·webview
2501_916007471 天前
深入理解HTTPS对称与非对称加密机制及Charles抓包实践
网络协议·http·ios·小程序·https·uni-app·iphone
小徐_23331 天前
AI 写 wot-ui 总在猜 API?我们把 Skills、MCP 和 CLI 都配好了
前端·uni-app·ai编程
Liu.7741 天前
uni-app 组件 uni-easyinput 常见 Bug 及解决方案
uni-app·bug
码农学院1 天前
微信小程序云开发实战:扫码点餐系统的高并发订单处理与外卖接单架构
微信小程序·架构·notepad++
小徐_23332 天前
uni-app 项目别再从零搭了!3 个 Wot UI 起手模板怎么选?
前端·uni-app
蜡台2 天前
uniapp pdf文件预览组件
pdf·uni-app·合同·pdfh5
凡泰AI2 天前
如何借助MCP打通企业APP内部服务:从统一调用到小程序承接
小程序·uni-app·app·mpaas·mcp·小程序容器
华玥作者2 天前
uniapp 万条数据不卡顿:我写了个虚拟列表组件 hy-list,原生支持瀑布流
数据结构·uni-app·list·vue3