You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
184 lines
4.9 KiB
184 lines
4.9 KiB
const formatTime = date => {
|
|
const year = date.getFullYear()
|
|
const month = date.getMonth() + 1
|
|
const day = date.getDate()
|
|
const hour = date.getHours()
|
|
const minute = date.getMinutes()
|
|
const second = date.getSeconds()
|
|
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
|
|
}
|
|
|
|
const formatNumber = n => {
|
|
n = n.toString()
|
|
return n[1] ? n : '0' + n
|
|
}
|
|
|
|
function beforeDay(day, format) {
|
|
var date = new Date()
|
|
var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
|
|
var returnArr = [];
|
|
returnArr.push(date.getFullYear());
|
|
returnArr.push(formatNumber(date.getMonth() + 1));
|
|
returnArr.push(formatNumber(date.getDate() - day));
|
|
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;
|
|
}
|
|
|
|
function formatDate(date, format) {
|
|
var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
|
|
var returnArr = [];
|
|
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;
|
|
}
|
|
|
|
function json2Form(json) {
|
|
var str = [];
|
|
for (var p in json) {
|
|
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]));
|
|
}
|
|
return str.join("&");
|
|
}
|
|
|
|
function isEmpty(val) {
|
|
if(val === 'undefined' || val === null || val === undefined){
|
|
return true
|
|
}
|
|
if(val){
|
|
val = val.toString().replace(/\s+/g, '')
|
|
}
|
|
return val === ''
|
|
}
|
|
|
|
function showTip(content) {
|
|
wx.showModal({
|
|
content: content,
|
|
showCancel: false
|
|
})
|
|
}
|
|
|
|
function showToast(content) {
|
|
if (isEmpty(content)) {
|
|
return
|
|
}
|
|
wx.showToast({
|
|
title: content,
|
|
icon: 'none'
|
|
})
|
|
}
|
|
|
|
function extend(obj) {
|
|
var o = {},
|
|
attr = Array.prototype.slice.call(arguments).slice(1);
|
|
attr.forEach(function (val, index) {
|
|
if (val in obj) {
|
|
o[val] = obj[val];
|
|
}
|
|
});
|
|
return o;
|
|
}
|
|
|
|
function playDing() {
|
|
const innerAudioContext = wx.createInnerAudioContext(); //新建一个createInnerAudioContext();
|
|
innerAudioContext.autoplay = true; //音频自动播放设置
|
|
innerAudioContext.src = '/assets/raw/ptt_playfinish.mp3'; //链接到音频的地址
|
|
innerAudioContext.onPlay(() => {}); //播放音效
|
|
innerAudioContext.onError((res) => { //打印错误
|
|
console.log(res.errMsg); //错误信息
|
|
console.log(res.errCode); //错误码
|
|
})
|
|
}
|
|
|
|
function showBackToast(content) {
|
|
if (isEmpty(content)) {
|
|
return
|
|
}
|
|
wx.showToast({
|
|
title: content,
|
|
icon: 'none',
|
|
duration: 1000,
|
|
success: function () {
|
|
setTimeout(function () {
|
|
wx.navigateBack()
|
|
}, 1000)
|
|
}
|
|
})
|
|
}
|
|
|
|
/* 验证手机号 */
|
|
function checkPhone(phone) {
|
|
var re = /^1\d{10}$/
|
|
return re.test(phone) && phone.length === 11
|
|
}
|
|
|
|
function checkId(id) {
|
|
// 1 "验证通过!", 0 //校验不通过 // id为身份证号码
|
|
var format = /^(([1][1-5])|([2][1-3])|([3][1-7])|([4][1-6])|([5][0-4])|([6][1-5])|([7][1])|([8][1-2]))\d{4}(([1][9]\d{2})|([2]\d{3}))(([0][1-9])|([1][0-2]))(([0][1-9])|([1-2][0-9])|([3][0-1]))\d{3}[0-9xX]$/;
|
|
//号码规则校验
|
|
if (!format.test(id)) {
|
|
return {
|
|
'status': 0,
|
|
'msg': '身份证号码不合规'
|
|
};
|
|
}
|
|
//区位码校验
|
|
//出生年月日校验 前正则限制起始年份为1900;
|
|
var year = id.substr(6, 4), //身份证年
|
|
month = id.substr(10, 2), //身份证月
|
|
date = id.substr(12, 2), //身份证日
|
|
time = Date.parse(month + '-' + date + '-' + year), //身份证日期时间戳date
|
|
now_time = Date.parse(new Date()), //当前时间戳
|
|
dates = (new Date(year, month, 0)).getDate(); //身份证当月天数
|
|
if (time > now_time || date > dates) {
|
|
return {
|
|
'status': 0,
|
|
'msg': '出生日期不合规'
|
|
}
|
|
}
|
|
//校验码判断
|
|
var c = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); //系数
|
|
var b = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); //校验码对照表
|
|
var id_array = id.split("");
|
|
var sum = 0;
|
|
for (var k = 0; k < 17; k++) {
|
|
sum += parseInt(id_array[k]) * parseInt(c[k]);
|
|
}
|
|
if (id_array[17].toUpperCase() != b[sum % 11].toUpperCase()) {
|
|
return {
|
|
'status': 0,
|
|
'msg': '身份证校验码不合规'
|
|
}
|
|
}
|
|
return {
|
|
'status': 1,
|
|
'msg': '校验通过'
|
|
}
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
isEmpty: isEmpty,
|
|
extend: extend,
|
|
formatTime: formatTime,
|
|
beforeDay: beforeDay,
|
|
formatDate: formatDate,
|
|
json2Form: json2Form,
|
|
showToast: showToast,
|
|
showTip: showTip,
|
|
playDing: playDing,
|
|
showBackToast: showBackToast,
|
|
checkPhone: checkPhone,
|
|
checkId: checkId
|
|
}
|