util.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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}&disableAutoShowChargeDialog=1`;
  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. export const goToVideoDetail = (options) => {
  52. console.log('goToVideoDetail option:', options);
  53. if (!options || (!options.videoId) || (!options.tagId)) {
  54. console.warn('视频ID或标签ID不能为空');
  55. return;
  56. }
  57. let url = '';
  58. // 拼接视频详情页的URL
  59. url = `/pages/video/detail?id=${options.videoId}&title=&tag_id=${options.tagId}`;
  60. wx.navigateTo({
  61. url,
  62. success: () => {
  63. wx.hideLoading();
  64. },
  65. fail: (err) => {
  66. console.error('跳转视频详情页失败:', err);
  67. console.log('url', url);
  68. wx.showToast({
  69. title: '跳转失败,请重试',
  70. icon: 'none'
  71. });
  72. }
  73. })
  74. };
  75. // 随机整数方法
  76. export const rnd = (min = 0, max = 1) => {
  77. const random = Math.floor(Math.random() * (max - min + 1) + min)
  78. return random
  79. };
  80. // 随机整数方法
  81. export const rndone = (self = []) => {
  82. return self.length > 0 ? self[rnd(0, self.length - 1)] : ''
  83. };
  84. /**
  85. * 拷贝
  86. */
  87. export function deepCopy(data) {
  88. let t = typeof data
  89. if (data instanceof Array) {
  90. t = 'array'
  91. }
  92. let o
  93. if (t === 'array') {
  94. o = []
  95. for (let i = 0; i < data.length; i++) {
  96. o.push(deepCopy(data[i]))
  97. }
  98. } else if (t === 'object') {
  99. o = {}
  100. for (let i in data) {
  101. o[i] = deepCopy(data[i])
  102. }
  103. } else {
  104. return data
  105. }
  106. return o
  107. }
  108. /**
  109. * 上报打点
  110. * @param {*} eventName 事件名
  111. * @param {*} eventParam 事件参数
  112. */
  113. export const SendEvent = (eventName, eventParam, self) => {
  114. try {
  115. const app = self || getApp()
  116. let eParam = { ...eventParam }
  117. eParam.from = app.globalData.from || ''
  118. const { brand, model, system, platform } = wx.getDeviceInfo()
  119. let opt = {
  120. app_id: apple.appKey,
  121. app_channel: apple.ghid,
  122. app_type: 3,
  123. event_id: eventName,
  124. os_type: 1,
  125. event_value: JSON.stringify(eParam),
  126. report_time: Date.now(),
  127. trace_id: uuid(),
  128. user_agent: JSON.stringify({
  129. brand,
  130. model,
  131. system,
  132. platform,
  133. })
  134. }
  135. opt.union_id = app.globalData.unionId || ''
  136. opt.open_id = app.globalData.openId || ''
  137. opt.user_id = app.globalData.userCode || ''
  138. console.log('uploadlog', eventName, opt.event_value, opt.union_id, opt.open_id, opt.user_id)
  139. recordLog(opt)
  140. } catch (error) {
  141. console.error('record error:', error)
  142. }
  143. }
  144. // 生成uuid
  145. export const uuid = function () {
  146. var s = [];
  147. var hexDigits = "0123456789abcdef";
  148. for (var i = 0; i < 36; i++) {
  149. s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  150. }
  151. s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  152. s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  153. s[8] = s[13] = s[18] = s[23] = "-";
  154. var uuid = s.join("");
  155. return uuid
  156. };
  157. module.exports = {
  158. formatTime,
  159. goToBookDetail,
  160. rnd,
  161. rndone,
  162. deepCopy,
  163. SendEvent,
  164. goToVideoDetail
  165. }