75 lines
1.8 KiB
JavaScript
75 lines
1.8 KiB
JavaScript
/**
|
|
* 日期格式化
|
|
* @param {Date} date
|
|
* @param {string} format 格式
|
|
*/
|
|
export function formatDate(date = new Date(), format = 'YYYY-MM-DD hh:mm:ss') {
|
|
date = new Date(date);
|
|
const YYYY = date.getFullYear();
|
|
const MM = ('0' + (date.getMonth() + 1)).slice(-2);
|
|
const DD = ('0' + date.getDate()).slice(-2);
|
|
const hh = ('0' + date.getHours()).slice(-2);
|
|
const mm = ('0' + date.getMinutes()).slice(-2);
|
|
const ss = ('0' + date.getSeconds()).slice(-2);
|
|
|
|
const result = format
|
|
.replace(/YYYY/, String(YYYY))
|
|
.replace(/MM/, MM)
|
|
.replace(/DD/, DD)
|
|
.replace(/hh/, hh)
|
|
.replace(/mm/, mm)
|
|
.replace(/ss/, ss);
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {路径} path
|
|
* @param {文件} file
|
|
*/
|
|
export function uploadQiuFile(path, file, is_public = 1) {
|
|
const fs = wx.getFileSystemManager();
|
|
wx.showLoading({
|
|
title: '上传中',
|
|
})
|
|
return new Promise((resolve) => {
|
|
fs.getFileInfo({
|
|
filePath: file,
|
|
async success(fileInfoRes) {
|
|
let suffix = file.split(".").at(-1)
|
|
let prefix = httpEnv == "release" ? '' : 'test/'
|
|
let qiuTokenRes = await request.get("/files/get_qiniu_upload_token", {
|
|
file_key: `curain/${prefix}${path}/${fileInfoRes.digest}.${suffix}`,
|
|
is_public: is_public
|
|
})
|
|
wx.uploadFile({
|
|
url: qiuTokenRes.upload_url,
|
|
filePath: file,
|
|
formData: {
|
|
token: qiuTokenRes.up_token,
|
|
fname: qiuTokenRes.file_key,
|
|
key: qiuTokenRes.file_key
|
|
},
|
|
timeout: 10000,
|
|
name: "file",
|
|
success(qiuRes) {
|
|
let qiuResJson = JSON.parse(qiuRes.data)
|
|
let url = `https://${qiuTokenRes.domain}/${qiuResJson.key}`
|
|
resolve({
|
|
url: url,
|
|
key: qiuResJson.key
|
|
})
|
|
},
|
|
fail() {
|
|
wx.showToast({
|
|
title: '上传失败',
|
|
icon: 'none'
|
|
})
|
|
resolve('')
|
|
}
|
|
})
|
|
wx.hideLoading()
|
|
}
|
|
})
|
|
})
|
|
} |