提交 e1989e1f 编写于 作者: M mehaotian

fix: 修改统计报警问题

上级 cdc04c71
import { Stat } from '@/uni_modules/uni-stat' import { Stat } from '@/uni_modules/uni-stat'
// // UniStatOptions, UniStatCollectItemsOptions
// 实例统计sdk // 实例统计sdk
const stat_instance = Stat.getInstance() const stat_instance = Stat.getInstance()
...@@ -7,19 +6,23 @@ const stat_instance = Stat.getInstance() ...@@ -7,19 +6,23 @@ const stat_instance = Stat.getInstance()
const lifecycle = defineMixin({ const lifecycle = defineMixin({
// onLaunch(options : OnLaunchOptions) { stat_instance.onLaunch(options, this) }, // onLaunch(options : OnLaunchOptions) { stat_instance.onLaunch(options, this) },
// @ts-ignore // @ts-ignore
onLoad() { onLoad() {
// @ts-ignore
stat_instance.onLoad(this) stat_instance.onLoad(this)
}, },
// @ts-ignore // @ts-ignore
onShow() { onShow() {
// @ts-ignore
stat_instance.onShow(this) stat_instance.onShow(this)
}, },
// @ts-ignore // @ts-ignore
onHide() { onHide() {
// @ts-ignore
stat_instance.onHide(this) stat_instance.onHide(this)
}, },
// @ts-ignore // @ts-ignore
onUnload() { onUnload() {
// @ts-ignore
stat_instance.onUnload(this) stat_instance.onUnload(this)
}, },
// onError(error : string) { stat_instance.onError(error) } // onError(error : string) { stat_instance.onError(error) }
......
import { Report } from "./report.uts"; import { Report } from "./report.uts";
import { StatType } from "./stat-type"; import { StatType } from "./stat-type";
import { EventParams, UniStatOptions, ErrorCallback } from '../../interface.uts' import { EventParams, UniStatOptions, ErrorCallback,ReportErrorCode } from '../../interface.uts'
import { is_page, is_page_report, get_space, is_push_clientid, calibration } from '../utils/pageInfo.uts' import { is_page, is_page_report, get_space, is_push_clientid, calibration } from '../utils/pageInfo.uts'
import { Config } from "../config"; import { Config } from "../config";
...@@ -222,10 +222,9 @@ export class Stat { ...@@ -222,10 +222,9 @@ export class Stat {
// 生命周期监听,暂时无用,需要手动调用api // 生命周期监听,暂时无用,需要手动调用api
} }
// 自定义参数上报 // 自定义参数上报
// fn : ErrorCallback
appEvent(name : string, options : any | null = null, fn : ErrorCallback) { appEvent(name : string, options : any | null = null, fn : ErrorCallback) {
if (!Stat.is_register) { if (!Stat.is_register) {
fn(false, '统计服务尚未初始化,请在main.uts中引入统计插件。') fn(false, 61001 as ReportErrorCode)
return return
} }
// const names = ['uni-app-launch', 'uni-app-show', 'uni-app-hide', 'uni-app-error'] // const names = ['uni-app-launch', 'uni-app-show', 'uni-app-hide', 'uni-app-error']
...@@ -235,50 +234,59 @@ export class Stat { ...@@ -235,50 +234,59 @@ export class Stat {
// return // return
// } // }
if (name == 'uni-app-launch' && options == null) { if (name == 'uni-app-launch' && options == null) {
fn(false, 'uniStatReport options参数错误,请检查!') fn(false, 61002 as ReportErrorCode)
return return
} }
fn(true, 'report:ok')
if (name == 'uni-app-launch') { if (name == 'uni-app-launch') {
this.registerEvent(StatType.LifeCycleLaunch, null, options)
// StatType.LifeCycleLaunch, null, options as OnLaunchOptions // 61001 占位,无实际用途
this.registerEvent(StatType.LifeCycleLaunch, null, options) fn(true, 61001 as ReportErrorCode)
return return
} }
if (name == 'uni-app-show') { if (name == 'uni-app-show') {
this.registerEvent(StatType.LifeCycleAppShow, null, null) this.registerEvent(StatType.LifeCycleAppShow, null, null)
// 61001 占位,无实际用途
fn(true, 61001 as ReportErrorCode)
return return
} }
if (name == 'uni-app-hide') { if (name == 'uni-app-hide') {
this.registerEvent(StatType.LifeCycleAppHide, null, null) this.registerEvent(StatType.LifeCycleAppHide, null, null)
// 61001 占位,无实际用途
fn(true, 61001 as ReportErrorCode)
return return
} }
if (name == 'uni-page-show') { if (name == 'uni-page-show') {
this.report.pageShow(options as Page) this.report.pageShow(options as Page)
// 61001 占位,无实际用途
fn(true, 61001 as ReportErrorCode)
return return
} }
if (name == 'uni-page-hide') { if (name == 'uni-page-hide') {
this.report.pageHide(options as Page) this.report.pageHide(options as Page)
// 61001 占位,无实际用途
fn(true, 61001 as ReportErrorCode)
return return
} }
if (name == 'uni-app-error') { if (name == 'uni-app-error') {
this.registerEvent(StatType.LifeCycleError, null, null, options)
this.registerEvent(StatType.LifeCycleError, null, null, options) // 61001 占位,无实际用途
fn(true, 61001 as ReportErrorCode)
return return
} }
// 校验 type 参数 // 校验 type 参数
const is_calibration = calibration(name, options) const is_calibration = calibration(name, options)
if (is_calibration) { if (is_calibration != null) {
fn(false, is_calibration)
return return
} }
if (name === 'title') { if (name === 'title') {
this.report._navigationBarTitle.report = (options as string) this.report._navigationBarTitle.report = (options as string)
return
} }
const value = (typeof options === 'object' ? JSON.stringify(options) : options) as string const value = (typeof options === 'object' ? JSON.stringify(options) : options) as string
...@@ -286,7 +294,6 @@ export class Stat { ...@@ -286,7 +294,6 @@ export class Stat {
key: name, key: name,
value: value as string, value: value as string,
} }
this.report.sendEventRequest(data) this.report.sendEventRequest(data)
} }
} }
import { OnLaunchOptionsWithCst, RouteParams, StatDefault } from '../../interface.uts' import { OnLaunchOptionsWithCst, RouteParams, StatDefault, ReportErrorCode } from '../../interface.uts'
import { STAT_VERSION, sys, sysAppBase, Config } from '../config.uts' import { STAT_VERSION, sys, sysAppBase, Config } from '../config.uts'
// import Config from '../config.uts' // import Config from '../config.uts'
import { get_time } from './pageTime.uts' import { get_time } from './pageTime.uts'
...@@ -46,28 +46,28 @@ const RUNTIME_VERSION = sys.appVersion ...@@ -46,28 +46,28 @@ const RUNTIME_VERSION = sys.appVersion
* 获取uuid * 获取uuid
*/ */
export const get_uuid = () : string => { export const get_uuid = () : string => {
// 有可能不存在 deviceId(一般不存在就是出bug了),就自己生成一个 // 有可能不存在 deviceId(一般不存在就是出bug了),就自己生成一个
// 目前 deviceId 肯定存在所以不用生成 // 目前 deviceId 肯定存在所以不用生成
// const uuid : string = sys.deviceId ?? getUuid() // const uuid : string = sys.deviceId ?? getUuid()
return sys.deviceId return sys.deviceId
} }
/** /**
* 获取老版的 deviceid ,兼容以前的错误 deviceid * 获取老版的 deviceid ,兼容以前的错误 deviceid
*/ */
export const get_odid = () : string => { export const get_odid = () : string => {
// let odid : string // let odid : string
// if (get_platform_name() === 'n') { // if (get_platform_name() === 'n') {
// try { // try {
// odid = plus.device.uuid // odid = plus.device.uuid
// } catch (e) { // } catch (e) {
// odid = '' // odid = ''
// } // }
// return odid // return odid
// } // }
// odid = sys.deviceId ?? getUuid() // odid = sys.deviceId ?? getUuid()
// TODO 需要获取老版本的uuid ,目前无法获取,只返回 deviceId // TODO 需要获取老版本的uuid ,目前无法获取,只返回 deviceId
return sys.deviceId return sys.deviceId
} }
...@@ -89,94 +89,94 @@ export const get_odid = () : string => { ...@@ -89,94 +89,94 @@ export const get_odid = () : string => {
*/ */
export const get_platform_name = () : string => { export const get_platform_name = () : string => {
let platformList = { let platformList = {
'app': 'n', 'app': 'n',
'app-plus': 'n', 'app-plus': 'n',
'h5': 'h5', 'h5': 'h5',
'web': 'web', 'web': 'web',
'mp-weixin': 'wx', 'mp-weixin': 'wx',
'mp-baidu': 'bd', 'mp-baidu': 'bd',
'mp-toutiao': 'tt', 'mp-toutiao': 'tt',
'mp-qq': 'qq', 'mp-qq': 'qq',
'quickapp-native': 'qn', 'quickapp-native': 'qn',
'mp-kuaishou': 'ks', 'mp-kuaishou': 'ks',
'mp-lark': 'lark', 'mp-lark': 'lark',
'quickapp-webview': 'qw', 'quickapp-webview': 'qw',
} }
// 苹果审核代码中禁止出现 alipay 字样 ,需要特殊处理一下 // 苹果审核代码中禁止出现 alipay 字样 ,需要特殊处理一下
const aliArr = ['y', 'a', 'p', 'mp-ali'] const aliArr = ['y', 'a', 'p', 'mp-ali']
const aliKey = aliArr.reverse().join('') const aliKey = aliArr.reverse().join('')
platformList[aliKey] = 'ali' platformList[aliKey] = 'ali'
const platform = sys.uniPlatform const platform = sys.uniPlatform
// TODO 兼容子平台 // TODO 兼容子平台
// if (platformList[process.env.VUE_APP_PLATFORM] === 'ali') { // if (platformList[process.env.VUE_APP_PLATFORM] === 'ali') {
// if (my && my.env) { // if (my && my.env) {
// const clientName = my.env.clientName // const clientName = my.env.clientName
// if (clientName === 'ap') return 'ali' // if (clientName === 'ap') return 'ali'
// if (clientName === 'dingtalk') return 'dt' // if (clientName === 'dingtalk') return 'dt'
// // TODO 缺少 ali 下的其他平台 // // TODO 缺少 ali 下的其他平台
// } // }
// } // }
return platformList[platform] as string return platformList[platform] as string
} }
/** /**
* 获取原生包名,或小程序 appid * 获取原生包名,或小程序 appid
*/ */
export const get_pack_name = () : string => { export const get_pack_name = () : string => {
let packName = '' let packName = ''
if (get_platform_name() === 'n') { if (get_platform_name() === 'n') {
if (sys.osName == 'android') { if (sys.osName == 'android') {
packName = sysAppBase.packageName ?? '' packName = sysAppBase.packageName ?? ''
} }
if (sys.osName == 'ios') { if (sys.osName == 'ios') {
packName = sysAppBase.bundleId ?? '' packName = sysAppBase.bundleId ?? ''
} }
} }
return packName return packName
} }
/** /**
* 应用版本 * 应用版本
*/ */
export const get_version = () : string => { export const get_version = () : string => {
return RUNTIME_VERSION return RUNTIME_VERSION
} }
/** /**
* 获取渠道 * 获取渠道
*/ */
export const get_channel = () : string => { export const get_channel = () : string => {
const platformName = get_platform_name() const platformName = get_platform_name()
let channel:string = '' let channel : string = ''
if (platformName === 'n') { if (platformName === 'n') {
channel = sysAppBase.channel ?? '' channel = sysAppBase.channel ?? ''
} }
// if (platformName === 'wx') { // if (platformName === 'wx') {
// // TODO 需要调研小程序二维码渠道如何获取; // // TODO 需要调研小程序二维码渠道如何获取;
// } // }
return channel return channel
} }
/** /**
* 获取小程序场景值 * 获取小程序场景值
*/ */
export const get_scene = (_ : OnLaunchOptionsWithCst) : number => { export const get_scene = (_ : OnLaunchOptionsWithCst) : number => {
// TODO 场景值获取有问题 ,暂时硬编码,需要修改 // TODO 场景值获取有问题 ,暂时硬编码,需要修改
// options : OnLaunchOptionsWithCst // options : OnLaunchOptionsWithCst
// const platformName = get_platform_name() // const platformName = get_platform_name()
// let scene = '' // let scene = ''
// if (options) { // if (options) {
// return options // return options
// } // }
// if (platformName === 'wx') { // if (platformName === 'wx') {
// scene = uni.getLaunchOptionsSync().scene // scene = uni.getLaunchOptionsSync().scene
// } // }
// return scene // return scene
// return options.scene as number // return options.scene as number
return 1001 return 1001
} }
/** /**
...@@ -185,13 +185,13 @@ export const get_scene = (_ : OnLaunchOptionsWithCst) : number => { ...@@ -185,13 +185,13 @@ export const get_scene = (_ : OnLaunchOptionsWithCst) : number => {
*/ */
// @ts-ignore // @ts-ignore
export const get_page_types = (appInstance : ComponentPublicInstance) : string => { export const get_page_types = (appInstance : ComponentPublicInstance) : string => {
// TODO 暂时无法获取是否应用还是页面,写的硬编码,只支持页面调用 // TODO 暂时无法获取是否应用还是页面,写的硬编码,只支持页面调用
// #ifdef WEB || APP-IOS // #ifdef WEB || APP-IOS
return appInstance?.$mpType ?? 'page' return appInstance?.$mpType ?? 'page'
// #endif // #endif
// #ifndef WEB // #ifndef WEB
return 'page' return 'page'
// #endif // #endif
} }
/** /**
...@@ -200,14 +200,14 @@ export const get_page_types = (appInstance : ComponentPublicInstance) : string = ...@@ -200,14 +200,14 @@ export const get_page_types = (appInstance : ComponentPublicInstance) : string =
*/ */
// @ts-ignore // @ts-ignore
export const is_page = (appInstance : ComponentPublicInstance) : Boolean => { export const is_page = (appInstance : ComponentPublicInstance) : Boolean => {
// #ifdef WEB ||APP-IOS // #ifdef WEB ||APP-IOS
const type = appInstance?.$mpType ?? 'page' const type = appInstance?.$mpType ?? 'page'
return type == 'page' ? true : false return type == 'page' ? true : false
// #endif // #endif
// #ifndef WEB // #ifndef WEB
// 其他平台没有 $mpType ,只有页面触发 // 其他平台没有 $mpType ,只有页面触发
return true return true
// #endif // #endif
} }
...@@ -217,39 +217,39 @@ export const is_page = (appInstance : ComponentPublicInstance) : Boolean => { ...@@ -217,39 +217,39 @@ export const is_page = (appInstance : ComponentPublicInstance) : Boolean => {
* @param {String} routepath 页面路由 * @param {String} routepath 页面路由
*/ */
export const get_page_name = (routepath : string) : string => { export const get_page_name = (routepath : string) : string => {
let page = get_page_vm() let page = get_page_vm()
if (page == null) return '' if (page == null) return ''
if (page.route != routepath) { if (page.route != routepath) {
const pages = getCurrentPages() const pages = getCurrentPages()
// 如果传入路由与当前页面不同,则从页面栈找一个,如果找不到返回空 // 如果传入路由与当前页面不同,则从页面栈找一个,如果找不到返回空
let page_now = pages.find((p): boolean => p.route == routepath) let page_now = pages.find((p) : boolean => p.route == routepath)
if (page_now == null) { if (page_now == null) {
return '' return ''
} }
// @ts-ignore // @ts-ignore
page = page_now.vm! page = page_now.vm!
} }
// @ts-ignore // @ts-ignore
const pageStyle = (page.$page as UniPage).getPageStyle() const pageStyle = (page.$page as UniPage).getPageStyle()
const titleText = pageStyle['navigationBarTitleText'] ?? '' const titleText = pageStyle['navigationBarTitleText'] ?? ''
return titleText as string return titleText as string
} }
/** /**
* 获取页面实例 * 获取页面实例
*/ */
export const get_page_vm = () : Page | null => { export const get_page_vm = () : Page | null => {
let pages = getCurrentPages() let pages = getCurrentPages()
if (pages.length == 0) { if (pages.length == 0) {
return null return null
} }
let page = pages[pages.length - 1] let page = pages[pages.length - 1]
// TODO 正常来说,调用当前方法的地方只在 生命周期内,数组内最少会有一个页面,所以理论上是不存在获取不到的情况的 // TODO 正常来说,调用当前方法的地方只在 生命周期内,数组内最少会有一个页面,所以理论上是不存在获取不到的情况的
// @ts-ignore // @ts-ignore
return page.vm return page.vm
} }
...@@ -258,38 +258,38 @@ export const get_page_vm = () : Page | null => { ...@@ -258,38 +258,38 @@ export const get_page_vm = () : Page | null => {
* @param {ComponentPublicInstance} page 页面实例 * @param {ComponentPublicInstance} page 页面实例
*/ */
export function get_route(page : Page | null = null) : RouteParams { export function get_route(page : Page | null = null) : RouteParams {
let _self = page ?? get_page_vm() let _self = page ?? get_page_vm()
if (_self == null) { if (_self == null) {
const data : RouteParams = { const data : RouteParams = {
path: '', path: '',
fullpath: '' fullpath: ''
} }
return data return data
} }
// TODO 条件编译处理参数问题,安卓上 options返回的是map,需要处理成 utsobject // TODO 条件编译处理参数问题,安卓上 options返回的是map,需要处理成 utsobject
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
const opts = new UTSJSONObject(_self.options) const opts = new UTSJSONObject(_self.options)
// TODO 上报页面参数需要处理 // TODO 上报页面参数需要处理
let url_params = Serialize(opts) let url_params = Serialize(opts)
// #endif // #endif
// #ifndef APP-ANDROID // #ifndef APP-ANDROID
// @ts-ignore // @ts-ignore
let url_params = Serialize(_self.options) let url_params = Serialize(_self.options)
// #endif // #endif
let params = '' let params = ''
// 如果参数只有 ?则说明没有参数 // 如果参数只有 ?则说明没有参数
if (url_params != '?') { if (url_params != '?') {
params = url_params params = url_params
} }
let route = _self.route let route = _self.route
const data : RouteParams = { const data : RouteParams = {
path: route, path: route,
fullpath: route + params fullpath: route + params
} }
return data return data
} }
...@@ -298,15 +298,15 @@ export function get_route(page : Page | null = null) : RouteParams { ...@@ -298,15 +298,15 @@ export function get_route(page : Page | null = null) : RouteParams {
* @param {ComponentPublicInstance} page 页面实例 * @param {ComponentPublicInstance} page 页面实例
*/ */
export function get_page_route(page : Page | null = null) : string { export function get_page_route(page : Page | null = null) : string {
let _self = page ?? get_page_vm() let _self = page ?? get_page_vm()
let lastPageRoute = uni.getStorageSync('_STAT_LAST_PAGE_ROUTE') let lastPageRoute = uni.getStorageSync('_STAT_LAST_PAGE_ROUTE')
if (_self == null) { if (_self == null) {
return lastPageRoute ?? '' return lastPageRoute ?? ''
} }
// 如果找不到 fullPath 就取 route 的值 // 如果找不到 fullPath 就取 route 的值
// TODO 取完整路径,目前最新为 optiosn 为携带参数,需要手动序列化 // TODO 取完整路径,目前最新为 optiosn 为携带参数,需要手动序列化
// return page.fullPath === '/' ? page.route : page.fullPath || page.route // return page.fullPath === '/' ? page.route : page.fullPath || page.route
return _self.route return _self.route
} }
/** /**
...@@ -314,14 +314,14 @@ export function get_page_route(page : Page | null = null) : string { ...@@ -314,14 +314,14 @@ export function get_page_route(page : Page | null = null) : string {
* @returns * @returns
*/ */
export const is_page_report = () : boolean => { export const is_page_report = () : boolean => {
const uniStatConfig = Config.getOptions(); const uniStatConfig = Config.getOptions();
const collectItems = uniStatConfig.collectItems; const collectItems = uniStatConfig.collectItems;
if (collectItems != null) { if (collectItems != null) {
const statPageLog = collectItems.uniStatPageLog const statPageLog = collectItems.uniStatPageLog
if (statPageLog == null) return true if (statPageLog == null) return true
return typeof statPageLog == 'boolean' ? statPageLog : true return typeof statPageLog == 'boolean' ? statPageLog : true
} }
return true return true
} }
...@@ -331,9 +331,9 @@ export const is_page_report = () : boolean => { ...@@ -331,9 +331,9 @@ export const is_page_report = () : boolean => {
*/ */
const IS_HANDLE_DEVECE_ID = 'is_handle_device_id' const IS_HANDLE_DEVECE_ID = 'is_handle_device_id'
export const is_handle_device = () : boolean => { export const is_handle_device = () : boolean => {
let isHandleDevice = dbGet(IS_HANDLE_DEVECE_ID) ?? '' let isHandleDevice = dbGet(IS_HANDLE_DEVECE_ID) ?? ''
dbSet(IS_HANDLE_DEVECE_ID, '1') dbSet(IS_HANDLE_DEVECE_ID, '1')
return isHandleDevice === '1' return isHandleDevice === '1'
} }
...@@ -342,38 +342,38 @@ export const is_handle_device = () : boolean => { ...@@ -342,38 +342,38 @@ export const is_handle_device = () : boolean => {
*/ */
export const get_default_data = () : StatDefault => { export const get_default_data = () : StatDefault => {
let statData : StatDefault = { let statData : StatDefault = {
uuid: get_uuid(), uuid: get_uuid(),
ak: APPID, ak: APPID,
p: sys.osName == 'android' ? 'a' : 'i', p: sys.osName == 'android' ? 'a' : 'i',
ut: get_platform_name(), ut: get_platform_name(),
mpn: get_pack_name(), mpn: get_pack_name(),
usv: STAT_VERSION, usv: STAT_VERSION,
v: RUNTIME_VERSION, v: RUNTIME_VERSION,
ch: get_channel(), ch: get_channel(),
cn: '', cn: '',
pn: '', pn: '',
ct: '', ct: '',
t: get_time(), t: get_time(),
tt: '', tt: '',
brand: sys.deviceBrand, brand: sys.deviceBrand,
md: sys.deviceModel, md: sys.deviceModel,
sv: sys.osVersion.replace(/(Android|iOS)\s/, ''), sv: sys.osVersion.replace(/(Android|iOS)\s/, ''),
mpsdk: sys.SDKVersion, mpsdk: sys.SDKVersion,
mpv: sys.uniCompilerVersionCode.toString(), mpv: sys.uniCompilerVersionCode.toString(),
// mpv: '', // mpv: '',
lang: sys.osLanguage, lang: sys.osLanguage,
pr: sys.devicePixelRatio, pr: sys.devicePixelRatio,
ww: sys.windowWidth, ww: sys.windowWidth,
wh: sys.windowHeight, wh: sys.windowHeight,
sw: sys.screenWidth, sw: sys.screenWidth,
sh: sys.screenHeight, sh: sys.screenHeight,
lat: '', lat: '',
lng: '', lng: '',
net: '', net: '',
odid: '' odid: ''
} }
return statData return statData
} }
/** /**
...@@ -381,17 +381,17 @@ export const get_default_data = () : StatDefault => { ...@@ -381,17 +381,17 @@ export const get_default_data = () : StatDefault => {
* @param {*} defaultTime 默认上报间隔时间 单位s * @param {*} defaultTime 默认上报间隔时间 单位s
*/ */
export const get_report_Interval = (defaultTime : number) : number => { export const get_report_Interval = (defaultTime : number) : number => {
const uniStatConfig = Config.getOptions() const uniStatConfig = Config.getOptions()
let time = uniStatConfig.reportInterval let time = uniStatConfig.reportInterval
// let reg = /(^[1-9]\d*$)/ // let reg = /(^[1-9]\d*$)/
// 如果不是整数,则默认为上报间隔时间 // 如果不是整数,则默认为上报间隔时间
// if (!reg.test(time)) return defaultTime // if (!reg.test(time)) return defaultTime
if (!IsNumber(time)) return defaultTime if (!IsNumber(time)) return defaultTime
// 如果上报时间配置为0 相当于立即上报 // 如果上报时间配置为0 相当于立即上报
if (time == 0) return 0 if (time == 0) return 0
// time = time ?? defaultTime // time = time ?? defaultTime
return time as number return time as number
} }
...@@ -401,9 +401,9 @@ export const get_report_Interval = (defaultTime : number) : number => { ...@@ -401,9 +401,9 @@ export const get_report_Interval = (defaultTime : number) : number => {
* @returns {Object} * @returns {Object}
*/ */
export const uni_cloud_config = () : UniCloudInitOptions | null => { export const uni_cloud_config = () : UniCloudInitOptions | null => {
// return process.env.UNI_STAT_UNI_CLOUD || {} // return process.env.UNI_STAT_UNI_CLOUD || {}
// const custemUnicloudConfig: = {} // const custemUnicloudConfig: = {}
return null return null
} }
/** /**
...@@ -412,54 +412,54 @@ export const uni_cloud_config = () : UniCloudInitOptions | null => { ...@@ -412,54 +412,54 @@ export const uni_cloud_config = () : UniCloudInitOptions | null => {
* @returns * @returns
*/ */
export const get_space = (config : UniCloudInitOptions) : UniCloudInitOptions | null => { export const get_space = (config : UniCloudInitOptions) : UniCloudInitOptions | null => {
const uniCloudConfig = uni_cloud_config() const uniCloudConfig = uni_cloud_config()
if (uniCloudConfig == null) { if (uniCloudConfig == null) {
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
if (config.spaceId != '') { if (config.spaceId != '') {
return config return config
} }
// #endif // #endif
// #ifndef APP-ANDROID // #ifndef APP-ANDROID
if (config?.spaceId != '') { if (config?.spaceId != '') {
return config return config
} }
// #endif // #endif
return null return null
} }
let spaceId = uniCloudConfig.spaceId let spaceId = uniCloudConfig.spaceId
let provider = uniCloudConfig.provider let provider = uniCloudConfig.provider
let clientSecret = uniCloudConfig.clientSecret let clientSecret = uniCloudConfig.clientSecret
let secretKey = uniCloudConfig.secretKey let secretKey = uniCloudConfig.secretKey
let accessKey = uniCloudConfig.accessKey let accessKey = uniCloudConfig.accessKey
const space_type = ['tcb', 'tencent', 'aliyun', 'alipay'] const space_type = ['tcb', 'tencent', 'aliyun', 'alipay']
// @ts-ignore // @ts-ignore
const is_space_id = spaceId != '' const is_space_id = spaceId != ''
const is_provider = space_type.indexOf(provider) != -1 const is_provider = space_type.indexOf(provider) != -1
const is_aliyun = provider == 'aliyun' && is_space_id && clientSecret != null const is_aliyun = provider == 'aliyun' && is_space_id && clientSecret != null
const is_tcb = (provider == 'tcb' || provider === 'tencent') && is_space_id const is_tcb = (provider == 'tcb' || provider === 'tencent') && is_space_id
const is_alipay = provider == 'alipay' && is_space_id && secretKey != null && accessKey != null const is_alipay = provider == 'alipay' && is_space_id && secretKey != null && accessKey != null
if (is_provider && (is_aliyun || is_tcb || is_alipay)) { if (is_provider && (is_aliyun || is_tcb || is_alipay)) {
return uniCloudConfig return uniCloudConfig
} }
return null return null
} }
/** /**
* 获取隐私协议配置 * 获取隐私协议配置
*/ */
export const is_push_clientid = () : boolean => { export const is_push_clientid = () : boolean => {
const uniStatConfig = Config.getOptions() const uniStatConfig = Config.getOptions()
const collectItems = uniStatConfig.collectItems const collectItems = uniStatConfig.collectItems
if (collectItems != null) { if (collectItems != null) {
const ClientID = collectItems.uniPushClientID ?? false const ClientID = collectItems.uniPushClientID ?? false
return typeof ClientID == 'boolean' ? ClientID : false return typeof ClientID == 'boolean' ? ClientID : false
} }
return false return false
} }
...@@ -467,44 +467,44 @@ export const is_push_clientid = () : boolean => { ...@@ -467,44 +467,44 @@ export const is_push_clientid = () : boolean => {
/** /**
* 自定义事件参数校验 * 自定义事件参数校验
*/ */
export const calibration = (eventName : string, options : any | null) : boolean => { export const calibration = (eventName : string, options : any | null) : ReportErrorCode | null => {
// login 、 share 、pay_success 、pay_fail 、register 、title // login 、 share 、pay_success 、pay_fail 、register 、title
if (eventName == '') { if (eventName == '') {
console.error(`uni.report Missing [eventName] parameter`) // console.error(`uni.report Missing [eventName] parameter`)
return true return 61003
} }
if (typeof eventName != 'string') { if (typeof eventName != 'string') {
console.error( // console.error(
`uni.report [eventName] Parameter type error, it can only be of type String` // `uni.report [eventName] Parameter type error, it can only be of type String`
) // )
return true return 61004
} }
if (eventName.length > 255) { if (eventName.length > 255) {
console.error( // console.error(
`uni.report [eventName] Parameter length cannot be greater than 255` // `uni.report [eventName] Parameter length cannot be greater than 255`
) // )
return true return 61005
} }
if (typeof options != 'string' && typeof options != 'object') { if (typeof options != 'string' && typeof options != 'object') {
console.error( // console.error(
'uni.report [options] Parameter type error, Only supports String or Object type' // 'uni.report [options] Parameter type error, Only supports String or Object type'
) // )
return true return 61006
} }
if (typeof options == 'string' && (options as string).length > 255) { if (typeof options == 'string' && (options as string).length > 255) {
console.error( // console.error(
`uni.report [options] Parameter length cannot be greater than 255` // `uni.report [options] Parameter length cannot be greater than 255`
) // )
return true return 61007
} }
if (eventName == 'title' && typeof options != 'string') { if (eventName == 'title' && typeof options != 'string') {
console.error( // console.error(
`uni.report [eventName] When the parameter is title, the [options] parameter can only be of type String` // `uni.report [eventName] When the parameter is title, the [options] parameter can only be of type String`
) // )
return true return 61008
} }
return false return null
} }
\ No newline at end of file
import { Report, ReportOptions, ReportSuccess, ReportFail } from './interface.uts' import { Report, ReportOptions, ReportSuccess, ReportErrorCode } from './interface.uts'
import { ReportFailImpl } from "./unierror.uts"
import { Stat } from './common/core/stat.uts' import { Stat } from './common/core/stat.uts'
const stat = Stat.getInstance() const stat = Stat.getInstance()
...@@ -6,18 +7,16 @@ const stat = Stat.getInstance() ...@@ -6,18 +7,16 @@ const stat = Stat.getInstance()
export const report : Report = function (options : ReportOptions) { export const report : Report = function (options : ReportOptions) {
const name = options.name const name = options.name
const option = options.options const option = options.options
//创建一个UniError
stat.appEvent(name, option, (type : boolean, msg : string) => { stat.appEvent(name, option, (type : boolean, code : ReportErrorCode) => {
if (type) { if (type) {
const res : ReportSuccess = { const res : ReportSuccess = {
errMsg: 'report:ok', errMsg: 'report:ok',
} }
options.success?.(res) options.success?.(res)
options.complete?.(res) options.complete?.(res)
} else { } else {
const err : ReportFail = { let err = new ReportFailImpl(code);
errMsg: 'report fail:' + msg,
}
options.fail?.(err) options.fail?.(err)
options.complete?.(err) options.complete?.(err)
} }
...@@ -26,4 +25,4 @@ export const report : Report = function (options : ReportOptions) { ...@@ -26,4 +25,4 @@ export const report : Report = function (options : ReportOptions) {
export { Stat } from './common/core/stat.uts' export { Stat } from './common/core/stat.uts'
// --- 导出统计类型 --- // --- 导出统计类型 ---
export { UniStatOptions, UniStatCollectItemsOptions } from './interface.uts' export { UniStatOptions, UniStatCollectItemsOptions, ReportFail } from './interface.uts'
\ No newline at end of file
export type ReportSuccess = { /**
/** * 接口调用成功回调
*/
export type ReportSuccess = {
/**
* 成功的详细信息 * 成功的详细信息
*/ */
errMsg : string, errMsg : string
} }
export type ReportFail = { /**
* 错误码
*/
export type ReportErrorCode =
/** /**
* 错误的详细信息 * 统计服务尚未初始化,需在`main.uts`中引入统计插件
*/ */
errMsg : string, 61001 |
/**
* name参数是uni-app-launch时, options 参数未填写
*/
61002 |
/**
* name参数未填写
*/
61003 |
/**
* name参数类型错误,应为`String`类型
*/
61004 |
/**
* name参数长度超限,最大不超过255
*/
61005 |
/**
* options参数错误,应为String或Object类型
*/
61006 |
/**
* options参数为String类型时,长度超限,最大不超过255
*/
61007 |
/**
* name参数为title时,options参数类型错误,应为String
*/
61008
/**
* 错误回调
*/
export interface ReportError extends IUniError {
/**
* 错误码
*/
errCode : ReportErrorCode
} }
/**
* 接口调用失败回调
*/
export type ReportFail = ReportError
/**
* 接口调用成功回调
*/
export type ReportSuccessCallback = (res : ReportSuccess) => void
/**
* 接口调用失败回调
*/
export type ReportFailCallback = (err : ReportFail) => void
/**
* 接口调用结束回调(调用成功、失败都会执行)
*/
export type ReportCompleteCallback = (res : any) => void
/**
* Report 参数定义
*/
export type ReportOptions = { export type ReportOptions = {
/** /**
* 自定义事件名称,内置名称(不允许覆盖): * 自定义事件名称,内置名称不允许覆盖,可选值:
* uni-app-launch : 应用启动 * `uni-app-launch`:应用启动,options 参数必填,值为 onLaunch 返回值
* uni-app-show : 应用进入前台 * `uni-app-show`:应用进入前台
* uni-app-hide : 应用进入后台 * `uni-app-hide`:应用进入后台
* uni-app-error : 应用发生错误 * `uni-app-error`:应用发生错误,options 参数必填,值为错误信息,类型为String
* `title`:标题采集
* `自定义name`:用户自定义
*/ */
name : string name : string
/** /**
* 额外参数 * 额外参数
*/ */
options ?: any options ?: any | null
/** /**
* 接口调用结束的回调函数(调用成功、失败都会执行) * 接口调用成功的回调函数
*/ */
success ?: (res : ReportSuccess) => void success ?: ReportSuccessCallback | null,
/** /**
* 接口调用失败的回调函数 * 接口调用失败的回调函数
*/ */
fail ?: (res : ReportFail) => void fail ?: ReportFailCallback | null,
/** /**
* 接口调用成功的回调函数 * 接口调用结束的回调函数(调用成功、失败都会执行)
*/ */
complete ?: (res : any) => void complete ?: ReportCompleteCallback | null
} }
export type ReportResult = {}
/** /**
* 自定义事件信息 * 自定义事件信息
* @param {ReportOptions} options * @param {ReportOptions} options
...@@ -54,34 +118,50 @@ export type Report = (options : ReportOptions) => void ...@@ -54,34 +118,50 @@ export type Report = (options : ReportOptions) => void
export interface Uni { export interface Uni {
/** /**
* 统计自定义事件 * 统计自定义事件
* @description 统计自定义事件上报 * @description uni统计自定义上报方法。
* @param {ReportOptions} options * @param {ReportOptions} options 自定义事件参数
* @uniPlatform {
* "app": {
* "android": {
* "unixVer": "4.33"
* },
* "ios": {
* "unixVer": "4.33"
* }
* },
* "web": {
* "unixVer": "4.33"
* }
* }
* @example * @example
* ```typescript * ```typescript
* uni.report({ * uni.report({
* name:'uni-app-launch', * name:'uni-app-launch',
* success(res) { * success(res) {
* console.log(res); * console.log(res);
* } * },
* }) * fail(err) {
* console.log(err);
* }
* })
* ``` * ```
* @remark * @remark
* - 该接口需要同步调用 * - 该接口需要同步调用
*/ */
report(options : ReportOptions) : void, report(options : ReportOptions) : void;
} }
// 统计插件参数类型 // 统计插件参数类型
export type UniStatOptions = { export type UniStatOptions = {
/** /**
* 是否开启debug模式 * 是否开启debug模式
* @defaultValue false * @defaultValue false
*/ */
debug ?: boolean debug ?: boolean
/** /**
* 前端数据上报周期 ,单位s * 前端数据上报周期 ,单位s
* @defaultValue 10 * @defaultValue 10
*/ */
reportInterval ?: number reportInterval ?: number
/* 多服务空间配置 */ /* 多服务空间配置 */
uniCloud ?: UniCloudInitOptions uniCloud ?: UniCloudInitOptions
...@@ -90,15 +170,15 @@ export type UniStatOptions = { ...@@ -90,15 +170,15 @@ export type UniStatOptions = {
} }
export type UniStatCollectItemsOptions = { export type UniStatCollectItemsOptions = {
/** /**
* 是否开启推送PushClientID的采集 * 是否开启推送PushClientID的采集
* @defaultValue false * @defaultValue false
*/ */
uniPushClientID ?: boolean uniPushClientID ?: boolean
/** /**
* 是否开启页面数据采集 * 是否开启页面数据采集
* @defaultValue true * @defaultValue true
*/ */
uniStatPageLog ?: boolean uniStatPageLog ?: boolean
} }
...@@ -146,7 +226,6 @@ export type TitleConfigParams = { ...@@ -146,7 +226,6 @@ export type TitleConfigParams = {
report : string report : string
/** 统计数据类型 */ /** 统计数据类型 */
lt : string lt : string
} }
/** /**
...@@ -198,9 +277,9 @@ export type CustomUnicloudConfig = { ...@@ -198,9 +277,9 @@ export type CustomUnicloudConfig = {
/** secretId */ /** secretId */
secretId ?: string secretId ?: string
} }
/** /**
* 事件类型 * 事件类型
*/ */
export type EventParams = { export type EventParams = {
/** 事件名字*/ /** 事件名字*/
...@@ -303,9 +382,9 @@ export type StatDefault = { ...@@ -303,9 +382,9 @@ export type StatDefault = {
/** 经度 */ /** 经度 */
lng ?: string lng ?: string
/** 网络 */ /** 网络 */
net ?: string net ?: string
/** 错误信息 */ /** 错误信息 */
em ?:string em ?: string
} }
/** /**
...@@ -449,4 +528,4 @@ export type PageReportParams = { ...@@ -449,4 +528,4 @@ export type PageReportParams = {
cst ?: number cst ?: number
} }
export type ErrorCallback = (is_err : boolean, errMsg : string) => void export type ErrorCallback = (is_err : boolean, code : ReportErrorCode) => void
import { ReportErrorCode, ReportError } from "./interface.uts"
/**
* 错误主题
*/
export const ReportUniErrorSubject = 'uni-report';
/**
* 错误码
* @UniError
*/
export const ReportUniErrors:Map<number, string> = new Map([
/**
* 统计已集成,但未初始化
*/
[61001, '统计服务尚未初始化,请在main.uts中引入统计插件!'],
/**
* 调用失败
*/
[61002, 'uni-app-launch 下 options 参数必填,请检查!'],
[61003, 'Report的 name参数必填'],
[61004, 'Report的name参数类型必须为字符串'],
[61005, 'Report的name参数长度最大为255'],
[61006, 'Report的options参数只能为String或者Object类型'],
[61007, 'Report的options参数若为String类型,则长度最大为255'],
[61008, 'Report的name参数为title时,options参数类型只能为String'],
]);
/**
* ReportFail的实现
*/
export class ReportFailImpl extends UniError implements ReportError {
override errCode: ReportErrorCode
constructor (
errCode: ReportErrorCode
) {
super()
this.errSubject = ReportUniErrorSubject
this.errCode = errCode
this.errMsg = ReportUniErrors.get(errCode) ?? ''
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册