util.js 4.5 KB

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