|
@@ -630,72 +630,99 @@ export default {
|
|
|
*/
|
|
|
uploadImageOne: function(opt, successCallback, errorCallback) {
|
|
|
let that = this;
|
|
|
- if (typeof opt === 'string') {
|
|
|
- let url = opt;
|
|
|
- opt = {};
|
|
|
- opt.url = url;
|
|
|
+ // #ifdef APP
|
|
|
+ let boolad = uni.getStorageSync("boolLoad") || false
|
|
|
+ // if (permision.isIOS) {
|
|
|
+ if (boolad) {
|
|
|
+ upimg()
|
|
|
+ } else {
|
|
|
+ let title = '申请相册权限';
|
|
|
+ let content = "是否允许访问相册用于上传图片?"
|
|
|
+ uni.showModal({
|
|
|
+ title,
|
|
|
+ content,
|
|
|
+ cancelText: '拒绝',
|
|
|
+ confirmText: '允许',
|
|
|
+ success: res => {
|
|
|
+ if (res.confirm) {
|
|
|
+ uni.setStorageSync("boolLoad", true)
|
|
|
+ upimg()
|
|
|
+ }
|
|
|
+ },
|
|
|
+ });
|
|
|
}
|
|
|
- let count = opt.count || 1,
|
|
|
- sizeType = opt.sizeType || ['compressed'],
|
|
|
- sourceType = opt.sourceType || ['album', 'camera'],
|
|
|
- is_load = opt.is_load || true,
|
|
|
- uploadUrl = opt.url || '',
|
|
|
- inputName = opt.name || 'pics',
|
|
|
- fileType = opt.fileType || 'image';
|
|
|
- uni.chooseImage({
|
|
|
- count: count, //最多可以选择的图片总数
|
|
|
- sizeType: sizeType, // 可以指定是原图还是压缩图,默认二者都有
|
|
|
- sourceType: sourceType, // 可以指定来源是相册还是相机,默认二者都有
|
|
|
- success: function(res) {
|
|
|
- //启动上传等待中...
|
|
|
- uni.showLoading({
|
|
|
- title: '图片上传中',
|
|
|
- });
|
|
|
- uni.uploadFile({
|
|
|
- url: HTTP_REQUEST_URL + '/api/' + uploadUrl,
|
|
|
- filePath: res.tempFilePaths[0],
|
|
|
- fileType: fileType,
|
|
|
- name: inputName,
|
|
|
- formData: {
|
|
|
- 'filename': inputName
|
|
|
- },
|
|
|
- header: {
|
|
|
- // #ifdef MP
|
|
|
- "Content-Type": "multipart/form-data",
|
|
|
- // #endif
|
|
|
- [TOKENNAME]: 'Bearer ' + store.state.app.token
|
|
|
- },
|
|
|
- success: function(res) {
|
|
|
- uni.hideLoading();
|
|
|
- if (res.statusCode == 403) {
|
|
|
- that.Tips({
|
|
|
- title: res.data
|
|
|
- });
|
|
|
- } else if (res.statusCode == 413) {
|
|
|
- that.Tips({
|
|
|
- title: '上传图片失败,请重新上传小尺寸图片'
|
|
|
- });
|
|
|
- } else {
|
|
|
- let data = res.data ? JSON.parse(res.data) : {};
|
|
|
- if (data.status == 200) {
|
|
|
- successCallback && successCallback(data)
|
|
|
- } else {
|
|
|
- errorCallback && errorCallback(data);
|
|
|
+ // #endif
|
|
|
+ // #ifndef APP
|
|
|
+ upimg()
|
|
|
+ // #endif
|
|
|
+ function upimg() {
|
|
|
+ if (typeof opt === 'string') {
|
|
|
+ let url = opt;
|
|
|
+ opt = {};
|
|
|
+ opt.url = url;
|
|
|
+ }
|
|
|
+ let count = opt.count || 1,
|
|
|
+ sizeType = opt.sizeType || ['compressed'],
|
|
|
+ sourceType = opt.sourceType || ['album', 'camera'],
|
|
|
+ is_load = opt.is_load || true,
|
|
|
+ uploadUrl = opt.url || '',
|
|
|
+ inputName = opt.name || 'pics',
|
|
|
+ fileType = opt.fileType || 'image';
|
|
|
+ uni.chooseImage({
|
|
|
+ count: count, //最多可以选择的图片总数
|
|
|
+ sizeType: sizeType, // 可以指定是原图还是压缩图,默认二者都有
|
|
|
+ sourceType: sourceType, // 可以指定来源是相册还是相机,默认二者都有
|
|
|
+ success: function(res) {
|
|
|
+ //启动上传等待中...
|
|
|
+ uni.showLoading({
|
|
|
+ title: '图片上传中',
|
|
|
+ });
|
|
|
+ uni.uploadFile({
|
|
|
+ url: HTTP_REQUEST_URL + '/api/' + uploadUrl,
|
|
|
+ filePath: res.tempFilePaths[0],
|
|
|
+ fileType: fileType,
|
|
|
+ name: inputName,
|
|
|
+ formData: {
|
|
|
+ 'filename': inputName
|
|
|
+ },
|
|
|
+ header: {
|
|
|
+ // #ifdef MP
|
|
|
+ "Content-Type": "multipart/form-data",
|
|
|
+ // #endif
|
|
|
+ [TOKENNAME]: 'Bearer ' + store.state.app.token
|
|
|
+ },
|
|
|
+ success: function(res) {
|
|
|
+ uni.hideLoading();
|
|
|
+ if (res.statusCode == 403) {
|
|
|
+ that.Tips({
|
|
|
+ title: res.data
|
|
|
+ });
|
|
|
+ } else if (res.statusCode == 413) {
|
|
|
that.Tips({
|
|
|
- title: data.msg
|
|
|
+ title: '上传图片失败,请重新上传小尺寸图片'
|
|
|
});
|
|
|
+ } else {
|
|
|
+ let data = res.data ? JSON.parse(res.data) : {};
|
|
|
+ if (data.status == 200) {
|
|
|
+ successCallback && successCallback(data)
|
|
|
+ } else {
|
|
|
+ errorCallback && errorCallback(data);
|
|
|
+ that.Tips({
|
|
|
+ title: data.msg
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
+ },
|
|
|
+ fail: function(res) {
|
|
|
+ uni.hideLoading();
|
|
|
+ that.Tips({
|
|
|
+ title: '上传图片失败'
|
|
|
+ });
|
|
|
}
|
|
|
- },
|
|
|
- fail: function(res) {
|
|
|
- uni.hideLoading();
|
|
|
- that.Tips({
|
|
|
- title: '上传图片失败'
|
|
|
- });
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
- })
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
},
|
|
|
/*
|
|
|
* 单图上传压缩版
|
|
@@ -705,81 +732,110 @@ export default {
|
|
|
*/
|
|
|
uploadImageChange: function(opt, successCallback, errorCallback, sizeCallback) {
|
|
|
let that = this;
|
|
|
- if (typeof opt === 'string') {
|
|
|
- let url = opt;
|
|
|
- opt = {};
|
|
|
- opt.url = url;
|
|
|
+ // #ifdef APP
|
|
|
+ let boolad = uni.getStorageSync("boolLoad") || false
|
|
|
+ // if (permision.isIOS) {
|
|
|
+ if (boolad || plus.os.name == "iOS") {
|
|
|
+ upimg()
|
|
|
+ } else {
|
|
|
+ let title = '申请相册权限';
|
|
|
+ let content = "是否允许访问相册用于上传图片?"
|
|
|
+ uni.showModal({
|
|
|
+ title,
|
|
|
+ content,
|
|
|
+ cancelText: '拒绝',
|
|
|
+ confirmText: '允许',
|
|
|
+ success: res => {
|
|
|
+ if (res.confirm) {
|
|
|
+ uni.setStorageSync("boolLoad", true)
|
|
|
+ upimg()
|
|
|
+ }
|
|
|
+ },
|
|
|
+ });
|
|
|
}
|
|
|
- let count = opt.count || 1,
|
|
|
- sizeType = opt.sizeType || ['compressed'],
|
|
|
- sourceType = opt.sourceType || ['album', 'camera'],
|
|
|
- is_load = opt.is_load || true,
|
|
|
- uploadUrl = opt.url || '',
|
|
|
- inputName = opt.name || 'pics',
|
|
|
- fileType = opt.fileType || 'image';
|
|
|
- uni.chooseImage({
|
|
|
- count: count, //最多可以选择的图片总数
|
|
|
- sizeType: sizeType, // 可以指定是原图还是压缩图,默认二者都有
|
|
|
- sourceType: sourceType, // 可以指定来源是相册还是相机,默认二者都有
|
|
|
- success: function(res) {
|
|
|
- //启动上传等待中...
|
|
|
- let imgSrc
|
|
|
- let objImg = res.tempFilePaths;
|
|
|
- objImg.forEach(item => {
|
|
|
- uni.getImageInfo({
|
|
|
- src: item,
|
|
|
- success(ress) {
|
|
|
- uni.showLoading({
|
|
|
- title: '图片上传中',
|
|
|
- });
|
|
|
- if (res.tempFiles[0].size <= 2097152) {
|
|
|
- uploadImg(ress.path)
|
|
|
- return
|
|
|
- }
|
|
|
- // uploadImg(canvasPath.tempFilePath)
|
|
|
- let canvasWidth, canvasHeight, xs, maxWidth = 750
|
|
|
- xs = ress.width / ress.height // 宽高比例
|
|
|
- if (ress.width > maxWidth) {
|
|
|
- canvasWidth = maxWidth // 这里是最大限制宽度
|
|
|
- canvasHeight = maxWidth / xs
|
|
|
- } else {
|
|
|
- canvasWidth = ress.width
|
|
|
- canvasHeight = ress.height
|
|
|
- }
|
|
|
- sizeCallback && sizeCallback({
|
|
|
- w: canvasWidth,
|
|
|
- h: canvasHeight
|
|
|
- })
|
|
|
- let canvas = uni.createCanvasContext('canvas');
|
|
|
- canvas.width = canvasWidth
|
|
|
- canvas.height = canvasHeight
|
|
|
- canvas.clearRect(0, 0, canvasWidth, canvasHeight);
|
|
|
- canvas.drawImage(ress.path, 0, 0, canvasWidth, canvasHeight)
|
|
|
- canvas.save();
|
|
|
- // 这里的画布drawImage是一种异步属性 可能存在未绘制全就执行了draw的问题 so添加延迟
|
|
|
- setTimeout(e => {
|
|
|
- canvas.draw(true, () => {
|
|
|
- uni.canvasToTempFilePath({
|
|
|
- canvasId: 'canvas',
|
|
|
- fileType: 'JPEG',
|
|
|
- destWidth: canvasWidth,
|
|
|
- destHeight: canvasHeight,
|
|
|
- quality: 0.7,
|
|
|
- success: function(
|
|
|
- canvasPath) {
|
|
|
- uploadImg(
|
|
|
+ // #endif
|
|
|
+ // #ifndef APP
|
|
|
+ upimg()
|
|
|
+ // #endif
|
|
|
+ function upimg() {
|
|
|
+ if (typeof opt === 'string') {
|
|
|
+ let url = opt;
|
|
|
+ opt = {};
|
|
|
+ opt.url = url;
|
|
|
+ }
|
|
|
+ let count = opt.count || 1,
|
|
|
+ sizeType = opt.sizeType || ['compressed'],
|
|
|
+ sourceType = opt.sourceType || ['album', 'camera'],
|
|
|
+ is_load = opt.is_load || true,
|
|
|
+ uploadUrl = opt.url || '',
|
|
|
+ inputName = opt.name || 'pics',
|
|
|
+ fileType = opt.fileType || 'image';
|
|
|
+ uni.chooseImage({
|
|
|
+ count: count, //最多可以选择的图片总数
|
|
|
+ sizeType: sizeType, // 可以指定是原图还是压缩图,默认二者都有
|
|
|
+ sourceType: sourceType, // 可以指定来源是相册还是相机,默认二者都有
|
|
|
+ success: function(res) {
|
|
|
+ //启动上传等待中...
|
|
|
+ let imgSrc
|
|
|
+ let objImg = res.tempFilePaths;
|
|
|
+ objImg.forEach(item => {
|
|
|
+ uni.getImageInfo({
|
|
|
+ src: item,
|
|
|
+ success(ress) {
|
|
|
+ uni.showLoading({
|
|
|
+ title: '图片上传中',
|
|
|
+ });
|
|
|
+ if (res.tempFiles[0].size <= 2097152) {
|
|
|
+ uploadImg(ress.path)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // uploadImg(canvasPath.tempFilePath)
|
|
|
+ let canvasWidth, canvasHeight, xs, maxWidth = 750
|
|
|
+ xs = ress.width / ress.height // 宽高比例
|
|
|
+ if (ress.width > maxWidth) {
|
|
|
+ canvasWidth = maxWidth // 这里是最大限制宽度
|
|
|
+ canvasHeight = maxWidth / xs
|
|
|
+ } else {
|
|
|
+ canvasWidth = ress.width
|
|
|
+ canvasHeight = ress.height
|
|
|
+ }
|
|
|
+ sizeCallback && sizeCallback({
|
|
|
+ w: canvasWidth,
|
|
|
+ h: canvasHeight
|
|
|
+ })
|
|
|
+ let canvas = uni.createCanvasContext('canvas');
|
|
|
+ canvas.width = canvasWidth
|
|
|
+ canvas.height = canvasHeight
|
|
|
+ canvas.clearRect(0, 0, canvasWidth, canvasHeight);
|
|
|
+ canvas.drawImage(ress.path, 0, 0, canvasWidth,
|
|
|
+ canvasHeight)
|
|
|
+ canvas.save();
|
|
|
+ // 这里的画布drawImage是一种异步属性 可能存在未绘制全就执行了draw的问题 so添加延迟
|
|
|
+ setTimeout(e => {
|
|
|
+ canvas.draw(true, () => {
|
|
|
+ uni.canvasToTempFilePath({
|
|
|
+ canvasId: 'canvas',
|
|
|
+ fileType: 'JPEG',
|
|
|
+ destWidth: canvasWidth,
|
|
|
+ destHeight: canvasHeight,
|
|
|
+ quality: 0.7,
|
|
|
+ success: function(
|
|
|
canvasPath
|
|
|
- .tempFilePath
|
|
|
+ ) {
|
|
|
+ uploadImg(
|
|
|
+ canvasPath
|
|
|
+ .tempFilePath
|
|
|
)
|
|
|
- }
|
|
|
- })
|
|
|
- });
|
|
|
- }, 200)
|
|
|
- }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ });
|
|
|
+ }, 200)
|
|
|
+ }
|
|
|
+ })
|
|
|
})
|
|
|
- })
|
|
|
- }
|
|
|
- })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
|
|
|
function uploadImg(filePath) {
|
|
|
uni.uploadFile({
|