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 => { });
|
}
|
}
|