mirror of
https://gitee.com/elegant_wings/dbd-meeting-wx-small.git
synced 2025-06-21 21:19:37 +08:00
390 lines
10 KiB
JavaScript
390 lines
10 KiB
JavaScript
let app = getApp();
|
||
|
||
//返回 例 2017
|
||
const formatYear = data => {
|
||
var date = data.date;
|
||
var types = data.types;
|
||
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()
|
||
if (types == 'year') {
|
||
return [year].map(formatNumber).join('-')
|
||
} else if (types == 'month') {
|
||
return [month].map(formatNumber).join('-')
|
||
} else if (types == 'day') {
|
||
return [day].map(formatNumber).join('-')
|
||
}
|
||
|
||
}
|
||
|
||
//返回 例 12月02日
|
||
const formatDate2 = date => {
|
||
const month = date.getMonth() + 1
|
||
const day = date.getDate()
|
||
return [month].map(formatNumber) + '月' + [day].map(formatNumber) + '日'
|
||
}
|
||
|
||
//返回 例 2017-12-12
|
||
const formatDate = 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('-')
|
||
}
|
||
//返回 例 2017-12-12 12:30:00
|
||
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(':')
|
||
}
|
||
//返回 例 12-12 12:30:00
|
||
const formatTime2 = date => {
|
||
const month = date.getMonth() + 1
|
||
const day = date.getDate()
|
||
const hour = date.getHours()
|
||
const minute = date.getMinutes()
|
||
|
||
return [month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
|
||
}
|
||
//返回 小时12:00:00
|
||
const formatHour = data => {
|
||
var date = data.date;
|
||
var types = data.types;
|
||
const hour = date.getHours()
|
||
const minute = date.getMinutes()
|
||
const second = date.getSeconds()
|
||
|
||
if (types == 'hour') {
|
||
return [hour].map(formatNumber)
|
||
} else if (types == 'minute') {
|
||
return [minute].map(formatNumber).join('-')
|
||
} else if (types == 'second') {
|
||
return [second].map(formatNumber)
|
||
}
|
||
}
|
||
function isString(value) {
|
||
return typeof value === "string";
|
||
}
|
||
|
||
//返回 指定秒数59 , 2017-12-12 12:30:59
|
||
function selfFormatTimeReturnSecond59(time) {
|
||
if (typeof time === "string" && time.includes('-')) {
|
||
time = time.replaceAll('-', '/')
|
||
}
|
||
let date = new Date(time);
|
||
let year = date.getFullYear()
|
||
let month = date.getMonth() + 1
|
||
let day = date.getDate()
|
||
let hour = date.getHours()
|
||
let minute = date.getMinutes()
|
||
let second = date.getSeconds()
|
||
|
||
return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, '59'].map(formatNumber).join(':')
|
||
}
|
||
|
||
//返回 2017-12-12 12:30:59
|
||
function selfFormatTimeYMDHMS(time) {
|
||
if (typeof time === "string" && time.includes('-')) {
|
||
time = time.replaceAll('-', '/')
|
||
}
|
||
let date = new Date(time);
|
||
let year = date.getFullYear()
|
||
let month = date.getMonth() + 1
|
||
let day = date.getDate()
|
||
let hour = date.getHours()
|
||
let minute = date.getMinutes()
|
||
let second = date.getSeconds()
|
||
|
||
return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
|
||
}
|
||
|
||
|
||
//返回 2017-12-12 12:30
|
||
function selfFormatTimeYMDHM(time) {
|
||
if (typeof time === "string" && time.includes('-')) {
|
||
time = time.replaceAll('-', '/')
|
||
}
|
||
let date = new Date(time);
|
||
let year = date.getFullYear()
|
||
let month = date.getMonth() + 1
|
||
let day = date.getDate()
|
||
let hour = date.getHours()
|
||
let minute = date.getMinutes()
|
||
let second = date.getSeconds()
|
||
|
||
return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
|
||
}
|
||
|
||
//返回 2017-12-12 12
|
||
function selfFormatTimeYMDH(time) {
|
||
if (typeof time === "string" && time.includes('-')) {
|
||
time = time.replaceAll('-', '/')
|
||
}
|
||
let date = new Date(time);
|
||
let year = date.getFullYear()
|
||
let month = date.getMonth() + 1
|
||
let day = date.getDate()
|
||
let hour = date.getHours()
|
||
let minute = date.getMinutes()
|
||
let second = date.getSeconds()
|
||
|
||
return [year, month, day].map(formatNumber).join('-') + ' ' + [hour].map(formatNumber).join(':')
|
||
}
|
||
|
||
//返回 例 2017-12-12
|
||
function selfFormatTimeYMD(time) {
|
||
if (typeof time === "string" && time.includes('-')) {
|
||
time = time.replaceAll('-', '/')
|
||
}
|
||
let date = new Date(time)
|
||
let year = date.getFullYear()
|
||
let month = date.getMonth() + 1
|
||
let day = date.getDate()
|
||
let hour = date.getHours()
|
||
let minute = date.getMinutes()
|
||
let second = date.getSeconds()
|
||
return [year, month, day].map(formatNumber).join('-')
|
||
}
|
||
|
||
//返回 12:30
|
||
function selfFormatTimeHM(time) {
|
||
if (typeof time === "string" && time.includes('-')) {
|
||
time = time.replaceAll('-', '/')
|
||
}
|
||
let date = new Date(time);
|
||
let year = date.getFullYear()
|
||
let month = date.getMonth() + 1
|
||
let day = date.getDate()
|
||
let hour = date.getHours()
|
||
let minute = date.getMinutes()
|
||
let second = date.getSeconds()
|
||
|
||
return [hour, minute].map(formatNumber).join(':')
|
||
}
|
||
|
||
/**
|
||
* JS 计算两个时间间隔多久(时分秒)
|
||
* @param startTime "2019-10-23 15:27:23"
|
||
* @param endTime "2019-10-23 15:27:55"
|
||
* @return 1天2时3分5秒
|
||
*/
|
||
function twoTimeInterval(startTime, endTime) {
|
||
if (typeof startTime === "string" && startTime.includes('-')) {
|
||
startTime = startTime.replaceAll('-', '/')
|
||
}
|
||
if (typeof endTime === "string" && endTime.includes('-')) {
|
||
endTime = endTime.replaceAll('-', '/')
|
||
}
|
||
// 开始时间
|
||
let date1 = new Date(startTime);
|
||
// 结束时间
|
||
let date2 = new Date(endTime);
|
||
// 时间相差秒数
|
||
let dateDiff = date2.getTime() - date1.getTime();
|
||
// 计算出相差天数
|
||
let days = Math.floor(dateDiff / (24 * 3600 * 1000));
|
||
// 计算出小时数
|
||
let residue1 = dateDiff % (24 * 3600 * 1000); // 计算天数后剩余的毫秒数
|
||
let hours = Math.floor(residue1 / (3600 * 1000));
|
||
// 计算相差分钟数
|
||
let residue2 = residue1 % (3600 * 1000); // 计算小时数后剩余的毫秒数
|
||
let minutes = Math.floor(residue2 / (60 * 1000));
|
||
// 计算相差秒数
|
||
let residue3 = residue2 % (60 * 1000); // 计算分钟数后剩余的毫秒数
|
||
let seconds = Math.round(residue3 / 1000);
|
||
let returnVal =
|
||
((days == 0) ? "" : days + "天") +
|
||
((hours == 0) ? "" : hours + "时") +
|
||
((minutes == 0) ? "" : minutes + "分")
|
||
// + ((seconds == 0) ? "" : seconds + "秒");
|
||
return returnVal;
|
||
}
|
||
|
||
|
||
/**
|
||
* JS 计算两个时间间隔多久(时)
|
||
* @param startTime "2019-10-23 15:27:23"
|
||
* @param endTime "2019-10-23 15:27:55"
|
||
* @return 12时
|
||
*/
|
||
function twoTimeIntervalReturnHours(startTime, endTime) {
|
||
if (typeof startTime === "string" && startTime.includes('-')) {
|
||
startTime = startTime.replaceAll('-', '/')
|
||
}
|
||
if (typeof endTime === "string" && endTime.includes('-')) {
|
||
endTime = endTime.replaceAll('-', '/')
|
||
}
|
||
// 开始时间
|
||
let date1 = new Date(startTime);
|
||
// 结束时间
|
||
let date2 = new Date(endTime);
|
||
// 时间相差秒数
|
||
let dateDiff = date2.getTime() - date1.getTime();
|
||
// 计算出小时数
|
||
let hours = Math.floor(dateDiff / (3600 * 1000));
|
||
return hours;
|
||
}
|
||
|
||
const formatNumber = n => {
|
||
n = n.toString()
|
||
return n[1] ? n : '0' + n
|
||
}
|
||
|
||
// 数组sum合计
|
||
function selfArrSum(list) {
|
||
let count = 0;
|
||
list.map(item => {
|
||
count = count + item
|
||
})
|
||
return count
|
||
}
|
||
|
||
// 返回url参数对象
|
||
function getUrlParamsObj(url) {
|
||
// 通过 ? 分割获取后面的参数字符串
|
||
let urlStr = url.split('?')[1]
|
||
// 创建空对象存储参数
|
||
let obj = {};
|
||
// 再通过 & 将每一个参数单独分割出来
|
||
let paramsArr = urlStr.split('&')
|
||
for (let i = 0, len = paramsArr.length; i < len; i++) {
|
||
// 再通过 = 将每一个参数分割为 key:value 的形式
|
||
let arr = paramsArr[i].split('=')
|
||
obj[arr[0]] = arr[1];
|
||
}
|
||
return obj
|
||
}
|
||
|
||
// 判断是否是图片
|
||
function checkIsImg(url) {
|
||
if (!/\.(jpg|jpeg|png|GIF|JPG|PNG)$/.test(url)) {
|
||
return false;
|
||
} else {
|
||
return true;
|
||
}
|
||
}
|
||
|
||
function uploadFile(url) {
|
||
let _this = this
|
||
return new Promise((resolve, reject) => {
|
||
wx.uploadFile({
|
||
url: app.DOMAIN_NAME + '/api/dfs/upload',
|
||
filePath: url,
|
||
name: 'file',
|
||
header: {
|
||
"Authorization": 'Bearer ' + wx.getStorageSync('token')
|
||
},
|
||
formData: {},
|
||
success: res => {
|
||
resolve(JSON.parse(res.data));
|
||
},
|
||
fail: err => {
|
||
reject(err);
|
||
}
|
||
})
|
||
})
|
||
}
|
||
|
||
|
||
// 报修上传图片视频
|
||
// url: localhost:9227/app/repairAttach/upload?repairId=&operate=repair\feedback\eval
|
||
// repairId : 工单id,报修人不传
|
||
// operate : repair报修,feedback维修,eval评价
|
||
function repairAttachUpload(data) {
|
||
let _this = this
|
||
let paramUrl = "?a=a"
|
||
if (data.repairId) {
|
||
paramUrl = paramUrl + '&repairId=' + data.repairId
|
||
}
|
||
if (data.operate) {
|
||
paramUrl = paramUrl + '&operate=' + data.operate
|
||
}
|
||
return new Promise((resolve, reject) => {
|
||
wx.uploadFile({
|
||
url: app.DOMAIN_NAME + '/app/repairAttach/upload' + paramUrl,
|
||
filePath: data.url,
|
||
name: 'file',
|
||
header: {
|
||
"Authorization": 'Bearer ' + wx.getStorageSync('token')
|
||
},
|
||
formData: {},
|
||
success: res => {
|
||
resolve(JSON.parse(res.data));
|
||
},
|
||
fail: err => {
|
||
reject(err);
|
||
}
|
||
})
|
||
})
|
||
}
|
||
|
||
// 报修上传语音
|
||
// url: localhost:9227/app/repairAttach/upload/voice
|
||
function uploadVoice(url) {
|
||
let _this = this
|
||
return new Promise((resolve, reject) => {
|
||
wx.uploadFile({
|
||
url: app.DOMAIN_NAME + '/app/repairAttach/upload/voice',
|
||
filePath: url,
|
||
name: 'file',
|
||
header: {
|
||
"Authorization": 'Bearer ' + wx.getStorageSync('token')
|
||
},
|
||
formData: {},
|
||
success: res => {
|
||
resolve(JSON.parse(res.data));
|
||
},
|
||
fail: err => {
|
||
reject(err);
|
||
}
|
||
})
|
||
})
|
||
}
|
||
|
||
function getWeekday(dateStr) {
|
||
const weekDays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
|
||
const date = new Date(dateStr);
|
||
const weekday = date.getDay();
|
||
return weekDays[weekday];
|
||
}
|
||
|
||
function getNowDate() {
|
||
const today = new Date();
|
||
return today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate()
|
||
}
|
||
|
||
module.exports = {
|
||
formatTime: formatTime,
|
||
formatDate: formatDate,
|
||
formatYear: formatYear,
|
||
formatDate2: formatDate2,
|
||
formatHour: formatHour,
|
||
formatTime2: formatTime2,
|
||
selfFormatTimeReturnSecond59,
|
||
selfFormatTimeYMDHMS,
|
||
selfFormatTimeYMDHM,
|
||
selfFormatTimeYMD,
|
||
selfFormatTimeHM,
|
||
selfFormatTimeYMDH,
|
||
twoTimeInterval,
|
||
getWeekday,
|
||
getNowDate,
|
||
twoTimeIntervalReturnHours,
|
||
selfArrSum,
|
||
getUrlParamsObj,
|
||
checkIsImg,
|
||
uploadFile,
|
||
repairAttachUpload,
|
||
uploadVoice
|
||
} |