Commit 70928b94 by Denglingling

新增本地工具类

parent 00131332
Showing with 130 additions and 28 deletions
import Toast from 'react-native-root-toast';
import {Dimensions, Platform,PixelRatio, Text} from "react-native";
import { Dimensions, Platform, NativeModules, PixelRatio, Text } from "react-native";
/**
* 提示框
......@@ -11,20 +11,20 @@ export const show = (data, type) => {
let text_color = '#fafafa'
let back_color = '#222'
let tip_data = data
if(type == 'error') {
if (type == 'error') {
text_color = '#f5222d'
back_color ='#fff1f0'
back_color = '#fff1f0'
tip_data = ${data}`
} else if(type == 'warn') {
} else if (type == 'warn') {
text_color = '#fa8c16'
back_color ='#fff7e6'
back_color = '#fff7e6'
tip_data = `! ${data}`
} else if(type == 'success') {
} else if (type == 'success') {
text_color = '#52c41a'
back_color ='#f6ffed'
back_color = '#f6ffed'
tip_data = `√ ${data}`
}
if(data != null) {
if (data != null) {
Toast.show(tip_data, {
duration: Toast.durations.LONG,
position: Toast.positions.CENTER,
......@@ -42,7 +42,7 @@ export const show = (data, type) => {
// 判断属性是否为空
export const isEmpty = (data) => {
let flag = false
if(!data || data == '') {
if (!data || data == '') {
flag = true
}
return flag
......@@ -64,9 +64,14 @@ export const isIphoneX = () => {
)
};
// 修改日期格式 2020-04-23 17:41 yyyy-MM-ddThh:mm:ss
/**
* 格式化日期 yyyy-MM-ddThh:mm:ss 为 2020-04-23 17:41
* @param {string} time 'yyyy-MM-ddThh:mm:ss'
* @param {string} format 'yyyy-MM-dd'
*/
export const changeDateFormat = (time, format) => {
if( time === '' || !time ){ return }
if (time === '' || !time) { return }
let _time = new Date(time);
let date = {
"M+": _time.getMonth() + 1,
......@@ -89,9 +94,12 @@ export const changeDateFormat = (time, format) => {
return format;
}
// 格式化 2020-04-23 17:41 为日期
/**
* 格式化日期 'yyyy-MM-dd hh:mm' 为 Date 对象
* @param {string} str '2020-04-23 17:41'
*/
export const formatStrForDate = (str) => {
if(str === '' || !str) {
if (str === '' || !str) {
return
}
let date
......@@ -115,12 +123,11 @@ export const formatStrForDate = (str) => {
* @param {Object} params 请求参数
*/
export const getUrlParams = (url, params) => {
if(_typeof(params) !== 'object'){
show(`${params} 类型不是对象,请确认!`)
return
}
let paramsArray = [];
// params = {
// access_token : 'token1313i12213',
// org_code: 'A02',
// eller_code : 'shi.ming'
// }
Object.keys(params).forEach(
key => paramsArray.push(`${key}=${params[key]}`)
)
......@@ -139,9 +146,9 @@ export const getUrlParams = (url, params) => {
*/
export const dedupQuoteArray = (arr, typeName) => {
let result = [], tmp = {}
if( arr.length ) {
result = arr.reduce(function(init, item) {
tmp[item[typeName]] ? '' : ( tmp[item[typeName]] = true && init.push(item) )
if (arr.length) {
result = arr.reduce(function (init, item) {
tmp[item[typeName]] ? '' : (tmp[item[typeName]] = true && init.push(item))
return init
}, [])
}
......@@ -163,7 +170,7 @@ export function cloneObject(obj) {
* @param {String} typeName 排序字段名
*/
export function ascArray(arr, typeName) {
if(!arr.length) {
if (!arr.length) {
return
}
arr.sort((obj1, obj2) => {
......@@ -178,7 +185,7 @@ export function ascArray(arr, typeName) {
* @param {String} typeName 排序字段名
*/
export function descArray(arr, typeName) {
if(!arr.length) {
if (!arr.length) {
return
}
arr.sort((obj1, obj2) => {
......@@ -188,7 +195,7 @@ export function descArray(arr, typeName) {
}
/**
* 引用数组每一个对象,指定字段名及子项指定字段,的排序字段,都进行排序
* 引用数组指定元素中,根据某字段进行排序(正序/降序)
* 如:arr= [{id:2, name: 'xiaomi', child: [{ id:8, name: 'caiyan', child: [] }, { id:6, name: 'change', child: [] }, { id:1, name: 'wangsan', child: [] }]
* @param {Array} arr 原数组 如:arr
* @param {String} speciField 指定字段 如:'child'
......@@ -196,21 +203,115 @@ export function descArray(arr, typeName) {
* @param {String} sortType 排序方式 默认 'asc', 降序'desc'
*/
export function referenceArrSort(arr, speciField, typeName, sortType) {
if(!arr.length) {
if (!arr.length) {
return
}
if(sortType == 'desc') {
if (sortType == 'desc') {
descArray(arr, typeName)
} else {
ascArray(arr, typeName)
}
arr = arr.map(item => {
if(item[speciField] && item[speciField].length) {
referenceArrSort(item[speciField], speciField, typeName, sortType )
if (item[speciField] && item[speciField].length) {
referenceArrSort(item[speciField], speciField, typeName, sortType)
}
return item
})
return arr
}
/**
* 调用安卓下载
* @param {String} path
*/
export const openInstallActivity = (path) => {
if (path == null || path.length == 0) return;
console.log('path------下载地址--------', path);
NativeModules.RNToolsManager.openInstallActivity(path)
}
/**
* 判断数据类型
* @param {any} data
*/
export const _typeof = function (data) {
try {
let value = /\[object (\w+)\]/.exec(
Object.prototype.toString.call(data)
);
return value[1].toLowerCase();
} catch (e) {
throw `_typeof内部错误,${e}`
}
}
/**
* 接口常见错误提示
* @param {function} dispatch
* @param {object} res
* @param {function} exitCallback 退出回调
* @param {function} errCallback 错误回调
*/
export const showWarnErrorMessage = (dispatch, res, exitCallback, errCallback) => {
if (_typeof(dispatch) !== 'function') {
show(`${dispatch} 不是函数,请确认!`);
return
}
if (_typeof(exitCallback) !== 'function') {
show(`${exitCallback} 不是函数,请确认!`);
return
}
if (_typeof(errCallback) !== 'function') {
show(`${errCallback} 不是函数,请确认!`);
return
}
if (res.error_code === 41006) {
show('登录过期,请重新登录');
dispatch(exitCallback());
} else if (res.status === 401) {
show(`未授权${res.status},请重新登录!`)
dispatch(errCallback());
} else if (res.status === 403) {
show(`拒绝访问${res.status}!`)
dispatch(errCallback());
} else if (res.status === 404) {
show(`请求资源未找到${res.status},请联系管理员!`)
dispatch(errCallback());
} else if (res.status === 500) {
show(`服务器内部错误${res.status},请联系管理员!`)
dispatch(errCallback());
} else if (res.status === 503) {
show(`服务不可用${res.status},请联系管理员!`)
dispatch(errCallback());
} else if (res.status === 504) {
show(`请求网络超时!`)
dispatch(errCallback());
} else {
let error_msg = res.error_msg || res.message
show(error_msg);
dispatch(errCallback());
}
}
/**
* 接口异常提示
* @param {function} dispatch
* @param {object} err
* @param {function} errorCallback 错误回调
* @param {string} logTit 错误打印标题
*/
export const showErrorMessage = (dispatch, err, errorCallback, logTit) => {
if (_typeof(dispatch) !== 'function') {
show(`${dispatch} 不是函数,请确认!`);
return
}
if (_typeof(errorCallback) !== 'function') {
show(`${errorCallback} 不是函数,请确认!`);
return
}
console.log(`【${logTit}】接口请求错误:`)
console.log(err)
let error_msg = err.error || '请求接口错误,请联系管理员!'
show(error_msg);
dispatch(errorCallback());
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment