upload_photo_modify.js 5.6 KB

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