/** * 拍照,从相册选择图片 * */ 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.pathToBase64(data.tempFilePaths[0]) } }) }, // 从相册选择 pickPicture(){ uni.chooseImage({ count: 1, sourceType: ['album'], success: (data) => { 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: 'data:image/jpeg;base64,'+ file }, 'POST').then((res) => { if(res.status == 200){ this.callback && this.callback(res.mapInfo.file) }else{ toast.info(res.info) } }) } } export default ImageUploadUtils