| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- import { recordLog } from "../api/api.js";
- import { apple } from "../config/config";
- const formatTime = (date) => {
- const year = date.getFullYear();
- const month = date.getMonth() + 1;
- const day = date.getDate();
- const hour = date.getHours();
- const minute = date.getMinutes();
- const second = date.getSeconds();
- return `${[year, month, day].map(formatNumber).join("/")} ${[hour, minute, second].map(formatNumber).join(":")}`;
- };
- const formatNumber = (n) => {
- n = n.toString();
- return n[1] ? n : `0${n}`;
- };
- // 跳转到书籍详情页
- export const goToBookDetail = (options, query) => {
- console.log("goToBookDetail option:", options);
- // 检查必要参数
- if (!options || !options.bookId || !options.wxBookId) {
- console.warn("书籍ID不能为空");
- return;
- }
- let url = "";
- // 根据不同参数构建不同的URL
- if (options.wxBookId) {
- // 微信书籍ID跳转
- url = `plugin-private://wx293c4b6097a8a4d0/pages/novel/index?share=1&bookId=${options.wxBookId}&disableAutoShowChargeDialog=1`;
- // 如果有章节ID,添加到URL
- if (options.chapterId) {
- url += `&chapterId=${options.chapterId}`;
- }
- if (options.bookId) {
- url += `&innerBookId=${options.bookId}`;
- }
- if (query) {
- url += query;
- }
- }
- wx.navigateTo({
- url,
- fail: (err) => {
- console.error("跳转书籍详情页失败:", err);
- console.log("url", url);
- wx.showToast({
- title: "跳转失败,请重试",
- icon: "none",
- });
- },
- });
- };
- export const goToVideoDetail = (options) => {
- console.log("goToVideoDetail option:", options);
- if (!options || !options.videoId || !options.tagId) {
- console.warn("视频ID或标签ID不能为空");
- return;
- }
- let url = "";
- // 拼接视频详情页的URL
- url = `/pages/video/detail?id=${options.videoId}&title=&tag_id=${options.tagId}`;
- wx.navigateTo({
- url,
- success: () => {
- wx.hideLoading();
- },
- fail: (err) => {
- console.error("跳转视频详情页失败:", err);
- console.log("url", url);
- wx.showToast({
- title: "跳转失败,请重试",
- icon: "none",
- });
- },
- });
- };
- // 随机整数方法
- export const rnd = (min = 0, max = 1) => {
- const random = Math.floor(Math.random() * (max - min + 1) + min);
- return random;
- };
- // 随机整数方法
- export const rndone = (self = []) => {
- return self.length > 0 ? self[rnd(0, self.length - 1)] : "";
- };
- /**
- * 拷贝
- */
- export function deepCopy(data) {
- let t = typeof data;
- if (data instanceof Array) {
- t = "array";
- }
- let o;
- if (t === "array") {
- o = [];
- for (let i = 0; i < data.length; i++) {
- o.push(deepCopy(data[i]));
- }
- } else if (t === "object") {
- o = {};
- for (let i in data) {
- o[i] = deepCopy(data[i]);
- }
- } else {
- return data;
- }
- return o;
- }
- /**
- * 上报打点
- * @param {*} eventName 事件名
- * @param {*} eventParam 事件参数
- */
- export const SendEvent = (eventName, eventParam, self) => {
- try {
- const app = self || getApp();
- let eParam = { ...eventParam };
- eParam.from = app.globalData.from || "";
- const { brand, model, system, platform } = wx.getDeviceInfo();
- let opt = {
- app_id: apple.appKey,
- app_channel: apple.ghid,
- app_type: 3,
- event_id: eventName,
- os_type: 1,
- event_value: JSON.stringify(eParam),
- report_time: Date.now(),
- trace_id: uuid(),
- user_agent: JSON.stringify({
- brand,
- model,
- system,
- platform,
- }),
- };
- console.log(wx.getStorageSync("openId"));
- opt.union_id = wx.getStorageSync("unionId") || "";
- opt.open_id = wx.getStorageSync("openId") || "";
- opt.user_id = wx.getStorageSync("userCode") || "";
- console.log(
- "uploadlog",
- eventName,
- opt.event_value,
- opt.union_id,
- opt.open_id,
- opt.user_id,
- );
- recordLog(opt);
- } catch (error) {
- console.error("record error:", error);
- }
- };
- // 生成uuid
- export const uuid = function () {
- var s = [];
- var hexDigits = "0123456789abcdef";
- for (var i = 0; i < 36; i++) {
- s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
- }
- s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
- s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
- s[8] = s[13] = s[18] = s[23] = "-";
- var uuid = s.join("");
- return uuid;
- };
- module.exports = {
- formatTime,
- goToBookDetail,
- rnd,
- rndone,
- deepCopy,
- SendEvent,
- goToVideoDetail,
- };
|