104 lines
2.8 KiB
JavaScript
104 lines
2.8 KiB
JavaScript
import AppService from './appService.js'
|
||
|
||
const uploadImage = (type, count = 9) => {
|
||
return new Promise((presolve, preject) => {
|
||
wx.chooseImage({
|
||
count: count, // 默认9
|
||
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
|
||
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
|
||
success: function (res) {
|
||
// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
|
||
const tempFiles = res.tempFiles
|
||
const tempFilePaths = res.tempFilePaths
|
||
const length = res.tempFilePaths.length
|
||
wx.showLoading({
|
||
title: '正在上传中',
|
||
mask: 'true'
|
||
})
|
||
uploadMulti(tempFilePaths, tempFiles, type).then((res) => {
|
||
presolve(res)
|
||
})
|
||
}
|
||
})
|
||
})
|
||
}
|
||
|
||
// 递归上传多张图片
|
||
const uploadMulti = (tempFilePaths, tempFiles) => {
|
||
return new Promise((presolve, preject) => {
|
||
let uploads = []
|
||
tempFilePaths.forEach((item, i) => {
|
||
uploads[i] = new Promise((resolve, reject) => {
|
||
wx.uploadFile({
|
||
url: `${AppService.apiConfig.configurl}/admin/upload`,
|
||
filePath: item,
|
||
name: 'file',
|
||
formData: {},
|
||
success: function (response) {
|
||
console.log(response)
|
||
const data = JSON.parse(response.data)
|
||
console.log(data)
|
||
if (data.code == 0) {
|
||
resolve({ url: data.data })
|
||
}
|
||
},
|
||
fail: function (err) {
|
||
wx.hideLoading()
|
||
wx.showToast({
|
||
title: '上传照片失败(′⌒`)',
|
||
icon: 'none'
|
||
})
|
||
reject(err)
|
||
}
|
||
})
|
||
})
|
||
})
|
||
Promise.all(uploads).then((res) => {
|
||
//图片上传完成
|
||
let data = []
|
||
res.forEach((item) => {
|
||
console.log(item)
|
||
data.push(item.url)
|
||
})
|
||
presolve(data)
|
||
wx.hideLoading()
|
||
}).catch(err => {
|
||
preject(err)
|
||
})
|
||
})
|
||
}
|
||
|
||
// 判断对象是否为空
|
||
const isEmptyObject = function (obj) {
|
||
for (const value in obj) {
|
||
if ({}.hasOwnProperty.call(obj, value)) {
|
||
return false
|
||
}
|
||
}
|
||
return true
|
||
}
|
||
|
||
const formatNumber = n => {
|
||
n = n.toString()
|
||
return n[1] ? n : '0' + n
|
||
}
|
||
|
||
// 获取当前时间
|
||
function formatTime(date) {
|
||
var year = date.getFullYear()
|
||
var month = date.getMonth() + 1
|
||
var day = date.getDate()
|
||
|
||
var hour = date.getHours()
|
||
var minute = date.getMinutes()
|
||
var second = date.getSeconds()
|
||
|
||
return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
|
||
}
|
||
|
||
module.exports = {
|
||
uploadImage: uploadImage,
|
||
isEmptyObject: isEmptyObject,
|
||
formatTime: formatTime
|
||
}
|