pageInfo.uts 12.2 KB
Newer Older
M
mehaotian 已提交
1
import { OnLaunchOptionsWithCst, RouteParams, StatDefault, ReportErrorCode } from '../../interface.uts'
M
mehaotian 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
import { STAT_VERSION, sys, sysAppBase, Config } from '../config.uts'
// import Config from '../config.uts'
import { get_time } from './pageTime.uts'
import { dbGet, dbSet } from './db.uts'
import { Serialize, IsNumber } from './index.uts'

const APPID = sys.appId
const RUNTIME_VERSION = sys.appVersion

/**
 * 获取uuid
 */
export const get_uuid = () : string => {
M
mehaotian 已提交
15 16 17 18
	// 有可能不存在 deviceId(一般不存在就是出bug了),就自己生成一个
	// 目前 deviceId 肯定存在所以不用生成
	// const uuid : string = sys.deviceId ?? getUuid()
	return sys.deviceId
M
mehaotian 已提交
19 20 21 22 23 24
}

/**
 * 获取老版的 deviceid ,兼容以前的错误 deviceid
 */
export const get_odid = () : string => {
M
mehaotian 已提交
25 26 27 28 29 30 31 32 33 34 35 36
	// let odid : string
	// if (get_platform_name() === 'n') {
	// 	try {
	// 		odid = plus.device.uuid
	// 	} catch (e) {
	// 		odid = ''
	// 	}
	// 	return odid
	// }
	// odid = sys.deviceId ?? getUuid()
	// TODO 需要获取老版本的uuid ,目前无法获取,只返回 deviceId
	return sys.deviceId
M
mehaotian 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
}



/**
 * 获取当前平台
 * 移动端  : 'n',
 * h5	  : 'h5',
 * 微信	  : 'wx',
 * 阿里	  : 'ali',
 * 百度	  : 'bd',
 * 头条	  : 'tt',
 * qq	  : 'qq',
 * 快应用  : 'qn',
 * 快手	  : 'ks',
 * 飞书	  : 'lark',
 * 快应用  : 'qw',
 * 钉钉	  : 'dt'
 */
export const get_platform_name = () : string => {

M
mehaotian 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
	let platformList = {
		'app': 'n',
		'app-plus': 'n',
		'h5': 'h5',
		'web': 'web',
		'mp-weixin': 'wx',
		'mp-baidu': 'bd',
		'mp-toutiao': 'tt',
		'mp-qq': 'qq',
		'quickapp-native': 'qn',
		'mp-kuaishou': 'ks',
		'mp-lark': 'lark',
		'quickapp-webview': 'qw',
	}

	// 苹果审核代码中禁止出现 alipay 字样 ,需要特殊处理一下
	const aliArr = ['y', 'a', 'p', 'mp-ali']
	const aliKey = aliArr.reverse().join('')
	platformList[aliKey] = 'ali'

	const platform = sys.uniPlatform
	// TODO 兼容子平台
	// if (platformList[process.env.VUE_APP_PLATFORM] === 'ali') {
	// 	if (my && my.env) {
	// 		const clientName = my.env.clientName
	// 		if (clientName === 'ap') return 'ali'
	// 		if (clientName === 'dingtalk') return 'dt'
	// 		// TODO 缺少 ali 下的其他平台
	// 	}
	// }
	return platformList[platform] as string
M
mehaotian 已提交
89 90 91 92 93 94
}

/**
 * 获取原生包名,或小程序 appid
 */
export const get_pack_name = () : string => {
M
mehaotian 已提交
95 96 97 98 99 100 101 102 103 104
	let packName = ''
	if (get_platform_name() === 'n') {
		if (sys.osName == 'android') {
			packName = sysAppBase.packageName ?? ''
		}
		if (sys.osName == 'ios') {
			packName = sysAppBase.bundleId ?? ''
		}
	}
	return packName
M
mehaotian 已提交
105 106 107 108 109 110
}

/**
 * 应用版本
 */
export const get_version = () : string => {
M
mehaotian 已提交
111
	return RUNTIME_VERSION
M
mehaotian 已提交
112 113 114 115 116 117
}

/**
 * 获取渠道
 */
export const get_channel = () : string => {
M
mehaotian 已提交
118 119 120 121 122 123 124 125 126
	const platformName = get_platform_name()
	let channel : string = ''
	if (platformName === 'n') {
		channel = sysAppBase.channel ?? ''
	}
	// if (platformName === 'wx') {
	//   // TODO 需要调研小程序二维码渠道如何获取;
	// }
	return channel
M
mehaotian 已提交
127 128 129 130 131
}

