Files
CompanyProject/衣莎项目/源码/gc_nwjs/src/utils/utils.js
2024-09-27 01:32:49 +08:00

144 lines
4.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}