提交 1643d812 编写于 作者: M mehaotian

fix: 修改统计报警问题

上级 f69744df
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 { 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.
先完成此消息的编辑!
想要评论请 注册