/**
 * 获取小程序场景值
 */
M
mehaotian 已提交
132
export const get_scene = (options : OnLaunchOptionsWithCst) : number => {
M
mehaotian 已提交
133
	// const platformName = get_platform_name()
M
mehaotian 已提交
134 135 136 137 138 139 140
	let scene = 1001
	if (options.scene != null) {
		return options.scene ?? 1001
	}
	// if (platformName == 'wx') {
	// 	// @ts-ignore
	// 	scene = uni.getLaunchOptionsSync()?.scene ?? 1001
M
mehaotian 已提交
141
	// }
M
mehaotian 已提交
142
	return scene
M
mehaotian 已提交
143 144 145 146 147 148 149 150
}

/**
 * 是否获取 page 页面
 * @param {VueComponent} appInstance 页面实例
 */
// @ts-ignore
export const is_page = (appInstance : ComponentPublicInstance) : Boolean => {
M
mehaotian 已提交
151
	// #ifdef WEB || APP-IOS || MP-WEIXIN
M
mehaotian 已提交
152 153 154
	const type = appInstance?.$mpType ?? 'page'
	return type == 'page' ? true : false
	// #endif
M
mehaotian 已提交
155
	// #ifndef WEB || APP-IOS || MP-WEIXIN
M
mehaotian 已提交
156 157 158
	// 其他平台没有 $mpType ,只有页面触发
	return true
	// #endif
M
mehaotian 已提交
159 160 161 162 163 164 165 166
}


/**
 * 获取页面标题
 * @param {String} routepath 页面路由
 */
export const get_page_name = (routepath : string) : string => {
M
mehaotian 已提交
167 168
	let page = get_page_vm()
	if (page == null) return ''
M
mehaotian 已提交
169 170 171 172 173 174 175
	let route : string
	// #ifdef MP-WEIXIN
	route = (page.$scope && page.$scope.route) ?? ""
	// #endif
	// #ifndef MP-WEIXIN
	route = page.route
	// #endif
M
mehaotian 已提交
176

M
mehaotian 已提交
177
	if (route != routepath) {
M
mehaotian 已提交
178 179 180 181 182 183 184 185 186
		const pages = getCurrentPages()
		// 如果传入路由与当前页面不同,则从页面栈找一个,如果找不到返回空
		let page_now = pages.find((p) : boolean => p.route == routepath)
		if (page_now == null) {
			return ''
		}
		// @ts-ignore
		page = page_now.vm!
	}
M
mehaotian 已提交
187 188 189 190 191 192 193
	let titleText : string
	// #ifdef MP-WEIXIN
	// @ts-ignore
	const title_json = process.env.UNI_STAT_TITLE_JSON
	titleText = title_json[route] ?? ''
	// #endif
	// #ifndef MP-WEIXIN
M
mehaotian 已提交
194 195
	// @ts-ignore
	const pageStyle = (page.$page as UniPage).getPageStyle()
M
mehaotian 已提交
196 197
	titleText = (pageStyle['navigationBarTitleText'] as string)
	// #endif
M
mehaotian 已提交
198
	return titleText as string
M
mehaotian 已提交
199

M
mehaotian 已提交
200 201 202 203 204 205
}

/**
 * 获取页面实例
 */
export const get_page_vm = () : Page | null => {
M
mehaotian 已提交
206 207 208 209 210 211 212 213
	let pages = getCurrentPages()
	if (pages.length == 0) {
		return null
	}
	let page = pages[pages.length - 1]
	// TODO 正常来说,调用当前方法的地方只在 生命周期内,数组内最少会有一个页面,所以理论上是不存在获取不到的情况的
	// @ts-ignore
	return page.vm
M
mehaotian 已提交
214 215 216 217 218 219 220 221
}


/**
 * 获取页面url,不包含参数
 * @param {ComponentPublicInstance} page 页面实例
 */
