appInit.js 3.6 KB
Newer Older
L
23  
linju 已提交
1
import baseappConfig from '@/baseapp.config.js';
芊里 已提交
2
// #ifdef APP-PLUS
L
23  
linju 已提交
3
import checkUpdate from '@/uni_modules/uni-upgrade-center-app/utils/check-update';
芊里 已提交
4
import callCheckVersion from '@/uni_modules/uni-upgrade-center-app/utils/call-check-version';
L
23  
linju 已提交
5 6
// #endif
export default function() {
芊里 已提交
7
	// 初始化appVersion(仅app生效)
L
23  
linju 已提交
8
	initAppVersion();
L
123  
linju 已提交
9 10

	//自定义路由拦截
L
123  
linju 已提交
11
	const {"router":{needLogin,login}} = baseappConfig //需要登陆的页面
L
123  
linju 已提交
12 13 14 15 16
	//uni.addInterceptor的写法
	let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"];
	list.forEach(item=>{
		uni.addInterceptor(item,{
			invoke(e){// 调用前拦截
L
23  
linju 已提交
17
				//console.log(e);
L
123  
linju 已提交
18
				const token = uni.getStorageSync('uni_id_token')
L
23  
linju 已提交
19
				// console.log(token);
L
123  
linju 已提交
20 21
				const url = e.url.split('?')[0]
				//拦截强制登陆页面
L
123  
linju 已提交
22 23 24 25 26 27 28 29
				if (needLogin.includes(url) && token == '') {
					console.log('该页面需要登陆,即将跳转到login页面');
					uni.showToast({title:'该页面需要登陆,即将跳转到login页面',icon:'none'})
					uni.navigateTo({
						url:"/uni_modules/uni-login-page/pages/index/index"
					})
					return false
				}
L
123  
linju 已提交
30 31
				//控制登陆优先级
				if(url=='/uni_modules/uni-login-page/pages/index/index'){
L
123  
linju 已提交
32
					//一键登录(univerify)、密码登陆(username)、快捷登录&验证码登陆(!univerify&password)
L
123  
linju 已提交
33
					if(login[0]=='univerify'){
L
23  
linju 已提交
34
						// console.log(e.url,url);
L
123  
linju 已提交
35 36
						if(e.url==url){ e.url+= '?' }
						e.url += "univerify_first=true"
L
123  
linju 已提交
37
					}else if(login[0]=='username'){
L
23  
linju 已提交
38
						e.url = "/uni_modules/uni-login-page/pages/pwd-login/pwd-login"
L
123  
linju 已提交
39 40 41 42
					}else{
						//默认即是
					}
				}
L
123  
linju 已提交
43
				return true
L
123  
linju 已提交
44 45 46 47
			},
			success(){	// 成功回调拦截 
				
			},
L
23  
linju 已提交
48 49
			fail(err){		// 失败回调拦截 
				console.log(err);
L
123  
linju 已提交
50 51
			},
			complete(e){	// 完成回调拦截 
L
23  
linju 已提交
52
				//console.log(e);
L
123  
linju 已提交
53 54 55
			},
			returnValue(){// 返回结果拦截 
				
L
123  
linju 已提交
56
			}
L
123  
linju 已提交
57
		})// 移除拦截器API removeInterceptor('request')
L
123  
linju 已提交
58
	})
芊里 已提交
59
	
L
123  
linju 已提交
60 61
	//提示网络变化
	eventListenerNetwork()
L
123  
linju 已提交
62 63 64 65 66 67
	/*
		当某个权限调用失败
		1.先检测手机的该模块是否打开
		2.检测当前应用是否被授权了该模块对应的权限
		提示,并点击跳转到设置
	*/
L
23  
linju 已提交
68 69 70 71 72 73 74 75
}
/**
 * // 初始化appVersion
 */
function initAppVersion() {
	// #ifdef APP-PLUS
	let appid = plus.runtime.appid;
	plus.runtime.getProperty(appid, (wgtInfo) => {
芊里 已提交
76
		let appVersion = plus.runtime;
L
23  
linju 已提交
77 78 79
		let currentVersion = appVersion.versionCode > wgtInfo.versionCode ? appVersion : wgtInfo;
		getApp({
			allowDefault: true
芊里 已提交
80
		}).appVersion = {
L
23  
linju 已提交
81 82
			...currentVersion,
			appid,
L
123  
linju 已提交
83
			hasNew:false
芊里 已提交
84 85
		}
		// 检查更新小红点
L
123  
linju 已提交
86 87
		callCheckVersion().then(res=>{
			console.log('检查是否有可以更新的版本',res);
芊里 已提交
88 89 90 91 92 93
			if(res.result.code>0){
				// 有新版本
				getApp({
					allowDefault: true
				}).appVersion.hasNew = true;
			}
L
23  
linju 已提交
94
		})
芊里 已提交
95 96 97
	});
	
	// 检查更新
L
23  
linju 已提交
98 99 100
	checkUpdate();
	// #endif
}
L
123  
linju 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

// 设备网络状态变化事件
function eventListenerNetwork () {
	uni.onNetworkStatusChange(function(res) {
		console.log(res.isConnected);
		console.log(res.networkType);
		if (!res.isConnected) {
			uni.showModal({
				content: "你未打开网络连接",
				confirmText: "前往打开",
				complete: (e) => {
					console.log(e);
					if (uni.getSystemInfoSync().platform == "ios") {
						plus.runtime.launchApplication({
							action: 'App-Prefs:root=WIFI'
						}, function(e) {
							console.log(JSON.stringify(e));
						});
					} else {
						var main = plus.android.runtimeMainActivity();
						var Intent = plus.android.importClass("android.content.Intent");
						var mIntent = new Intent('android.settings.DATA_ROAMING_SETTINGS');
						main.startActivity(mIntent);
					}
				}
			});
		}
芊里 已提交
128 129
	});
}