• 保存到桌面  加入收藏  设为首页
小程序

微信小程序-js-批量上传文件-上传图片-封装-file-uploader.js-递归调用

时间:2018-05-10 21:21:53   作者:江节胜   来源:胜行天下网   阅读:1016   评论:0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
 * 文件上传工具(后期可以把Img都换成File)
 
 * file-uploader.js
 *
 * 江节胜创建 dev@jiangjiesheng.cn
 
 * 2018年05月09日
 */
//调用示例:
//var fileUploader = require("../../../utils/file-uploader.js");
// fileUploader.doUploadImgs(_this, uploadPhotosInfos, function (uploadAllCallback) {
// console.log("上传文件框架回调结果---" + JSON.stringify(uploadAllCallback));
//   // { "isAllSuccess":true, "totalCount":1, "successCount":1, "successUploadInfos":{ "http://tmp/wx50dd5a7c9d6fd93a.o6zAJszH6vI-5Mw190wEucpfXnaQ.SdsDWT5a5363b670fc97fa36b5cda77d737d38dc91de.png":"upload/a24d6996-a03f-4fa5-8086-af2f8caac5d5.png" }}
//   if (uploadAllCallback.isAllSuccess == true) {
//     //TODO something...
//   } else {
//     wx.showModal({
//       title: '警告:文件上传结果',
//       content: '有' + (totalCount - successCount) + "张文件上传失败,继续保存?",
//       success: function (res) {
//         if (res.confirm) {
//           console.log('用户点击确定')
//             //TODO something...
//         } else if (res.cancel) {
//           console.log('用户点击取消')
//         }
//       }
//     })
//   }
// }
var configapi = require("./../config/appconfig.js");
//如果后续发现不同的模块引入此appconfig模块有路径错误时,就把这个路径当成参数传入
function _doUploadImgs(_this, uploadPhotosInfos, uploadAllCallback) {
    var uploadAllCallbackJSON = {
        isAllSuccess: false,
        totalCount: 0,
        successCount: 0,
        successUploadInfos: {}
      };
    if (!uploadPhotosInfos || uploadPhotosInfos.length == 0) {
        return uploadAllCallbackJSON;
    }
    var _totalCount = uploadPhotosInfos.length;
    uploadAllCallbackJSON.totalCount = _totalCount;
    //初始化
    lastUploadImgIndex = 0;
    uploadAllResArr.splice(0, uploadAllResArr.length);
    uploadImgsAll(uploadPhotosInfos, function (uploadAllRes) {
        console.log("所有照片上传结束----共" + _totalCount + "张");
        console.log("所有照片上传结束----共" + JSON.stringify(uploadAllRes));
        // [{ "imgOrignPath": "http://tmp/wx50dd5a7c9d6fd93a.o6zAJszH6vI-5Mw190wEucpfXnaQ.SdsDWT5a5363b670fc97fa36b5cda77d737d38dc91de.png", "imgReletivevalUrl": "upload/f335a92b-53de-4f87-8021-cb8064747762.png" }]
        var _successCount = uploadAllRes.length;
        if (_successCount != _totalCount) {
            console.log("所有文件上传结束--共" + _totalCount + "张,但有" + (_totalCount - _successCount) + "张上传失败");
            uploadAllCallbackJSON.isAllSuccess = false;
        else {
            console.log("所有文件上传结束-共" + _totalCount + "张,且全部上传成功");
            uploadAllCallbackJSON.isAllSuccess = true;
        }
        uploadAllCallbackJSON.successCount = _successCount;
        var uploadAllDetail = {};
        //数组转成json形式 更方便处理  转成 key value形式
        for (var i = 0; i < uploadAllRes.length; i++) {
            var _item = uploadAllRes[i];
            var _key = _item.imgOrignPath;
            var _value = _item.imgReletivevalUrl;
            uploadAllDetail[_key] = _value;
        }
        uploadAllCallbackJSON.successUploadInfos = uploadAllDetail;
        uploadAllCallback && typeof uploadAllCallback == 'function' && uploadAllCallback(uploadAllCallbackJSON)
    }
    );
}
//定义
var uploadAllResArr = [];
var lastUploadImgIndex = 0;
function uploadImgsAll(uploadPhotosInfos, cb) {
    var totalCount = uploadPhotosInfos.length;
    uploadImgOne(uploadPhotosInfos, lastUploadImgIndex, function (uploadOneRes) {
        ++lastUploadImgIndex;
        // oneRes.imgIndex = lastUploadImgIndex;
        // oneRes.imgOrignPath = _imgPath;
        // oneRes.imgReletivevalUrl = "";
        // oneRes.errormsg = "innererr";
        // oneRes.success = false;
        var _s = uploadOneRes.success;
        if (_s == true) {
            var uploadAllRes = {}
                  uploadAllRes.imgOrignPath = uploadOneRes.imgOrignPath;
            uploadAllRes.imgReletivevalUrl = uploadOneRes.imgReletivevalUrl;
            uploadAllResArr.push(uploadAllRes);
        else {
            //TODO 后期记录下失败的原始Path
        }
        if (lastUploadImgIndex > totalCount - 1) {
            //最后一张都已近传完了,完全结束
            cb(uploadAllResArr);
            return;
        else {
            uploadImgsAll(uploadPhotosInfos, cb);
        }
    }
    )
}
function uploadImgOne(uploadPhotosInfos, lastUploadImgIndex, cb) {
    //imgOrignPath
    var _uploadFileUrl = configapi.getUploadFileHost() + "UploadHandler.ashx?action=minpupload&FileModule=projectinspect&FileType=imgaes&FileName=test-0427&FileExtName=jpg";
    //这些参数暂时不需要
    console.log('_uploadFileUrl=' + _uploadFileUrl);
    var _imgPath = uploadPhotosInfos[lastUploadImgIndex];
    var oneRes = {}
      if (!_imgPath) {
        oneRes.imgIndex = lastUploadImgIndex;
        oneRes.imgOrignPath = _imgPath;
        oneRes.imgReletivevalUrl = "";
        oneRes.errormsg = "undefineFilePath";
        oneRes.success = false;
        cb(oneRes)
            return;
    }
    wx.uploadFile({
        url: _uploadFileUrl,
            filePath: _imgPath,
            name: 'file',//这里根据自己的实际情况改
        formData: null,
            success: function (res) {
            var _data = res.data
                  _data = JSON.parse(_data)
                  console.log('结果返回res.data==' + JSON.stringify(_data));
            if (_data.code == 0) {
                var _imgReletivevalUrl = _data.result;
                oneRes.imgIndex = lastUploadImgIndex;
                oneRes.imgOrignPath = _imgPath;
                oneRes.imgReletivevalUrl = _imgReletivevalUrl;
                oneRes.errormsg = "ok";
                oneRes.success = true;
            else {
                oneRes.imgIndex = lastUploadImgIndex;
                oneRes.imgOrignPath = _imgPath;
                oneRes.imgReletivevalUrl = "";
                oneRes.errormsg = "innererr" + _data.message;
                ;
                oneRes.success = false;
            }
            cb(oneRes)
        }
        ,
            fail: function () {
            oneRes.imgIndex = lastUploadImgIndex;
            oneRes.imgOrignPath = _imgPath;
            oneRes.imgReletivevalUrl = "";
            oneRes.errormsg = "failcallback";
            oneRes.success = false;
            cb(oneRes)
        }
    }
    )
}
module.exports={
  doUploadImgs: _doUploadImgs
}

有任何疑问或技术合作都可联系我

微信:767000122 【推荐】

QQ:596957738


上一篇:没有了
下一篇:微信小程序-js-宽度单位转换-px转rpx
相关评论

加我微信 596957738 (QQ同号)加我微信   QQ联系:596957738   地址:江苏省南京市雨花台区

苏公网安备32011402010305号

皖ICP备16019989号

江节胜的Gitee,江节胜的Git地址