You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
5.9 KiB

export function deprecate(name=""){
try{
throw new Error(name + "这个方法或界面被弃用了");
}catch(e){
console.info(e);
}
}
export function getFormatDate(date){
date = date||new Date();
let sep = "-";
let year = date.getFullYear();
let month = date.getMonth() + 1;
let strDate = date.getDate();
if (month >= 1 && month <= 9)
month = "0" + month;
if (strDate >= 0 && strDate <= 9)
strDate = "0" + strDate;
var currentdate = year + sep + month + sep + strDate;
return currentdate;
}
export function getFormatTime(date){
date = date || new Date();
let hour = date.getHours();
let minu = date.getMinutes();
let sec = date.getSeconds();
if (hour < 10) hour = "0" + hour;
if (minu < 10) minu = "0" + minu;
if (sec < 10) sec = "0" + sec;
return hour + ":" + minu + ":" + sec;
}
export function getFormatDatetime(date){
date = date||new Date();
return getFormatDate(date) + " " + getFormatTime(date);
}
export function getNowFormatDate() {
deprecate();
return getFormatDate();
}
export function getNextWeekFormatDate(date) {
var date = date||new Date();
var date = new Date(date.getTime() + 7 * 24 * 3600 * 1000);
return getFormatDate(date);
}
export function getNowFormatTime() {
deprecate();
return getFormatTime();
}
export function getResConstruction(res = "") {
switch ((res || "").constructor) {
case Array:
return "[" + getResConstruction(res[0] || "") + "]";
case Object:
return "{" + Object.keys(res).map(key => {
let value = getResConstruction(res[key]);
return key + (value ? ":" + value : "")
}).join(",") + "}"
default:
return "";
}
}
export function processObj(obj) {
for (var key in obj) {
if (!obj.hasOwnProperty(key) || typeof obj[key] != "object")
continue
processObj(obj[key]);
obj[key].__proto__ = obj;
}
}
export function get(obj, key, default_) {
if(key in obj)
return obj[key]
if (typeof default_ == "object")
default_.__proto__ = obj;
if(obj.hasOwnProperty("__"))
return default_||obj.__;
else
return default_||obj._;
}
export function format(str, values) {
return str.replace(/\$\{(.*?)\}/g, function (match, key) {
return values[key] || "";
});
}
class AccountManager{
constructor(){
this.salt = "weR4i2j53CnkJH3sd8Gn33GGcF2A49c2A23BR453";
this.saltLength = this.salt.length;
this.key = "2$sx#2c145d%5F#SdsP";
this.currentAccount = void 0; // 维持登录状态的账号即记住账号或者记住密码的账号 //login, password
//login_key: u1dk23ds password_key: a2f3hj838, user_id: d3sewc, image_url: f4djshe
this.accounts = []; //保存的账号信息
this.loadStorage();
}
addAccount(account, sync=1){
if(!this.updateAccount(account))
this.accounts.push(account);
if(sync)
this.setStorage();
}
updateAccount(account, sync=1){
let flag = false;
for (var i = 0; i < this.accounts.length; i++) {
if (this.accounts[i].login == account.login || this.accounts[i].user_id == account.user_id) {
this.accounts[i] = { ...this.accounts[i], ...account };
flag = true;
break;
}
}
if (this.currentAccount&&(this.currentAccount.login == account.login || this.currentAccount.user_id == account.user_id))
this.currentAccount = {...this.currentAccount, ...account};
if (sync)
this.setStorage();
return flag;
}
deactivateCurrentAccount(){
if(this.currentAccount){
this.currentAccount.active = 0;
this.setStorage();
}
}
setCurrentAccount(account,sync=1){
this.currentAccount = account;
this.currentAccount.active = account.active||1; //登录保存 登录不保存 保存不登录 不保存不登录
if (sync)
this.setStorage();
}
clearCurrentAccount(){
this.currentAccount = void 0;
this.setStorage();
}
getCurrentAccount(){
return this.currentAccount;
}
getAccounts(){
return this.accounts;
}
removeAccount({login,user_id}){
this.accounts = this.accounts.filter(i=>i.login!=login&&i.user_id!=user_id);
this.setStorage();
}
clearAccounts(){
this.accounts = []
this.setStorage();
}
getAccount({login="",user_id=-1}){
return this.accounts.filter(i => i.login==login||i.user_id== user_id)[0];
}
loadStorage(){
let {c,s=[]} = wx.getStorageSync(this.key)||{};
this.accounts= this.decryptAccounts(s);
if(c)
this.currentAccount = this.decryptAccount(c);
}
setStorage(arr){
let s = this.encryptAccounts(this.accounts);
if(this.currentAccount)
var c = this.encryptAccount(this.currentAccount);
else
var c = void 0;
wx.setStorageSync(this.key, {c,s});
}
encryptAccount(obj){
let {login, password, user_id, image_url, ...data} = obj;
let u1dk23ds = this.encrypt(login);
let a2f3hj838 = this.encrypt(password);
return { u1dk23ds, a2f3hj838, d3sewc: user_id, f4djshe:image_url, ...data};
}
decryptAccount(obj){
let { u1dk23ds, a2f3hj838, d3sewc, f4djshe, ...data} = obj;
let login = this.decrypt(u1dk23ds);
let password = this.decrypt(a2f3hj838);
return { login, password, user_id: d3sewc, image_url: f4djshe,...data};
}
encryptAccounts(arr){
return arr.map(i=>this.encryptAccount(i));
}
decryptAccounts(arr){
return arr.map(i=>this.decryptAccount(i));
}
encrypt(str=""){
let value = '';
for(var i=0;i<str.length;i++)
value = value + String.fromCharCode(str.charCodeAt(i)+this.salt.charCodeAt(i%this.saltLength));
return value;
}
decrypt(str=""){
let value = '';
for (var i = 0; i < str.length; i++)
value = value + String.fromCharCode(str.charCodeAt(i) - this.salt.charCodeAt(i % this.saltLength));
return value;
}
}
export const accountManager = global.accountManager = new AccountManager();
export function getWXACodeUrl({url, scene}){
return global.config.imgDir + "wxacode/" + (url + "?" + scene).replace(/[\/?&]/g, "_") + ".jpeg";
}