@sadais/uploader 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,60 @@
1
+ ## 开始
2
+
3
+ ```bash
4
+ # 安装依赖
5
+ npx yarn
6
+ # 监听实时构建`/dist`
7
+ npm run dev
8
+ # 只构建`/dist`一次,不作监听
9
+ npm run build
10
+ ```
11
+
12
+ ### 发布包到私服
13
+
14
+ ```bash
15
+ # 登录npm服务器
16
+ npm login --registry https://registry.npmjs.org
17
+ # 发布
18
+ npm publish
19
+ ```
20
+
21
+ ## 发布之前测试
22
+
23
+ 在当前工程,没有创建组件的单元测试,只能通过把构建后的东西复制到某个项目的 node_modules
24
+
25
+ 比如当前 package.json 的包名是 "@sadais/uploader" , 发布前要在 A 项目中测试 "@sadais/uploader", 在 A 项目的 node_modules 新建 @sadais/uploader , 再把 /dist/ 和 package.json 拷贝到 A 项目的 node_modules/@sadais/uploader
26
+
27
+ 使用
28
+
29
+ ```js
30
+
31
+ import { UploaderInstance,UPLOADER_TYPE } from '@sadais/uploader';
32
+ const uploader = new UploaderInstance(
33
+ {type:UPLOADER_TYPE.ALIYUN,getOptionFun:async (type) => {
34
+ // 获取option示例
35
+ const { head, data } = await apiGetUploadOption(type)
36
+ if (head.ret !== this.$consts.RET_CODE.SUCCESS) return null
37
+ return data
38
+ }}
39
+ )
40
+
41
+ export const apiGetUploadOption = async type => {
42
+ const params = {
43
+ storageenum: type
44
+ }
45
+ const { data } = await request.get('/api/manage/v1/storage/getstoragesignature', params)
46
+ return data
47
+ }
48
+ ```
49
+
50
+ ## 发布完成后
51
+
52
+ 在 A 项目安装
53
+
54
+ ```bash
55
+ # use yarn
56
+ npx yarn add @sadais/uploader -S
57
+
58
+ # use npm or pnpm
59
+ npx pnpm install @sadais/uploader -S
60
+ ```
@@ -0,0 +1,265 @@
1
+ /*
2
+ * @Author: fuwenlong
3
+ * @Date: 2022-01-14 14:22:39
4
+ * @LastEditTime: 2022-01-18 16:20:57
5
+ * @LastEditors: zhangzhenfei
6
+ * @Description: 文件上传
7
+ */
8
+
9
+ // 对象存储类型
10
+ const UPLOADER_TYPE = {
11
+ ALIYUN: 'ALIYUN', // 阿里云
12
+ TXYUN: 'TXYUN', // 腾讯云
13
+ HUAWEI: 'HUAWEI', // 华为云
14
+ };
15
+
16
+ const DEFAULT_OPTIONS = {
17
+ // type: UPLOADER_TYPE.ALIYUN, //
18
+ // accessKeyId: 'LTAIXeOzClQBtKs3',
19
+ // bucketName: 'sadais-oss',
20
+ // endPoint: 'https://oss-cn-hangzhou.aliyuncs.com',
21
+ // path: 'file',
22
+ // signature: '6Y5l5k56y9VgxDU1vhsyCLC99QM=',
23
+ // domain: 'https://m.antibao.cn',
24
+ // policy:'',
25
+ // timeOut: 1642476191415
26
+ };
27
+
28
+ /**
29
+ * @param {String} params.type
30
+ * @param {String} params.getOptionFun
31
+ */
32
+ const UploaderInstance = (function () {
33
+ let instance;
34
+ return function (params) {
35
+ if (!instance) {
36
+ instance = new SadaisUploader(params);
37
+ }
38
+ return instance;
39
+ };
40
+ })();
41
+
42
+ class SadaisUploader {
43
+ constructor(params) {
44
+ this._init(params);
45
+ }
46
+
47
+ /**
48
+ * 初始化
49
+ * @param {Object} options
50
+ */
51
+ async _init(params) {
52
+ this.params = params;
53
+ this._initOptions(params);
54
+ }
55
+
56
+ /**
57
+ * 获取配置信息
58
+ */
59
+ async _initOptions() {
60
+ let { type, getOptionFun } = this.params;
61
+
62
+ if (getOptionFun) {
63
+ const options = await getOptionFun(type);
64
+ if (!options) return;
65
+ if (options.path && !options.path.endsWith('/')) {
66
+ options.path = options.path + '/';
67
+ }
68
+ this.options = { ...options, type };
69
+ }
70
+ }
71
+
72
+ /**
73
+ * 是否超时
74
+ */
75
+ _isTimeOut() {
76
+ // 比服务器timeOut时间提前10s过期
77
+ const timeout = this.options.timeOut - 1000 * 10;
78
+ return new Date().getTime() > timeout;
79
+ }
80
+
81
+ /**
82
+ * 单文件上传
83
+ * @param {File} file
84
+ * @return Promise
85
+ */
86
+ async uploadFile(file) {
87
+ // 超时判断
88
+ if (this._isTimeOut()) {
89
+ await this._initOptions();
90
+ }
91
+ if (!file.name && file.path) {
92
+ const suffix = file.path.substring(file.path.lastIndexOf('.'));
93
+ file.name = this._guid() + suffix;
94
+ }
95
+ return new Promise((resolve, reject) => {
96
+ const { domain } = this.options;
97
+
98
+ // 请求地址
99
+ const formData = this._buildParams(file);
100
+
101
+ const apiUrl = formData.url;
102
+ const uploadedFilePath = `${domain}${
103
+ domain.endsWith('/') ? '' : '/'
104
+ }${formData.key}`;
105
+ const success = {
106
+ url: uploadedFilePath,
107
+ success: 1,
108
+ errMsg: 'success',
109
+ };
110
+ const error = {
111
+ success: 0,
112
+ url: file.name,
113
+ errMsg: 'fail',
114
+ };
115
+
116
+ try {
117
+ // uniapp
118
+ uni.uploadFile({
119
+ url: apiUrl,
120
+ filePath: file.path, // uni api提供的blob格式图片地址
121
+ name: 'file',
122
+ formData,
123
+ success: (res) => {
124
+ const { statusCode } = res;
125
+ if (statusCode == 200) {
126
+ resolve(success);
127
+ } else {
128
+ error.errMsg = res;
129
+ reject(error);
130
+ }
131
+ },
132
+ fail: (error) => {
133
+ console.log(error);
134
+ reject(error);
135
+ },
136
+ });
137
+ // H5 input:file接收内容
138
+ } catch (e) {
139
+ console.log('uni报错,调用XMLHttpRequest', e);
140
+ const data = new FormData();
141
+ Object.keys(formData).forEach((key) => {
142
+ data.append(key, formData[key]);
143
+ });
144
+ data.append('file', file);
145
+ const xhr = new XMLHttpRequest();
146
+ xhr.open('POST', apiUrl, true);
147
+ xhr.onreadystatechange = () => {
148
+ if (xhr.readyState === XMLHttpRequest.DONE) {
149
+ if (xhr.status === 200) {
150
+ resolve(success);
151
+ } else {
152
+ reject(error);
153
+ }
154
+ }
155
+ };
156
+ xhr.send(data);
157
+ }
158
+ });
159
+ }
160
+
161
+ /**
162
+ * 多文件上传
163
+ * @param {Array<File>} files
164
+ * @return Promise
165
+ */
166
+ async uploadFiles(files) {
167
+ const successUrls = [];
168
+ const failUrls = [];
169
+ for (const file of files) {
170
+ const { success, url } = await this.uploadFile(file);
171
+ if (success === 1) {
172
+ successUrls.push(url);
173
+ } else {
174
+ failUrls.push(url);
175
+ }
176
+ }
177
+ return { successUrls, failUrls };
178
+ }
179
+
180
+ // 按类型组装过参数
181
+ _buildParams(file) {
182
+ const opt = this.options;
183
+ const type = opt.type;
184
+ const params = {
185
+ name: file.name,
186
+ policy: opt.policy,
187
+ success_action_status: '200',
188
+ key: this._getUploadFilePath(file.name),
189
+ };
190
+ const endPoint = opt.endPoint
191
+ .replace('https://', '')
192
+ .replace('http://', '');
193
+ if (UPLOADER_TYPE.TXYUN === type) {
194
+ params['q-ak'] = opt.accessKeyId;
195
+ params['q-signature'] = opt.signature; // 签名
196
+ params['q-sign-algorithm'] = 'sha1'; // 签名算法
197
+ params['q-key-time'] = opt.param['q-sign-time'];
198
+ // params['q-sign-time'] = opt.param['q-sign-time']
199
+ params.url = `https://${opt.bucketName}.cos.${endPoint}.myqcloud.com`;
200
+ } else if (UPLOADER_TYPE.HUAWEI === type) {
201
+ params.AccessKeyId = opt.accessKeyId;
202
+ params.signature = opt.signature;
203
+ params.url = `https://${opt.bucketName}.obs.${endPoint}.myhuaweicloud.com`;
204
+ } else {
205
+ params.signature = opt.signature;
206
+ params.OSSAccessKeyId = opt.accessKeyId;
207
+ params.bucket = opt.bucketName;
208
+
209
+ params.url = `https://${opt.bucketName}.${endPoint}`;
210
+ }
211
+ return params;
212
+ }
213
+
214
+ /**
215
+ * 获取上传文件路径
216
+ * @param {String} name 文件名
217
+ * @returns
218
+ */
219
+ _getUploadFilePath(name) {
220
+ const { useOriginalName, path } = this.options;
221
+ const lastIndexOf = name.lastIndexOf('.');
222
+ const originalName = name.substring(0, lastIndexOf);
223
+ const suffix = name.substring(lastIndexOf + 1);
224
+ const fileName = useOriginalName ? `/${originalName}` : '';
225
+ // 为了保证使用文件原名时,文件不被覆盖,仍然使用随机生成方式
226
+ // ${this._getNowDate()}/
227
+ return `${path}${this._getNowDate()}/${this._guid()}${fileName}.${suffix}`;
228
+ }
229
+
230
+ /**
231
+ * 获取当前日期
232
+ * @returns YYYY-MM-DD
233
+ */
234
+ _getNowDate() {
235
+ const date = new Date();
236
+ const year = date.getFullYear();
237
+ let month = date.getMonth() + 1;
238
+ let day = date.getDate();
239
+ if (month < 10) {
240
+ month = `0${month}`;
241
+ }
242
+ if (day < 10) {
243
+ day = `0${day}`;
244
+ }
245
+
246
+ return `${year}-${month}-${day}`;
247
+ }
248
+
249
+ /**
250
+ * 生成guid,用于生成随机文件名
251
+ * @return String
252
+ */
253
+ _guid() {
254
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(
255
+ /[xy]/g,
256
+ function (c) {
257
+ var r = (Math.random() * 16) | 0,
258
+ v = c == 'x' ? r : (r & 0x3) | 0x8;
259
+ return v.toString(16);
260
+ }
261
+ );
262
+ }
263
+ }
264
+
265
+ export { DEFAULT_OPTIONS, UPLOADER_TYPE, UploaderInstance };
@@ -0,0 +1 @@
1
+ "use strict";function e(e,n){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!r){if(Array.isArray(e)||(r=function(e,n){if(!e)return;if("string"==typeof e)return t(e,n);var r=Object.prototype.toString.call(e).slice(8,-1);"Object"===r&&e.constructor&&(r=e.constructor.name);if("Map"===r||"Set"===r)return Array.from(e);if("Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r))return t(e,n)}(e))||n&&e&&"number"==typeof e.length){r&&(e=r);var a=0,o=function(){};return{s:o,n:function(){return a>=e.length?{done:!0}:{done:!1,value:e[a++]}},e:function(e){throw e},f:o}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var i,c=!0,u=!1;return{s:function(){r=r.call(e)},n:function(){var e=r.next();return c=e.done,e},e:function(e){u=!0,i=e},f:function(){try{c||null==r.return||r.return()}finally{if(u)throw i}}}}function t(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function n(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function r(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?n(Object(r),!0).forEach((function(t){a(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):n(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function a(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function o(e,t,n,r,a,o,i){try{var c=e[o](i),u=c.value}catch(e){return void n(e)}c.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var i=e.apply(t,n);function c(e){o(i,r,a,c,u,"next",e)}function u(e){o(i,r,a,c,u,"throw",e)}c(void 0)}))}}function c(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}Object.defineProperty(exports,"__esModule",{value:!0});var u,s={ALIYUN:"ALIYUN",TXYUN:"TXYUN",HUAWEI:"HUAWEI"},p=function(e){return u||(u=new l(e)),u},l=function(){function t(e){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,t),this._init(e)}var n,a,o,u,p,l,f;return n=t,a=[{key:"_init",value:(f=i(regeneratorRuntime.mark((function e(t){return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:this.params=t,this._initOptions(t);case 2:case"end":return e.stop()}}),e,this)}))),function(e){return f.apply(this,arguments)})},{key:"_initOptions",value:(l=i(regeneratorRuntime.mark((function e(){var t,n,a,o;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(t=this.params,n=t.type,!(a=t.getOptionFun)){e.next=9;break}return e.next=4,a(n);case 4:if(o=e.sent){e.next=7;break}return e.abrupt("return");case 7:o.path&&!o.path.endsWith("/")&&(o.path=o.path+"/"),this.options=r(r({},o),{},{type:n});case 9:case"end":return e.stop()}}),e,this)}))),function(){return l.apply(this,arguments)})},{key:"_isTimeOut",value:function(){var e=this.options.timeOut-1e4;return(new Date).getTime()>e}},{key:"uploadFile",value:(p=i(regeneratorRuntime.mark((function e(t){var n,r=this;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!this._isTimeOut()){e.next=3;break}return e.next=3,this._initOptions();case 3:return!t.name&&t.path&&(n=t.path.substring(t.path.lastIndexOf(".")),t.name=this._guid()+n),e.abrupt("return",new Promise((function(e,n){var a=r.options.domain,o=r._buildParams(t),i=o.url,c={url:"".concat(a).concat(a.endsWith("/")?"":"/").concat(o.key),success:1,errMsg:"success"},u={success:0,url:t.name,errMsg:"fail"};try{uni.uploadFile({url:i,filePath:t.path,name:"file",formData:o,success:function(t){200==t.statusCode?e(c):(u.errMsg=t,n(u))},fail:function(e){console.log(e),n(e)}})}catch(r){console.log("uni报错,调用XMLHttpRequest",r);var s=new FormData;Object.keys(o).forEach((function(e){s.append(e,o[e])})),s.append("file",t);var p=new XMLHttpRequest;p.open("POST",i,!0),p.onreadystatechange=function(){p.readyState===XMLHttpRequest.DONE&&(200===p.status?e(c):n(u))},p.send(s)}})));case 5:case"end":return e.stop()}}),e,this)}))),function(e){return p.apply(this,arguments)})},{key:"uploadFiles",value:(u=i(regeneratorRuntime.mark((function t(n){var r,a,o,i,c,u,s,p;return regeneratorRuntime.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:r=[],a=[],o=e(n),t.prev=3,o.s();case 5:if((i=o.n()).done){t.next=15;break}return c=i.value,t.next=9,this.uploadFile(c);case 9:u=t.sent,s=u.success,p=u.url,1===s?r.push(p):a.push(p);case 13:t.next=5;break;case 15:t.next=20;break;case 17:t.prev=17,t.t0=t.catch(3),o.e(t.t0);case 20:return t.prev=20,o.f(),t.finish(20);case 23:return t.abrupt("return",{successUrls:r,failUrls:a});case 24:case"end":return t.stop()}}),t,this,[[3,17,20,23]])}))),function(e){return u.apply(this,arguments)})},{key:"_buildParams",value:function(e){var t=this.options,n=t.type,r={name:e.name,policy:t.policy,success_action_status:"200",key:this._getUploadFilePath(e.name)},a=t.endPoint.replace("https://","").replace("http://","");return s.TXYUN===n?(r["q-ak"]=t.accessKeyId,r["q-signature"]=t.signature,r["q-sign-algorithm"]="sha1",r["q-key-time"]=t.param["q-sign-time"],r.url="https://".concat(t.bucketName,".cos.").concat(a,".myqcloud.com")):s.HUAWEI===n?(r.AccessKeyId=t.accessKeyId,r.signature=t.signature,r.url="https://".concat(t.bucketName,".obs.").concat(a,".myhuaweicloud.com")):(r.signature=t.signature,r.OSSAccessKeyId=t.accessKeyId,r.bucket=t.bucketName,r.url="https://".concat(t.bucketName,".").concat(a)),r}},{key:"_getUploadFilePath",value:function(e){var t=this.options,n=t.useOriginalName,r=t.path,a=e.lastIndexOf("."),o=e.substring(0,a),i=e.substring(a+1),c=n?"/".concat(o):"";return"".concat(r).concat(this._getNowDate(),"/").concat(this._guid()).concat(c,".").concat(i)}},{key:"_getNowDate",value:function(){var e=new Date,t=e.getFullYear(),n=e.getMonth()+1,r=e.getDate();return n<10&&(n="0".concat(n)),r<10&&(r="0".concat(r)),"".concat(t,"-").concat(n,"-").concat(r)}},{key:"_guid",value:function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(function(e){var t=16*Math.random()|0;return("x"==e?t:3&t|8).toString(16)}))}}],a&&c(n.prototype,a),o&&c(n,o),Object.defineProperty(n,"prototype",{writable:!1}),t}();exports.DEFAULT_OPTIONS={},exports.UPLOADER_TYPE=s,exports.UploaderInstance=p;
package/package.json ADDED
@@ -0,0 +1,96 @@
1
+ {
2
+ "name": "@sadais/uploader",
3
+ "version": "0.1.2",
4
+ "main": "dist/index.min.js",
5
+ "module": "dist/index.es.js",
6
+ "keywords": [
7
+ "sadais"
8
+ ],
9
+ "author": "sadais",
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "scripts": {
14
+ "_postinstall": "husky install",
15
+ "prepublishOnly": "pinst --disable",
16
+ "postpublish": "pinst --enable",
17
+ "prepublish": "npm run build",
18
+ "dev": "rollup -c rollup.config.js -w",
19
+ "build": "rollup -c rollup.config.js",
20
+ "deploy": "bash deploy.sh",
21
+ "updateTheme": "npm uninstall vuepress-theme-vdoing && rm -rf node_modules && npm i && npm i vuepress-theme-vdoing -D",
22
+ "lint": "vue-cli-service lint"
23
+ },
24
+ "dependencies": {},
25
+ "devDependencies": {
26
+ "@commitlint/cli": "^13.1.0",
27
+ "@commitlint/config-conventional": "^13.1.0",
28
+ "@rollup/plugin-babel": "^5.3.0",
29
+ "@rollup/plugin-commonjs": "^19.0.1",
30
+ "@rollup/plugin-node-resolve": "^13.0.2",
31
+ "@vue/babel-helper-vue-jsx-merge-props": "^1.2.1",
32
+ "@vue/babel-preset-jsx": "^1.2.4",
33
+ "@vue/babel-sugar-inject-h": "^1.2.2",
34
+ "@vue/cli-plugin-babel": "^4.5.15",
35
+ "@vue/cli-plugin-eslint": "^4.5.13",
36
+ "@vue/cli-service": "^4.5.15",
37
+ "@vue/compiler-sfc": "^3.1.5",
38
+ "@vue/eslint-config-prettier": "^6.0.0",
39
+ "babel-eslint": "^10.1.0",
40
+ "change-case": "^4.1.2",
41
+ "dayjs": "^1.9.7",
42
+ "eslint": "^6.7.2",
43
+ "eslint-plugin-prettier": "^3.3.1",
44
+ "eslint-plugin-vue": "^6.2.2",
45
+ "glob": "^7.1.7",
46
+ "husky": "^7.0.1",
47
+ "inquirer": "^7.1.0",
48
+ "json2yaml": "^1.1.0",
49
+ "lint-staged": "^11.1.1",
50
+ "pinst": "^2.1.6",
51
+ "plupload": "^2.3.9",
52
+ "prettier": "^2.3.2",
53
+ "rimraf": "^3.0.2",
54
+ "rollup": "^2.53.3",
55
+ "rollup-plugin-terser": "^7.0.2"
56
+ },
57
+ "license": "MIT",
58
+ "eslintConfig": {
59
+ "root": true,
60
+ "env": {
61
+ "node": true
62
+ },
63
+ "extends": [
64
+ "plugin:vue/essential",
65
+ "eslint:recommended",
66
+ "@vue/prettier"
67
+ ],
68
+ "parserOptions": {
69
+ "parser": "babel-eslint"
70
+ },
71
+ "rules": {
72
+ "max-lines": [
73
+ "warn",
74
+ {
75
+ "max": 400
76
+ }
77
+ ],
78
+ "max-params": [
79
+ "error",
80
+ {
81
+ "max": 3
82
+ }
83
+ ]
84
+ }
85
+ },
86
+ "lint-staged": {
87
+ "*.{js,vue,jsx}": [
88
+ "prettier --write",
89
+ "vue-cli-service lint",
90
+ "git add"
91
+ ]
92
+ },
93
+ "publishConfig": {
94
+ "registry": "https://registry.npmjs.org"
95
+ }
96
+ }