upload-util.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // ======================= upload-util.js 公共方法 ===========================
  2. // 依赖库:jquery
  3. // 本代码更新于:2019-5-1
  4. // 新增更简单的写法
  5. // 相关配置
  6. var upload_cfg = {
  7. upload_image_url: sa.cfg.api_url + '/upload/image', // 图片上传地址
  8. upload_video_url: sa.cfg.api_url + '/upload/video', // 视频上传地址
  9. upload_audio_url: sa.cfg.api_url + '/upload/audio', // 音频上传地址
  10. upload_apk_url: sa.cfg.api_url + '/upload/apk', // apk安装包上传地址
  11. upload_file_url: sa.cfg.api_url + '/upload/file', // file上传地址
  12. }
  13. // 将方法挂载到sa对象上
  14. window.sa = window.sa || {};
  15. // 上传图片
  16. sa.uploadImage = function(successCB) {
  17. sa.uploadFn(upload_cfg.upload_image_url, successCB);
  18. }
  19. // 上传视频
  20. sa.uploadVideo = function(successCB) {
  21. sa.uploadFn(upload_cfg.upload_video_url, successCB);
  22. }
  23. // 上传音频
  24. sa.uploadAudio = function(successCB) {
  25. sa.uploadFn(upload_cfg.upload_audio_url, successCB);
  26. }
  27. // 上传apk
  28. sa.uploadApk = function(successCB) {
  29. sa.uploadFn(upload_cfg.upload_apk_url, successCB);
  30. }
  31. // 上传任意文件
  32. sa.uploadFile = function(successCB) {
  33. sa.uploadFn(upload_cfg.upload_file_url, successCB);
  34. }
  35. // 上传的内部函数 (要上传到的地址,成功的回调)
  36. sa.uploadFn = function(url, successCB) {
  37. // 创建input
  38. var fileInput = document.createElement("input"); //创建input
  39. fileInput.type = "file"; //设置类型为file
  40. fileInput.id = 'uploadfile-' + randomString(12);
  41. fileInput.style.display = 'none';
  42. fileInput.onchange = function(evt) {
  43. startUpload(evt.target.files[0], url, successCB);
  44. }
  45. // 添加到body,并触发其点击事件
  46. document.body.appendChild(fileInput);
  47. document.querySelector('#' + fileInput.id).click();
  48. }
  49. // 上传多张图片
  50. sa.uploadImageList = function(successCB) {
  51. sa.uploadListFn(upload_cfg.upload_image_url, successCB);
  52. }
  53. // 上传多个音频
  54. sa.uploadAudioList = function(successCB) {
  55. sa.uploadListFn(upload_cfg.upload_audio_url, successCB);
  56. }
  57. // 上传多个视频
  58. sa.uploadVideoList = function(successCB) {
  59. sa.uploadListFn(upload_cfg.upload_video_url, successCB);
  60. }
  61. // 上传多个文件
  62. sa.uploadFileList = function(successCB) {
  63. sa.uploadListFn(upload_cfg.upload_file_url, successCB);
  64. }
  65. // 上传多个的内部函数 (要上传到的地址,成功的回调)
  66. sa.uploadListFn = function(url, successCB) {
  67. // 创建input
  68. var fileInput = document.createElement("input"); //创建input
  69. fileInput.type = "file"; // 设置类型为file
  70. fileInput.multiple = "multiple"; // 多选
  71. fileInput.id = 'uploadfile-' + randomString(12);
  72. fileInput.style.display = 'none';
  73. fileInput.onchange = function(evt) {
  74. // 开始上传
  75. var files = evt.target.files;
  76. for (var i = 0; i < files.length; i++) {
  77. let ii = i;
  78. startUpload(evt.target.files[ii], url, successCB);
  79. }
  80. }
  81. // 添加到body,并触发其点击事件
  82. document.body.appendChild(fileInput);
  83. document.querySelector('#' + fileInput.id).click();
  84. }
  85. // ======================= 以下方法为过时的旧方法 ===========================
  86. // 开始上传,图片版
  87. function startUploadImage(file, successCB) {
  88. startUpload(file, upload_cfg.upload_image_url, successCB);
  89. }
  90. var startUploadImage2 = startUploadImage; // 兼容以前的写法
  91. // 开始上传,视频版
  92. function startUploadVideo(file, successCB) {
  93. startUpload(file, upload_cfg.upload_video_url, successCB);
  94. }
  95. // 开始上传,音频版
  96. function startUploadAudio(file, successCB) {
  97. startUpload(file, upload_cfg.upload_audio_url, successCB);
  98. }
  99. // 开始上传,apk版
  100. function startUploadApk(file, successCB) {
  101. startUpload(file, upload_cfg.upload_apk_url, successCB);
  102. }
  103. // 开始上传
  104. function startUpload(file, url, successCB) {
  105. // 准备参数
  106. var form = new FormData();
  107. form.append('file', file);
  108. // 开始上传
  109. $.ajax({
  110. url: url,
  111. data: form,
  112. processData: false, // 默认true,设置为 false,不需要进行序列化处理
  113. cache: false, // 设置为false将不会从浏览器缓存中加载请求信息
  114. contentType: false, // 避免服务器不能正常解析文件
  115. dataType: 'json',
  116. type: 'post',
  117. beforeSend: function (xhr) {
  118. show_jdt();
  119. },
  120. complete: function (xhr) {
  121. close_jdt();
  122. },
  123. xhr: xhrOnProgress(function(e) {
  124. var percent = e.loaded / e.total; // 计算进度百分比, 取值结果为 0~1 之间的小无限不循环小数
  125. // progressCB(percent * 100);
  126. set_jdt_value(percent * 100);
  127. // console.log('进度百分比' + percent);
  128. }),
  129. success: function(res) {
  130. if(res.code == 200) {
  131. successCB(res.data); // 把地址给回调函数
  132. } else {
  133. sa.alert(res.msg);
  134. }
  135. },
  136. error: function(e) {
  137. sa.alert('异常: ' + JSON.stringify(e));
  138. }
  139. });
  140. }
  141. // ======================= 工具方法 ===========================
  142. // 返回后缀名
  143. function get_suffix(filename) {
  144. var pos = filename.lastIndexOf('.');
  145. if (pos != -1) {
  146. suffix = filename.substring(pos + 1);
  147. }
  148. return suffix;
  149. }
  150. // 返回带有上传回调功能的 xhr
  151. function xhrOnProgress(fun) {
  152. xhrOnProgress.onprogress = fun; //绑定监听
  153. //使用闭包实现监听绑
  154. return function() {
  155. //通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
  156. var xhr = $.ajaxSettings.xhr();
  157. //判断监听函数是否为函数
  158. if (typeof xhrOnProgress.onprogress !== 'function')
  159. return xhr;
  160. //如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
  161. if (xhrOnProgress.onprogress && xhr.upload) {
  162. xhr.upload.onprogress = xhrOnProgress.onprogress;
  163. }
  164. return xhr;
  165. }
  166. }
  167. //
  168. function randomString(len) {
  169.   len = len || 32;
  170.   var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
  171.   var maxPos = $chars.length;
  172.   var str = '';
  173.   for (i = 0; i < len; i++) {
  174.     str += $chars.charAt(Math.floor(Math.random() * maxPos));
  175.   }
  176.   return str;
  177. }
  178. // ======================= 进度条相关 ===========================
  179. // 显示进度条
  180. function show_jdt() {
  181. close_jdt(); // 先清除原来的
  182. // 创建节点并添加到body
  183. var str = '' +
  184. '<div class="jdt-fox" style="z-index: 999999999; width: 500px; height: 20px; position: fixed; top: calc(50% - 5px); left: calc(50% - 250px); ">'+
  185. ' <div class="jdt-fox2" style="width: calc(100% - 100px); height: 6px; margin-top: 7px; border-radius: 3px; float: left; background-color: #FFF; box-shadow: 0 0 10px #aaa;">'+
  186. ' <div class="jdt-fox-value" style=" transition: all 0.1s; position: relative; width: 0.0%; height: 100%; border-radius: 3px; background-color: green; box-shadow: 0 0 10px green;">'+
  187. ' <div class="jdt-fox-yh" style="position: absolute; right: -10px; top: -5px;">'+
  188. ' <div style="transition: all 1s; background-color: green; width: 16px; height: 16px; border-radius: 50%;"></div>'+
  189. ' </div>'+
  190. ' </div>'+
  191. ' </div>'+
  192. ' <div class="jdt-value-text" style="float: left; font-size: 14px; margin-left: 14px; color: #111;"> 0.0% </div>'+
  193. '</div>';
  194. var div = document.createElement("div");
  195. div.innerHTML = str;
  196. div.className = "jdt-box";
  197. document.body.appendChild(div);
  198. // 开启圆点的呼吸动画效果
  199. if (window.my_interval_index) {
  200. clearInterval(window.my_interval_index);
  201. }
  202. window.my_interval_index = setInterval(function() {
  203. if (window.one_num === undefined) {
  204. window.one_num = 0;
  205. }
  206. window.one_num++;
  207. var n_px = window.one_num % 2 == 0 ? '0px' : '20px';
  208. var box_shadow = "0 0 " + n_px + " green";
  209. document.querySelector('.jdt-fox-yh div').style.boxShadow = box_shadow;
  210. }, 1000);
  211. }
  212. // 关闭进度条
  213. function close_jdt() {
  214. // 先关闭动画
  215. if (window.my_interval_index) {
  216. clearInterval(window.my_interval_index);
  217. }
  218. // 再销毁dom
  219. var box = document.querySelector('.jdt-box');
  220. if (box) {
  221. box.parentNode.removeChild(box);
  222. }
  223. }
  224. // 设置进度条进度, 参数为一个0~100之间的小数
  225. function set_jdt_value(value) {
  226. value = parseInt(value * 10) / 10.0 + '%'; // 保证小数点后一位
  227. // 改变进度条宽度
  228. var dft = document.querySelector('.jdt-fox-value');
  229. if(dft){
  230. dft.style.width = value;
  231. }
  232. // 改变文字百分比值
  233. var dvt = document.querySelector('.jdt-value-text');
  234. if(dvt) {
  235. dvt.innerHTML = value;
  236. }
  237. // console.log(value);
  238. }