Files
U8-Import/NX.ERP.Webapp/src/libs/tools.js

244 lines
5.9 KiB
JavaScript
Raw Normal View History

2021-04-30 16:39:20 +08:00
import formatDate from 'dateformat'
import axios from 'axios'
import XLSX from 'xlsx'
2021-04-30 11:51:01 +08:00
export const dateFormat = (date) => {
return formatDate(date, 'yyyy-mm-dd')
}
export const dateFormatFull = (date) => {
return formatDate(date, 'yyyy-mm-dd HH:MM:ss')
}
export const numberFormat = (data) => {
if (data != null) {
return data.toFixed(2).replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,')
} else {
return ''
}
}
2021-04-30 16:39:20 +08:00
export const edit2 = (k, data) => {
var aa = axios.post('/api/erp/wmsgz1', { date: k, duration: data })
return aa
2021-04-30 11:51:01 +08:00
}
2021-04-30 16:39:20 +08:00
export const edit1 = (k, data) => {
var aa = axios.post('/api/erp/wmsgz2', { date: k, times: data })
return aa
2021-04-30 11:51:01 +08:00
}
2021-04-30 16:39:20 +08:00
export const updateDay = (k, data) => {
var aa = axios.post('/api/Fault/updateDay', { Id: k, Day: data })
return aa
2021-04-30 11:51:01 +08:00
}
2021-04-30 16:39:20 +08:00
export const updateBeginTime = (k, data) => {
var aa = axios.post('/api/Fault/updateBeginTime', { Id: k, BeginTime: data })
return aa
2021-04-30 11:51:01 +08:00
}
2021-04-30 16:39:20 +08:00
export const updateFinishTime = (k, data) => {
var aa = axios.post('/api/Fault/updateFinishTime', { Id: k, FinishTime: data })
return aa
2021-04-30 11:51:01 +08:00
}
2021-04-30 16:39:20 +08:00
export const updateFaultDes = (k, data) => {
var aa = axios.post('/api/Fault/updateFaultDes', { Id: k, FaultDes: data })
return aa
2021-04-30 11:51:01 +08:00
}
2021-04-30 16:39:20 +08:00
export const updateImproveDes = (k, data) => {
var aa = axios.post('/api/Fault/updateImproveDes', { Id: k, ImproveDes: data })
return aa
2021-04-30 11:51:01 +08:00
}
2021-04-30 16:39:20 +08:00
export const updateOpname = (k, data) => {
var aa = axios.post('/api/Fault/updateOpname', { Id: k, Opname: data })
return aa
2021-04-30 11:51:01 +08:00
}
export const remove = (k) => {
2021-04-30 16:39:20 +08:00
var aa = axios.post('/api/Fault/updatedelete', { Id: k })
return aa
2021-04-30 11:51:01 +08:00
}
export const gethours = (k) => {
2021-04-30 16:39:20 +08:00
var allHours = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
2021-04-30 11:51:01 +08:00
let ar = allHours.filter(function(elem) {
2021-04-30 16:39:20 +08:00
return elem < Number(k.split(':')[0])
})
2021-04-30 11:51:01 +08:00
2021-04-30 16:39:20 +08:00
return ar
2021-04-30 11:51:01 +08:00
}
export const pageingData = (data, pagesize, index) => {
if (!data) return []
let pageNo = 1
if (typeof index === 'function') {
let _index = date.findIndex(index)
pageNo = index > 0 ? 1 : Math.ceil(_index / pagesize)
} else if (typeof index === 'number') {
pageNo = index
}
var offset = (pageNo - 1) * pagesize
return offset + pagesize >= data.length ? data.slice(offset, data.length) : data.slice(offset, offset + pagesize)
2021-04-30 16:39:20 +08:00
}
function getJsonWidth(jsonObj, level) {
if (Array.isArray(jsonObj)) {
let width = 0
jsonObj.forEach((el) => {
let _width = getJsonWidth(el, level)
width += _width - 1
})
width += jsonObj.length
return width
} else {
let _level = level ? level : 0
_level++
let width = jsonObj.children ? getJsonWidth(jsonObj.children, _level) : 1
jsonObj['width'] = width
jsonObj['level'] = _level
return width
}
}
function getHeght(jsonObj) {
let height = 0
BFSTree(jsonObj, (el) => {
height = Math.max(height, el.level)
})
return height
}
function DFSTree(_tree, fn) {
if (Array.isArray(_tree)) {
_tree.forEach((el) => {
DFSTree(el, fn)
})
} else {
if (_tree.children) {
DFSTree(_tree.children, fn)
}
fn(_tree)
}
}
function BFSTree(_tree, fn) {
let tree = JSON.parse(JSON.stringify(_tree))
let nodes = []
if (Array.isArray(tree)) {
tree.forEach((el) => {
nodes.push(el)
})
} else {
nodes.push(tree)
}
while (nodes.length > 0) {
let _node = nodes.shift()
if (_node.children) {
_node.children.forEach((el) => {
nodes.push(el)
})
}
fn(_node)
}
return tree
}
function SetLeft(tree, left) {
let totalWidth = left
if (Array.isArray(tree)) {
tree.forEach((el) => {
SetLeft(el, totalWidth)
totalWidth += el.width
})
} else {
tree['left'] = totalWidth
if (tree.children) {
SetLeft(tree.children, totalWidth)
}
}
}
export const export_col_to_excel = (colums, data, filename) => {
var _colums = JSON.parse(JSON.stringify(colums))
const wb = XLSX.utils.book_new()
var ws = {}
ws['!merges'] = []
let _row = -1
let _col = 0
///最大宽度
let maxWidth = getJsonWidth(_colums)
//头高度
let hedaHight = getHeght(_colums)
let maxHeight = hedaHight + data.length
SetLeft(_colums, 0)
let dataCol = {}
var _tree = BFSTree(_colums, (node) => {
if (!node.children) {
dataCol[node.key] = node
}
if (_row !== node.level - 1) {
_row = node.level - 1
}
ws[XLSX.utils.encode_cell({ c: node.left, r: _row })] = {
t: 's',
v: node.title
}
ws['!merges'].push({
s: {
c: node.left, //开始列
r: _row
},
e: {
//e结束
c: node.left + node.width - 1, //结束列
r: node.children ? _row : hedaHight - 1
}
})
})
let _dataRow = hedaHight
data.forEach((el) => {
Object.keys(el).forEach((key) => {
var _col = dataCol[key]
if (_col) {
var v = el[key]
var t = 'z'
//var z = "";
if (v && typeof v === 'Object' && !(v instanceof Date)) {
ws[XLSX.utils.encode_cell({ c: _col.left, r: _dataRow })] = v
} else {
if (v) {
if (typeof v == 'number') t = 'n'
else if (typeof v == 'boolean') t = 'b'
else if (v instanceof Date) {
t = 'd'
// t = "n";
// v = datenum(v);
// if(!o.cellDates) { t = 'n'; v = datenum(v); }
// z = o.dateNF || SSF._table[14];
} else {
t = 's'
// else if (typeof v == "string" ) t = "s";
}
ws[XLSX.utils.encode_cell({ c: _col.left, r: _dataRow })] = ({
t: t,
v: v.toString()
} /*:any*/)
// if(z) cell.z = z;
}
}
}
})
_dataRow++
})
var range = { s: { c: 0, r: 0 }, e: { c: maxWidth, r: maxHeight } }
ws['!ref'] = XLSX.utils.encode_range(range)
XLSX.utils.book_append_sheet(wb, ws, 'sheet1')
let _filename = filename ? filename : 'export.xlsx'
XLSX.writeFile(wb, _filename)
}