微信小程序:util.js函数集锦
Posted wuwuFQ
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信小程序:util.js函数集锦相关的知识,希望对你有一定的参考价值。
wxml使用函数的方法
- 创建
tools.wxs
文件 - 声明函数和函数实现,完全和
util.js
一样 - 在wxml引用
<wxs src="../../utils/tools.wxs" module="tools" />
- 使用eg:
<view class="amount">行程消费:¥{{tools.numberToFix(item.amount, 2)}}</view>
module.exports = {
formatDate: formatDate,
formatTime: formatTime,
formatDateTime: formatDateTime,
isNull: isNull,
numberToFix: numberToFix,
formatTimeDate: formatTimeDate,
dateTimeStringToTimeStamp: dateTimeStringToTimeStamp,
};
// obj是空值?
function isNullObj(obj) {
if (obj == null || obj == "" || obj.length == 0) {
return true;
} else {
return false;
}
}
/**
* 时间戳转化为年 月 日 时 分 秒
* number: 传入时间戳 毫秒
* format:返回格式自定义,但参数必须与formateArr里保持一致 (Y年M月D日 h:m:s) (Y-M-D h:m:s)
*/
function formatTimeDate(number, format) {
var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
var returnArr = [];
var date = new Date(parseInt(number));
returnArr.push(date.getFullYear());
returnArr.push(formatNumber(date.getMonth() + 1));
returnArr.push(formatNumber(date.getDate()));
returnArr.push(formatNumber(date.getHours()));
returnArr.push(formatNumber(date.getMinutes()));
returnArr.push(formatNumber(date.getSeconds()));
for (var i in returnArr) {
format = format.replace(formateArr[i], returnArr[i]);
}
return format;
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
/**时间字符串 转 时间戳
* date 2021-03-01 11:13:26
* 返回时间戳timestamp = 1614569100000 毫秒
*/
function dateTimeStringToTimeStamp(dateStr) {
//把日期'-'转为'/'
// date = date.replace(/-/g,'/');
var timestamp = new Date(dateStr).getTime();
return timestamp;
}
//timestamp 时间戳 或2020-10字符串
//option 格式(年月日 就输入YY-MM-DD 时分 就输入 hh-mm)
function formatDate(timestamp, option) {
var date = getDate(timestamp);
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate()
var hour = function () {
return date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
}
var minute = function () {
return date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
}
var second = function () {
return date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
}
//获取 年月日
if (option == 'YY-MM-DD') return " " + year + "-" + month + "-" + day;
//获取年月
if (option == 'YY-MM') return " " + year + "-" + month;
//获取年
if (option == 'YY') return " " + year;
//获取月
if (option == 'MM') return " " + month;
//获取日
if (option == 'DD') return " " + day;
//获取昨天
if (option == 'yesterday') return " " + day - 1;
//获取时分秒
if (option == 'hh-mm-ss') return " " + hour() + ":" + minute() + ":" + second();
//获取时分
if (option == 'hh-mm') return " " + hour() + ":" + minute();
//获取分秒
if (option == 'mm-ss') return minute() + ":" + second();
//获取分
if (option == 'mm') return minute();
//获取秒
if (option == 'ss') return second();
//默认时分秒年月日
return year + '-' + month + '-' + day + ' ' + hour() + ':' + minute() + ":" + second();
}
// *号替换
function strReplace(value) {
value = value.toString();
var str = '';
str = value.substring(0, 3) + '****' + value.substring(7, value.length);
return str;
}
/**把数字转换成字符串,保留几位小数
* value: 数字
* num: 保留几位小数
*/
function numberToFix(value, num) {
return value.toFixed(num)
}
//编码的方法
function base64encode(str) {
//下面是64个基本的编码
var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64DecodeChars = new Array(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
var out, i, len;
var c1, c2, c3;
len = str.length;
i = 0;
out = "";
while (i < len) {
c1 = str.charCodeAt(i++) & 0xff;
if (i == len) {
out += base64EncodeChars.charAt(c1 >> 2);
out += base64EncodeChars.charAt((c1 & 0x3) << 4);
out += "==";
break;
}
c2 = str.charCodeAt(i++);
if (i == len) {
out += base64EncodeChars.charAt(c1 >> 2);
out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
out += base64EncodeChars.charAt((c2 & 0xF) << 2);
out += "=";
break;
}
c3 = str.charCodeAt(i++);
out += base64EncodeChars.charAt(c1 >> 2);
out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
out += base64EncodeChars.charAt(c3 & 0x3F);
}
return out;
}
//解码的方法
function base64decode(str) {
var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64DecodeChars = new Array(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
var c1, c2, c3, c4;
var i, len, out;
len = str.length;
i = 0;
out = "";
while (i < len) {
do {
c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
} while (i < len && c1 == -1);
if (c1 == -1)
break;
do {
c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
} while (i < len && c2 == -1);
if (c2 == -1)
break;
out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4以上是关于微信小程序:util.js函数集锦的主要内容,如果未能解决你的问题,请参考以下文章