暫無描述

feedback-page-work.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. var fileArr = [],
  2. fileSrcLength, imgUpLength = 0,imgsLength=0;
  3. mui.init({
  4. swipeBack: true //启用右滑关闭功能
  5. });
  6. document.getElementById('headImage').addEventListener('tap', function() {
  7. if (mui.os.plus) {
  8. var buttonTit = [{
  9. title: "拍照"
  10. }, {
  11. title: "从手机相册选择"
  12. }];
  13. plus.nativeUI.actionSheet({
  14. title: "上传图片",
  15. cancel: "取消",
  16. buttons: buttonTit
  17. }, function(b) { /*actionSheet 按钮点击事件*/
  18. switch (b.index) {
  19. case 0:
  20. break;
  21. case 1:
  22. getImage(); /*拍照*/
  23. break;
  24. case 2:
  25. galleryImg(); /*打开相册*/
  26. break;
  27. default:
  28. break;
  29. }
  30. })
  31. }
  32. }, false);
  33. // 拍照获取图片  
  34. function getImage() {
  35. var c = plus.camera.getCamera();
  36. fileArr = [];
  37. imgUpLength = 0;
  38. c.captureImage(function(e) {
  39. plus.io.resolveLocalFileSystemURL(e, function(entry) {
  40. plus.nativeUI.showWaiting
  41. var imgSrc = entry.toLocalURL() + "?version=" + new Date().getTime(); //拿到图片路径  
  42. imgUpLength++;
  43. appendFile(imgSrc);
  44. }, function(e) {
  45. console.log("读取拍照文件错误:" + e.message);
  46. });
  47. }, function(s) {
  48. console.log("error" + s.message);
  49. }, {
  50. filename: "_doc/camera/"
  51. })
  52. }
  53. // 从相册中选择图片   
  54. function galleryImg() {
  55. // 从相册中选择图片  
  56. fileArr = [];
  57. imgUpLength = 0;
  58. plus.gallery.pick(function(e) {
  59. for (var i in e.files) {
  60. var fileSrc = e.files[i];
  61. fileSrcLength = e.files.length;
  62. imgUpLength++;
  63. appendFile(fileSrc);
  64. }
  65. plus.nativeUI.showWaiting();
  66. }, function(e) {
  67. console.log("取消选择图片");
  68. }, {
  69. filter: "image",
  70. multiple: true,
  71. //maximum: 5,
  72. system: false,
  73. onmaxed: function() {
  74. plus.nativeUI.alert('最多只能选择5张图片');
  75. }
  76. });
  77. }
  78. function setHtml(path, fl) {
  79. var str = '';
  80. str = '<li class="mui-table-view-cell" imgId=""><div class="img_div">' +
  81. '<img src="' + path + '" base64="' + f1 + '">' +
  82. '<div class="mui-icon mui-icon-trash deleteBtn"></div>' +
  83. '</div></li>';
  84. $("#imgs").append(str);
  85. }
  86. $("#imgs").on("tap", ".deleteBtn", function() {
  87. var btnArray = ['取消', '确定'];
  88. var index = $(this).parent().parent();
  89. mui.confirm('确认删除', '提示', btnArray, function(e) {
  90. if (e.index == 1) {
  91. index.remove();
  92. imgsLength--;
  93. } else {
  94. plus.nativeUI.toast('取消');
  95. }
  96. })
  97. })
  98. function upFile() {
  99. mui.ajax(huayi.config.callcenter_url + '/ApplicationsVersion/UploadBase64', {
  100. data: {
  101. "dataurl": fileArr
  102. },
  103. dataType: 'json', //服务器返回json格式数据
  104. type: 'post', //HTTP请求类型
  105. timeout: 10000, //超时时间设置为10秒
  106. success: function(data) {
  107. if (data.state == "success") {
  108. for (var i in data.acs) {
  109. $("#imgs li").eq(Number(imgsLength)).attr("imgId", data.acs[i].F_Id)
  110. imgsLength++;
  111. }
  112. mui.alert("图片上传成功");
  113. plus.nativeUI.closeWaiting();
  114. } else {
  115. mui.alert("上传失败");
  116. plus.nativeUI.closeWaiting();
  117. }
  118. },
  119. error: function(xhr, type, errorThrown) {
  120. mui.alert(errorThrown);
  121. plus.nativeUI.closeWaiting();
  122. }
  123. });
  124. }
  125. // 添加文件
  126. var f1 = null;
  127. function appendFile(path) {
  128. var bitmap = new plus.nativeObj.Bitmap("test");
  129. // 从本地加载Bitmap图片
  130. bitmap.load(path,function(){
  131. var base64 = bitmap.toBase64Data()
  132. f1 = base64; // 把base64数据丢过去,上传要用。
  133. setHtml(path, f1);
  134. fileArr.push(f1);
  135. if (imgUpLength == fileArr.length) {
  136. upFile()
  137. }
  138. },function(e){
  139. console.log('加载图片失败:'+JSON.stringify(e));
  140. });
  141. }