export function get_route(page : Page | null = null) : RouteParams {
M
mehaotian 已提交
222 223 224 225 226 227 228 229 230
	let _self = page ?? get_page_vm()

	if (_self == null) {
		const data : RouteParams = {
			path: '',
			fullpath: ''
		}
		return data
	}
M
mehaotian 已提交
231 232 233 234 235 236 237 238
	let url_params : string
	let options : any
	// #ifdef MP-WEIXIN
	options = _self.$scope.options
	// #endif
	// #ifndef MP-WEIXIN
	options = _self.options
	// #endif
M
mehaotian 已提交
239 240
	// TODO 条件编译处理参数问题,安卓上 options返回的是map,需要处理成 utsobject
	// #ifdef APP-ANDROID
M
mehaotian 已提交
241
	const opts = new UTSJSONObject(options)
M
mehaotian 已提交
242
	// TODO 上报页面参数需要处理
M
mehaotian 已提交
243
	url_params = Serialize(opts)
M
mehaotian 已提交
244 245 246
	// #endif
	// #ifndef APP-ANDROID
	// @ts-ignore
M
mehaotian 已提交
247
	url_params = Serialize(options)
M
mehaotian 已提交
248 249 250 251 252 253
	// #endif
	let params = ''
	// 如果参数只有 ?则说明没有参数
	if (url_params != '?') {
		params = url_params
	}
M
mehaotian 已提交
254 255 256 257 258 259 260 261 262 263

	// 兼容不同小程序下的不同取值
	let route : string

	// #ifdef MP-WEIXIN
	route = (_self.$scope && _self.$scope.route) ?? ""
	// #endif
	// #ifndef MP-WEIXIN
	route = _self.route
	// #endif
M
mehaotian 已提交
264 265 266 267 268 269

	const data : RouteParams = {
		path: route,
		fullpath: route + params
	}
	return data
M
mehaotian 已提交
270 271 272 273 274 275 276 277
}


/**
 * 获取页面url, 包含参数 ,暂时不用,合并到 get_route
 * @param {ComponentPublicInstance} page 页面实例
 */
export function get_page_route(page : Page | null = null) : string {
M
mehaotian 已提交
278 279 280 281 282 283 284 285 286
	let _self = page ?? get_page_vm()
	let lastPageRoute = uni.getStorageSync('_STAT_LAST_PAGE_ROUTE')
	if (_self == null) {
		return lastPageRoute ?? ''
	}
	// 如果找不到 fullPath 就取 route 的值
	// TODO 取完整路径,目前最新为 optiosn 为携带参数,需要手动序列化
	// return page.fullPath === '/' ? page.route : page.fullPath || page.route
	return _self.route
M
mehaotian 已提交
287 288 289 290 291 292 293
}

/**
 * 是否上报页面数据
 * @returns
 */
export const is_page_report = () : boolean => {
M
mehaotian 已提交
294 295 296 297 298 299 300 301
	const uniStatConfig = Config.getOptions();
	const collectItems = uniStatConfig.collectItems;
	if (collectItems != null) {
		const statPageLog = collectItems.uniStatPageLog
		if (statPageLog == null) return true
		return typeof statPageLog == 'boolean' ? statPageLog : true
	}
	return true
M
mehaotian 已提交
302 303 304 305 306 307 308 309 310
}


/**
 * 是否已处理设备 DeviceId
 * 如果值为 1 则表示已处理
 */
const IS_HANDLE_DEVECE_ID = 'is_handle_device_id'
export const is_handle_device = () : boolean => {
M
mehaotian 已提交
311 312 313
	let isHandleDevice = dbGet(IS_HANDLE_DEVECE_ID) ?? ''
	dbSet(IS_HANDLE_DEVECE_ID, '1')
	return isHandleDevice === '1'
M
mehaotian 已提交
314 315 316 317 318 319 320 321
}


/**
 * 获取上报数据默认值
 */
export const get_default_data = () : StatDefault => {

M
mehaotian 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
	let statData : StatDefault = {
		uuid: get_uuid(),
		ak: APPID,
		p: sys.osName == 'android' ? 'a' : 'i',
		ut: get_platform_name(),
		mpn: get_pack_name(),
		usv: STAT_VERSION,
		v: RUNTIME_VERSION,
		ch: get_channel(),
		cn: '',
		pn: '',
		ct: '',
		t: get_time(),
		tt: '',
		brand: sys.deviceBrand,
		md: sys.deviceModel,
		sv: sys.osVersion.replace(/(Android|iOS)\s/, ''),
		mpsdk: sys.SDKVersion,
		mpv: sys.uniCompilerVersionCode.toString(),
		// mpv: '',
		lang: sys.osLanguage,
		pr: sys.devicePixelRatio,
		ww: sys.windowWidth,
		wh: sys.windowHeight,
		sw: sys.screenWidth,
		sh: sys.screenHeight,
		lat: '',
		lng: '',
		net: '',
		odid: ''
	}
	return statData
M
mehaotian 已提交
354 355 356 357 358 359 360
}

