闫增涛
2024-05-22 3609ed68a88e03d2db0d5723abca9cc613f4c628
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import config from "@/assets/js/config.js";
 
import jobApi from "./middleGround/api/job"; // newJobWithNewView // newSession,
 
export function setSessionGuid(type, id) {
  var cityCode = null;
  var SnIp = null;
  try {
    // eslint-disable-next-line
    SnIp = window.returnCitySN;
  } catch (error) {
    SnIp = null;
    console.log(error);
  }
  if (SnIp != undefined && SnIp != null) {
    cityCode = SnIp;
  } else {
    cityCode = {
      cip: "0.0.0.0",
      cname: "未知",
    };
  }
  const _city = cityCode.cname.substring(3);
  const _province = cityCode.cname.substring(0, 3);
  const params = {
    appRefCode: config.appRefCode,
    hostName: config.requestCtx,
    ipAddress: cityCode.cip,
    browser: myBrowser(),
    os: navigator.platform,
    device: "pc",
    province: _province,
    city: _city ? _city : _province,
  };
 
  jobApi.newSession(params).then(res => {
    storage.set("sessionGuid", res, 30);
    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;
  },
};
 
function myBrowser() {
  const userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
  const isOpera = userAgent.indexOf("Opera") > -1;
  if (isOpera) {
    //判断是否Opera浏览器
    return "Opera";
  }
  if (userAgent.indexOf("Firefox") > -1) {
    //判断是否Firefox浏览器
    return "Firefox";
  }
  if (userAgent.indexOf("Chrome") > -1) {
    return "Chrome";
  }
  if (userAgent.indexOf("Safari") > -1) {
    //判断是否Safari浏览器
    return "Safari";
  }
  if (
    userAgent.indexOf("compatible") > -1 &&
    userAgent.indexOf("MSIE") > -1 &&
    !isOpera
  ) {
    //判断是否IE浏览器
    return "IE";
  }
  return "";
}
 
export function setNewView(type, id) {
  if (!sessionStorage.getItem("sessionGuid")) {
    setSessionGuid(type, id);
  }
 
  const sessionGuid = sessionStorage.getItem("sessionGuid");
  const fromPath = sessionStorage.getItem("fromPath");
  const toPath = sessionStorage.getItem("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 => {});
  }
}