upload.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. const upload = {
  2. data : {},
  3. handle : function(key, count, callback) {
  4. var self = this;
  5. count = parseInt(count);
  6. uni.chooseImage({
  7. count: count,
  8. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  9. sourceType: ['album'], //从相册选择
  10. success: res => {
  11. uni.showLoading();
  12. res.tempFilePaths.forEach(function(item, index) {
  13. //请求上传接口
  14. self.uploadFile(item, key, count, callback);
  15. });
  16. },
  17. fail: () => {
  18. uni.showToast({ title: '图片上传失败,请重试', icon: 'none' });
  19. uni.hideLoading();
  20. }
  21. });
  22. },
  23. upfile : function(e, key, count, callback, ext, title, style) {
  24. if (!count) {
  25. count = 1;
  26. }
  27. var type = 1;
  28. if (count > 1) {
  29. type = 2;
  30. }
  31. if (!title) {
  32. title = '点击这里上传文件';
  33. }
  34. var self = this;
  35. count = parseInt(count);
  36. var a = document.createElement('button');
  37. a.className = 'file';
  38. if (style) {
  39. a.style.cssText = style;
  40. } else {
  41. a.style.cssText = 'padding: 4px 10px;height: 20px;line-height: 20px;position: relative;cursor: pointer;color: #888;background: #fafafa;border: 1px solid #ddd;border-radius: 4px;overflow: hidden;display: inline-block;*display: inline;*zoom: 1;';
  42. }
  43. var style = 'position: absolute;opacity: 0;filter: alpha(opacity=0);cursor: pointer';
  44. a.innerHTML = '<input style="'+style+'" type="file" name="upload_file" id="upload_file" />' + title;
  45. e.$refs.input.$el.appendChild(a)
  46. var input = document.getElementById('upload_file');
  47. input.onchange = (event) => {
  48. uni.showLoading();
  49. var reader = new FileReader();
  50. var type = input.files[0].type;
  51. var temp = type.split('/');
  52. type = temp[1];
  53. //console.info(input.files[0]);
  54. if (ext && ext.indexOf(type) == -1) {
  55. uni.showToast({ title: '您上传的文件类型错误,请上传带有' + ext + '这些后缀的文件', icon: 'none' });
  56. } else {
  57. reader.readAsDataURL(input.files[0]);
  58. reader.onload = function(){
  59. //读取完成后,数据保存在对象的result属性中
  60. var pic = this.result;
  61. var temp = pic.split('base64,');
  62. pic = temp[1];
  63. dever.debug(pic);
  64. var url = "http://up.qiniu.com/putb64/-1";
  65. var xhr = new XMLHttpRequest();
  66. xhr.onreadystatechange=function(){
  67. if (xhr.readyState==4){
  68. dever.debug(xhr.responseText);
  69. var data = JSON.parse(xhr.responseText);
  70. uni.hideLoading();
  71. if (data.uploaded) {
  72. var backUrl = data.url;
  73. if (count > 1) {
  74. if (!self.data[key]) {
  75. self.data[key] = [];
  76. }
  77. self.data[key].push(backUrl);
  78. } else {
  79. self.data[key] = backUrl;
  80. }
  81. if (callback) {
  82. callback(type, self.data[key], input.files[0]);
  83. }
  84. } else if (data.uploaded == false) {
  85. uni.showToast({ title: data.error.message, icon: 'none' });
  86. }
  87. }
  88. }
  89. xhr.open("POST", url, true);
  90. xhr.setRequestHeader("Content-Type", "application/octet-stream");
  91. xhr.setRequestHeader("Authorization", "UpToken " + e.token);
  92. xhr.send(pic);
  93. }
  94. }
  95. }
  96. },
  97. qnUpload : function(qn, config, key, count, callback) {
  98. if (!count) {
  99. count = 1;
  100. }
  101. var type = 1;
  102. if (count > 1) {
  103. type = 2;
  104. }
  105. var self = this;
  106. count = parseInt(count);
  107. var call = function(data) {
  108. var backUrl = data.url;
  109. if (count > 1) {
  110. if (!self.data[key]) {
  111. self.data[key] = [];
  112. }
  113. self.data[key].push(backUrl);
  114. } else {
  115. self.data[key] = backUrl;
  116. }
  117. if (callback) {
  118. callback(type, self.data[key]);
  119. }
  120. }
  121. uni.chooseImage({
  122. count: count,
  123. //sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  124. sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
  125. sourceType: ['album'], //从相册选择
  126. success: res => {
  127. uni.showLoading();
  128. res.tempFilePaths.forEach(function(item, index) {
  129. self.qiniu(qn, item, config.domain, config.token, call);
  130. });
  131. },
  132. fail: () => {
  133. uni.showToast({ title: '图片上传失败,请重试', icon: 'none' });
  134. uni.hideLoading();
  135. }
  136. });
  137. },
  138. qiniu : function(qn, file, domain, token, call, handle) {
  139. //请求上传接口
  140. var config = {};
  141. config = {
  142. region: 'ECN',
  143. domain: domain,
  144. //key: 'test.mp4',
  145. };
  146. if (token.indexOf('http') == -1) {
  147. config.uptoken = token;
  148. } else {
  149. config.uptokenURL = token;
  150. }
  151. uni.showLoading({title: '上传中', mask: true});
  152. qn.upload(
  153. file,
  154. res => {
  155. dever.debug(res);
  156. var data = res;
  157. uni.hideLoading();
  158. if (data.uploaded) {
  159. call(data);
  160. } else {
  161. uni.showToast({title: data.error.message, icon: 'none' });
  162. }
  163. },
  164. error => {
  165. uni.showToast({title: '上传失败,请重试', icon: 'none' });
  166. uni.hideLoading();
  167. },
  168. config,
  169. res => {
  170. //上传进度
  171. if (handle) {
  172. handle(res.progress);
  173. } else {
  174. uni.showToast({title: '上传进度:%' + res.progress, icon: 'none' });
  175. }
  176. }
  177. );
  178. },
  179. uploadDel : function(key, index, callback) {
  180. var self = this;
  181. uni.showModal({
  182. content: '确定要删除吗?',
  183. cancelText: '取消',
  184. confirmText: '确定',
  185. success: res => {
  186. if (res.confirm) {
  187. // self.data[key].splice(index, 1);
  188. if (callback) {
  189. callback(res, index);
  190. }
  191. }
  192. }
  193. });
  194. }
  195. }
  196. export default upload;