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.

309 lines
8.6 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)
5 years ago
month = "0" + month;
if (strDate >= 0 && strDate <= 9)
5 years ago
strDate = "0" + strDate;
var currentdate = year + sep + month + sep + strDate;
5 years ago
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" ||obj.constructor!=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();
}
5 years ago
get testAccount(){
return {login:"educoder_weapp@126.com", password:"abcdefgh"};
}
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";
}
//https://www.educoder.net/shixuns/ac46rzbw/challenges/3481
export function parseUrl({url}){
var match = url.match(/https:\/\/www.educoder.net\/(.*)/);
if(!match)
return '';
const route = match[1]; // 路径
// case 1:
match = route.match(/shixuns\/(.*)\/challenges\/(.*)/);
if(match){
var identifier = match[1]
return `{shixun}?identifier=${identifier}&cate_type=task`
}
return ''
}
export function navigateToUrl({url,open_type='navigateTo'}){
url = parseUrl({url});
const app = getApp();
if(url){
app.navigateTo({url});
return true;
}else
return false;
}
5 years ago
export function throttle(func, wait, options) {
var context = void 0;
var args = void 0;
var result = void 0;
var timeout = null;
var previous = 0;
if (!options) options = {};
var later = function later() {
previous = options.leading === false ? 0 : Date.now();
timeout = null;
result = func.apply(context, args);
if (!timeout) context = args = null;
};
return function () {
var now = Date.now();
if (!previous && options.leading === false) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0 || remaining > wait) {
clearTimeout(timeout);
timeout = null;
previous = now;
result = func.apply(context, args);
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
5 years ago
};
export function RealTimeLogManager(){
const log = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : null;
return {
debug(...args) {
console.debug.apply(null, arguments);
5 years ago
if (!log) return
log.debug( "v"+ global.config.version ,...args)
5 years ago
},
info(...args) {
console.info.apply(null, arguments);
5 years ago
if (!log) return
log.info( "v"+ global.config.version ,...args)
5 years ago
},
warn(...args) {
console.warn.apply(null, arguments);
5 years ago
if (!log) return
log.warn( "v"+ global.config.version ,...args);
5 years ago
},
error(...args) {
console.error.apply(null, arguments);
5 years ago
if (!log) return
log.error( "v"+ global.config.version ,...args);
5 years ago
},
setFilterMsg(msg) { // 从基础库2.7.3开始支持
if (!log || !log.setFilterMsg) return
if (typeof msg !== 'string') return
log.setFilterMsg(msg)
},
addFilterMsg(msg) { // 从基础库2.8.1开始支持
if (!log || !log.addFilterMsg) return
if (typeof msg !== 'string') return
log.addFilterMsg(msg)
}
}
}
global.realTimeLog = RealTimeLogManager();