litian
2025-01-08 0f9f58798cb5df9e99e60fed882b73224313915d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import config from "./config.js";
import jobApi from "./middleGround/api/job";// newJobWithNewView // newSession,
 
export function setSessionGuid(type, id) {
  const params = {
    appRefCode: config.appRefCode,
    hostName: config.requestCtx,
    ipAddress: "0.0.0.0", // 后台获取,前台随便传
    browser: 'WeChatApp',
    os: 'WeChat',
    device: "pc",
    province: "未知",// 后台获取,前台随便传
    city: "未知",// 后台获取,前台随便传
  };
 
  jobApi.newSession(params).then(res => {
    wx.setStorageSync("sessionGuid", res);
    setNewView(type, id);
  });
}
 
export const storage = {
  /*
   * set 存储方法
   * @ param {String}     key 键
   * @ param {String}     value 值,
   * @ param {String}     expired 过期时间,以分钟为单位,非必须
   */
  set(key, val, expired) {
    if (typeof val !== "string") {
      val = JSON.stringify(val);
    }
    window.sessionStorage.setItem(key, val);
    if (expired) {
      window.sessionStorage.setItem(
        `${key}__expires__`,
        `${Date.now() + 1000 * 60 * expired}`
      );
    }
  },
  /*
   * get 获取方法
   * @ param {String}     key 键
   * @ param {String}     expired 存储时为非必须字段,所以有可能取不到,默认为 Date.now+1
   */
  get(key) {
    const expired =
      window.sessionStorage.getItem(`${key}__expires__`) || Date.now + 1;
    const now = Date.now();
 
    if (now >= expired) {
      window.sessionStorage.removeItem(key);
      return;
    }
    let val = window.sessionStorage.getItem(key);
    try {
      val = JSON.parse(val);
    } catch (e) {
      return e;
    }
    return val;
  },
};
 
export function setNewView(type, id) {
  if (!wx.getStorageSync("sessionGuid")) {
    setSessionGuid(type, id);
  }
  const sessionGuid = wx.getStorageSync("sessionGuid");
  const fromPath = wx.getStorageSync("fromPath");
  const toPath = wx.getStorageSync("toPath");
  if (sessionGuid) {
    let params = {
      sessionGuid: sessionGuid,
      appRefCode: config.appRefCode,
      type: "View",
      url: toPath == null ? "/" : toPath,
      ref: fromPath == null ? "/" : fromPath,
    };
    if (id) {
      params[type] = id;
    }
    jobApi.newJobWithNewView(params).then(res => { });
  }
}