作者 开飞机的舒克

后台修改

@@ -5,14 +5,22 @@ return [ @@ -5,14 +5,22 @@ return [
5 'hooks' => [ 5 'hooks' => [
6 'app_init' => [ 6 'app_init' => [
7 'barcode', 7 'barcode',
  8 + 'qiniu',
8 ], 9 ],
9 - 'user_delete_successed' => [ 10 + 'module_init' => [
  11 + 'qiniu',
10 'third', 12 'third',
11 ], 13 ],
12 - 'user_logout_successed' => [ 14 + 'upload_config_init' => [
  15 + 'qiniu',
  16 + ],
  17 + 'upload_delete' => [
  18 + 'qiniu',
  19 + ],
  20 + 'user_delete_successed' => [
13 'third', 21 'third',
14 ], 22 ],
15 - 'module_init' => [ 23 + 'user_logout_successed' => [
16 'third', 24 'third',
17 ], 25 ],
18 'action_begin' => [ 26 'action_begin' => [
1 define([], function () { 1 define([], function () {
2 - if (Config.modulename === 'index' && Config.controllername === 'user' && ['login', 'register'].indexOf(Config.actionname) > -1 && $("#register-form,#login-form").length > 0 && $(".social-login").length == 0) { 2 + //修改上传的接口调用
  3 +require(['upload'], function (Upload) {
  4 +
  5 + var _onInit = Upload.events.onInit;
  6 + //初始化中完成判断
  7 + Upload.events.onInit = function () {
  8 + _onInit.apply(this, Array.prototype.slice.apply(arguments));
  9 + //如果上传接口不是七牛云,则不处理
  10 + if (this.options.url !== Config.upload.uploadurl) {
  11 + return;
  12 + }
  13 + var _success = this.options.success;
  14 +
  15 + $.extend(this.options, {
  16 + //关闭自动处理队列功能
  17 + autoQueue: false,
  18 + chunkSuccess: function (chunk, file, response) {
  19 + this.contexts = this.contexts ? this.contexts : [];
  20 + this.contexts.push(typeof response.ctx !== 'undefined' ? response.ctx : response.data.ctx);
  21 + },
  22 + chunksUploaded: function (file, done) {
  23 + var that = this;
  24 + var params = $(that.element).data("params") || {};
  25 + var category = typeof params.category !== 'undefined' ? params.category : ($(that.element).data("category") || '');
  26 + category = typeof category === 'function' ? category.call(this, file) : category;
  27 + Fast.api.ajax({
  28 + url: "/addons/qiniu/index/upload",
  29 + data: {
  30 + action: 'merge',
  31 + filesize: file.size,
  32 + filename: file.name,
  33 + chunkid: file.upload.uuid,
  34 + chunkcount: file.upload.totalChunkCount,
  35 + width: file.width || 0,
  36 + height: file.height || 0,
  37 + type: file.type,
  38 + category: category,
  39 + qiniutoken: Config.upload.multipart.qiniutoken,
  40 + contexts: this.contexts
  41 + },
  42 + }, function (data, ret) {
  43 + done(JSON.stringify(ret));
  44 + return false;
  45 + }, function (data, ret) {
  46 + file.accepted = false;
  47 + that._errorProcessing([file], ret.msg);
  48 + return false;
  49 + });
  50 +
  51 + },
  52 + });
  53 +
  54 + //先移除已有的事件
  55 + this.off("success", _success).on("success", function (file, response) {
  56 + var that = this;
  57 + var ret = {code: 0, msg: response};
  58 + try {
  59 + ret = typeof response === 'string' ? JSON.parse(response) : response;
  60 + if (file.xhr.status === 200) {
  61 + if (Config.upload.uploadmode === 'client') {
  62 + if (typeof ret.key !== 'undefined') {
  63 + ret = {code: 1, msg: "", data: {url: '/' + ret.key, hash: ret.hash}};
  64 + }
  65 + }
  66 +
  67 + if (ret.code == 1) {
  68 + var url = ret.data.url || '';
  69 + var params = $(that.element).data("params") || {};
  70 + var category = typeof params.category !== 'undefined' ? params.category : ($(that.element).data("category") || '');
  71 + category = typeof category === 'function' ? category.call(that, file) : category;
  72 + Fast.api.ajax({
  73 + url: "/addons/qiniu/index/notify",
  74 + 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}
  75 + }, function () {
  76 + return false;
  77 + }, function () {
  78 + return false;
  79 + });
  80 + } else {
  81 + console.error(ret);
  82 + }
  83 + } else {
  84 + console.error(file.xhr);
  85 + }
  86 + } catch (e) {
  87 + console.error(e);
  88 + }
  89 + _success.call(this, file, ret);
  90 + });
  91 +
  92 + this.on("addedfile", function (file) {
  93 + var that = this;
  94 + setTimeout(function () {
  95 + if (file.status === 'error') {
  96 + return;
  97 + }
  98 +
  99 + var md5 = ''; //七牛云无需本地获取文件MD5
  100 + var chunk = that.options.chunking && file.size > that.options.chunkSize ? 1 : 0;
  101 + var params = $(that.element).data("params") || {};
  102 + var category = typeof params.category !== 'undefined' ? params.category : ($(that.element).data("category") || '');
  103 + category = typeof category === 'function' ? category.call(that, file) : category;
  104 + Fast.api.ajax({
  105 + url: "/addons/qiniu/index/params",
  106 + 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},
  107 + }, function (data) {
  108 + file.qiniutoken = data.qiniutoken;
  109 + file.params = data;
  110 + file.category = category;
  111 +
  112 + if (file.status != 'error') {
  113 + //开始上传
  114 + that.enqueueFile(file);
  115 + } else {
  116 + that.removeFile(file);
  117 + }
  118 + return false;
  119 + }, function () {
  120 + that.removeFile(file);
  121 + });
  122 + }, 0);
  123 + });
  124 +
  125 + //如果是直传模式
  126 + if (Config.upload.uploadmode === 'client') {
  127 + var _url = this.options.url;
  128 +
  129 + //分片上传时URL链接不同
  130 + this.options.url = function (files) {
  131 + this.options.headers = {"Authorization": "UpToken " + Config.upload.multipart.qiniutoken};
  132 + if (files[0].upload.chunked) {
  133 + var chunk = null;
  134 + files[0].upload.chunks.forEach(function (item) {
  135 + if (item.status === 'uploading') {
  136 + chunk = item;
  137 + }
  138 + });
  139 + if (!chunk) {
  140 + return Config.upload.uploadurl + '/mkfile/' + files[0].size;
  141 + } else {
  142 + return Config.upload.uploadurl + '/mkblk/' + chunk.dataBlock.data.size;
  143 + }
  144 + }
  145 + return _url;
  146 + };
  147 +
  148 + this.options.params = function (files, xhr, chunk) {
  149 + var params = Config.upload.multipart;
  150 + if (chunk) {
  151 + return $.extend({}, params, {
  152 + filesize: chunk.file.size,
  153 + filename: chunk.file.name,
  154 + chunkid: chunk.file.upload.uuid,
  155 + chunkindex: chunk.index,
  156 + chunkcount: chunk.file.upload.totalChunkCount,
  157 + chunkfilesize: chunk.dataBlock.data.size,
  158 + width: chunk.file.width || 0,
  159 + height: chunk.file.height || 0,
  160 + type: chunk.file.type,
  161 + });
  162 + } else {
  163 + var retParams = $.extend({}, params, files[0].params || {});
  164 + //七牛云直传使用的是token参数
  165 + retParams.token = retParams.qiniutoken;
  166 + delete retParams.qiniutoken;
  167 + return retParams;
  168 + }
  169 + };
  170 +
  171 + //分片上传时需要变更提交的内容
  172 + this.on("sending", function (file, xhr, formData) {
  173 + if (file.upload.chunked) {
  174 + var _send = xhr.send;
  175 + xhr.send = function () {
  176 + var chunk = null;
  177 + file.upload.chunks.forEach(function (item) {
  178 + if (item.status == 'uploading') {
  179 + chunk = item;
  180 + }
  181 + });
  182 + if (chunk) {
  183 + _send.call(xhr, chunk.dataBlock.data);
  184 + }
  185 + };
  186 + }
  187 + });
  188 + }
  189 + };
  190 +
  191 +});
  192 +
  193 +if (Config.modulename === 'index' && Config.controllername === 'user' && ['login', 'register'].indexOf(Config.actionname) > -1 && $("#register-form,#login-form").length > 0 && $(".social-login").length == 0) {
3 $("#register-form,#login-form").append(Config.third.loginhtml || ''); 194 $("#register-form,#login-form").append(Config.third.loginhtml || '');
4 } 195 }
5 196