//時間格式化 yyyy/MM/dd HH:mm:ss
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(':')
}
//時間格式化 yyyy/MM/dd HH:mm
const toformatTime = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute].map(formatNumber).join(':')
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
module.exports = {
formatTime: formatTime
}
module.exports.toformatTime = toformatTime;
/**
* 數字校驗
*/
const isNumber = num => {
if (!isNaN(num) && num != null && (num + '').length > 0) {
return true;
} else {
return false;
}
}
module.exports.isNumber = isNumber;
//+===========================================================================================================================
const finalUrl = 'http://localhost:8387/wizardlisa';
//+===========================================================================================================================
/**
* 請求封裝GET
*/
var httpGet = function (url, data) {
const Authorization = wx.getStorageSync('api_key');
const sys_source = wx.getStorageSync('sys_source');
var promise = new Promise(function (resolve, reject) {
wx.request({
url: finalUrl + url,
data: data,
method: 'GET',
header: {
'content-type': 'application/json',
'Authorization': Authorization,
'sys_source': sys_source
},
success: resolve,
fail: reject
})
});
return promise;
};
module.exports.httpGet = httpGet;
/**
* 請求封裝POST
*/
var httpPost = function (url, data) {
const Authorization = wx.getStorageSync('api_key');
const sys_source = wx.getStorageSync('sys_source');
var promise = new Promise(function (resolve, reject) {
wx.request({
url: finalUrl + url,
data: data,
method: 'POST',
header: {
'content-type': 'application/json',
'Authorization': Authorization,
'sys_source': sys_source
},
success: resolve,
fail: reject
})
});
return promise;
};
module.exports.httpPost = httpPost;
/**
* POST 上傳圖片
*/
var uploadFile = function (url, filePath, data) {
const Authorization = wx.getStorageSync('api_key');
const sys_source = wx.getStorageSync('sys_source');
var promise = new Promise(function (resolve, reject) {
wx.uploadFile({
url: finalUrl + url,
filePath: filePath,
name: 'file',
formData: data,
header: {
'content-type': 'application/json',
'Authorization': Authorization,
'sys_source': sys_source
},
success: resolve,
fail: reject
})
});
return promise;
};
module.exports.uploadFile = uploadFile;
/**
* 記錄小程序用戶formId
*/
var insertFormId = function (formId) {
if (formId && formId.indexOf('form') == -1) {
const Authorization = wx.getStorageSync('api_key');
const sys_source = wx.getStorageSync('sys_source');
wx.request({
url: finalUrl + '/notification/formidSave',
data: { formId: formId},
method: 'POST',
header: {
'content-type': 'application/json',
'Authorization': Authorization,
'sys_source': sys_source
},
})
}
};
module.exports.insertFormId = insertFormId;
/**
* 時間會話列表微信方式顯示
*/
var geWXtDate = function (dateTimeStamp) {
var result='';
var minute = 1000 * 60;
var hour = minute * 60;
var day = hour * 24;
var halfamonth = day * 15;
var month = day * 30;
var now = Date.parse(new Date());
var diffValue = now - dateTimeStamp;
var monthC = diffValue / month;
var weekC = diffValue / (7 * day);
var dayC = diffValue / day;
var hourC = diffValue / hour;
var minC = diffValue / minute;
if (monthC >= 1) {
result = "" + parseInt(monthC) + "月前";
}
else if (weekC >= 1) {
result = "" + parseInt(weekC) + "周前";
}
else if (dayC >= 1) {
result = "" + parseInt(dayC) + "天前";
}
else if (hourC >= 1) {
result = "" + parseInt(hourC) + "小時前";
}
else if (minC >= 1) {
result = "" + parseInt(minC) + "分鐘前";
} else{
result = "剛剛";
}
return result;
};
module.exports.geWXtDate = geWXtDate;
//會話 詳情 時間
var geWXtDetailDate = function (dateTimeStamp) {
var result = '';
const time = new Date(new Date().setHours(0, 0, 0, 0)).getTime();//當天0點時間
var date = new Date(dateTimeStamp)//傳參數時間
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
if(dateTimeStamp>time){//今天內
result = [hour, minute].map(formatNumber).join(':')
}else{
result = [month, day].map(formatNumber).join('.') + ' ' + [hour, minute].map(formatNumber).join(':')
}
return result;
};
module.exports.geWXtDetailDate = geWXtDetailDate;