react.js 公共方法 集合

时间:2023-03-09 18:00:06
react.js 公共方法  集合

截取七牛上传图片的后缀名:

export function getInputKey(info){

    let self = this;
let obj = JSON.parse(info);
let qiniuKey;
let imaName;
let imgSuffix;
if(obj){
qiniuKey = obj.key;
imaName = obj.fileName;
imgSuffix = imaName.substring(imaName.lastIndexOf('.') + 1);
} this.props.callback(qiniuKey,imgSuffix);
}

 批量新增:

 addContent(e){
if(this.state.number.length >= this.state.maxNum){
return;
}
this.state.number.push(this.state.number[this.state.number.length -1] + 1);
let temp = this.state.number;
this.setState({
number: temp
});
}

单组删除:

removeContent(index){
if(this.state.number.length <= 1){
return;
}
this.state.number.splice(index, 1);
this.setState({
number: this.state.number
});
}
/**
* fetch函数res状态处理
*/
export function alertPre(type,params){
if (params.ok) {
alert("Outstanding! Your settings are saved.");
} else if (params.status == 401) {
alert("Warning! You are not authorized.");
} else if (params.status == 200) {
alert("Perfect! Your requests have bean received.");
} else {
alert("Upsetting! The request failed. status = " + params.status);
}
}
/**
* Storage 工具类
*/
class StorageUtils {
localSetItem(key, val) {
var value;
if (typeof val == 'object') {
value = 'o_' + JSON.stringify(val);
localStorage.setItem(key, value);
} else {
value = 's_' + val;
localStorage.setItem(key, value);
}
}
localGetItem(key) {
var value = localStorage.getItem(key);
if (value) {
if (value.indexOf('o') >= 0) {
value = value.substring(2);
value = JSON.parse(value);
} else {
value = value.substring(2);
}
}
return value;
}
localRemoveItem(key) {
localStorage.removeItem(key);
}
localRemoveAllItem() {
localStorage.clear();
} sessionSetItem(key, val) {
var value;
if (typeof val == 'object') {
value = 'o_' + JSON.stringify(val);
sessionStorage.setItem(key, value);
} else {
value = 's_' + val;
sessionStorage.setItem(key, value);
}
}
sessionGetItem(key) {
var value = sessionStorage.getItem(key);
if (value) {
if (value.indexOf('o') >= 0) {
value = value.substring(2);
value = JSON.parse(value);
} else {
value = value.substring(2);
}
}
return value;
}
sessionRemoveItem(key) {
sessionStorage.removeItem(key);
}
sessionRemoveAllItem() {
sessionStorage.clear();
}
}
/**
* 格式化工具类
*/
class FormatUtils {
/**
* 格式化删除数组
* @param rows state里的已选中行
* @param row 当前操作的行
* @param param 操作数据的唯一标示:字符串
* @returns {*}
*/
formatDeleteList(rows ,row ,param) {
let paramId = row[param];
let paramItem = {};
paramItem[param] = paramId;
if (rows) {
let index = -1;
for (let i = 0 ;i < rows.length ;i++) {
let rowItem = rows[i];
let id = rowItem[param];
if (paramId == id) {
index = i;
}
}
if (index > -1) {
rows.splice(index,1);
} else {
rows.push(paramItem);
}
} else {
rows = [];
rows.push(paramItem);
}
return rows;
} /**
* 时间搓转换为格式化的字符串
* @param timestamp 时间搓
* @param formatString 格式化格式
* @returns {*}
*/
formatTimestampToString(timestamp ,formatString) {
let type = typeof timestamp;
let times;
let formatTimes;
if (type === 'string') {
times = new Date(timestamp);
} else if (type === 'object' && timestamp instanceof Date) {
times = timestamp;
} else {
times = new Date;
}
let year = times.getFullYear();
let month = times.getMonth() + 1;
let date = times.getDate();
let hour = times.getHours();
let minute = times.getMinutes();
let second = times.getSeconds();
if(month < 10) month = '0' + month;
if(date < 10) date = '0' + date;
if(hour < 10) hour = '0' + hour;
if(minute < 10) minute = '0' + minute;
if(second < 10) second = '0' + second;
if (formatString === 'yyyy-mm-dd'){
formatTimes = `${year}-${month}-${date}`;
} else if(formatString === 'yyyy-mm-dd hh:mm:ss'){
formatTimes = `${year}-${month}-${date} ${hour}:${minute}:${second}`;
} else if(formatString === 'yyyy-mm-dd hh:mm'){
formatTimes = `${year}-${month}-${date} ${hour}:${minute}`;
} else {
formatTimes = `${year}-${month}-${date} ${hour}:${minute}:${second}`;
}
return formatTimes;
}
}
  • 将当前时间换算为北京时间
 let date = new Date();
