import { pathToBase64, base64ToPath } from '../../utils/imageTools.js';
// 调用相机
toCamera(){
let that=this;
uni.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['camera'], //这要注意,camera调拍照,album是打开手机相册
success: (res)=> {
console.log(res);
const tempFilePaths = res.tempFilePaths[0];
pathToBase64(tempFilePaths)
.then(base64 => {
// console.log("转换成功==>",base64)
that.imgList = base64;
that.upLoad(that.imgList);
})
.catch(error => {
console.error("转换失败==>",error)
})
},
fail: (err) => {
console.log("imgchoose",err);
uni.navigateTo({
url:"../productionProcess/productionProcess"
})
}
});
},
pathToBase64(tempFilePaths)
.then(base64 => {
// console.log("转换成功==>",base64)
that.imgList = base64;
that.upLoad(that.imgList);
})
.catch(error => {
console.error("转换失败==>",error)
})
// 上传图片
async upLoad(filePath){
// console.log(this.$baseUrl)
let uniIdToken=uni.getStorageSync('uniIdToken');
const upLoadRes = await this.$myRequest({
url: this.$baseUrl + '/api/v1/Systen.Core/FileSystem/UpdadBase64ToImage',
header: {
'content-type': 'application/x-www-form-urlencoded',
'Authorization':uniIdToken
},
method:'POST',
data:{
Base64ImageData:filePath
}
})
// console.log(upLoadRes)
if(upLoadRes.statusCode == 200){
// 保存用户信息
this.pic=this.$baseUrl +upLoadRes.data.Result.ImagePath;
this.imgPath=upLoadRes.data.Result.ImagePath;
console.log("上传图片",upLoadRes)
}else {
// 打印错误日志
console.log("上传图片错误",upLoadRes)
}
}
/* 处理文件路径 */
function getLocalFilePath(path) {
if (path.indexOf('_www') === 0 || path.indexOf('_doc') === 0 || path.indexOf('_documents') === 0 || path.indexOf('_downloads') === 0) {
return path
}
if (path.indexOf('file://') === 0) {
return path
}
if (path.indexOf('/storage/emulated/0/') === 0) {
return path
}
if (path.indexOf('/') === 0) {
var localFilePath = plus.io.convertAbsoluteFileSystem(path)
if (localFilePath !== path) {
return localFilePath
} else {
path = path.substr(1)
}
}
return '_www/' + path
}
/* // 图片转base64ToPath */
export function pathToBase64(path) {
return new Promise(function(resolve, reject) {
if (typeof window === 'object' && 'document' in window) {
if (typeof FileReader === 'function') {
var xhr = new XMLHttpRequest()
xhr.open('GET', path, true)
xhr.responseType = 'blob'
xhr.onload = function() {
if (this.status === 200) {
let fileReader = new FileReader()
fileReader.onload = function(e) {
resolve(e.target.result)
}
fileReader.onerror = reject
fileReader.readAsDataURL(this.response)
}
}
xhr.onerror = reject
xhr.send()
return
}
var canvas = document.createElement('canvas')
var c2x = canvas.getContext('2d')
var img = new Image
img.onload = function() {
canvas.width = img.width
canvas.height = img.height
c2x.drawImage(img, 0, 0)
resolve(canvas.toDataURL())
canvas.height = canvas.width = 0
}
img.onerror = reject
img.src = path
return
}
if (typeof plus === 'object') {
plus.io.resolveLocalFileSystemURL(getLocalFilePath(path), function(entry) {
entry.file(function(file) {
var fileReader = new plus.io.FileReader()
fileReader.onload = function(data) {
resolve(data.target.result)
}
fileReader.onerror = function(error) {
reject(error)
}
fileReader.readAsDataURL(file)
}, function(error) {
reject(error)
})
}, function(error) {
reject(error)
})
return
}
if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
wx.getFileSystemManager().readFile({
filePath: path,
encoding: 'base64',
success: function(res) {
resolve('data:image/png;base64,' + res.data)
},
fail: function(error) {
reject(error)
}
})
return
}
reject(new Error('not support'))
})
}
/* base64转文件路径 */
export function base64ToPath(base64) {
return new Promise(function(resolve, reject) {
if (typeof window === 'object' && 'document' in window) {
base64 = base64.split(',')
var type = base64[0].match(/:(.*?);/)[1]
var str = atob(base64[1])
var n = str.length
var array = new Uint8Array(n)
while (n--) {
array[n] = str.charCodeAt(n)
}
return resolve((window.URL || window.webkitURL).createObjectURL(new Blob([array], { type: type })))
}
var extName = base64.match(/data\:\S+\/(\S+);/)
if (extName) {
extName = extName[1]
} else {
reject(new Error('base64 error'))
}
var fileName = Date.now() + '.' + extName
if (typeof plus === 'object') {
var bitmap = new plus.nativeObj.Bitmap('bitmap' + Date.now())
bitmap.loadBase64Data(base64, function() {
var filePath = '_doc/uniapp_temp/' + fileName
bitmap.save(filePath, {}, function() {
bitmap.clear()
resolve(filePath)
}, function(error) {
bitmap.clear()
reject(error)
})
}, function(error) {
bitmap.clear()
reject(error)
})
return
}
if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
var filePath = wx.env.USER_DATA_PATH + '/' + fileName
wx.getFileSystemManager().writeFile({
filePath: filePath,
data: base64.replace(/^data:\S+\/\S+;base64,/, ''),
encoding: 'base64',
success: function() {
resolve(filePath)
},
fail: function(error) {
reject(error)
}
})
return
}
reject(new Error('not support'))
})
}