提交 e1989e1f 编写于 作者: M mehaotian

fix: 修改统计报警问题

上级 cdc04c71
import { Stat } from '@/uni_modules/uni-stat'
// // UniStatOptions, UniStatCollectItemsOptions
// 实例统计sdk
const stat_instance = Stat.getInstance()
......@@ -7,19 +6,23 @@ const stat_instance = Stat.getInstance()
const lifecycle = defineMixin({
// onLaunch(options : OnLaunchOptions) { stat_instance.onLaunch(options, this) },
// @ts-ignore
onLoad() {
onLoad() {
// @ts-ignore
stat_instance.onLoad(this)
},
// @ts-ignore
onShow() {
onShow() {
// @ts-ignore
stat_instance.onShow(this)
},
// @ts-ignore
onHide() {
onHide() {
// @ts-ignore
stat_instance.onHide(this)
},
// @ts-ignore
onUnload() {
onUnload() {
// @ts-ignore
stat_instance.onUnload(this)
},
// onError(error : string) { stat_instance.onError(error) }
......
import { Report } from "./report.uts";
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 { Config } from "../config";
......@@ -222,10 +222,9 @@ export class Stat {
// 生命周期监听,暂时无用,需要手动调用api
}
// 自定义参数上报
// fn : ErrorCallback
appEvent(name : string, options : any | null = null, fn : ErrorCallback) {
if (!Stat.is_register) {
fn(false, '统计服务尚未初始化,请在main.uts中引入统计插件。')
fn(false, 61001 as ReportErrorCode)
return
}
// const names = ['uni-app-launch', 'uni-app-show', 'uni-app-hide', 'uni-app-error']
......@@ -235,50 +234,59 @@ export class Stat {
// return
// }
if (name == 'uni-app-launch' && options == null) {
fn(false, 'uniStatReport options参数错误,请检查!')
fn(false, 61002 as ReportErrorCode)
return
}
fn(true, 'report:ok')
}
if (name == 'uni-app-launch') {
// StatType.LifeCycleLaunch, null, options as OnLaunchOptions
this.registerEvent(StatType.LifeCycleLaunch, null, options)
this.registerEvent(StatType.LifeCycleLaunch, null, options)
// 61001 占位,无实际用途
fn(true, 61001 as ReportErrorCode)
return
}
if (name == 'uni-app-show') {
this.registerEvent(StatType.LifeCycleAppShow, null, null)
this.registerEvent(StatType.LifeCycleAppShow, null, null)
// 61001 占位,无实际用途
fn(true, 61001 as ReportErrorCode)
return
}
if (name == 'uni-app-hide') {
this.registerEvent(StatType.LifeCycleAppHide, null, null)
this.registerEvent(StatType.LifeCycleAppHide, null, null)
// 61001 占位,无实际用途
fn(true, 61001 as ReportErrorCode)
return
}
if (name == 'uni-page-show') {
this.report.pageShow(options as Page)
// 61001 占位,无实际用途
fn(true, 61001 as ReportErrorCode)
return
}
if (name == 'uni-page-hide') {
this.report.pageHide(options as Page)
// 61001 占位,无实际用途
fn(true, 61001 as ReportErrorCode)
return
}
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
}
// 校验 type 参数
const is_calibration = calibration(name, options)
if (is_calibration) {
if (is_calibration != null) {
fn(false, is_calibration)
return
}
if (name === 'title') {
this.report._navigationBarTitle.report = (options as string)
return
}
const value = (typeof options === 'object' ? JSON.stringify(options) : options) as string
......@@ -286,7 +294,6 @@ export class Stat {
key: name,
value: value as string,
}
this.report.sendEventRequest(data)
}
}
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'
const stat = Stat.getInstance()
......@@ -6,18 +7,16 @@ const stat = Stat.getInstance()
export const report : Report = function (options : ReportOptions) {
const name = options.name
const option = options.options
stat.appEvent(name, option, (type : boolean, msg : string) => {
if (type) {
//创建一个UniError
stat.appEvent(name, option, (type : boolean, code : ReportErrorCode) => {
if (type) {
const res : ReportSuccess = {
errMsg: 'report:ok',
}
options.success?.(res)
options.complete?.(res)
} else {
const err : ReportFail = {
errMsg: 'report fail:' + msg,
}
let err = new ReportFailImpl(code);
options.fail?.(err)
options.complete?.(err)
}
......@@ -26,4 +25,4 @@ export const report : Report = function (options : ReportOptions) {
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 =
/**
* 错误的详细信息
*/
errMsg : string,
* 统计服务尚未初始化,需在`main.uts`中引入统计插件
*/
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 = {
/**
* 自定义事件名称,内置名称(不允许覆盖):
* uni-app-launch : 应用启动
* uni-app-show : 应用进入前台
* uni-app-hide : 应用进入后台
* uni-app-error : 应用发生错误
* 自定义事件名称,内置名称不允许覆盖,可选值:
* `uni-app-launch`:应用启动,options 参数必填,值为 onLaunch 返回值
* `uni-app-show`:应用进入前台
* `uni-app-hide`:应用进入后台
* `uni-app-error`:应用发生错误,options 参数必填,值为错误信息,类型为String
* `title`:标题采集
* `自定义name`:用户自定义
*/
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
......@@ -54,34 +118,50 @@ export type Report = (options : ReportOptions) => void
export interface Uni {
/**
* 统计自定义事件
* @description 统计自定义事件上报
* @param {ReportOptions} options
* @description uni统计自定义上报方法。
* @param {ReportOptions} options 自定义事件参数
* @uniPlatform {
* "app": {
* "android": {
* "unixVer": "4.33"
* },
* "ios": {
* "unixVer": "4.33"
* }
* },
* "web": {
* "unixVer": "4.33"
* }
* }
* @example
* ```typescript
* uni.report({
* name:'uni-app-launch',
* success(res) {
* console.log(res);
* }
* })
* uni.report({
* name:'uni-app-launch',
* success(res) {
* console.log(res);
* },
* fail(err) {
* console.log(err);
* }
* })
* ```
* @remark
* - 该接口需要同步调用
*/
report(options : ReportOptions) : void,
report(options : ReportOptions) : void;
}
// 统计插件参数类型
export type UniStatOptions = {
/**
* 是否开启debug模式
* @defaultValue false
*/
/**
* 是否开启debug模式
* @defaultValue false
*/
debug ?: boolean
/**
* 前端数据上报周期 ,单位s
* @defaultValue 10
*/
/**
* 前端数据上报周期 ,单位s
* @defaultValue 10
*/
reportInterval ?: number
/* 多服务空间配置 */
uniCloud ?: UniCloudInitOptions
......@@ -90,15 +170,15 @@ export type UniStatOptions = {
}
export type UniStatCollectItemsOptions = {
/**
* 是否开启推送PushClientID的采集
* @defaultValue false
*/
/**
* 是否开启推送PushClientID的采集
* @defaultValue false
*/
uniPushClientID ?: boolean
/**
* 是否开启页面数据采集
* @defaultValue true
*/
/**
* 是否开启页面数据采集
* @defaultValue true
*/
uniStatPageLog ?: boolean
}
......@@ -146,7 +226,6 @@ export type TitleConfigParams = {
report : string
/** 统计数据类型 */
lt : string
}
/**
......@@ -198,9 +277,9 @@ export type CustomUnicloudConfig = {
/** secretId */
secretId ?: string
}
/**
* 事件类型
/**
* 事件类型
*/
export type EventParams = {
/** 事件名字*/
......@@ -303,9 +382,9 @@ export type StatDefault = {
/** 经度 */
lng ?: string
/** 网络 */
net ?: string
/** 错误信息 */
em ?:string
net ?: string
/** 错误信息 */
em ?: string
}
/**
......@@ -449,4 +528,4 @@ export type PageReportParams = {
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.
先完成此消息的编辑!
想要评论请 注册