let defaultDate;
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
let hour = date.getHours();
let minute = date.getMinutes();
let second = date.getSeconds();
month = month < 10 ? "0" + month : month;
day = day < 10 ? "0" + day : day; defaultDate = year + "-" + month + "-" + day; //将当前时间换算为北京时间
let currentDate = defaultDate;
let d = new Date();
let tmpHours = currentDate.getHours();
//算得时区
var time_zone = -d.getTimezoneOffset() / 60;
//少于0的是西区 西区应该用时区绝对值加京八区 重新设置时间(西区时间比东区时间早 所以加时区间隔)
if (time_zone < 0) {
time_zone = Math.abs(time_zone) + 8; currentDate.setHours(tmpHours + time_zone);
} else {
//大于0的是东区 东区时间直接跟京八区相减
time_zone -= 8; currentDate.setHours(tmpHours - time_zone);
} return currentDate; let stateTwo = getState().iosAndroidState; let dateStart = stateTwo.startTimes ? stateTwo.startTimes : currentDate;
let dateEnd = stateTwo.endTimes ? stateTwo.endTimes : currentDate;
/**
* 获取北京时间
*/
export function BJTime(e){
return(dispatch,getState) => { //获得当前运行环境时间
var d = new Date(), currentDate = new Date(), tmpHours = currentDate.getHours();
//算得时区
var time_zone = -d.getTimezoneOffset() / 60;
//少于0的是西区 西区应该用时区绝对值加京八区 重新设置时间(西区时间比东区时间早 所以加时区间隔)
if (time_zone < 0) {
time_zone = Math.abs(time_zone) + 8; currentDate.setHours(tmpHours + time_zone);
} else {
//大于0的是东区 东区时间直接跟京八区相减
time_zone -= 8; currentDate.setHours(tmpHours - time_zone);
} dispatch(setBJTime(currentDate));
}
} export const BEIJING_TIME = 'BEIJING_TIME';
function setBJTime(currentDate){
return{
type: BEIJING_TIME,
data: {
currentDate
}
}
}
 let localTime = date.getTime() -8 * 3600000 - date.getTimezoneOffset() * 60000;   //有效转换为北京时区的方法

将对象转换为query类型,在api中使用,通过这个方法可以对查询参数类型做判断:

//将对象转换为query路径
entityFormatQueryParam(params) {
let query = '';
let flag = false;
//效验一下
if (typeof params !== 'object' || null === params) {
query = '';
} else {
for (let key in params) {
let value = params[key];
if (!value || '' === value || !params.hasOwnProperty(key)) {
//排除undefined,null,'',原型链上的属性
continue;
}
let path = `${key}=${value}`;
if (flag) {
query += `&${path}`;
} else {
query += `?${path}`;
flag = true;
}
}
}
return query;
}

 判断查询接口传过来的参数是否为对象:

 parseParams(params){
let str = '?';
for(let key in params){
if(params[key] === undefined || params[key] === null || params[key] === ''){
continue;
}
str += key + '=' + params[key] + '&';
}
return str + '_s=0';
}