appInit.js 6.1 KB
Newer Older
DCloud_JSON's avatar
123  
DCloud_JSON 已提交
1 2 3 4 5 6 7
import baseappConfig from '@/baseapp.config.js';
// #ifdef APP-PLUS
import checkUpdate from '@/uni_modules/uni-upgrade-center-app/utils/check-update';
import callCheckVersion from '@/uni_modules/uni-upgrade-center-app/utils/call-check-version';
// #endif
export default function() {
	// 初始化appVersion(仅app生效)
DCloud_JSON's avatar
DCloud_JSON 已提交
8 9 10
	initAppVersion();
	
	// baseappConfig挂载到getApp().
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
11
	getApp({allowDefault: true}).config = baseappConfig;
DCloud_JSON's avatar
123  
DCloud_JSON 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

	//自定义路由拦截
	const {
		"router": {
			needLogin,
			login
		}
	} = baseappConfig //需要登陆的页面
	//uni.addInterceptor的写法
	let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"];
	list.forEach(item => {
		uni.addInterceptor(item, {
			invoke(e) { // 调用前拦截
				//console.log(e);
				const token = uni.getStorageSync('uni_id_token')
				// console.log(token);
				const url = e.url.split('?')[0]
				//拦截强制登陆页面
				if (needLogin.includes(url) && token == '') {
					console.log('该页面需要登陆,即将跳转到login页面');
					uni.showToast({
						title: '该页面需要登陆,即将跳转到login页面',
						icon: 'none'
					})
					uni.navigateTo({
37
						url: "/pages/ucenter/login-page/index/index"
DCloud_JSON's avatar
123  
DCloud_JSON 已提交
38 39 40 41
					})
					return false
				}
				//控制登陆优先级
42
				if (url == '/pages/ucenter/login-page/index/index') {
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
43 44
					//一键登录(uniVerify)、账号(username)、验证码登陆(短信smsCode)
					if (login[0] == 'uniVerify') {
DCloud_JSON's avatar
123  
DCloud_JSON 已提交
45 46 47 48 49 50
						// console.log(e.url,url);
						if (e.url == url) {
							e.url += '?'
						}
						e.url += "univerify_first=true"
					} else if (login[0] == 'username') {
51
						e.url = "/pages/ucenter/login-page/pwd-login/pwd-login"
DCloud_JSON's avatar
123  
DCloud_JSON 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
					} else {
						//默认即是
					}
				}
				return true
			},
			fail(err) { // 失败回调拦截 
				console.log(err);
			},
		})
	})

	//当应用无访问摄像头/相册权限,引导跳到设置界面
	uni.addInterceptor('chooseImage', {
		fail(e) { // 失败回调拦截 
			console.log(e);
68
			if (
DCloud_JSON's avatar
重构  
DCloud_JSON 已提交
69
				e.errCode === 11 && uni.getSystemInfoSync().platform == "android" ||
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
				e.errCode === 2 && uni.getSystemInfoSync().platform == "ios"
			){
				uni.showModal({
					title:"无法访问摄像头",
					content: "当前无摄像头访问权限,建议前往设置",
					confirmText: "前往设置",
					success(e) {
						if (e.confirm) {
							openAppPermissionSetting()
						}
					}
				});
			}
			if(e.errCode === 12 && uni.getSystemInfoSync().platform == "android"){
				uni.showModal({
					title:"无法访问相册",
					content: "当前无系统相册访问权限,建议前往设置",
					confirmText: "前往设置",
					success(e) {
						if (e.confirm) {
							openAppPermissionSetting()
						}
					}
				});
			}
DCloud_JSON's avatar
123  
DCloud_JSON 已提交
95
		}
96 97 98
	})
	
// #ifdef APP-PLUS
DCloud_JSON's avatar
312  
DCloud_JSON 已提交
99
// 设备网络状态变化事件
100 101
	eventListenerNetwork()
// #endif
DCloud_JSON's avatar
312  
DCloud_JSON 已提交
102

DCloud_JSON's avatar
123  
DCloud_JSON 已提交
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 128 129 130 131 132 133 134 135 136 137
}
/**
 * // 初始化appVersion
 */