/**
 * 获取上报时间间隔
 * @param {*} defaultTime 默认上报间隔时间 单位s
 */
export const get_report_Interval = (defaultTime : number) : number => {
M
mehaotian 已提交
361 362 363 364 365 366 367 368 369 370 371
	const uniStatConfig = Config.getOptions()
	let time = uniStatConfig.reportInterval
	// let reg = /(^[1-9]\d*$)/
	// 如果不是整数,则默认为上报间隔时间
	// if (!reg.test(time)) return defaultTime
	if (!IsNumber(time)) return defaultTime

	// 如果上报时间配置为0 相当于立即上报
	if (time == 0) return 0
	// time = time ?? defaultTime
	return time as number
M
mehaotian 已提交
372 373 374 375 376 377 378
}

/**
 * 获取uniCloud服务空间配置
 * @returns {Object}
 */
export const uni_cloud_config = () : UniCloudInitOptions | null => {
M
mehaotian 已提交
379
	// TODO 当前版本暂时不使用
M
mehaotian 已提交
380 381
	// return process.env.UNI_STAT_UNI_CLOUD || {}
	return null
M
mehaotian 已提交
382 383 384 385 386 387 388 389
}

/**
 * 获取服务空间
 * @param {*} config
 * @returns
 */
export const get_space = (config : UniCloudInitOptions) : UniCloudInitOptions | null => {
M
mehaotian 已提交
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
	const uniCloudConfig = uni_cloud_config()
	if (uniCloudConfig == null) {
		// #ifdef APP-ANDROID
		if (config.spaceId != '') {
			return config
		}
		// #endif
		// #ifndef APP-ANDROID
		if (config?.spaceId != '') {
			return config
		}
		// #endif

		return null
	}

	let spaceId = uniCloudConfig.spaceId
	let provider = uniCloudConfig.provider
	let clientSecret = uniCloudConfig.clientSecret
	let secretKey = uniCloudConfig.secretKey
	let accessKey = uniCloudConfig.accessKey

	const space_type = ['tcb', 'tencent', 'aliyun', 'alipay']
	// @ts-ignore
	const is_space_id = spaceId != ''
	const is_provider = space_type.indexOf(provider) != -1
	const is_aliyun = provider == 'aliyun' && is_space_id && clientSecret != null
	const is_tcb = (provider == 'tcb' || provider === 'tencent') && is_space_id
	const is_alipay = provider == 'alipay' && is_space_id && secretKey != null && accessKey != null

	if (is_provider && (is_aliyun || is_tcb || is_alipay)) {
		return uniCloudConfig
	}

	return null
M
mehaotian 已提交
425 426 427 428 429 430
}

/**
 * 获取隐私协议配置
 */
export const is_push_clientid = () : boolean => {
M
mehaotian 已提交
431 432 433 434 435 436 437
	const uniStatConfig = Config.getOptions()
	const collectItems = uniStatConfig.collectItems
	if (collectItems != null) {
		const ClientID = collectItems.uniPushClientID ?? false
		return typeof ClientID == 'boolean' ? ClientID : false
	}
	return false
M
mehaotian 已提交
438 439 440 441 442 443 444
}



/**
 * 自定义事件参数校验
 */
M
mehaotian 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
export const calibration = (eventName : string, options : any | null) : ReportErrorCode | null => {
	// login 、 share 、pay_success 、pay_fail 、register 、title
	if (eventName == '') {
		// console.error(`uni.report Missing [eventName] parameter`)
		return 61003
	}
	if (typeof eventName != 'string') {
		// console.error(
		// 	`uni.report [eventName] Parameter type error, it can only be of type String`
		// )
		return 61004
	}
	if (eventName.length > 255) {
		// console.error(
		// 	`uni.report [eventName] Parameter length cannot be greater than 255`
		// )
		return 61005
	}

	if (typeof options != 'string' && typeof options != 'object') {
		// console.error(
		// 	'uni.report [options] Parameter type error, Only supports String or Object type'
		// )
		return 61006
	}

	if (typeof options == 'string' && (options as string).length > 255) {
		// console.error(
		// 	`uni.report [options] Parameter length cannot be greater than 255`
		// )
		return 61007
	}

	if (eventName == 'title' && typeof options != 'string') {
		// console.error(
		// 	`uni.report [eventName] When the parameter is title, the [options] parameter can only be of type String`
		// )
		return 61008
	}
	return null
}