stat.uts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import { Report } from "./report.uts";
  2. import { StatType } from "./stat-type";
  3. import { EventParams, UniStatOptions, ErrorCallback, ReportErrorCode } from '../../interface.uts'
  4. import { is_page, is_page_report, get_space, is_push_clientid, calibration,get_crash_logs } from '../utils/pageInfo.uts'
  5. import { Config } from "../config";
  6. export class Stat {
  7. static __stat_instance : Stat | null = null;
  8. static is_register : boolean = false
  9. static no_space : boolean = false
  10. // 上报逻辑实例
  11. report : Report;
  12. // 使用单例,只初始化一次
  13. static getInstance() : Stat {
  14. // 获取服务空间配置信息
  15. let space = get_space(uniCloud.config)
  16. if (Report.uniCloudInstance == null) {
  17. // 判断不为空对象
  18. if (space != null) {
  19. // 重新构造 uniCloud
  20. let spaceData : UniCloudInitOptions = {
  21. provider: space.provider,
  22. spaceId: space.spaceId,
  23. clientSecret: space.clientSecret,
  24. }
  25. const endpoint = space.endpoint
  26. if (endpoint != null) {
  27. spaceData.endpoint = space.endpoint
  28. }
  29. // 支付宝单独处理一些参数
  30. if (space.provider == 'alipay') {
  31. spaceData.secretKey = space.secretKey
  32. spaceData.accessKey = space.accessKey
  33. spaceData.spaceAppId = space.spaceAppId
  34. }
  35. // 初始化 uniCloud
  36. // @ts-ignore
  37. Report.uniCloudInstance = uniCloud.init(spaceData)
  38. } else {
  39. if (!Stat.no_space) {
  40. // #ifdef APP-IOS
  41. console.error('应用已集成uni统计,但未关联服务空间,请在uniCloud目录右键关联服务空间')
  42. // #endif
  43. // #ifndef APP-IOS
  44. console.log("\u001b[31m应用已集成uni统计,但未关联服务空间,请在uniCloud目录右键关联服务空间\u001b[0m")
  45. // #endif
  46. Stat.no_space = true
  47. }
  48. }
  49. }
  50. // 实例化统计sdk ,要在 实例 unicloud 之后进行,避免 Report 无法拿到 uniCloud 实例
  51. if (this.__stat_instance == null) {
  52. this.__stat_instance = new Stat()
  53. }
  54. return this.__stat_instance as Stat
  55. }
  56. // 当前生命周期内的页面或应用实例
  57. appInstance ?: Page | null = null
  58. private isHide : boolean = false
  59. constructor() {
  60. this.report = new Report()
  61. }
  62. /**
  63. * 初始化插件参数
  64. * @param {Object} options
  65. */
  66. init(options : UTSJSONObject) {
  67. // 插件挂载玩成,可以进行后续操作
  68. Stat.is_register = true
  69. // 参数处理
  70. Config.setOptions({ ...options } as UniStatOptions)
  71. const uniStatConfig = Config.getOptions()
  72. // 设置上报周期时间
  73. this.report.eportInterval = uniStatConfig.reportInterval ?? 10
  74. // #ifdef APP
  75. // 获取崩溃日志
  76. get_crash_logs((logs)=>{
  77. this.registerEvent(StatType.Crash, null, logs)
  78. })
  79. // #endif
  80. }
  81. /**
  82. * 应用启动
  83. * @param {OnLaunchOptions} options 应用参数
  84. * @param {ComponentPublicInstance} appInstance 应用实例
  85. */
  86. // options : OnLaunchOptions, appInstance : ComponentPublicInstance
  87. onLaunch(options : OnLaunchOptions, appInstance : ComponentPublicInstance) {
  88. // 注册事件 onLaunch ,需要手动触发
  89. // this.registerEvent(StatType.LifeCycleLaunch, appInstance, options as any)
  90. }
  91. /**
  92. * 页面加载
  93. * @param {ComponentPublicInstance} appInstance 应用实例
  94. */
  95. onLoad(appInstance : Page) {
  96. this.registerEvent(StatType.LifeCycleLoad, appInstance)
  97. }
  98. /**
  99. * 显示页面或应用进入前台
  100. * @param {ComponentPublicInstance} appInstance 应用实例
  101. */
  102. onShow(appInstance : Page) {
  103. this.isHide = false
  104. // @ts-ignore
  105. const mptype = is_page(appInstance)
  106. // 页面执行,应用需要手动调用
  107. if (mptype) {
  108. this.registerEvent(StatType.LifeCyclePageShow, appInstance, null)
  109. }
  110. // const life_type = mptype == 'app' ? StatType.LifeCycleAppShow : StatType.LifeCyclePageShow
  111. // this.registerEvent(life_type, appInstance, null)
  112. }
  113. /**
  114. * 页面隐藏或应用进入后台
  115. * @param {ComponentPublicInstance} appInstance 应用实例
  116. */
  117. onHide(appInstance : Page) {
  118. this.isHide = true
  119. // @ts-ignore
  120. const mptype = is_page(appInstance)
  121. // 页面执行,应用需要手动调用
  122. if (mptype) {
  123. this.registerEvent(StatType.LifeCyclePageHide, appInstance, null)
  124. }
  125. // const life_type = mptype == 'app' ? StatType.LifeCycleAppHide : StatType.LifeCyclePageHide
  126. // this.registerEvent(life_type, appInstance, null)
  127. }
  128. /**
  129. * 卸载页面
  130. * @param {ComponentPublicInstance} appInstance 应用实例
  131. */
  132. onUnload(appInstance : Page) {
  133. // 如果 isHide 为true 说明页面隐藏了,不走卸载逻辑,如果走卸载逻辑,isHide 必不可能是true
  134. if (this.isHide) {
  135. this.isHide = false
  136. return
  137. }
  138. this.registerEvent(StatType.LifeCyclePageUnLoad, appInstance, null)
  139. }
  140. /**
  141. * 错误
  142. * @param {String} error 应用实例
  143. */
  144. onError(error : string) {
  145. // 单独处理错误上报
  146. this.error(error)
  147. }
  148. /**
  149. * 获取推送ID
  150. */
  151. // pushEvent(options : any) {
  152. // // TODO uni x 暂不支持,如需要开启,请放开注释
  153. // const ClientID = is_push_clientid()
  154. // if (ClientID) {
  155. // uni.getPushClientId({
  156. // success: (res) => {
  157. // const cid = res.cid
  158. // // 只有获取到才会上传
  159. // // if (cid != null) {
  160. // this.report.sendPushRequest(options, cid)
  161. // // }
  162. // },
  163. // } as GetPushClientIdOptions)
  164. // }
  165. // }
  166. /**
  167. * 注册事件
  168. * @param {number} EventType 事件类型
  169. * @param {Page} appInstance 当前页面实例
  170. * @param {UTSJSONObject} options 应用参数
  171. */
  172. registerEvent(EventType : number, appInstance : Page | null, options : any | null = null, error : any | null = '') {
  173. this.appInstance = appInstance
  174. // 是否要上报页面数据
  175. const isPageReport = is_page_report()
  176. switch (EventType) {
  177. case StatType.LifeCycleLaunch:
  178. // 使用非空断言,options在这里肯定非空
  179. this.report.launch(options!)
  180. // this.pushEvent(options)
  181. break
  182. case StatType.LifeCycleAppShow:
  183. // TODO 目前只兼容 web 和 app ,小程序等平台需要调用 api onAppHide
  184. this.report.appShow()
  185. break
  186. case StatType.LifeCycleAppHide:
  187. this.report.appHide(true)
  188. break
  189. case StatType.LifeCycleLoad:
  190. break
  191. case StatType.LifeCyclePageShow:
  192. if (isPageReport) {
  193. this.report.pageShow(appInstance!)
  194. }
  195. break
  196. case StatType.LifeCyclePageHide:
  197. if (isPageReport) {
  198. this.report.pageHide(appInstance!)
  199. }
  200. break
  201. case StatType.LifeCyclePageUnLoad:
  202. if (isPageReport) {
  203. this.report.pageHide(appInstance!)
  204. }
  205. break
  206. case StatType.LifeCycleError:
  207. if (error != null) {
  208. this.report.appError(error)
  209. }
  210. break
  211. case StatType.Crash:
  212. this.report.appCrash(options as string[])
  213. break
  214. }
  215. }
  216. error(em : string) {
  217. // 生命周期监听,暂时无用,需要手动调用api
  218. }
  219. // 自定义参数上报
  220. appEvent(name : string, options : any | null = null, fn : ErrorCallback) {
  221. if (Stat.no_space) {
  222. fn(false, 61000 as ReportErrorCode)
  223. return
  224. }
  225. if (!Stat.is_register) {
  226. fn(false, 61001 as ReportErrorCode)
  227. return
  228. }
  229. // const names = ['uni-app-launch', 'uni-app-show', 'uni-app-hide', 'uni-app-error']
  230. // if (names.indexOf(name) <= -1) {
  231. // // console.error('uniStatReport 事件名不存在,请检查!');
  232. // fn(false, 'uniStatReport 事件名不存在,请检查!')
  233. // return
  234. // }
  235. if (name == 'uni-app-launch' && options == null) {
  236. fn(false, 61002 as ReportErrorCode)
  237. return
  238. }
  239. if (name == 'uni-app-launch') {
  240. this.registerEvent(StatType.LifeCycleLaunch, null, options)
  241. // 61001 占位,无实际用途
  242. fn(true, 61001 as ReportErrorCode)
  243. return
  244. }
  245. if (name == 'uni-app-show') {
  246. this.registerEvent(StatType.LifeCycleAppShow, null, null)
  247. // 61001 占位,无实际用途
  248. fn(true, 61001 as ReportErrorCode)
  249. return
  250. }
  251. if (name == 'uni-app-hide') {
  252. this.registerEvent(StatType.LifeCycleAppHide, null, null)
  253. // 61001 占位,无实际用途
  254. fn(true, 61001 as ReportErrorCode)
  255. return
  256. }
  257. if (name == 'uni-page-show') {
  258. this.report.pageShow(options as Page)
  259. // 61001 占位,无实际用途
  260. fn(true, 61001 as ReportErrorCode)
  261. return
  262. }
  263. if (name == 'uni-page-hide') {
  264. this.report.pageHide(options as Page)
  265. // 61001 占位,无实际用途
  266. fn(true, 61001 as ReportErrorCode)
  267. return
  268. }
  269. if (name == 'uni-app-error') {
  270. this.registerEvent(StatType.LifeCycleError, null, null, options)
  271. // 61001 占位,无实际用途
  272. fn(true, 61001 as ReportErrorCode)
  273. return
  274. }
  275. // 校验 type 参数
  276. const is_calibration = calibration(name, options)
  277. if (is_calibration != null) {
  278. fn(false, is_calibration)
  279. return
  280. }
  281. if (name === 'title') {
  282. this.report._navigationBarTitle.report = (options as string)
  283. }
  284. const value = (typeof options === 'object' ? JSON.stringify(options) : options) as string
  285. const data : EventParams = {
  286. key: name,
  287. value: value as string,
  288. }
  289. this.report.sendEventRequest(data)
  290. // 61001 占位,无实际用途
  291. fn(true, 61001 as ReportErrorCode)
  292. }
  293. }