فهرست منبع

fix 登录后openId,unionId缓存本地

erekook 11 ماه پیش
والد
کامیت
4dbc9fa400
3فایلهای تغییر یافته به همراه169 افزوده شده و 156 حذف شده
  1. 13 4
      app.js
  2. 2 2
      pages/collect-video/video.js
  3. 154 150
      utils/util.js

+ 13 - 4
app.js

@@ -70,9 +70,18 @@ App({
               console.log(res);
               console.log(res);
               wx.setStorageSync("accessToken", res.accessToken);
               wx.setStorageSync("accessToken", res.accessToken);
               this.globalData.isLogin = true;
               this.globalData.isLogin = true;
-              this.globalData.userCode = res.userCode;
-              this.globalData.openId = res.openId;
-              this.globalData.unionId = res.unionId;
+              if (res.userCode) {
+                this.globalData.userCode = res.userCode;
+                wx.setStorageSync("userCode", res.userCode);
+              }
+              if (res.openId) {
+                this.globalData.openId = res.openId;
+                wx.setStorageSync("openId", res.openId);
+              }
+              if (res.unionId) {
+                this.globalData.unionId = res.unionId;
+                wx.setStorageSync("unionId", res.unionId);
+              }
               console.log("登录成功", this.globalData);
               console.log("登录成功", this.globalData);
               userActive({
               userActive({
                 appId: apple.appid,
                 appId: apple.appid,
@@ -111,7 +120,7 @@ App({
         return;
         return;
       }
       }
       // 未登录,执行登录
       // 未登录,执行登录
-      const res = this.login(promotionid);
+      this.login(promotionid);
     } catch (error) {
     } catch (error) {
       console.error("登录失败:", error);
       console.error("登录失败:", error);
     }
     }

+ 2 - 2
pages/collect-video/video.js

@@ -44,10 +44,10 @@ Page({
     currentTime: 0, // 当前播放时间
     currentTime: 0, // 当前播放时间
   },
   },
 
 
-  onLoad(options) {
+  async onLoad(options) {
     console.log("打开视频合集页面--------------------------");
     console.log("打开视频合集页面--------------------------");
     // wx.setStorageSync("jump", parseInt(options.jump)); // 是否跳转阅读器
     // wx.setStorageSync("jump", parseInt(options.jump)); // 是否跳转阅读器
-    this.autoLogin(options.promotionid);
+    await this.autoLogin(options.promotionid);
     if (!app.globalData.uuid) {
     if (!app.globalData.uuid) {
       this.getUUID().then((uuid) => {
       this.getUUID().then((uuid) => {
         // getApp().globalData.uuid = uuid
         // getApp().globalData.uuid = uuid

+ 154 - 150
utils/util.js

@@ -1,129 +1,126 @@
-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(':')}`
-}
+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}`
-}
+const formatNumber = (n) => {
+  n = n.toString();
+  return n[1] ? n : `0${n}`;
+};
 
 
 // 跳转到书籍详情页
 // 跳转到书籍详情页
 export const goToBookDetail = (options, query) => {
 export const goToBookDetail = (options, query) => {
-    console.log('goToBookDetail option:', options)
-    // 检查必要参数
-    if (!options || (!options.bookId || !options.wxBookId)) {
-        console.warn('书籍ID不能为空');
-        return;
+  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}`;
     }
     }
 
 
-    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
-        }
+    if (options.bookId) {
+      url += `&innerBookId=${options.bookId}`;
     }
     }
-
-    wx.navigateTo({
-        url,
-        fail: (err) => {
-            console.error('跳转书籍详情页失败:', err);
-            console.log('url', url);
-            wx.showToast({
-                title: '跳转失败,请重试',
-                icon: 'none'
-            });
-        }
-    });
+    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) => {
 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'
-            });
-        }
-    })
-
+  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) => {
 export const rnd = (min = 0, max = 1) => {
-    const random = Math.floor(Math.random() * (max - min + 1) + min)
-    return random
+  const random = Math.floor(Math.random() * (max - min + 1) + min);
+  return random;
 };
 };
 
 
 // 随机整数方法
 // 随机整数方法
 export const rndone = (self = []) => {
 export const rndone = (self = []) => {
-    return self.length > 0 ? self[rnd(0, self.length - 1)] : ''
+  return self.length > 0 ? self[rnd(0, self.length - 1)] : "";
 };
 };
 
 
 /**
 /**
  * 拷贝
  * 拷贝
  */
  */
 export function deepCopy(data) {
 export function deepCopy(data) {
-    let t = typeof data
-    if (data instanceof Array) {
-        t = 'array'
-    }
+  let t = typeof data;
+  if (data instanceof Array) {
+    t = "array";
+  }
+
+  let o;
 
 
-    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
+  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
+  return o;
 }
 }
 
 
 /**
 /**
@@ -132,60 +129,67 @@ export function deepCopy(data) {
  * @param {*} eventParam 事件参数
  * @param {*} eventParam 事件参数
  */
  */
 export const SendEvent = (eventName, eventParam, self) => {
 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,
-            })
-        }
-
-        opt.union_id = app.globalData.unionId || ''
-        opt.open_id = app.globalData.openId || ''
-        opt.user_id = app.globalData.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)
-    }
-}
+  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
 // 生成uuid
 export const uuid = function () {
 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
+  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 = {
 module.exports = {
-    formatTime,
-    goToBookDetail,
-    rnd,
-    rndone,
-    deepCopy,
-    SendEvent,
-    goToVideoDetail
-}
+  formatTime,
+  goToBookDetail,
+  rnd,
+  rndone,
+  deepCopy,
+  SendEvent,
+  goToVideoDetail,
+};