|
|
import request from '../request.js'
|
|
|
import common from "../common.js"
|
|
|
import file from "./file.js"
|
|
|
|
|
|
// #ifdef H5
|
|
|
let jweixin = require('jweixin-module')
|
|
|
// #endif
|
|
|
|
|
|
/* 2022.08.11 兼容了微信公众号
|
|
|
2022.09.21 兼容了微信小程序 */
|
|
|
|
|
|
let image = {}
|
|
|
|
|
|
/**
|
|
|
* 选择图片并上传
|
|
|
* @param {number} count 选择图片数量
|
|
|
* @param {array} sourceType album 从相册选图,camera 使用相机,默认二者都有。如需直接开相机或直接选相册,请只使用一个选项
|
|
|
* @param {array} sizeType original 原图,compressed 压缩图,默认二者都有
|
|
|
* @param {boolean} isOriginalName false--随机文件名 true--原文件名
|
|
|
* @date 2022-12-09
|
|
|
*/
|
|
|
image.chooseImageUpload = function(count, sourceType = ['album', 'camera'], sizeType = ['original', 'compressed'],
|
|
|
isOriginalName = false) {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
/* 公众号平台
|
|
|
采用JSSDK方式实现,比uni.chooseImage体验好 */
|
|
|
if (common.userAgent() == 'weixin') {
|
|
|
// 选择图片
|
|
|
jweixin.chooseImage({
|
|
|
count: count, // 默认9
|
|
|
sizeType: sizeType, // 可以指定是原图还是压缩图,默认二者都有
|
|
|
sourceType: sourceType, // 可以指定来源是相册还是相机,默认二者都有
|
|
|
success: (res) => {
|
|
|
// 返回选定照片的本地 ID 列表,localId可以作为 img 标签的 src 属性显示图片
|
|
|
let localIds = res.localIds
|
|
|
|
|
|
// 微信上传图片
|
|
|
this.wxUploadImage(localIds).then(res => {
|
|
|
resolve(res)
|
|
|
}).catch(res => {
|
|
|
reject(res)
|
|
|
})
|
|
|
},
|
|
|
})
|
|
|
/* 非公众号平台
|
|
|
采用uni-app的选择图片方式实现 */
|
|
|
} else {
|
|
|
uni.chooseImage({
|
|
|
count: count,
|
|
|
sizeType: sizeType,
|
|
|
sourceType: sourceType,
|
|
|
success: (res) => {
|
|
|
/* res返回示例
|
|
|
微信小程序客户端:
|
|
|
{
|
|
|
errMsg: "chooseImage:ok",
|
|
|
failedCount: 0,
|
|
|
// 图片的本地文件路径列表
|
|
|
tempFilePaths:['http://tmp/zeRKON5CMOCH92a913d1a7eed9197d97beb8d52d3e67.png'],
|
|
|
// 图片的本地文件列表,每一项是一个 File 对象 path--本地文件路径 size--本地文件大小,单位:B
|
|
|
tempFiles:[{path: "http://tmp/zeRKON5CMOCH92a913d1a7eed9197d97beb8d52d3e67.png", size: 15687}]
|
|
|
}
|
|
|
|
|
|
H5端:
|
|
|
{
|
|
|
errMsg: "chooseImage:ok",
|
|
|
// 图片的本地文件路径列表
|
|
|
tempFilePaths:['blob:http://localhost:8080/f5474a64-ebe4-4b50-84b4-8253e4e80eb2'],
|
|
|
// 图片的本地文件列表,每一项是一个 File 对象 path--本地文件路径 size--本地文件大小,单位:B
|
|
|
tempFiles:[{path: "blob:http://localhost:8080/f5474a64-ebe4-4b50-84b4-8253e4e80eb2", size: 75833,
|
|
|
lastModified: 1640587715774, name: "微信图片1.jpg", type: "image/jpeg"}]
|
|
|
}
|
|
|
*/
|
|
|
|
|
|
console.log('chooseImageSuccess', res)
|
|
|
uni.showLoading({
|
|
|
mask: true,
|
|
|
title: '正在上传'
|
|
|
})
|
|
|
|
|
|
let tempFiles = res.tempFiles
|
|
|
// 待上传的文件列表
|
|
|
let files = []
|
|
|
|
|
|
tempFiles.forEach(value => {
|
|
|
// 原文件名称
|
|
|
let localFileName,
|
|
|
// 上传后文件名称
|
|
|
fileName
|
|
|
|
|
|
// 获取源文件名称
|
|
|
// #ifdef H5
|
|
|
localFileName = value.name
|
|
|
// #endif
|
|
|
// #ifdef APP-PLUS|MP-WEIXIN|MP-ALIPAY
|
|
|
let pos = value.path.lastIndexOf('/')
|
|
|
if (pos != -1) {
|
|
|
localFileName = value.path.substring(pos + 1)
|
|
|
}
|
|
|
// #endif
|
|
|
|
|
|
// 生成上传后文件名称
|
|
|
if (isOriginalName) {
|
|
|
// 原文件名
|
|
|
fileName = localFileName
|
|
|
} else {
|
|
|
// 随机文件名
|
|
|
let suffix = common.getSuffix(localFileName)
|
|
|
fileName = common.getRandomStr(10) + suffix
|
|
|
}
|
|
|
|
|
|
|
|
|
let file = {
|
|
|
// 图片的本地文件路径列表
|
|
|
path: value.path,
|
|
|
// 上传后的文件名
|
|
|
fileName: fileName
|
|
|
}
|
|
|
files.push(file)
|
|
|
})
|
|
|
|
|
|
// 上传到OSS
|
|
|
file.uploadFilesToOss(files, [], {}, isOriginalName, 'image').then(res => {
|
|
|
uni.hideLoading()
|
|
|
resolve(res)
|
|
|
}).catch(res => {
|
|
|
uni.hideLoading()
|
|
|
reject(res)
|
|
|
})
|
|
|
|
|
|
// 上传到COS
|
|
|
// file.uploadFilesToCos(files, [], {}, isOriginalName, 'image').then(res => {
|
|
|
// uni.hideLoading()
|
|
|
// resolve(res)
|
|
|
// }).catch(res => {
|
|
|
// uni.hideLoading()
|
|
|
// reject(res)
|
|
|
// })
|
|
|
},
|
|
|
fail: (err) => {
|
|
|
console.log('chooseImageFail', err)
|
|
|
reject('')
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 图片预览
|
|
|
* @date 2022-08-11
|
|
|
*/
|
|
|
image.previewImage = function(imageUrls = [], index = 0) {
|
|
|
// 公众号平台
|
|
|
if (common.userAgent() == 'weixin') {
|
|
|
jweixin.previewImage({
|
|
|
// 当前显示图片的 http 链接
|
|
|
current: imageUrls[index],
|
|
|
// 需要预览的图片 http 链接列表
|
|
|
urls: imageUrls
|
|
|
})
|
|
|
// 其他平台
|
|
|
} else {
|
|
|
uni.previewImage({
|
|
|
current: index,
|
|
|
urls: imageUrls
|
|
|
})
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// ****************************************以下方法为封装方法,一般不被页面直接调用******************************************
|
|
|
/**
|
|
|
* 微信上传图片,返回服务端ID
|
|
|
* @param {array} localIds 选定照片的localId列表
|
|
|
* @param {array} imageArray 已经上传成功的图片云端路径
|
|
|
* @param {boolean} isOriginalName false--随机文件名 true--原文件名
|
|
|
* @date 2022-08-11
|
|
|
*/
|
|
|
image.wxUploadImage = function(localIds, imageArray = [], isOriginalName = false) {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
if (localIds.length > 0) {
|
|
|
// 取出队列第一个元素
|
|
|
let localId = localIds.shift()
|
|
|
|
|
|
// 上传图片
|
|
|
jweixin.uploadImage({
|
|
|
// 需要上传的图片的本地ID,由 chooseImage 接口获得
|
|
|
localId: localId,
|
|
|
// 默认为1,显示进度提示
|
|
|
isShowProgressTips: 1,
|
|
|
success: (res) => {
|
|
|
// 返回图片的服务器端ID
|
|
|
let serverId = res.serverId
|
|
|
|
|
|
// 将前台图片文件的MediaId(即serverId)文件下载到本地并上传至OSS
|
|
|
request.getData('base/wechat/api/Media/getImageMediaToOss', {
|
|
|
media_id: serverId,
|
|
|
is_original_name: isOriginalName ? 1 : 0
|
|
|
}).then(res => {
|
|
|
imageArray.push(res.data.oss_url)
|
|
|
this.wxUploadImage(localIds, imageArray, isOriginalName)
|
|
|
.then(res => {
|
|
|
resolve(res)
|
|
|
}).catch(res => {
|
|
|
reject(res)
|
|
|
})
|
|
|
}).catch(res => {
|
|
|
console.log(res)
|
|
|
reject(res)
|
|
|
})
|
|
|
},
|
|
|
fail: (res) => {
|
|
|
reject(res)
|
|
|
}
|
|
|
});
|
|
|
} else {
|
|
|
resolve(imageArray)
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
|
|
|
export default image
|