util.js 4.8 KB

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