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

148 lines
3.7 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

const configDefault = {
showError: true,
canEmpty: false,
returnOrigin: false,
withoutCheck: false,
mock: false,
timeout: 10000,
responseType: 'json',
// credentials: 'include',
};
function request(method, path, data, config) {
const myInit = {
...configDefault,
...config,
method,
// body: JSON.stringify(data),
body: (data),
};
if (method === 'GET') {
let params = '';
let ifp = '';
if (data) {
// 对象转url参数
params = new URLSearchParams(data).toString();
ifp = params ? '?' : ifp;
}
ifp = path.includes('?') ? '' : ifp;
return fetch(`${path}${ifp}${params}`, {
...configDefault,
...config,
});
}
return fetch(path, myInit);
}
// get
async function get(path, data, config) {
try {
const r = await request('GET', path, data, config);
const r_1 = await r.json();
return r_1;
} catch (err) {
return console.log('Request Failed', err);
}
}
// post
async function post(path, data, config) {
try {
const body = JSON.stringify(data);
const r = await request('POST', path, body, config);
const r_1 = await r.json();
return r_1;
} catch (err) {
return console.log('Request Failed', err);
}
}
// postForm
async function postForm(path, data, config) {
try {
const formData = new FormData();
Object.keys(data).forEach(key => {
formData.append(key, data[key]);
});
const r = await request('POST', path, formData, config);
const r_1 = await r.json();
return r_1;
} catch (err) {
return console.log('Request Failed', err);
}
}
/**
* 千分位 格式化数字
*/
const numberFormatter = (number, d = 2, m = 2) => {
return new Intl.NumberFormat(undefined, {
minimumFractionDigits: d,
maximumFractionDigits: m,
}).format(number);
};
/**
* 人民币:元 转换为 万元
*/
const CNYConvertK = (number, scale = 10) => {
return numberFormatter((number/(1000*scale)), 0, 0);
};
function isEmpty(val) {
// return val === undefined || val === null || val === "";
return [Object, Array].includes((val || {}).constructor) && !Object.entries((val || {})).length;
}
function groupBy(array, callback) {
return array.reduce((groups, item) => {
const key = typeof callback === 'function' ? callback(item) : item[callback];
if (!groups[key]) {
groups[key] = [];
}
groups[key].push(item);
return groups;
}, {});
}
var now = new Date(); //当前日期
var nowDayOfWeek = now.getDay(); //今天本周的第几天
var nowDay = now.getDate(); //当前日
var nowMonth = now.getMonth(); //当前月
var nowYear = now.getYear(); //当前年
nowYear += nowYear < 2000 ? 1900 : 0; //
var lastMonthDate = new Date(); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
var lastYear = lastMonthDate.getYear();
var lastMonth = lastMonthDate.getMonth();
//格式化日期yyyy-MM-dd
function formatDate(date) {
var myyear = date.getFullYear();
var mymonth = date.getMonth() + 1;
var myweekday = date.getDate();
if (mymonth < 10) {
mymonth = '0' + mymonth;
}
if (myweekday < 10) {
myweekday = '0' + myweekday;
}
return myyear + '-' + mymonth + '-' + myweekday;
}
//获得某月的天数
function getMonthDays(myMonth) {
var monthStartDate = new Date(nowYear, myMonth, 1);
var monthEndDate = new Date(nowYear, myMonth + 1, 1);
var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
return days;
}
//获得本月的开始日期
function getMonthStartDate() {
var monthStartDate = new Date(nowYear, nowMonth, 1);
return formatDate(monthStartDate);
}
//获得本月的结束日期
function getMonthEndDate() {
var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
return formatDate(monthEndDate);
}