appInit.js 5.7 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2 3 4 5
import uniStarterConfig from '@/uni-starter.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';
6 7

// 实现,路由拦截。当应用无访问摄像头/相册权限,引导跳到设置界面
DCloud_JSON's avatar
DCloud_JSON 已提交
8
import interceptorChooseImage from '@/uni_modules/json-interceptor-chooseImage/js_sdk/main.js';
9 10
interceptorChooseImage()

DCloud_JSON's avatar
DCloud_JSON 已提交
11 12 13
// #endif
const db = uniCloud.database()
export default async function() {
14
	const debug = uniStarterConfig.debug;
DCloud_JSON's avatar
DCloud_JSON 已提交
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

	// uniStarterConfig挂载到getApp().globalData.config
	setTimeout(() => {
		getApp({
			allowDefault: true
		}).globalData.config = uniStarterConfig;
	}, 1)


	// 初始化appVersion(仅app生效)
	initAppVersion();

	//clientDB的错误提示
	function onDBError({
		code, // 错误码详见https://uniapp.dcloud.net.cn/uniCloud/clientdb?id=returnvalue
		message
	}) {
		console.log('onDBError', {
			code,
			message
		});
		// 处理错误
		console.error(code, message);
	}
	// 绑定clientDB错误事件
	db.on('error', onDBError)


43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
	//拦截云对象请求
	uniCloud.interceptObject({
		async invoke({
			objectName, // 云对象名称
			methodName, // 云对象的方法名称
			params // 参数列表
		}) {
			console.log('interceptObject',{
				objectName, // 云对象名称
				methodName, // 云对象的方法名称
				params // 参数列表
			});
			if(objectName == "uni-id-co" && (methodName.includes('loginBy') ||  ['login','registerUser'].includes(methodName) )){
				console.log('执行登录相关云对象');
				params[0].inviteCode = await new Promise((callBack) => {
					uni.getClipboardData({
						success: function(res) {
							console.log('剪切板内容:'+res.data);
							if (res.data.slice(0, 18) == 'uniInvitationCode:') {
								let uniInvitationCode = res.data.slice(18, 38)
63
								console.log('当前用户是其他用户推荐下载的,推荐者的code是:' + uniInvitationCode);
64 65 66 67 68 69 70
								// uni.showModal({
								// 	content: '当前用户是其他用户推荐下载的,推荐者的code是:'+uniInvitationCode,
								// 	showCancel: false
								// });
								callBack(uniInvitationCode)
								//当前用户是其他用户推荐下载的。这里登记他的推荐者id 为当前用户的myInviteCode。判断如果是注册
							} else {
71
								callBack()
72 73 74 75
							}
						},
						fail() {
							console.log('error--');
76
							callBack()
77
						},
78
						complete() {
79 80 81
							// #ifdef MP-WEIXIN
							uni.hideToast()
							// #endif
82 83 84 85
						}
					});
				})
				console.log(7897897897899,params);
DCloud_JSON's avatar
DCloud_JSON 已提交
86
			}
87
			console.log(params);
DCloud_JSON's avatar
DCloud_JSON 已提交
88
		},
89 90
		success(e) {
			console.log(e);
DCloud_JSON's avatar
DCloud_JSON 已提交
91
		},
92 93 94 95 96 97 98 99 100 101 102
		complete() {

		},
		fail(e){
			if (debug) {
				uni.showModal({
					content: JSON.stringify(e),
					showCancel: false
				});
				console.error(e);
			}
DCloud_JSON's avatar
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
		}
	})


	// #ifdef APP-PLUS
	// 监听并提示设备网络状态变化
	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
			})
		}
	});
	// #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 => {
			// console.log('检查是否有可以更新的版本', res);
			if (res.result.code > 0) {
				// 有新版本
				getApp({
					allowDefault: true
				}).appVersion.hasNew = true;
			}
		})
	});
	// 检查更新
	// #endif
}
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 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 214

async function getDeviceInfo() {
	let deviceInfo = {
		"uuid": '',
		"vendor": '',
		"push_clientid": '',
		"imei": '',
		"oaid": '',
		"idfa": '',
		"model": '',
		"platform": '',
	}
	const {
		model,
		platform,
	} = uni.getSystemInfoSync();
	Object.assign(deviceInfo, {
		model,
		platform
	});

	// #ifdef APP-PLUS
	const oaid = await new Promise((callBack, fail) => {
			if (deviceInfo.platform == "android") {
				plus.device.getOAID({
					success: function(e) {
						callBack(e.oaid)
						// console.log('getOAID success: '+JSON.stringify(e));
					},
					fail: function(e) {
						callBack()
						console.log('getOAID failed: ' + JSON.stringify(e));
					}
				});
			} else {
				callBack()
			}
		}),
		{
			imei,
			uuid
		} = await new Promise((callBack, fail) => {
			plus.device.getInfo({
				success: function(e) {
					callBack(e)
					// console.log('getOAID success: '+JSON.stringify(e));
				},
				fail: function(e) {
					callBack()
					console.log('getOAID failed: ' + JSON.stringify(e));
				}
			});
		}),
		idfa = plus.storage.getItem('idfa') || '', //idfa有需要的用户在应用首次启动时自己获取存储到storage中
		vendor = plus.device.vendor;
	try {
215
		deviceInfo.push_clientid = plus.push.getClientInfo().clientid
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	} catch (e) {
		uni.showModal({
			content: '获取推送标识失败。如果你的应用不需要推送功能,请注释掉本代码块',
			showCancel: false,
			confirmText: "好的"
		});
		console.log(e)
	}
	Object.assign(deviceInfo, {
		imei,
		uuid,
		idfa,
		vendor
	});
	// #endif
	return deviceInfo
232
}