手记

common-all笔记-vuesell

common/data.js:

export function formatDate(date, fmt) {
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
}
let o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds()
};
for (let k in o) {
if (new RegExp((${k})).test(fmt)) {
let str = o[k] + '';
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
}
}
return fmt;
};

function padLeftZero(str) {
return ('00' + str).substr(str.length);
}

common/store.js:

export function saveToLocal(id, key, value) {
let seller = window.localStorage.seller;
if (!seller) {
seller = {};
seller[id] = {};
} else {
seller = JSON.parse(seller);
if (!seller[id]) {
seller[id] = {};
}
}
seller[id][key] = value;
window.localStorage.seller = JSON.stringify(seller);
};

export function loadFromLocal(id, key, def) {
let seller = window.localStorage.seller;
if (!seller) {
return def;
}
seller = JSON.parse(seller)[id];
if (!seller) {
return def;
}
let ret = seller[key];
return ret || def;
};

common/util.js:
/**

  • 解析url参数
  • @example ?id=12345&a=b
  • @return Object {id:12345,a:b}
    */
    export function urlParse() {
    let url = window.location.search;
    let obj = {};
    let reg = /[?&][^?&]+=[^?&]+/g;
    let arr = url.match(reg);
    // ['?id=12345', '&a=b']
    if (arr) {
    arr.forEach((item) => {
    let tempArr = item.substring(1).split('=');
    let key = decodeURIComponent(tempArr[0]);
    let val = decodeURIComponent(tempArr[1]);
    obj[key] = val;
    });
    }
    return obj;
    };
0人推荐
随时随地看视频
慕课网APP