async cameraPickerControl() { let pathDir = getContext().filesDir let fileName = `${new Date().getTime()}` let filePath = pathDir + `/${fileName}.tmp` fileIo.createRandomAccessFileSync(filePath, fileIo.OpenMode.CREATE); let uri = fileUri.getUriFromPath(filePath); let pickerProfile: picker.PickerProfile = { cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK, saveUri: uri }; let result: picker.PickerResult = await picker.pick(getContext(), [picker.PickerMediaType.PHOTO], pickerProfile); emitter.emit('unlockEvent') //调用拍照会触发前台事件,需要在这里解锁 if (result.resultUri == '') { return } if (this.ref === 1) { this.imgSrc1 = uri this.imgLink1 = await pict_upload(fileName, 1) this.imgSrc1 = HttpConstant.TestApi + 'admin/file/' + this.imgLink1 } else if (this.ref === 2) { this.imgSrc2 = uri this.imgLink2 = await pict_upload(fileName, 1) this.imgSrc2 = HttpConstant.TestApi + 'admin/file/' + this.imgLink2 } else if (this.ref === 3) { this.imgSrc3 = uri this.imgLink3 = await pict_upload(fileName, 1) this.imgSrc3 = HttpConstant.TestApi + 'admin/file/' + this.imgLink3 } }
export function pict_upload(name: string, type: 0 | 1) { let pathDir = ''; type == 0 ? pathDir = context.cacheDir : pathDir = context.filesDir let filePath = pathDir + `/${name}${type == 0 ? '' : '.tmp'}` console.log(`filePath===>${filePath}`) let formData = new FormData() try { let file2 = fs.openSync(filePath, 0o2); let stat = fs.lstatSync(filePath); let buf2 = new ArrayBuffer(stat.size); fs.readSync(file2.fd, buf2); fs.fsyncSync(file2.fd); fs.closeSync(file2.fd); formData.append('file', buf2); } catch (err) { console.info('err:' + JSON.stringify(err)); } return new Promise<string>((resolve, reject) => { axios.post<string, AxiosResponse<string>, FormData>(HttpConstant.TestApi+FileApi.FILE_UPLOAD, formData, { headers: { 'Content-Type': 'multipart/form-data', } }).then((res: AxiosResponse) => { resolve(res.data.data.pictureName) console.info("===>result" + JSON.stringify(res)); }).catch((error: AxiosError) => { reject('发生错误') console.error("error:" + JSON.stringify(error)); }) }) }