upload_preview.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //切换发布版本 true:正式版,连接生产环境;false:测试版,连接测试环境。
  2. var upload_publish_version = true;
  3. //var upload_publish_version = false;
  4. var isdemo = false;
  5. //var isdemo = false;
  6. var server = "";
  7. if(upload_publish_version == true) {
  8. //正式服务器
  9. server = "http://www.xmtyw.cn/wlyy/";
  10. }
  11. else if(isdemo == true) {
  12. //demo 服务器
  13. server = "http://ehr.yihu.com/ssgg/";
  14. }
  15. else {
  16. // alert("注意:这是测试版!");
  17. //测试服务器
  18. // server = "http://weixin.xmtyw.cn/wlyy/";
  19. //172.19.103.85是公司内部服务器的地址
  20. //27.154.56.142是公司内部服务器的地址的外网映射
  21. // server = "http://172.19.103.87:9090/wlyy";
  22. server = "http://120.41.253.95:9660/wlyy/";
  23. // server = "http://27.154.56.142:9090/wlyy";
  24. }
  25. //上传图片
  26. function uploadImg(uploadImgUrl, fuUploadSucc) {
  27. var task = plus.uploader.createUpload(server + "upload/image", {
  28. method: "POST",
  29. timeout: 20000
  30. }, function(uploadObj, status) {
  31. // console.log(uploadObj.responseText);
  32. if(status == 200) {
  33. // console.log("上传成功 " + uploadObj.url);
  34. // clearImg();
  35. fuUploadSucc(uploadObj);
  36. // var resText=uploadObj.responseText;
  37. // var oRes=JSON.parse(resText);
  38. // var imgNames=oRes.images;
  39. // var imgUrls=oRes.urls;
  40. // sendPost(server+"doctor/consult_public/reply",{consult:consultId,content:content,imageCodes:imgNames,imageUrls:imgUrls});
  41. } else {
  42. mui.toast('上传失败: ' + status);
  43. }
  44. plus.nativeUI.closeWaiting();
  45. });
  46. var len = uploadImgUrl.length;
  47. if(len > 0) {
  48. for(var i = 0; i < len; i++) {
  49. // console.log(uploadImgUrl[i]);
  50. task.addFile(uploadImgUrl[i], {});
  51. }
  52. task.start();
  53. }
  54. }
  55. //获取压缩图片路径
  56. function getCompressImg() {
  57. var pressImgUrl = [];
  58. var imgs = document.querySelectorAll(".add-img li img");
  59. if(imgs.length > 0) {
  60. plus.nativeUI.showWaiting();
  61. var path, prepath = "_doc/press_img/";
  62. for(var i = 0; i < imgs.length; i++) {
  63. path = imgs[i].getAttribute("src");
  64. var aImg = path.split("/");
  65. var imgName = aImg[aImg.length - 1];
  66. compressImg(path, prepath + imgName, pressImgUrl);
  67. }
  68. return pressImgUrl;
  69. } else {
  70. return null;
  71. }
  72. }
  73. //压缩图片
  74. function compressImg(path, dstpath, imgurl) {
  75. plus.zip.compressImage({
  76. src: path,
  77. dst: dstpath,
  78. quality: 20,
  79. overwrite: true
  80. }, function(succ) {
  81. var url = succ.target;
  82. var size = succ.size;
  83. var width = succ.width;
  84. var height = succ.height;
  85. // console.log("压缩成功 " + url);
  86. imgurl.push(url);
  87. }, function(err) {
  88. console.error("压缩失败:" + err.message);
  89. if(err.message == "文件不存在") {
  90. mui.toast(err.message);
  91. plus.nativeUI.closeWaiting();
  92. plus.webview.currentWebview().reload();
  93. }
  94. });
  95. }
  96. //删除图片
  97. // function delImgItem() {
  98. // mui(".add-img").on("tap", ".icon-del", function() {
  99. // var oli = this.parentElement;
  100. // var oul = this.parentElement.parentElement;
  101. // oul.removeChild(oli);
  102. // });
  103. // };
  104. //上传成功后清空所有图片
  105. function clearImg() {
  106. var imgArea = document.querySelector(".add-img");
  107. var imgItems = imgArea.children;
  108. var len = imgItems.length;
  109. for(var j = len - 1; j >= 0; j--) {
  110. // var node = imgItems[j].nodeName;
  111. imgArea.removeChild(imgItems[j]);
  112. }
  113. }
  114. // 弹出选择照片方式
  115. function showActionSheet(imgArea, imgFlag) {
  116. plus.nativeUI.actionSheet({
  117. cancel: "取消",
  118. buttons: [{
  119. title: "从相册选择"
  120. }, {
  121. title: "拍照"
  122. }]
  123. },
  124. function(event) {
  125. if(event.index == 1) {
  126. getGallery(imgArea, imgFlag);
  127. } else if(event.index == 2) {
  128. getCamera(imgArea, imgFlag);
  129. }
  130. }
  131. );
  132. };
  133. //获取相册
  134. function getGallery(imgArea, imgFlag) {
  135. // plus.gallery.pick(function(scb) { //scb:SuccessCallBack
  136. plus.gallery.pick(function(cbFile) { //scb:SuccessCallBack
  137. var li = document.createElement("li");
  138. li.innerHTML = '<img class="dai-img" src="' + cbFile + '" /><i class="icon-del daili-posi"></i>';
  139. imgArea.insertBefore(li, imgFlag);
  140. }, function(ecb) {
  141. }, {
  142. filename: "_doc/gallery/",
  143. filter: "image"
  144. });
  145. }
  146. // 调用系统摄像头
  147. function getCamera(imgArea, imgFlag) {
  148. var cmr = plus.camera.getCamera();
  149. cmr.captureImage(function(path) {
  150. /**
  151. * 拍照成功后,图片本保存在本地,这时候我们需要调用本地文件
  152. * http://www.html5plus.org/doc/zh_cn/io.html#plus.io.resolveLocalFileSystemURL
  153. */
  154. plus.io.resolveLocalFileSystemURL(path, function(entry) {
  155. /*
  156. * 将获取目录路径转换为本地路径URL地址
  157. * http://www.html5plus.org/doc/zh_cn/io.html#plus.io.DirectoryEntry.toLocalURL
  158. */
  159. var li = document.createElement("li");
  160. li.innerHTML = '<img src="' + entry.toLocalURL() + '" /><i class="icon-del"></i>';
  161. imgArea.insertBefore(li, imgFlag);
  162. });
  163. }, function(error) {
  164. if(error.code==11 && error.message == "null") {
  165. plus.nativeUI.toast("您尚未授权拍照权限,无法使用拍照功能。");
  166. }
  167. // console.log(error.message);
  168. }, {
  169. filename: "_doc/camera/",
  170. index: 1 //ios指定主摄像头
  171. });
  172. }