util.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import {
  2. recordLog
  3. } from '../api/api.js'
  4. import { apple } from '../config/config'
  5. const formatTime = date => {
  6. const year = date.getFullYear()
  7. const month = date.getMonth() + 1
  8. const day = date.getDate()
  9. const hour = date.getHours()
  10. const minute = date.getMinutes()
  11. const second = date.getSeconds()
  12. return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  13. }
  14. const formatNumber = n => {
  15. n = n.toString()
  16. return n[1] ? n : `0${n}`
  17. }
  18. // 跳转到书籍详情页
  19. export const goToBookDetail = (options) => {
  20. console.log('goToBookDetail option:',options)
  21. // 检查必要参数
  22. if (!options || (!options.bookId || !options.wxBookId)) {
  23. console.warn('书籍ID不能为空');
  24. return;
  25. }
  26. let url = '';
  27. // 根据不同参数构建不同的URL
  28. if (options.wxBookId) {
  29. // 微信书籍ID跳转
  30. url = `plugin-private://wx293c4b6097a8a4d0/pages/novel/index?share=1&bookId=${options.wxBookId}`;
  31. // 如果有章节ID,添加到URL
  32. if (options.chapterId) {
  33. url += `&chapterId=${options.chapterId}`;
  34. }
  35. if (options.bookId) {
  36. url += `&innerBookId=${options.bookId}`;
  37. }
  38. }
  39. wx.navigateTo({
  40. url,
  41. fail: (err) => {
  42. console.error('跳转书籍详情页失败:', err);
  43. console.log('url',url);
  44. wx.showToast({
  45. title: '跳转失败,请重试',
  46. icon: 'none'
  47. });
  48. }
  49. });
  50. };
  51. // 随机整数方法
  52. export const rnd = (min = 0, max = 1) => {
  53. const random = Math.floor(Math.random() * (max - min + 1) + min)
  54. return random
  55. };
  56. // 随机整数方法
  57. export const rndone = (self = []) => {
  58. return self.length > 0 ? self[rnd(0, self.length - 1)] : ''
  59. };
  60. /**
  61. * 拷贝
  62. */
  63. export function deepCopy(data) {
  64. let t = typeof data
  65. if (data instanceof Array) {
  66. t = 'array'
  67. }
  68. let o
  69. if (t === 'array') {
  70. o = []
  71. for (let i = 0; i < data.length; i++) {
  72. o.push(deepCopy(data[i]))
  73. }
  74. } else if (t === 'object') {
  75. o = {}
  76. for (let i in data) {
  77. o[i] = deepCopy(data[i])
  78. }
  79. } else {
  80. return data
  81. }
  82. return o
  83. }
  84. /**
  85. * 上报打点
  86. * @param {*} eventName 事件名
  87. * @param {*} eventParam 事件参数
  88. */
  89. export const SendEvent = (eventName, eventParam, self) => {
  90. try {
  91. const app = self || getApp()
  92. let eParam = { ...eventParam }
  93. let opt = {
  94. app_id: apple.appKey,
  95. app_channel: apple.ghid,
  96. app_type: 3,
  97. event_id: eventName,
  98. os_type: 1,
  99. event_value: JSON.stringify(eParam),
  100. report_time: Date.now(),
  101. trace_id: uuid()
  102. }
  103. opt.union_id = app.globalData.unionId || ''
  104. opt.open_id = app.globalData.openId || ''
  105. opt.user_id = app.globalData.userCode || ''
  106. // console.log('uploadlog', eventName, opt.event_value)
  107. recordLog(opt)
  108. } catch (error) {
  109. console.error('record error:', error)
  110. }
  111. }
  112. // 生成uuid
  113. export const uuid = function () {
  114. var s = [];
  115. var hexDigits = "0123456789abcdef";
  116. for (var i = 0; i < 36; i++) {
  117. s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  118. }
  119. s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  120. s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  121. s[8] = s[13] = s[18] = s[23] = "-";
  122. var uuid = s.join("");
  123. return uuid
  124. };
  125. module.exports = {
  126. formatTime,
  127. goToBookDetail,
  128. rnd,
  129. rndone,
  130. deepCopy,
  131. SendEvent
  132. }