uniapp小程序检查、提示更新及其他常见方法

218 阅读1分钟

本文已参与[新人创作礼]活动,一起开启掘金创作之路

xcxCheckUpdate.js

export default function xcxCheckUpdate(){
	const updateManager = uni.getUpdateManager();
	
	updateManager.onCheckForUpdate(function (res) {
	  // 请求完新版本信息的回调
	});
	
	updateManager.onUpdateReady(function (res) {
	  uni.showModal({
	    title: '更新提示',
	    content: '新版本已经准备好,是否重启应用?',
	    success(res) {
	      if (res.confirm) {
	        // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
	        updateManager.applyUpdate();
	      }
	    }
	  });
	
	});
	
	updateManager.onUpdateFailed(function (res) {
	  // 新的版本下载失败
	});
}

App.vue:

<script>
    import xcxCheckUpdate from 'xcxCheckUpdate.js'
    export default {
       // 检测小程序更新
		xcxCheckUpdate() 
    }
</script>

uniapp返回页面方法以及判断:

方法

/**
 * 返回页面执行方法
 * index 返回的页数,默认1 返回上一页
 */
export function navigateBack(index=1) {
	return new Promise((r, j) => {
		const pages = getCurrentPages();
		const beforePage = pages[pages.length - (index+1)];
		if (!beforePage) {
			// uni.showToast({
			// 	title: '前面没有页面啦~',
			// 	icon: 'none'
			// })
			j()
		}
		// #ifdef H5
		r({
			beforePage,
			navigateBack() {
				uni.navigateBack({
					delta:index
				})
			}
		})
		// #endif
		// #ifndef H5
		r({
			beforePage: beforePage.$vm,
			navigateBack() {
				uni.navigateBack({
					delta:index
				})
			}
		})
		// #endif
	})
 
}

使用:

//可以传值 navigateBack(2),表示返回上两页

navigateBack().then(res => { const { beforePage, navigateBack } = res //beforePage,返回页面的this,可以获取返回页面的数据和方法 navigateBack()//返回 })

通过addInterceptor拦截跳转,在页面跳转之前判断是否需要登录

App.vue:

import store from '@/store'
   export default {
   	onLaunch: function() {
   		if(process.env.NODE_ENV !== 'development'){
   			console.log=()=>{
   				return ''
   			}
   		}
   		// const noLogin=['/mime/index/index']//不需要登录的页面
   		const noLogin=[]//不需要登录的页面
   		let setIntervalL=null
   		let curPage = null//当前页面
   		let curPageData=null//当前页面数据
   		
   		// 登录方法
   		const xcxLogin=()=>{
   			return store.dispatch('user/xcxLogin')
   		}
   		// 拦截器方法
   		const addInterceptorFN=(item,url=null)=>{
   			// 路由拦截
   			const routerObject={
   				invoke(args) {
   					// 没有url,禁止跳转
   					if(!args.url){
   						return false;
   					}
   					let url=args.url.split('?')[0]
   					let notNeed=noLogin.includes(url)
   					// 初始换页面进入的,登录后刷新页面
   					if(args.isLaunch){
   						// 第一种刷新页面
   						uni.reLaunch({
   							url:args.url
   						})
   						// 第二种刷新页面
   						// curPage.data=curPageData
   						// if(curPage.onLoad)curPage.onLoad(curPage.options) // 传入参数
   						// if(curPage.onShow)curPage.onShow()
   						// if(curPage.onReady)curPage.onReady()
   						return false;
   					}
   					// 不需要验证登录
   					if(notNeed){
   						return true
   					}else{
   						// 没有登录
   						uni.hideLoading()
   						if(!store.state.user.userInfo || Object.keys(store.state.user.userInfo).length==0){
   							// 小程序自动登录
   							// 登录后重新跳转
   							if(!notNeedLoding){
   								uni.showLoading({
   									title:'登录中'
   								})
   							}
   							xcxLogin().then(res=>{
   								uni.hideLoading()
   								// 继续跳转
   								uni[item]({
   									url:args.url
   								})
   							}).catch((err)=>{
   								uni.hideLoading()
   								console.log('登录失败',err)
   							})
   							return false
   						}else{
   							return true
   						}
   					}
   					
   				}
   			}
   			// 初始化,会将当前的路由传过来,然后验证
   			if(url){
   				routerObject.invoke({url:url,isLaunch:true})
   			}
   			// uniapp 拦截器
   			uni.addInterceptor(item,routerObject)
   		}
   		// 初始化拦截
   		const initPage=()=>{
   			let pages=getCurrentPages()
   			curPage=pages[pages.length-1]
   			if(curPage){
   				curPageData=curPage.data
   				// 页面加载完成后
   				if(setIntervalL)clearInterval(setIntervalL)
   				// 登录
   				let url=curPage.$page.fullPath
   				url=url.substr(0,1)=='/'?url:('/'+url)
   				let urlThis=url.split('?')[0]
   				let notNeed=noLogin.includes(urlThis)
   				let isReLaunch=false
   				// 没有登录过,需要登录后刷新页面
   				if(!store.state.user.userInfo || Object.keys(store.state.user.userInfo).length==0){
   					isReLaunch=true
   				}
   				if(!notNeed){
   					uni.hideLoading()
   					if(isReLaunch){
   						uni.showLoading({
   							title:'登录中'
   						})
   					}
   					xcxLogin().then(res=>{
   						uni.hideLoading()
   						if(isReLaunch){
   							addInterceptorFN('reLaunch',url)
   						}
   					}).catch((err)=>{
   						uni.hideLoading()
   						console.log('登录失败',err)
   					})
   				}
   			}
   		}
   		// 第一次进入
   		setIntervalL=setInterval(()=>{
   			initPage();
   		},100)
   		const addInterceptor=['navigateTo','redirectTo','reLaunch','switchTab']
   		addInterceptor.forEach(item=>{
   			// 挂载拦截器
   			addInterceptorFN(item)
   		})
   	
   		

   	},
   	onShow: function() {
   		console.log('App Show')
   	},
   	onHide: function() {
   		console.log('App Hide')
   	},
   	
   }

效果:

image.png