/src/utils/index.js
// 本地存储
function locset(str, val) {
localStorage.setItem(str, val);
}
function locget(str) {
let _val = localStorage.getItem(str) ? localStorage.getItem(str) : null;
return _val;
}
function locremove(str) {
localStorage.removeItem(str);
}
// 将数字转换成 金钱格式
// 比如:1000000 转换成 1.000.000
function formatNum(num) {
var str = String(num);
var newStr = "";
var count = 0;
for (var i = str.length - 1; i >= 0; i--) {
if (count % 3 == 0 && count != 0) {
newStr = str.charAt(i) + "." + newStr;
} else {
newStr = str.charAt(i) + newStr;
}
count++;
}
return newStr;
}
// 数组排序
function jsonArraySort(arr, key, type) {
type = type || "small";
return arr.sort(function (a, b) {
if (type === "small") {
return a[key] - b[key];
} else {
return b[key] - a[key];
}
});
}
// 格式化时间 转化成 03/06/2019
function formatTime(cTime) {
let newDate;
cTime = parseInt(cTime);
let date = new Date(cTime);
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
month = month < 10 ? "0" + month : month;
day = day < 10 ? "0" + day : day;
newDate = month + "/" + day + "/" + year + "";
return newDate;
}
// 格式化时间,各种形式
// 使用 formatDate(date,"YYYY-MM-DD")
function formatDate(date, fromat) {
if (!(date instanceof Date)) return "data为Date格式"
const dateObj = {
YYYY: date.getFullYear(),
MM: date.getMonth(),
DD: date.getDate(),
hh: date.getHours(),
mm: date.getMinutes(),
ss: date.getSeconds(),
TT: date.getHours() >= 12 ? 'PM' : 'AM'
}
for (var k in dateObj) {
if (new RegExp('(' + k + ')').test(fromat)) {
fromat = fromat.replace(RegExp.$1, typeof dateObj[k] !== 'undefined' && dateObj[k] < 10 ? '0' + dateObj[k] : dateObj[k])
}
}
return fromat
}
//客户端调用h5自定义方法
function andrewMethod(name, callback) {
window[name] = callback;
}
// 倒计时
function CounterDown(time, target) {
const _id = setTimeout(() => {
this.CounterDown(time, target);
}, 1000);
time--;
target.isCount = true;
target.isClk = false;
target.clkTxt = (time > 9 ? time : "0" + time) + "s";
if (time == 0) {
target.clkTxt = "xxx";
target.false = false;
target.isClk = true;
clearTimeout(_id);
}
}
// 特殊数字
function LabelNumbers(num) {
// 创建连续或相同的数字
let arr = [];
for (let i = 0; i < num.len; i++) {
for (let j = 0; j < 10; j++) {
if (j < 7) {
arr.push(`${j}${j+1}${j+2}${j+3}`);
}
arr.push(j + '' + j + '' + j + '' + j);
if (j > 3) {
arr.push(`${j}${j-1}${j-2}${j-3}`);
}
}
}
return arr;
}
function doubleNum(n) {
return n > 9 ? n : "0" + n;
}
// 时分秒倒计时
function countdown(time, fun) {
if (time <= 0) return
time -= 1000; // 总的时间(毫秒)
var scend = parseInt((time / 1000) % 60); // 秒
var minute = parseInt((time / 1000 / 60) % 60); // 分钟
var hour = parseInt((time / 1000 / 60 / 60) % 24); // 小时
setTimeout(countdown.bind(this, time, fun), 1000); // 不加括号
fun({
seconds: doubleNum(scend),
minutes: doubleNum(minute),
hours: doubleNum(hour)
})
}
module.exports = {
locget,
locset,
locremove,
formatNum,
jsonArraySort,
formatTime,
doubleNum,
andrewMethod,
CounterDown,
LabelNumbers,
formatDate,
countdown
}
使用方式
import { countdown } from "../utils/index";
methods:{
countdown(this.investInfo.left_time * 1000, (d) => {
Object.assign(this, d);
});
}