appInit.js 2.3 KB
Newer Older
L
123  
linju 已提交
1 2 3 4 5 6 7 8 9 10 11
import checkUpdate from '@/uni_modules/uni-upgrade-center-app/utils/check-update';
export default function() {

	// 初始化appVersion
	initAppVersion();

	// 检查更新
	 checkUpdate();

	//自定义路由拦截
	// setRouter()
芊里 已提交
12
	
L
123  
linju 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
	//提示网络变化
	eventListenerNetwork()
	
}
/**
 * // 初始化appVersion
 */
function initAppVersion() {
	// #ifdef APP-PLUS
	let appid = plus.runtime.appid;
	plus.runtime.getProperty(appid, (wgtInfo) => {
		wgtInfo.version
		let appVersion = plus.runtime;
		getApp({
			allowDefault: true
		}).appVersion = {
			appid,
			version: appVersion,
			wgtVersion: wgtInfo,
			finall: appVersion.versionCode > wgtInfo.versionCode ? appVersion : wgtInfo
		}
	});
	// #endif
}

//用于拦截路由
function setRouter() {
	let before_action = e => {
		let res = true
		//需要登陆的页面
		let needLoginUrls = ['/pages/grid/grid']
		let token = uni.getStorageSync('uni-id-token')
		if (needLoginUrls.includes(e.url) && token == '') {
			res = false
			console.log('该页面需要登陆,即将跳转到login页面');
		}
		return res
	}
芊里 已提交
51
	
L
123  
linju 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
	let before_after = e => {
		console.log('跳转之后');
	}

	let actions = ["navigateTo", "redirectTo", "reLaunch", "switchTab"]
	actions.forEach(action => {
		let old_action = uni[action]
		uni[action] = e => {
			//console.log(e);
			if (before_action(e)) {
				old_action(e)
				before_after(e)
			}
		}
	})
	uni.reLaunch({
		url: '/pages/grid/grid'
	})
芊里 已提交
70
}
L
123  
linju 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

// 设备网络状态变化事件
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);
					}
				}
			});
		}
芊里 已提交
98 99
	});
}