appInit.js 5.2 KB
Newer Older
1
import uniStarterConfig from '@/uni-starter.config.js';
DCloud_JSON's avatar
DCloud_JSON 已提交
2
//应用初始化页
3 4 5 6
// #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
7 8 9
export default function() {
	
	setTimeout(()=>{
10
		// uniStarterConfig挂载到getApp().
11
		const app = getApp({allowDefault: true})
12
		app.globalData.config = uniStarterConfig;
13
	},30)
14 15
	
	// 初始化appVersion(仅app生效)
DCloud_JSON's avatar
DCloud_JSON 已提交
16
	initAppVersion();
DCloud_JSON's avatar
DCloud_JSON 已提交
17
	/*
DCloud_JSON's avatar
DCloud_JSON 已提交
18
		这里应用了[拦截器](https://uniapp.dcloud.io/api/interceptor?id=addinterceptor)实现了,路由拦截。当应用无访问摄像头/相册权限,引导跳到设置界面
DCloud_JSON's avatar
DCloud_JSON 已提交
19 20
	*/
	//自定义路由拦截
DCloud_JSON's avatar
DCloud_JSON 已提交
21
	const {"router": {needLogin,login} } = uniStarterConfig //需要登录的页面
22
	let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"];
DCloud_JSON's avatar
DCloud_JSON 已提交
23
	list.forEach(item => { //用遍历的方式分别为,uni.navigateTo,uni.redirectTo,uni.reLaunch,uni.switchTab这4个路由方法添加拦截器
24 25
		uni.addInterceptor(item, {
			invoke(e) { // 调用前拦截
DCloud_JSON's avatar
DCloud_JSON 已提交
26
				//获取用户的token
27
				const token = uni.getStorageSync('uni_id_token')
DCloud_JSON's avatar
DCloud_JSON 已提交
28
				//获取当前页面路径(即url去掉"?"和"?"后的参数)
29
				const url = e.url.split('?')[0]
DCloud_JSON's avatar
DCloud_JSON 已提交
30
				//控制登录优先级
31
				if (url == '/pages/ucenter/login-page/index/index') {
DCloud_JSON's avatar
DCloud_JSON 已提交
32
					//一键登录(univerify)、账号(username)、验证码登录(短信smsCode)
33
					if (login[0] == 'univerify') {
DCloud_JSON's avatar
DCloud_JSON 已提交
34
						if(e.url == url) { e.url += '?' } //添加参数之前判断是否带了`?`号如果没有就补上,因为当开发场景本身有参数的情况下是已经带了`?`号
35 36 37 38
						e.url += "univerify_first=true"
					} else if (login[0] == 'username') {
						e.url = "/pages/ucenter/login-page/pwd-login/pwd-login"
					}
DCloud_JSON's avatar
DCloud_JSON 已提交
39
				}else{
DCloud_JSON's avatar
DCloud_JSON 已提交
40
					//拦截强制登录页面
DCloud_JSON's avatar
DCloud_JSON 已提交
41 42
					if (needLogin.includes(url) && token == '') {
						uni.showToast({
DCloud_JSON's avatar
DCloud_JSON 已提交
43
							title: '请先登录',
DCloud_JSON's avatar
DCloud_JSON 已提交
44 45 46 47 48 49
							icon: 'none'
						})
						return uni.navigateTo({
							url: "/pages/ucenter/login-page/index/index"
						})
					}
50 51 52 53 54 55 56 57 58 59 60 61
				}
			},
			fail(err) { // 失败回调拦截 
				console.log(err);
			},
		})
	})

	//当应用无访问摄像头/相册权限,引导跳到设置界面
	uni.addInterceptor('chooseImage', {
		fail(e) { // 失败回调拦截 
			console.log(e);
62 63 64 65 66 67 68 69 70 71
			if(uni.getSystemInfoSync().platform == "android" && e.errMsg == 'chooseImage:fail No Permission'){
				if(e.code === 11){
					uni.showModal({
						title:"无法访问摄像头",
						content: "当前无摄像头访问权限,建议前往设置",
						confirmText: "前往设置",
						success(e) {
							if (e.confirm) {
								openAppPermissionSetting()
							}
72
						}
73 74 75 76 77 78 79 80 81 82
					});
				}else{
					uni.showModal({
						title:"无法访问相册",
						content: "当前无系统相册访问权限,建议前往设置",
						confirmText: "前往设置",
						success(e) {
							if (e.confirm) {
								openAppPermissionSetting()
							}
83
						}
84 85
					});
				}
86 87 88 89 90
			}
		}
	})
	
// #ifdef APP-PLUS
DCloud_JSON's avatar
DCloud_JSON 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
// 监听并提示设备网络状态变化
	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{
			uni.showToast({
				title:'网络类型:'+res.networkType,
				icon:'none',
				duration:3000
			})
		}
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
	});
// #endif

}
/**
 * // 初始化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 => {
DCloud_JSON's avatar
DCloud_JSON 已提交
130
			// console.log('检查是否有可以更新的版本', res);
131 132 133 134 135 136 137 138 139 140 141
			if (res.result.code > 0) {
				// 有新版本
				getApp({
					allowDefault: true
				}).appVersion.hasNew = true;
			}
		})
	});
	// 检查更新
	checkUpdate();
	// #endif
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
}

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
DCloud_JSON 已提交
168
}