(function (exports) { // 弹出选择照片方式 function showActionSheet() { return new Promise(function (resolve, reject) { plus.nativeUI.actionSheet({ cancel: "取消", buttons: [{ title: "从相册选择" }, { title: "拍照" }] }, function (event) { if (event.index == 1) { getGallery(resolve, reject); } else if (event.index == 2) { getCamera(resolve, reject); } } ); }) }; //获取相册 function getGallery(resolve, reject) { plus.gallery.pick(function (path) { //scb:SuccessCallBack plus.io.resolveLocalFileSystemURL(path, function(entry) { var fileUrl = entry.toLocalURL(); resolve({file:entry,fileUrl:fileUrl}) }, function(e) { reject() plus.nativeUI.toast("读取拍照文件错误:" + e.message); }); }, function (error) { //ecb:ErrorCallBack if (error.code == 11 && error.message == "null") { plus.nativeUI.toast("您尚未授权拍照权限,无法使用拍照功能。"); reject() } }); } // 调用系统摄像头 function getCamera(resolve, reject) { var cmr = plus.camera.getCamera(); cmr.captureImage(function (path) { plus.io.resolveLocalFileSystemURL(path, function (entry) { var fileUrl = entry.toLocalURL(); resolve({file:entry,fileUrl:fileUrl}) }); }, function (error) { if (error.code == 11 && error.message == "null") { plus.nativeUI.toast("您尚未授权拍照权限,无法使用拍照功能。"); reject() } }, { filename: "_doc/camera/", index: 1 //ios指定主摄像头 }); } //上传图片 function UploadImg(url, params) { return new Promise(function (resolve, reject) { var task = plus.uploader.createUpload(httpRequest.server + url, { method: "POST", blocksize: 204800, priority: 100 }, function (t, status) { // 上传完成 if (status == 200) { resolve(t); } else { reject(t) } } ); task.addFile(params.file, { key: "file" }); var oauthInfo = JSON.parse(plus.storage.getItem("oauthInfo")); task.addData("creator", params.creator); task.addData("objectId", params.objectId); task.addData("accessToken ", oauthInfo.accessToken); task.start(); }) } cameraChoose = { showActionSheet: showActionSheet, UploadImg: UploadImg } exports.cameraChoose = cameraChoose })(window)