upload_photo_single.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. var fileUrl;
  2. var fileHttpUrl;
  3. //上传图片
  4. var isModify = false;
  5. var exper, intro, mobile;
  6. var isOK = false;
  7. var mobileModify = false;
  8. function uploadSingleImg(expertise, introduce, mobileVal) {
  9. plus.nativeUI.showWaiting();
  10. exper = expertise;
  11. intro = introduce;
  12. mobile = mobileVal;
  13. if (isModify) {
  14. uploader();
  15. } else {
  16. var param = {
  17. photo: "",
  18. sex:"",
  19. province:"",
  20. city:"",
  21. expertise: exper,
  22. introduce: intro,
  23. mobile: mobile
  24. }
  25. sendPost("doctor/save", param, function() {
  26. }, handSendSucc1);
  27. }
  28. }
  29. function uploader() {
  30. var uploadServerUrl = server + "upload/image";
  31. var task = plus.uploader.createUpload(uploadServerUrl, {
  32. method: "post"
  33. }, function(t, status) {
  34. if (status == 200) {
  35. fileHttpUrl = JSON.parse(t.responseText).urls;
  36. saveInfo();
  37. } else {
  38. mui.toast("头像修改失败,请稍后重试");
  39. plus.nativeUI.closeWaiting();
  40. }
  41. });
  42. task.addFile(fileUrl, {})
  43. task.start();
  44. }
  45. function saveInfo() {
  46. var param = {
  47. photo: fileHttpUrl,
  48. expertise: exper,
  49. introduce: intro,
  50. mobile: mobile
  51. }
  52. sendPost("doctor/save", param, function() {
  53. }, handSendSucc);
  54. }
  55. function handSendSucc(res) {
  56. if (res.status == 200) {
  57. if(mobileModify){
  58. logout();
  59. return;
  60. }
  61. plus.nativeUI.closeWaiting();
  62. // mui.toast("资料修改成功");
  63. flushDocPhoto(res.data);//刷新头像
  64. // var wv = plus.webview.getWebviewById("mine.html");
  65. // if (wv) {
  66. // mui.fire(wv, 'updataPhoto', {
  67. // photo: res.data
  68. // });
  69. // }
  70. }
  71. mui.toast(res.msg);
  72. plus.nativeUI.closeWaiting();
  73. isModify=false;
  74. }
  75. function handSendSucc1(res) {
  76. if (res.status == 200) {
  77. if(mobileModify){
  78. logout();
  79. return;
  80. }
  81. }
  82. mui.toast(res.msg);
  83. plus.nativeUI.closeWaiting();
  84. }
  85. // 弹出选择照片方式
  86. function showActionSheet(imgDom, imgFlag) {
  87. plus.nativeUI.actionSheet({
  88. cancel: "取消",
  89. buttons: [{
  90. title: "从相册选择"
  91. }, {
  92. title: "拍照"
  93. }]
  94. },
  95. function(event) {
  96. if (event.index == 1) {
  97. getGallery(imgDom, imgFlag);
  98. } else if (event.index == 2) {
  99. getCamera(imgDom, imgFlag);
  100. }
  101. }
  102. );
  103. };
  104. //获取相册
  105. function getGallery(imgDom, imgFlag) {
  106. plus.gallery.pick(function(cbFile) { //scb:SuccessCallBack
  107. imgDom.src = cbFile;
  108. handlePickSucc(cbFile);
  109. }, function(ecb) { //ecb:ErrorCallBack
  110. }, {
  111. filename: "_doc/gallery/",
  112. filter: "image"
  113. });
  114. }
  115. function handlePickSucc(srcUrl) {
  116. var lastIdx = srcUrl.lastIndexOf("/"),
  117. imgName = srcUrl;
  118. if(lastIdx>-1) {
  119. imgName = srcUrl.slice(lastIdx+1);
  120. }
  121. plus.zip.compressImage({
  122. src: srcUrl,
  123. dst: "_doc/compressImg/"+imgName,
  124. quality: 20,
  125. overwrite: true
  126. }, function(succ) {
  127. var urlSucc = succ.target;
  128. // var size = succ.size;
  129. // var width = succ.width;
  130. // var height = succ.height;
  131. fileUrl = urlSucc;
  132. isModify = true; //修改标记
  133. }, function(err) {
  134. mui.toast("压缩失败: " + err.message);
  135. });
  136. }
  137. // 调用系统摄像头
  138. function getCamera(imgDom, imgFlag) {
  139. // var pp = plus.navigator.checkPermission("CAMERA");
  140. // if(pp == "denied") {
  141. // mui.toast("拍照权限未开启")
  142. // return ;
  143. // }
  144. var cmr = plus.camera.getCamera();
  145. cmr.captureImage(function(path) {
  146. console.log(path);
  147. /**
  148. * 拍照成功后,图片本保存在本地,这时候我们需要调用本地文件
  149. * http://www.html5plus.org/doc/zh_cn/io.html#plus.io.resolveLocalFileSystemURL
  150. */
  151. plus.io.resolveLocalFileSystemURL(path, function(entry) {
  152. /*
  153. * 将获取目录路径转换为本地路径URL地址
  154. * http://www.html5plus.org/doc/zh_cn/io.html#plus.io.DirectoryEntry.toLocalURL
  155. */
  156. imgDom.src = entry.toLocalURL();
  157. fileUrl = entry.toLocalURL();
  158. console.log(fileUrl);
  159. handlePickSucc(fileUrl);
  160. });
  161. }, function(error) {
  162. if(error.code==11 && error.message == "null") {
  163. plus.nativeUI.toast("您尚未授权拍照权限,无法使用拍照功能。");
  164. }
  165. // mui.toast(error.message)
  166. }, {
  167. filename: "_doc/camera/",
  168. index: 1 //ios指定主摄像头
  169. });
  170. }
  171. //登出
  172. function logout() {
  173. plus.storage.removeItem("userMobile");
  174. plus.storage.removeItem("flag");
  175. var userAgent = plus.storage.getItem("userAgent");
  176. imClient.Users.logout(userAgent.uid);
  177. plus.storage.removeItem("userAgent");
  178. plus.storage.removeItem("docInfo");
  179. plus.storage.removeItem("docType");
  180. alert("您已修改手机号码,请重新登录!");
  181. mui.openWindow('../../login/html/login.html', 'login', {
  182. extras: {
  183. exit: "exit"
  184. },
  185. createNew: true,
  186. styles: {
  187. softinputMode: "adjustResize"
  188. }
  189. });
  190. mui.later(function() {
  191. plus.webview.currentWebview().close();
  192. }, 1000)
  193. }