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

	// 初始化appVersion
	initAppVersion();

	//自定义路由拦截
L
123  
linju 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
	const {"router":{needLogin}} = baseappConfig //需要登陆的页面
	changeAction(["navigateTo", "redirectTo", "reLaunch", "switchTab"], {
		before_action: e => {
			let token = uni.getStorageSync('uni-id-token')
			if (needLogin.includes(e.url) && token == '') {
				console.log('该页面需要登陆,即将跳转到login页面');
				uni.showToast({title:'该页面需要登陆,即将跳转到login页面',icon:'none'})
				uni.redirectTo({
					url:"/uni_modules/uni-login-page/pages/index/index"
				})
				return false
			}
			return true
		}
	})
芊里 已提交
27
	
L
123  
linju 已提交
28 29
	//提示网络变化
	eventListenerNetwork()
L
123  
linju 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
	
	
	/*
		当某个权限调用失败
		1.先检测手机的该模块是否打开
		2.检测当前应用是否被授权了该模块对应的权限
		提示,并点击跳转到设置
	*/
   // #ifndef H5
   // changeAction('chooseImage', {
   // 	after_action: e => {
   // 		console.log('changeAction', e);
   // 		if(e.errCode === 11){
   // 			uni.showModal({
   // 				content: '无权限',
   // 				confirmText:"前往设置",
   // 				success(e) {
   // 					if(e.confirm){
   // 						permision.gotoAppPermissionSetting()
   // 					}
   // 				}
   // 			});
   // 		}
   // 	}
   // })
   // #endif
L
123  
linju 已提交
56 57 58 59 60 61 62 63
}
/**
 * // 初始化appVersion
 */
function initAppVersion() {
	// #ifdef APP-PLUS
	let appid = plus.runtime.appid;
	plus.runtime.getProperty(appid, (wgtInfo) => {
芊里 已提交
64 65
		let appVersion = plus.runtime;
		let currentVersion = appVersion.versionCode > wgtInfo.versionCode ? appVersion : wgtInfo;
L
123  
linju 已提交
66 67
		getApp({
			allowDefault: true
芊里 已提交
68 69
		}).appVersion = {
			...currentVersion,
L
123  
linju 已提交
70
			appid,
芊里 已提交
71 72 73 74 75 76 77 78 79 80 81 82
			hasNew:true
		}
		// 检查更新小红点
		callCheckVersion()
		.then(res=>{
			if(res.result.code>0){
				// 有新版本
				getApp({
					allowDefault: true
				}).appVersion.hasNew = true;
			}
		})
芊里 已提交
83 84 85 86
	});
	
	// 检查更新
	checkUpdate();
L
123  
linju 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
	// #endif
}

// 设备网络状态变化事件
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);
					}
				}
			});
		}
芊里 已提交
116
	});
L
123  
linju 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
}


function changeAction(actions, {before_action,after_action}) {
	if(typeof actions == 'string'){
		actions = [actions]
	}
	if (!before_action) {
		before_action = () => true
	}
	actions.forEach(action=>{
		let old_action = uni[action]
		uni[action] = e => {
			if (before_action(e)) {
				console.log(after_action);
				if (after_action) {
					var compose = function(f, g) {
						return function(x) {
							return f(x,g(x));
						};
					};
					e.complete = compose(e.complete,after_action)
				}
				old_action(e)
			}
		}
	})
芊里 已提交
144
}