123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- const upload = {
- data : {},
-
- handle : function(key, count, callback) {
- var self = this;
- count = parseInt(count);
- uni.chooseImage({
- count: count,
- sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
- sourceType: ['album'], //从相册选择
- success: res => {
- uni.showLoading();
- res.tempFilePaths.forEach(function(item, index) {
- //请求上传接口
- self.uploadFile(item, key, count, callback);
- });
- },
- fail: () => {
- uni.showToast({ title: '图片上传失败,请重试', icon: 'none' });
- uni.hideLoading();
- }
- });
- },
-
- upfile : function(e, key, count, callback, ext, title, style) {
- if (!count) {
- count = 1;
- }
- var type = 1;
- if (count > 1) {
- type = 2;
- }
- if (!title) {
- title = '点击这里上传文件';
- }
- var self = this;
- count = parseInt(count);
- var a = document.createElement('button');
- a.className = 'file';
- if (style) {
- a.style.cssText = style;
- } else {
- 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;';
- }
- var style = 'position: absolute;opacity: 0;filter: alpha(opacity=0);cursor: pointer';
- a.innerHTML = '<input style="'+style+'" type="file" name="upload_file" id="upload_file" />' + title;
- e.$refs.input.$el.appendChild(a)
- var input = document.getElementById('upload_file');
-
- input.onchange = (event) => {
- uni.showLoading();
- var reader = new FileReader();
- var type = input.files[0].type;
- var temp = type.split('/');
- type = temp[1];
- //console.info(input.files[0]);
- if (ext && ext.indexOf(type) == -1) {
- uni.showToast({ title: '您上传的文件类型错误,请上传带有' + ext + '这些后缀的文件', icon: 'none' });
- } else {
- reader.readAsDataURL(input.files[0]);
- reader.onload = function(){
- //读取完成后,数据保存在对象的result属性中
- var pic = this.result;
- var temp = pic.split('base64,');
- pic = temp[1];
- dever.debug(pic);
- var url = "http://up.qiniu.com/putb64/-1";
- var xhr = new XMLHttpRequest();
- xhr.onreadystatechange=function(){
- if (xhr.readyState==4){
- dever.debug(xhr.responseText);
- var data = JSON.parse(xhr.responseText);
- uni.hideLoading();
- if (data.uploaded) {
- var backUrl = data.url;
- if (count > 1) {
- if (!self.data[key]) {
- self.data[key] = [];
- }
- self.data[key].push(backUrl);
- } else {
- self.data[key] = backUrl;
- }
- if (callback) {
- callback(type, self.data[key], input.files[0]);
- }
- } else if (data.uploaded == false) {
- uni.showToast({ title: data.error.message, icon: 'none' });
- }
- }
- }
- xhr.open("POST", url, true);
- xhr.setRequestHeader("Content-Type", "application/octet-stream");
- xhr.setRequestHeader("Authorization", "UpToken " + e.token);
- xhr.send(pic);
- }
- }
-
- }
- },
-
- qnUpload : function(qn, config, key, count, callback) {
- if (!count) {
- count = 1;
- }
- var type = 1;
- if (count > 1) {
- type = 2;
- }
- var self = this;
- count = parseInt(count);
- var call = function(data) {
- var backUrl = data.url;
- if (count > 1) {
- if (!self.data[key]) {
- self.data[key] = [];
- }
- self.data[key].push(backUrl);
- } else {
- self.data[key] = backUrl;
- }
- if (callback) {
- callback(type, self.data[key]);
- }
- }
- uni.chooseImage({
- count: count,
- //sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
- sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
- sourceType: ['album'], //从相册选择
- success: res => {
- uni.showLoading();
- res.tempFilePaths.forEach(function(item, index) {
- self.qiniu(qn, item, config.domain, config.token, call);
- });
- },
- fail: () => {
- uni.showToast({ title: '图片上传失败,请重试', icon: 'none' });
- uni.hideLoading();
- }
- });
- },
-
- qiniu : function(qn, file, domain, token, call, handle) {
- //请求上传接口
- var config = {};
- config = {
- region: 'ECN',
- domain: domain,
- //key: 'test.mp4',
- };
- if (token.indexOf('http') == -1) {
- config.uptoken = token;
- } else {
- config.uptokenURL = token;
- }
- uni.showLoading({title: '上传中', mask: true});
- qn.upload(
- file,
- res => {
- dever.debug(res);
- var data = res;
- uni.hideLoading();
- if (data.uploaded) {
- call(data);
- } else {
- uni.showToast({title: data.error.message, icon: 'none' });
- }
- },
- error => {
- uni.showToast({title: '上传失败,请重试', icon: 'none' });
- uni.hideLoading();
- },
- config,
- res => {
- //上传进度
- if (handle) {
- handle(res.progress);
- } else {
- uni.showToast({title: '上传进度:%' + res.progress, icon: 'none' });
- }
- }
- );
- },
-
- uploadDel : function(key, index, callback) {
- var self = this;
- uni.showModal({
- content: '确定要删除吗?',
- cancelText: '取消',
- confirmText: '确定',
- success: res => {
- if (res.confirm) {
- // self.data[key].splice(index, 1);
- if (callback) {
- callback(res, index);
- }
- }
- }
- });
- }
- }
- export default upload;
|