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

fix: 修改统计报警问题

上级 f69744df
import { Stat } from '@/uni_modules/uni-stat'
// // UniStatOptions, UniStatCollectItemsOptions
// 实例统计sdk
const stat_instance = Stat.getInstance()
......@@ -8,18 +7,22 @@ const lifecycle = defineMixin({
// onLaunch(options : OnLaunchOptions) { stat_instance.onLaunch(options, this) },
// @ts-ignore
onLoad() {
// @ts-ignore
stat_instance.onLoad(this)
},
// @ts-ignore
onShow() {
// @ts-ignore
stat_instance.onShow(this)
},
// @ts-ignore
onHide() {
// @ts-ignore
stat_instance.onHide(this)
},
// @ts-ignore
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
if (name == 'uni-app-launch') {
this.registerEvent(StatType.LifeCycleLaunch, null, options)
// 61001 占位,无实际用途
fn(true, 61001 as ReportErrorCode)
return
}
if (name == 'uni-app-show') {
this.registerEvent(StatType.LifeCycleAppShow, null, null)
// 61001 占位,无实际用途
fn(true, 61001 as ReportErrorCode)
return
}
if (name == 'uni-app-hide') {
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)
// 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 { OnLaunchOptionsWithCst, RouteParams, StatDefault } from '../../interface.uts'
import { OnLaunchOptionsWithCst, RouteParams, StatDefault, ReportErrorCode } from '../../interface.uts'
import { STAT_VERSION, sys, sysAppBase, Config } from '../config.uts'
// import Config from '../config.uts'
import { get_time } from './pageTime.uts'
......@@ -150,7 +150,7 @@ export const get_version = () : string => {
*/
export const get_channel = () : string => {
const platformName = get_platform_name()
let channel:string = ''
let channel : string = ''
if (platformName === 'n') {
channel = sysAppBase.channel ?? ''
}
......@@ -224,7 +224,7 @@ export const get_page_name = (routepath : string) : string => {
if (page.route != routepath) {
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) {
return ''
}
......@@ -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
if (eventName == '') {
console.error(`uni.report Missing [eventName] parameter`)
return true
// 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 true
// 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 true
// 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 true
// 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 true
// 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 true
// console.error(
// `uni.report [eventName] When the parameter is title, the [options] parameter can only be of type String`
// )
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'
const stat = Stat.getInstance()
......@@ -6,8 +7,8 @@ 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) => {
//创建一个UniError
stat.appEvent(name, option, (type : boolean, code : ReportErrorCode) => {
if (type) {
const res : ReportSuccess = {
errMsg: 'report:ok',
......@@ -15,9 +16,7 @@ export const report : Report = function (options : ReportOptions) {
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 = {
/**
* 成功的详细信息
*/
errMsg : string,
errMsg : string
}
export type ReportFail = {
/**
* 错误码
*/
export type ReportErrorCode =
/**
* 统计服务尚未初始化,需在`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 {
/**
* 错误的详细信息
* 错误码
*/
errMsg : string,
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,21 +118,37 @@ 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);
* },
* fail(err) {
* console.log(err);
* }
* })
* ```
* @remark
* - 该接口需要同步调用
*/
report(options : ReportOptions) : void,
report(options : ReportOptions) : void;
}
// 统计插件参数类型
......@@ -146,7 +226,6 @@ export type TitleConfigParams = {
report : string
/** 统计数据类型 */
lt : string
}
/**
......@@ -305,7 +384,7 @@ export type StatDefault = {
/** 网络 */
net ?: string
/** 错误信息 */
em ?: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.
先完成此消息的编辑!
想要评论请 注册