first commit
This commit is contained in:
144
衣莎项目/源码/gc_nwjs/src/utils/utils.js
Normal file
144
衣莎项目/源码/gc_nwjs/src/utils/utils.js
Normal file
@@ -0,0 +1,144 @@
|
||||
import config from '@/config/index';
|
||||
|
||||
export default {
|
||||
/**
|
||||
* 使用百度api,把文字转换成语音并播放
|
||||
* @param {string} str 需要转换成语音的字符串
|
||||
*/
|
||||
voiceText(str) {
|
||||
var url = "http://tsn.baidu.com/text2audio?lan=zh&ie=UTF-8&text=" + encodeURI(str);
|
||||
var audio = new Audio(url);
|
||||
audio.src = url;
|
||||
audio.play();
|
||||
},
|
||||
/**
|
||||
* 获取扫码枪扫码的内容
|
||||
* @param {Function} cell 回调函数返回扫码结果
|
||||
*/
|
||||
sweepCode(cell) {
|
||||
window.onload = function () {
|
||||
var code = "";
|
||||
var lastTime, nextTime;
|
||||
var lastCode, nextCode;
|
||||
document.onkeypress = function (e) {
|
||||
nextCode = e.which;
|
||||
nextTime = new Date().getTime();
|
||||
if (lastCode != null && lastTime != null && nextTime - lastTime <= 30) {
|
||||
code += String.fromCharCode(lastCode);
|
||||
} else if (lastCode != null && lastTime != null && nextTime - lastTime > 100) {
|
||||
code = "";
|
||||
}
|
||||
lastCode = nextCode;
|
||||
lastTime = nextTime;
|
||||
}
|
||||
this.onkeypress = function (e) {
|
||||
if (e.which == 13) {
|
||||
cell(code)
|
||||
code = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 判断是字符且存在
|
||||
* @param {*} str
|
||||
*/
|
||||
isString(str) {
|
||||
if (Object.prototype.toString.call(str) !== '[object String]' && str.length <= 0) return true;
|
||||
return false
|
||||
},
|
||||
/**
|
||||
* 获取本地缓存
|
||||
* @param {string} key 需要获取缓存中的名字
|
||||
*/
|
||||
getStorage(key) {
|
||||
if (this.isString(key)) return;
|
||||
try {
|
||||
return JSON.parse(localStorage.getItem(`${config.prefix}${key}`));
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 存储本地缓存
|
||||
* @param {string} key 需要缓存的名字
|
||||
* @param {any} value 需要set的值
|
||||
*/
|
||||
setStorage(key, value) {
|
||||
if (this.isString(key)) return;
|
||||
try {
|
||||
localStorage.setItem(key, JSON.stringify(value))
|
||||
return 'ok'
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 获取本地缓存
|
||||
* @param {string} key 需要获取缓存中的名字
|
||||
*/
|
||||
getSession(key) {
|
||||
if (this.isString(key)) return;
|
||||
console.log(key);
|
||||
try {
|
||||
return JSON.parse(sessionStorage.getItem(`${config.prefix}${key}`));
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 存储本地缓存
|
||||
* @param {string} key 需要缓存的名字
|
||||
* @param {any} value 需要set的值
|
||||
*/
|
||||
setSession(key, value) {
|
||||
if (this.isString(key)) return;
|
||||
try {
|
||||
localStorage.setItem(key, JSON.stringify(value))
|
||||
return 'ok'
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 删除指定key
|
||||
* @param {string} key
|
||||
*/
|
||||
removeItemSession(key) {
|
||||
if (this.isString(key)) return;
|
||||
try {
|
||||
sessionStorage.removeItem(`${config.prefix}${key}`)
|
||||
return 'ok'
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
},
|
||||
// 格式化日期,如月、日、时、分、秒保证为2位数
|
||||
formatNumber(n) {
|
||||
n = n.toString()
|
||||
return n[1] ? n : '0' + n;
|
||||
},
|
||||
/**
|
||||
* 用法formatTime(1545903266795, 'Y-M-D h:m:s')
|
||||
* @param {number} number 毫秒时间戳
|
||||
* @param {string} format 日期格式
|
||||
*/
|
||||
formatTime(number, format) {
|
||||
if (typeof number !== 'number') {
|
||||
number = parseInt(number)
|
||||
}
|
||||
let time = new Date(number)
|
||||
let newArr = []
|
||||
let formatArr = ['Y', 'M', 'D', 'h', 'm', 's']
|
||||
newArr.push(time.getFullYear())
|
||||
newArr.push(this.formatNumber(time.getMonth() + 1))
|
||||
newArr.push(this.formatNumber(time.getDate()))
|
||||
newArr.push(this.formatNumber(time.getHours()))
|
||||
newArr.push(this.formatNumber(time.getMinutes()))
|
||||
newArr.push(this.formatNumber(time.getSeconds()))
|
||||
for (let i in newArr) {
|
||||
format = format.replace(formatArr[i], newArr[i])
|
||||
}
|
||||
return format;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user