function initAppVersion() {
	// #ifdef APP-PLUS
	let appid = plus.runtime.appid;
	plus.runtime.getProperty(appid, (wgtInfo) => {
		let appVersion = plus.runtime;
		let currentVersion = appVersion.versionCode > wgtInfo.versionCode ? appVersion : wgtInfo;
		getApp({
			allowDefault: true
		}).appVersion = {
			...currentVersion,
			appid,
			hasNew: false
		}
		// 检查更新小红点
		callCheckVersion().then(res => {
			console.log('检查是否有可以更新的版本', res);
			if (res.result.code > 0) {
				// 有新版本
				getApp({
					allowDefault: true
				}).appVersion.hasNew = true;
			}
		})
	});
	// 检查更新
	checkUpdate();
	// #endif
}

// 设备网络状态变化事件
function eventListenerNetwork() {
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
/*
	//网络掉线
	uni.getNetworkType({
	    success:res=>{
	        console.log(res);
			if(res.networkType=='none'){
				showNetworkErrPage()
			}
			uni.showToast({
				title:'当前网络类型:'+res.networkType,
				icon:'none',
				duration:3000
			})
	    }
	});
	//监听网络变化
	uni.onNetworkStatusChange(res=> {
	    console.log(res.isConnected);
	    console.log(res.networkType);
		if(res.networkType!='none'){
			uni.showToast({
				title:'当前网络类型:'+res.networkType,
				icon:'none',
				duration:3000
			})
		}else{
			showNetworkErrPage()
			uni.showToast({
				title:'网络类型:'+res.networkType,
				icon:'none',
				duration:3000
			})
		}
	});
	
	function showNetworkErrPage(){
		let pages = getCurrentPages();
		console.log('pages.length',pages.length);
		if(pages.length===0 || pages[pages.length - 1].route!='/pages/networkErr/networkErr.vue'){
			uni.navigateTo({
				url:'/pages/networkErr/networkErr'
			})
		}else{
			console.log('已经打开');
		}
	}
DCloud_JSON's avatar
312  
DCloud_JSON 已提交
184
	
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
}

function openAppPermissionSetting(){
	// 跳转到**应用**的权限页面
	if (uni.getSystemInfoSync().platform == "ios") {
		var UIApplication = plus.ios.import("UIApplication");
		var application2 = UIApplication.sharedApplication();
		var NSURL2 = plus.ios.import("NSURL");
		// var setting2 = NSURL2.URLWithString("prefs:root=LOCATION_SERVICES");		
		var setting2 = NSURL2.URLWithString("app-settings:");
		application2.openURL(setting2);
		plus.ios.deleteObject(setting2);
		plus.ios.deleteObject(NSURL2);
		plus.ios.deleteObject(application2);
	} else {
		// console.log(plus.device.vendor);
		var Intent = plus.android.importClass("android.content.Intent");
		var Settings = plus.android.importClass("android.provider.Settings");
		var Uri = plus.android.importClass("android.net.Uri");
		var mainActivity = plus.android.runtimeMainActivity();
		var intent = new Intent();
		intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
		var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
		intent.setData(uri);
		mainActivity.startActivity(intent);
	}
*/
}
/*
DCloud_JSON's avatar
123  
DCloud_JSON 已提交
214
	uni.addInterceptor(item, {
215 216
		invoke(e) { // 调用前拦截
		},
DCloud_JSON's avatar
123  
DCloud_JSON 已提交
217 218 219 220 221 222 223 224 225 226
		success() { // 成功回调拦截 
		},
		fail(err) { // 失败回调拦截 
			console.log(err);
		},
		complete(e) { // 完成回调拦截 
			//console.log(e);
		},
		returnValue() { // 返回结果拦截 
		}
227
	}) // 移除拦截器API removeInterceptor('request')
DCloud_JSON's avatar
123  
DCloud_JSON 已提交
228
*/