From 1643d812fff885e5f4c0b4fa8920999265aa2539 Mon Sep 17 00:00:00 2001 From: mehaotian <490272692@qq.com> Date: Fri, 15 Nov 2024 11:43:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E7=BB=9F=E8=AE=A1?= =?UTF-8?q?=E6=8A=A5=E8=AD=A6=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uni_modules/uni-stat/plugin.uts | 13 +- .../uni-stat/utssdk/common/core/stat.uts | 39 +- .../uni-stat/utssdk/common/utils/pageInfo.uts | 608 +++++++++--------- uni_modules/uni-stat/utssdk/index.uts | 15 +- uni_modules/uni-stat/utssdk/interface.uts | 193 ++++-- uni_modules/uni-stat/utssdk/unierror.uts | 43 ++ 6 files changed, 521 insertions(+), 390 deletions(-) diff --git a/uni_modules/uni-stat/plugin.uts b/uni_modules/uni-stat/plugin.uts index c95b88bc..9a5ece1c 100644 --- a/uni_modules/uni-stat/plugin.uts +++ b/uni_modules/uni-stat/plugin.uts @@ -1,5 +1,4 @@ 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) } diff --git a/uni_modules/uni-stat/utssdk/common/core/stat.uts b/uni_modules/uni-stat/utssdk/common/core/stat.uts index 69ed4197..ffcf8f75 100644 --- a/uni_modules/uni-stat/utssdk/common/core/stat.uts +++ b/uni_modules/uni-stat/utssdk/common/core/stat.uts @@ -1,7 +1,7 @@ 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) } } diff --git a/uni_modules/uni-stat/utssdk/common/utils/pageInfo.uts b/uni_modules/uni-stat/utssdk/common/utils/pageInfo.uts index 365a2afc..92684d6a 100644 --- a/uni_modules/uni-stat/utssdk/common/utils/pageInfo.uts +++ b/uni_modules/uni-stat/utssdk/common/utils/pageInfo.uts @@ -1,4 +1,4 @@ -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' @@ -46,28 +46,28 @@ const RUNTIME_VERSION = sys.appVersion * 获取uuid */ export const get_uuid = () : string => { - // 有可能不存在 deviceId(一般不存在就是出bug了),就自己生成一个 - // 目前 deviceId 肯定存在所以不用生成 - // const uuid : string = sys.deviceId ?? getUuid() - return sys.deviceId + // 有可能不存在 deviceId(一般不存在就是出bug了),就自己生成一个 + // 目前 deviceId 肯定存在所以不用生成 + // const uuid : string = sys.deviceId ?? getUuid() + return sys.deviceId } /** * 获取老版的 deviceid ,兼容以前的错误 deviceid */ export const get_odid = () : string => { - // 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 + // 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 } @@ -89,94 +89,94 @@ export const get_odid = () : string => { */ export const get_platform_name = () : string => { - 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 + 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 } /** * 获取原生包名,或小程序 appid */ export const get_pack_name = () : string => { - let packName = '' - if (get_platform_name() === 'n') { - if (sys.osName == 'android') { - packName = sysAppBase.packageName ?? '' - } - if (sys.osName == 'ios') { - packName = sysAppBase.bundleId ?? '' - } - } - return packName + let packName = '' + if (get_platform_name() === 'n') { + if (sys.osName == 'android') { + packName = sysAppBase.packageName ?? '' + } + if (sys.osName == 'ios') { + packName = sysAppBase.bundleId ?? '' + } + } + return packName } /** * 应用版本 */ export const get_version = () : string => { - return RUNTIME_VERSION + return RUNTIME_VERSION } /** * 获取渠道 */ export const get_channel = () : string => { - const platformName = get_platform_name() - let channel:string = '' - if (platformName === 'n') { - channel = sysAppBase.channel ?? '' - } - // if (platformName === 'wx') { - // // TODO 需要调研小程序二维码渠道如何获取; - // } - return channel + const platformName = get_platform_name() + let channel : string = '' + if (platformName === 'n') { + channel = sysAppBase.channel ?? '' + } + // if (platformName === 'wx') { + // // TODO 需要调研小程序二维码渠道如何获取; + // } + return channel } /** * 获取小程序场景值 */ export const get_scene = (_ : OnLaunchOptionsWithCst) : number => { - // TODO 场景值获取有问题 ,暂时硬编码,需要修改 - // options : OnLaunchOptionsWithCst - // const platformName = get_platform_name() - // let scene = '' - // if (options) { - // return options - // } - // if (platformName === 'wx') { - // scene = uni.getLaunchOptionsSync().scene - // } - // return scene - // return options.scene as number - return 1001 + // TODO 场景值获取有问题 ,暂时硬编码,需要修改 + // options : OnLaunchOptionsWithCst + // const platformName = get_platform_name() + // let scene = '' + // if (options) { + // return options + // } + // if (platformName === 'wx') { + // scene = uni.getLaunchOptionsSync().scene + // } + // return scene + // return options.scene as number + return 1001 } /** @@ -185,13 +185,13 @@ export const get_scene = (_ : OnLaunchOptionsWithCst) : number => { */ // @ts-ignore export const get_page_types = (appInstance : ComponentPublicInstance) : string => { - // TODO 暂时无法获取是否应用还是页面,写的硬编码,只支持页面调用 - // #ifdef WEB || APP-IOS - return appInstance?.$mpType ?? 'page' - // #endif - // #ifndef WEB - return 'page' - // #endif + // TODO 暂时无法获取是否应用还是页面,写的硬编码,只支持页面调用 + // #ifdef WEB || APP-IOS + return appInstance?.$mpType ?? 'page' + // #endif + // #ifndef WEB + return 'page' + // #endif } /** @@ -200,14 +200,14 @@ export const get_page_types = (appInstance : ComponentPublicInstance) : string = */ // @ts-ignore export const is_page = (appInstance : ComponentPublicInstance) : Boolean => { - // #ifdef WEB ||APP-IOS - const type = appInstance?.$mpType ?? 'page' - return type == 'page' ? true : false - // #endif - // #ifndef WEB - // 其他平台没有 $mpType ,只有页面触发 - return true - // #endif + // #ifdef WEB ||APP-IOS + const type = appInstance?.$mpType ?? 'page' + return type == 'page' ? true : false + // #endif + // #ifndef WEB + // 其他平台没有 $mpType ,只有页面触发 + return true + // #endif } @@ -217,39 +217,39 @@ export const is_page = (appInstance : ComponentPublicInstance) : Boolean => { * @param {String} routepath 页面路由 */ export const get_page_name = (routepath : string) : string => { - let page = get_page_vm() - - if (page == null) return '' - - if (page.route != routepath) { - const pages = getCurrentPages() - // 如果传入路由与当前页面不同,则从页面栈找一个,如果找不到返回空 - let page_now = pages.find((p): boolean => p.route == routepath) - if (page_now == null) { - return '' - } - // @ts-ignore - page = page_now.vm! - } - - // @ts-ignore - const pageStyle = (page.$page as UniPage).getPageStyle() - const titleText = pageStyle['navigationBarTitleText'] ?? '' - return titleText as string + let page = get_page_vm() + + if (page == null) return '' + + if (page.route != routepath) { + const pages = getCurrentPages() + // 如果传入路由与当前页面不同,则从页面栈找一个,如果找不到返回空 + let page_now = pages.find((p) : boolean => p.route == routepath) + if (page_now == null) { + return '' + } + // @ts-ignore + page = page_now.vm! + } + + // @ts-ignore + const pageStyle = (page.$page as UniPage).getPageStyle() + const titleText = pageStyle['navigationBarTitleText'] ?? '' + return titleText as string } /** * 获取页面实例 */ export const get_page_vm = () : Page | null => { - let pages = getCurrentPages() - if (pages.length == 0) { - return null - } - let page = pages[pages.length - 1] - // TODO 正常来说,调用当前方法的地方只在 生命周期内,数组内最少会有一个页面,所以理论上是不存在获取不到的情况的 - // @ts-ignore - return page.vm + let pages = getCurrentPages() + if (pages.length == 0) { + return null + } + let page = pages[pages.length - 1] + // TODO 正常来说,调用当前方法的地方只在 生命周期内,数组内最少会有一个页面,所以理论上是不存在获取不到的情况的 + // @ts-ignore + return page.vm } @@ -258,38 +258,38 @@ export const get_page_vm = () : Page | null => { * @param {ComponentPublicInstance} page 页面实例 */ export function get_route(page : Page | null = null) : RouteParams { - let _self = page ?? get_page_vm() - - if (_self == null) { - const data : RouteParams = { - path: '', - fullpath: '' - } - return data - } - - // TODO 条件编译处理参数问题,安卓上 options返回的是map,需要处理成 utsobject - // #ifdef APP-ANDROID - const opts = new UTSJSONObject(_self.options) - // TODO 上报页面参数需要处理 - let url_params = Serialize(opts) - // #endif - // #ifndef APP-ANDROID - // @ts-ignore - let url_params = Serialize(_self.options) - // #endif - let params = '' - // 如果参数只有 ?则说明没有参数 - if (url_params != '?') { - params = url_params - } - let route = _self.route - - const data : RouteParams = { - path: route, - fullpath: route + params - } - return data + let _self = page ?? get_page_vm() + + if (_self == null) { + const data : RouteParams = { + path: '', + fullpath: '' + } + return data + } + + // TODO 条件编译处理参数问题,安卓上 options返回的是map,需要处理成 utsobject + // #ifdef APP-ANDROID + const opts = new UTSJSONObject(_self.options) + // TODO 上报页面参数需要处理 + let url_params = Serialize(opts) + // #endif + // #ifndef APP-ANDROID + // @ts-ignore + let url_params = Serialize(_self.options) + // #endif + let params = '' + // 如果参数只有 ?则说明没有参数 + if (url_params != '?') { + params = url_params + } + let route = _self.route + + const data : RouteParams = { + path: route, + fullpath: route + params + } + return data } @@ -298,15 +298,15 @@ export function get_route(page : Page | null = null) : RouteParams { * @param {ComponentPublicInstance} page 页面实例 */ export function get_page_route(page : Page | null = null) : string { - 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 + 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 } /** @@ -314,14 +314,14 @@ export function get_page_route(page : Page | null = null) : string { * @returns */ export const is_page_report = () : boolean => { - 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 + 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 } @@ -331,9 +331,9 @@ export const is_page_report = () : boolean => { */ const IS_HANDLE_DEVECE_ID = 'is_handle_device_id' export const is_handle_device = () : boolean => { - let isHandleDevice = dbGet(IS_HANDLE_DEVECE_ID) ?? '' - dbSet(IS_HANDLE_DEVECE_ID, '1') - return isHandleDevice === '1' + let isHandleDevice = dbGet(IS_HANDLE_DEVECE_ID) ?? '' + dbSet(IS_HANDLE_DEVECE_ID, '1') + return isHandleDevice === '1' } @@ -342,38 +342,38 @@ export const is_handle_device = () : boolean => { */ export const get_default_data = () : StatDefault => { - 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 + 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 } /** @@ -381,17 +381,17 @@ export const get_default_data = () : StatDefault => { * @param {*} defaultTime 默认上报间隔时间 单位s */ export const get_report_Interval = (defaultTime : number) : number => { - 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 + 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 } @@ -401,9 +401,9 @@ export const get_report_Interval = (defaultTime : number) : number => { * @returns {Object} */ export const uni_cloud_config = () : UniCloudInitOptions | null => { - // return process.env.UNI_STAT_UNI_CLOUD || {} - // const custemUnicloudConfig: = {} - return null + // return process.env.UNI_STAT_UNI_CLOUD || {} + // const custemUnicloudConfig: = {} + return null } /** @@ -412,54 +412,54 @@ export const uni_cloud_config = () : UniCloudInitOptions | null => { * @returns */ export const get_space = (config : UniCloudInitOptions) : UniCloudInitOptions | null => { - 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 + 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 } /** * 获取隐私协议配置 */ export const is_push_clientid = () : boolean => { - const uniStatConfig = Config.getOptions() - const collectItems = uniStatConfig.collectItems - if (collectItems != null) { - const ClientID = collectItems.uniPushClientID ?? false - return typeof ClientID == 'boolean' ? ClientID : false - } - return false + const uniStatConfig = Config.getOptions() + const collectItems = uniStatConfig.collectItems + if (collectItems != null) { + const ClientID = collectItems.uniPushClientID ?? false + return typeof ClientID == 'boolean' ? ClientID : false + } + return false } @@ -467,44 +467,44 @@ export const is_push_clientid = () : boolean => { /** * 自定义事件参数校验 */ -export const calibration = (eventName : string, options : any | null) : boolean => { - // login 、 share 、pay_success 、pay_fail 、register 、title - if (eventName == '') { - console.error(`uni.report Missing [eventName] parameter`) - return true - } - if (typeof eventName != 'string') { - console.error( - `uni.report [eventName] Parameter type error, it can only be of type String` - ) - return true - } - if (eventName.length > 255) { - console.error( - `uni.report [eventName] Parameter length cannot be greater than 255` - ) - return true - } - - if (typeof options != 'string' && typeof options != 'object') { - console.error( - 'uni.report [options] Parameter type error, Only supports String or Object type' - ) - return true - } - - if (typeof options == 'string' && (options as string).length > 255) { - console.error( - `uni.report [options] Parameter length cannot be greater than 255` - ) - return true - } - - 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 - } - return false -} +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 +} \ No newline at end of file diff --git a/uni_modules/uni-stat/utssdk/index.uts b/uni_modules/uni-stat/utssdk/index.uts index 9af3a597..f5dbe658 100644 --- a/uni_modules/uni-stat/utssdk/index.uts +++ b/uni_modules/uni-stat/utssdk/index.uts @@ -1,4 +1,5 @@ -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 diff --git a/uni_modules/uni-stat/utssdk/interface.uts b/uni_modules/uni-stat/utssdk/interface.uts index bfcefc07..6261b9e1 100644 --- a/uni_modules/uni-stat/utssdk/interface.uts +++ b/uni_modules/uni-stat/utssdk/interface.uts @@ -1,46 +1,110 @@ -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 diff --git a/uni_modules/uni-stat/utssdk/unierror.uts b/uni_modules/uni-stat/utssdk/unierror.uts index e69de29b..f8a7baa0 100644 --- a/uni_modules/uni-stat/utssdk/unierror.uts +++ b/uni_modules/uni-stat/utssdk/unierror.uts @@ -0,0 +1,43 @@ +import { ReportErrorCode, ReportError } from "./interface.uts" + +/** + * 错误主题 + */ +export const ReportUniErrorSubject = 'uni-report'; + + +/** + * 错误码 + * @UniError + */ +export const ReportUniErrors:Map = 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) ?? '' + } +} -- GitLab