当前位置:网站首页>记项目 常用js方法
记项目 常用js方法
2022-07-28 14:48:00 【行行_出状元】
// 生成随机颜色
color16(){ //十六进制颜色随机
let r = Math.floor(Math.random()*256);
let g = Math.floor(Math.random()*256);
let b = Math.floor(Math.random()*256);
let color = '#'+(Array(6).join(0) + (r.toString(16)+g.toString(16)+b.toString(16))).slice(-6);
return color;
}
rgb(){ //rgb颜色随机
let r = Math.floor(Math.random()*256);
let g = Math.floor(Math.random()*256);
let b = Math.floor(Math.random()*256);
let rgb = 'rgb('+r+','+g+','+b+')';
return rgb;
}
let sum = this.seriesData.reduce((sum, e) => sum + e.value, 0); // 求和
let max = Math.max.apply(Math,this.seriesData.map(item => { return item.value })); // 求最大值//string格式化
repalceHtml (str) {
let s1 = str.replace(/<\/?.+?>/g, '')
let s2 = s1.replace(/ /g, '')// 去html
let s3 = s2.replace(/ /ig, '') // 转为空格
// let s4 = s3.replace(/\s*/g, '')// 去空格;
return s3
}// 百分数转小数
toPoint (percent) {
let str = percent.replace('%', '')
str = str / 100
return str
}// 大写金额
DX (n) {
if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n)) { return '数据非法' }
let unit = '千百拾亿千百拾万千百拾元角分'
let str = ''
n += '00'
let p = n.indexOf('.')
if (p >= 0) { n = n.substring(0, p) + n.substr(p + 1, 2) }
unit = unit.substr(unit.length - n.length)
for (let i = 0; i < n.length; i++) { str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i) }
return str.replace(/零(千|百|拾|角)/g, '零').replace(/(零)+/g, '零').replace(/零(万|亿|元)/g, '$1').replace(/(亿)万|壹(拾)/g, '$1$2').replace(/^元零?|零分/g, '').replace(/元$/g, '元整')
}// 手机号正则
let reg = 11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/
if (!reg.test(this.form.account)) {
this.$message.warning('手机号码格式不匹配!')
return false
}// 数组转对象
array2obj (array, key) {
let resObj = {}
for (let i = 0; i < array.length; i++) {
resObj[array[i][key]] = array[i]
}
return resObj
}// 数组转树状结构
let newArr = roleTree.data.treeList
if (newArr.length !== 0) {
newArr.forEach(item => {
const pid = item.pid
if (pid !== '') {
newArr.forEach(ele => {
if (ele.id === pid) {
let childArray = ele.children
if (!childArray) {
childArray = []
}
childArray.push(item)
ele.children = childArray
}
})
}
})
}// 文件下载
let fileName = data.data.replace(/redirect:/, '') // 文件地址
let downName = row.name // 文件下载名称
const blob = new Blob([fileName])
if (window.navigator.msSaveOrOpenBlob) {
// 兼容IE10
navigator.msSaveBlob(blob, downName)
} else {
// chrome/firefox
let aTag = document.createElement('a')
aTag.download = downName
aTag.href = URL.createObjectURL(blob)
aTag.click()
URL.revokeObjectURL(aTag.href)
}// 状态转换
convertStatus (status) {
switch (status) {
case 'NORMAL':
return '正常'
case 'REST':
return '停业'
case 'VIOLATION':
return '违规'
case 'TO_CHECK':
return '待审核'
case 'NO_PASS':
return '未通过'
}
}
isAbled (state) {
if (state === 'TO_PAY') {
return '待付款'
} else if (state === 'TO_CHECK') {
return '待审批'
} else if (state === 'TO_DELIVERY') {
return '调剂中'
} else if (state === 'TO_RECEIVED') {
return '待收货'
} else if (state === 'TO_APPRAISE') {
return '待评价'
} else if (state === 'COMPLETED') {
return '交易成功'
} else if (state === 'REFUNDING') {
return '退款中'
} else if (state === 'REFUND_COMPLETE') {
return '退款成功'
} else if (state === 'CLOSED') {
return '交易关闭'
}
}
item.state = this.isAbled(item.state)未完待续。。。
边栏推荐
猜你喜欢

Software architecture and design (IV) -- data flow architecture

FTP file transfer protocol

Nftscan and nftplay have reached strategic cooperation in the field of NFT data

Software architecture and design (VIII) -- distributed architecture

Easyexcel complex header export (one to many)

Endnote is associated with word

开光量输入/继电器输出rs485/232远程数据采集IO模块IBF70

Software architecture and design (I) -- key principles

5路DI/DO继电器输出远程IO采集模块Modbus TCP/IBF95

Flowable workflow all business concepts
随机推荐
19. Channel assignment task definition
DNS域名解析协议
Software architecture and design (IX) -- component based architecture
4.8 hd-gr GNSS navigation software source code
1200 times faster! MIT develops a new generation of drug research and development AI, and suspends the old model
5路DI/DO继电器输出远程IO采集模块Modbus TCP/IBF95
Endnote 与word关联
取组合数问题
德国电信否认加强与华为合作,并称过去3年已缩减与华为的合作
12V脉冲转速测量转24V电平信号转换变送器
Communication between client and server based on rsocket protocol
How to obtain and embed go binary execution package information
Among the three "difficult and miscellaneous diseases" of machine learning, causal learning is the breakthrough | Liu Li, Chongqing University
Matlab does not overwrite importing Excel
Flutter中是使用RxDart代替Stateful
关于word文档中插入的图片只显示下面一部分
How to build opengrok code server
Several slips of X rust, those things that have to be said
虚拟机之NAT模式下设置静态IP
Docker实现Redis Cluster(集群)模式 哈希槽分区进行亿级数据存储