/** * 拍照,从相册选择图片 * */ import httpUtils from './httpUtils.js'; import toast from './toast.js'; const ImageUploadUtils = { show(callback){ this.callback = callback; uni.showActionSheet({ itemList: ['拍照', '从相册选择'], success: (res) => { if(res.tapIndex == 0){ this.takingPicture() }else if(res.tapIndex == 1){ this.pickPicture() } } }); }, // 拍照 takingPicture(){ uni.chooseImage({ count: 1, sourceType: ['camera'], success: (data) => { this.upload(data.tempFilePaths[0]) // this.pathToBase64(data.tempFilePaths[0]) } }) }, // 从相册选择 pickPicture(){ uni.chooseImage({ count: 1, sourceType: ['album'], success: (data) => { this.upload(data.tempFilePaths[0]) // this.pathToBase64(data.tempFilePaths[0]) } }) }, pathToBase64(path){ //#ifdef APP-PLUS plus.io.resolveLocalFileSystemURL(path, (entry) => { entry.file((file) => { var fileReader = new plus.io.FileReader(); fileReader.readAsDataURL( file ); fileReader.onloadend = (evt) => { this.upload(evt.target.result) } }) }) //#endif }, upload(file){ // httpUtils.request('/api/common/uploadPhotoBase64', { // base64: file // }, 'POST').then((res) => { // console.log(res) // if(res.status == 200){ // this.callback && this.callback(res.mapInfo.file) // }else{ // toast.info(res.info) // } // }) uni.uploadFile({ url: httpUtils.baseUrl+'/api/common/uploadImg', filePath: file, name: 'file', success: (res) => { let result = JSON.parse(res.data); if(result.status == 200){ this.callback && this.callback(result.mapInfo.file) }else{ toast.info(result.info) } } }) } } export default ImageUploadUtils