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.
120 lines
2.8 KiB
120 lines
2.8 KiB
const app = getApp() |
|
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 chooseImages(callback) { |
|
wx.chooseImage({ |
|
count: 1, |
|
sourceType: ['album', 'camera'], |
|
success: res => { |
|
const tempFilePaths = res.tempFilePaths; |
|
for(let i = 0;i < tempFilePaths.length; i++) { |
|
wx.getImageInfo({ |
|
src: tempFilePaths[i], |
|
success: image => { |
|
wx.showLoading({ title: '图片上传中', mask: true }) |
|
wx.uploadFile({ |
|
url: `${app.globalData.baseURL}api/upload`, |
|
file: image, |
|
filePath: image.path, |
|
header: { |
|
Authorization: wx.getStorageSync('token'), |
|
}, |
|
name: 'file', |
|
success: res => { |
|
if (callback) { |
|
callback(JSON.parse(res.data).link) |
|
} |
|
}, |
|
fail: err => { |
|
wx.showToast({ |
|
title: '上传图片失败', |
|
icon: 'none', |
|
duration: 2000, |
|
}) |
|
}, |
|
complete: res => { |
|
wx.hideLoading() |
|
}, |
|
}) |
|
}, |
|
fail: err => { |
|
wx.showToast({ |
|
title: '获取图片信息失败', |
|
icon: 'none', |
|
duration: 2000, |
|
}) |
|
}, |
|
}) |
|
} |
|
} |
|
}) |
|
} |
|
|
|
function chooseImages2(callback) { |
|
wx.chooseImage({ |
|
count: 3, |
|
sourceType: ['album', 'camera'], |
|
success: res => { |
|
const tempFilePaths = res.tempFilePaths; |
|
for(let i = 0;i < tempFilePaths.length; i++) { |
|
wx.getImageInfo({ |
|
src: tempFilePaths[i], |
|
success: image => { |
|
wx.showLoading({ title: '图片上传中', mask: true }) |
|
wx.uploadFile({ |
|
url: `${app.globalData.baseURL}api/upload`, |
|
file: image, |
|
filePath: image.path, |
|
header: { |
|
Authorization: wx.getStorageSync('token'), |
|
}, |
|
name: 'file', |
|
success: res => { |
|
if (callback) { |
|
callback(JSON.parse(res.data).link) |
|
} |
|
}, |
|
fail: err => { |
|
wx.showToast({ |
|
title: '上传图片失败', |
|
icon: 'none', |
|
duration: 2000, |
|
}) |
|
}, |
|
complete: res => { |
|
wx.hideLoading() |
|
}, |
|
}) |
|
}, |
|
fail: err => { |
|
wx.showToast({ |
|
title: '获取图片信息失败', |
|
icon: 'none', |
|
duration: 2000, |
|
}) |
|
}, |
|
}) |
|
} |
|
} |
|
}) |
|
} |
|
|
|
module.exports = { |
|
formatTime, |
|
chooseImages, |
|
chooseImages2 |
|
}
|
|
|