Browse Source

cos换七牛

lizhen_gitee 1 year ago
parent
commit
26c77f8238

+ 1 - 1
addons/cos/info.ini

@@ -4,7 +4,7 @@ intro = 使用腾讯COS云存储作为默认云储存
 author = FastAdmin
 website = https://www.fastadmin.net
 version = 1.1.5
-state = 1
+state = 0
 url = /addons/cos
 license = regular
 licenseto = 19079

+ 1 - 1
application/config.php

@@ -353,7 +353,7 @@ return [
     'default_avatar' => '',
     'app_download_url' => 'https://tken.huxiukeji.cn',//app下载页域名
     'pay_notify_url' => 'https://tken.huxiukeji.cn',  //支付异步回调域名
-    'domain_cdnurl' => 'https://tken-1316687309.cos.ap-shanghai.myqcloud.com',//图片地址
+    'domain_cdnurl' => 'https://oss.tken.vip',//图片地址
 
     //开放平台的,不是小程序的
     'wxMiniProgram' => [

+ 10 - 7
application/extra/addons.php

@@ -4,16 +4,10 @@ return [
     'autoload' => false,
     'hooks' => [
         'app_init' => [
-            'cos',
             'epay',
+            'qiniu',
             'qrcode',
         ],
-        'upload_config_init' => [
-            'cos',
-        ],
-        'upload_delete' => [
-            'cos',
-        ],
         'user_sidenav_after' => [
             'poster',
         ],
@@ -29,6 +23,15 @@ return [
         'sms_check' => [
             'qcloudsms',
         ],
+        'module_init' => [
+            'qiniu',
+        ],
+        'upload_config_init' => [
+            'qiniu',
+        ],
+        'upload_delete' => [
+            'qiniu',
+        ],
     ],
     'route' => [
         '/qrcode$' => 'qrcode/index/index',

+ 1 - 1
application/extra/upload.php

@@ -9,7 +9,7 @@ return [
     /**
      * CDN地址
      */
-    'cdnurl'    => 'https://tken-1316687309.cos.ap-shanghai.myqcloud.com',
+    'cdnurl'    => 'https://oss.tken.vip',
     /**
      * 文件保存格式
      */

File diff suppressed because it is too large
+ 0 - 0
public/assets/addons/cos/js/spark.js


+ 169 - 232
public/assets/js/addons.js

@@ -1,261 +1,198 @@
 define([], function () {
-    if (typeof Config.upload.storage !== 'undefined' && Config.upload.storage === 'cos') {
-    require(['upload'], function (Upload) {
-        //获取文件MD5值
-        var getFileMd5 = function (file, cb) {
-            require(['../addons/cos/js/spark'], function (SparkMD5) {
-                var blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
-                    chunkSize = 10 * 1024 * 1024,
-                    chunks = Math.ceil(file.size / chunkSize),
-                    currentChunk = 0,
-                    spark = new SparkMD5.ArrayBuffer(),
-                    fileReader = new FileReader();
-
-                fileReader.onload = function (e) {
-                    spark.append(e.target.result);
-                    currentChunk++;
-                    if (currentChunk < chunks) {
-                        loadNext();
-                    } else {
-                        cb && cb(spark.end());
-                    }
-                };
-
-                fileReader.onerror = function () {
-                    console.warn('文件读取错误');
-                };
-
-                function loadNext() {
-                    var start = currentChunk * chunkSize,
-                        end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize;
-
-                    fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
-                }
-
-                loadNext();
-            });
-        };
+    require.config({
+    paths: {
+        'designer': '../addons/poster/js/designer',
+        'jquery.contextMenu': '../addons/poster/js/jquery.contextMenu',
+        'jquery-colorpicker': '../addons/poster/js/jquery.colorpicker.min',
+    }
+});
 
-        //初始化中完成判断
-        Upload.events.onInit = function () {
-            //如果上传接口不是COS,则不处理
-            if (this.options.url !== Config.upload.uploadurl) {
-                return;
-            }
-            $.extend(this.options, {
-                //关闭自动处理队列功能
-                autoQueue: false,
-                params: function (files, xhr, chunk) {
-                    var params = Config.upload.multipart;
-                    if (chunk) {
-                        return $.extend({}, params, {
-                            filesize: chunk.file.size,
-                            filename: chunk.file.name,
-                            chunkid: chunk.file.upload.uuid,
-                            chunkindex: chunk.index,
-                            chunkcount: chunk.file.upload.totalChunkCount,
-                            chunkfilesize: chunk.dataBlock.data.size,
-                            chunksize: this.options.chunkSize,
-                            width: chunk.file.width || 0,
-                            height: chunk.file.height || 0,
-                            type: chunk.file.type,
-                            uploadId: chunk.file.uploadId,
-                            key: chunk.file.key,
-                        });
-                    } else {
-                        params = $.extend({}, params, files[0].params);
-                        params.category = files[0].category || '';
-                    }
-                    return params;
-                },
-                chunkSuccess: function (chunk, file, response) {
-                    var etag = chunk.xhr.getResponseHeader("ETag").replace(/(^")|("$)/g, '');
-                    this.etags = this.etags ? this.etags : [];
-                    this.etags[chunk.index] = etag;
-                },
-                chunksUploaded: function (file, done) {
-                    var that = this;
+//修改上传的接口调用
+require(['upload'], function (Upload) {
 
-                    Fast.api.ajax({
-                        url: "/addons/cos/index/upload",
-                        data: {
-                            action: 'merge',
-                            filesize: file.size,
-                            filename: file.name,
-                            chunkid: file.upload.uuid,
-                            chunkcount: file.upload.totalChunkCount,
-                            md5: file.md5,
-                            key: file.key,
-                            uploadId: file.uploadId,
-                            etags: this.etags,
-                            category: file.category || '',
-                            costoken: Config.upload.multipart.costoken,
-                        },
-                    }, function (data, ret) {
-                        done(JSON.stringify(ret));
-                        return false;
-                    }, function (data, ret) {
-                        file.accepted = false;
-                        that._errorProcessing([file], ret.msg);
-                        return false;
-                    });
+    var _onInit = Upload.events.onInit;
+    //初始化中完成判断
+    Upload.events.onInit = function () {
+        _onInit.apply(this, Array.prototype.slice.apply(arguments));
+        //如果上传接口不是七牛云,则不处理
+        if (this.options.url !== Config.upload.uploadurl) {
+            return;
+        }
+        var _success = this.options.success;
 
-                },
-            });
+        $.extend(this.options, {
+            //关闭自动处理队列功能
+            autoQueue: false,
+            chunkSuccess: function (chunk, file, response) {
+                this.contexts = this.contexts ? this.contexts : [];
+                this.contexts.push(typeof response.ctx !== 'undefined' ? response.ctx : response.data.ctx);
+            },
+            chunksUploaded: function (file, done) {
+                var that = this;
+                var params = $(that.element).data("params") || {};
+                var category = typeof params.category !== 'undefined' ? params.category : ($(that.element).data("category") || '');
+                category = typeof category === 'function' ? category.call(this, file) : category;
+                Fast.api.ajax({
+                    url: "/addons/qiniu/index/upload",
+                    data: {
+                        action: 'merge',
+                        filesize: file.size,
+                        filename: file.name,
+                        chunkid: file.upload.uuid,
+                        chunkcount: file.upload.totalChunkCount,
+                        width: file.width || 0,
+                        height: file.height || 0,
+                        type: file.type,
+                        category: category,
+                        qiniutoken: Config.upload.multipart.qiniutoken,
+                        contexts: this.contexts
+                    },
+                }, function (data, ret) {
+                    done(JSON.stringify(ret));
+                    return false;
+                }, function (data, ret) {
+                    file.accepted = false;
+                    that._errorProcessing([file], ret.msg);
+                    return false;
+                });
 
-            var _success = this.options.success;
-            //先移除已有的事件
-            this.off("success", _success).on("success", function (file, response) {
-                var ret = {code: 0, msg: response};
-                try {
-                    if (response) {
-                        ret = typeof response === 'string' ? JSON.parse(response) : response;
-                    }
-                    if (file.xhr.status === 200 || file.xhr.status === 204) {
-                        var url = '/' + file.key;
-                        ret = {code: 1, data: {url: url}};
+            },
+        });
 
+        //先移除已有的事件
+        this.off("success", _success).on("success", function (file, response) {
+            var that = this;
+            var ret = {code: 0, msg: response};
+            try {
+                ret = typeof response === 'string' ? JSON.parse(response) : response;
+                if (file.xhr.status === 200) {
+                    if (Config.upload.uploadmode === 'client') {
+                        if (typeof ret.key !== 'undefined') {
+                            ret = {code: 1, msg: "", data: {url: '/' + ret.key, hash: ret.hash}};
+                        }
+                        var url = ret.data.url || '';
+                        var params = $(that.element).data("params") || {};
+                        var category = typeof params.category !== 'undefined' ? params.category : ($(that.element).data("category") || '');
+                        category = typeof category === 'function' ? category.call(that, file) : category;
                         Fast.api.ajax({
-                            url: "/addons/cos/index/notify",
-                            data: {name: file.name, url: url, md5: file.md5, size: file.size, width: file.width || 0, height: file.height || 0, type: file.type, category: file.category || '', costoken: Config.upload.multipart.costoken}
+                            url: "/addons/qiniu/index/notify",
+                            data: {name: file.name, url: ret.data.url, hash: ret.data.hash, size: file.size, width: file.width || 0, height: file.height || 0, type: file.type, category: category, qiniutoken: Config.upload.multipart.qiniutoken}
                         }, function () {
                             return false;
                         }, function () {
                             return false;
                         });
+                    } else {
+                        console.error(ret);
                     }
-                } catch (e) {
-                    console.error(e);
+                } else {
+                    console.error(file.xhr);
                 }
-                _success.call(this, file, ret);
-            });
+            } catch (e) {
+                console.error(e);
+            }
+            _success.call(this, file, ret);
+        });
 
-            this.on("addedfile", function (file) {
-                var that = this;
-                setTimeout(function () {
-                    if (file.status === 'error') {
-                        return;
-                    }
-                    getFileMd5(file, function (md5) {
-                        var chunk = that.options.chunking && file.size > that.options.chunkSize ? 1 : 0;
-                        var params = $(that.element).data("params") || {};
-                        var category = typeof params.category !== 'undefined' ? params.category : ($(that.element).data("category") || '');
-                        Fast.api.ajax({
-                            url: "/addons/cos/index/params",
-                            data: {method: 'POST', category: category, md5: md5, name: file.name, type: file.type, size: file.size, chunk: chunk, chunksize: that.options.chunkSize, costoken: Config.upload.multipart.costoken},
-                        }, function (data) {
-                            file.md5 = md5;
-                            file.id = data.id;
-                            file.key = data.key;
-                            file.date = data.date;
-                            file.uploadId = data.uploadId;
-                            file.policy = data.policy;
-                            file.signature = data.signature;
-                            file.partsAuthorization = data.partsAuthorization;
-                            file.params = data;
-                            file.category = category;
+        this.on("addedfile", function (file) {
+            var that = this;
+            setTimeout(function () {
+                if (file.status === 'error') {
+                    return;
+                }
 
-                            if (file.status != 'error') {
-                                //开始上传
-                                that.enqueueFile(file);
-                            } else {
-                                that.removeFile(file);
-                            }
-                            return false;
-                        });
-                    });
-                }, 0);
-            });
+                var md5 = ''; //七牛云无需本地获取文件MD5
+                var chunk = that.options.chunking && file.size > that.options.chunkSize ? 1 : 0;
+                var params = $(that.element).data("params") || {};
+                var category = typeof params.category !== 'undefined' ? params.category : ($(that.element).data("category") || '');
+                category = typeof category === 'function' ? category.call(that, file) : category;
+                Fast.api.ajax({
+                    url: "/addons/qiniu/index/params",
+                    data: {method: 'POST', category: category, md5: md5, name: file.name, type: file.type, size: file.size, chunk: chunk, chunksize: that.options.chunkSize, qiniutoken: Config.upload.multipart.qiniutoken},
+                }, function (data) {
+                    file.qiniutoken = data.qiniutoken;
+                    file.params = data;
+                    file.category = category;
 
+                    if (file.status != 'error') {
+                        //开始上传
+                        that.enqueueFile(file);
+                    } else {
+                        that.removeFile(file);
+                    }
+                    return false;
+                }, function () {
+                    that.removeFile(file);
+                });
+            }, 0);
+        });
 
-            if (Config.upload.uploadmode === 'client') {
-                var _method = this.options.method;
-                var _url = this.options.url;
-                this.options.method = function (files) {
-                    if (files[0].upload.chunked) {
-                        var chunk = null;
-                        files[0].upload.chunks.forEach(function (item) {
-                            if (item.status === 'uploading') {
-                                chunk = item;
-                            }
-                        });
-                        if (!chunk) {
-                            return "POST";
-                        } else {
-                            return "PUT";
+        //如果是直传模式
+        if (Config.upload.uploadmode === 'client') {
+            var _url = this.options.url;
+
+            //分片上传时URL链接不同
+            this.options.url = function (files) {
+                this.options.headers = {"Authorization": "UpToken " + Config.upload.multipart.qiniutoken};
+                if (files[0].upload.chunked) {
+                    var chunk = null;
+                    files[0].upload.chunks.forEach(function (item) {
+                        if (item.status === 'uploading') {
+                            chunk = item;
                         }
+                    });
+                    if (!chunk) {
+                        return Config.upload.uploadurl + '/mkfile/' + files[0].size;
+                    } else {
+                        return Config.upload.uploadurl + '/mkblk/' + chunk.dataBlock.data.size;
                     }
-                    return _method;
-                };
-                this.options.url = function (files) {
-                    if (files[0].upload.chunked) {
+                }
+                return _url;
+            };
+
+            this.options.params = function (files, xhr, chunk) {
+                var params = Config.upload.multipart;
+                if (chunk) {
+                    return $.extend({}, params, {
+                        filesize: chunk.file.size,
+                        filename: chunk.file.name,
+                        chunkid: chunk.file.upload.uuid,
+                        chunkindex: chunk.index,
+                        chunkcount: chunk.file.upload.totalChunkCount,
+                        chunkfilesize: chunk.dataBlock.data.size,
+                        width: chunk.file.width || 0,
+                        height: chunk.file.height || 0,
+                        type: chunk.file.type,
+                    });
+                } else {
+                    var retParams = $.extend({}, params, files[0].params || {});
+                    //七牛云直传使用的是token参数
+                    retParams.token = retParams.qiniutoken;
+                    delete retParams.qiniutoken;
+                    return retParams;
+                }
+            };
+
+            //分片上传时需要变更提交的内容
+            this.on("sending", function (file, xhr, formData) {
+                if (file.upload.chunked) {
+                    var _send = xhr.send;
+                    xhr.send = function () {
                         var chunk = null;
-                        files[0].upload.chunks.forEach(function (item) {
-                            if (item.status === 'uploading') {
+                        file.upload.chunks.forEach(function (item) {
+                            if (item.status == 'uploading') {
                                 chunk = item;
                             }
                         });
-                        var index = chunk.dataBlock.chunkIndex;
-                        // debugger;
-                        this.options.headers = {"Authorization": files[0]['partsAuthorization'][index], "x-date": files[0]['date']};
-                        if (!chunk) {
-                            return Config.upload.uploadurl + "/" + files[0].key + "?uploadId=" + files[0].uploadId;
-                        } else {
-                            return Config.upload.uploadurl + "/" + files[0].key + "?partNumber=" + (index + 1) + "&uploadId=" + files[0].uploadId;
+                        if (chunk) {
+                            _send.call(xhr, chunk.dataBlock.data);
                         }
-                    }
-                    return _url;
-                };
-                this.options.params = function (files, xhr, chunk) {
-                    var params = Config.upload.multipart;
-                    if (chunk) {
-                        return $.extend({}, params, {
-                            filesize: chunk.file.size,
-                            filename: chunk.file.name,
-                            chunkid: chunk.file.upload.uuid,
-                            chunkindex: chunk.index,
-                            chunkcount: chunk.file.upload.totalChunkCount,
-                            chunkfilesize: chunk.dataBlock.data.size,
-                            width: chunk.file.width || 0,
-                            height: chunk.file.height || 0,
-                            type: chunk.file.type,
-                        });
-                    } else {
-                        var retParams = $.extend({}, params, files[0].params || {});
-                        delete retParams.costoken;
-                        return retParams;
-                    }
-                };
-                this.on("sending", function (file, xhr, formData) {
-                    var that = this;
-                    if (file.upload.chunked) {
-                        var _send = xhr.send;
-                        xhr.send = function () {
-                            var chunk = null;
-                            file.upload.chunks.forEach(function (item) {
-                                if (item.status == 'uploading') {
-                                    chunk = item;
-                                }
-                            });
-                            if (chunk) {
-                                _send.call(xhr, chunk.dataBlock.data);
-                            }
-                        };
-                    } else {
-
-                    }
-                });
-            }
-        };
-    });
-}
+                    };
+                }
+            });
+        }
+    };
 
-require.config({
-    paths: {
-        'designer': '../addons/poster/js/designer',
-        'jquery.contextMenu': '../addons/poster/js/jquery.contextMenu',
-        'jquery-colorpicker': '../addons/poster/js/jquery.colorpicker.min',
-    }
 });
 
 require.config({

Some files were not shown because too many files changed in this diff