Repository navigation
Expand file tree
/
Copy pathbaseComponent.js
More file actions
185 lines (175 loc) · 4.89 KB
/
Copy pathbaseComponent.js
File metadata and controls
185 lines (175 loc) · 4.89 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import fetch from 'node-fetch';
import Ids from '../models/ids'
import formidable from 'formidable'
import path from 'path'
import fs from 'fs'
import qiniu from 'qiniu'
import gm from 'gm'
qiniu.conf.ACCESS_KEY = 'Ep714TDrVhrhZzV2VJJxDYgGHBAX-KmU1xV1SQdS';
qiniu.conf.SECRET_KEY = 'XNIW2dNffPBdaAhvm9dadBlJ-H6yyCTIJLxNM_N6';
export default class BaseComponent {
constructor(){
this.idList = ['restaurant_id', 'food_id', 'order_id', 'user_id', 'address_id', 'cart_id', 'img_id', 'category_id', 'item_id', 'sku_id', 'admin_id', 'statis_id'];
this.imgTypeList = ['shop', 'food', 'avatar','default'];
this.uploadImg = this.uploadImg.bind(this)
this.qiniu = this.qiniu.bind(this)
}
async fetch(url = '', data = {}, type = 'GET', resType = 'JSON'){
type = type.toUpperCase();
resType = resType.toUpperCase();
if (type == 'GET') {
let dataStr = ''; //数据拼接字符串
Object.keys(data).forEach(key => {
dataStr += key + '=' + data[key] + '&';
})
if (dataStr !== '') {
dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
url = url + '?' + dataStr;
}
}
let requestConfig = {
method: type,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
}
if (type == 'POST') {
Object.defineProperty(requestConfig, 'body', {
value: JSON.stringify(data)
})
}
let responseJson;
try {
const response = await fetch(url, requestConfig);
if (resType === 'TEXT') {
responseJson = await response.text();
}else{
responseJson = await response.json();
}
} catch (err) {
console.log('获取http数据失败', err);
throw new Error(err)
}
return responseJson
}
//获取id列表
async getId(type){
if (!this.idList.includes(type)) {
console.log('id类型错误');
throw new Error('id类型错误');
return
}
try{
const idData = await Ids.findOne();
idData[type] ++ ;
await idData.save();
return idData[type]
}catch(err){
console.log('获取ID数据失败');
throw new Error(err)
}
}
async uploadImg(req, res, next){
const type = req.params.type;
try{
//const image_path = await this.qiniu(req, type);
const image_path = await this.getPath(req);
res.send({
status: 1,
image_path,
})
}catch(err){
console.log('上传图片失败', err);
res.send({
status: 0,
type: 'ERROR_UPLOAD_IMG',
message: '上传图片失败'
})
}
}
async getPath(req){
return new Promise((resolve, reject) => {
const form = formidable.IncomingForm();
form.uploadDir = './public/img';
form.parse(req, async (err, fields, files) => {
let img_id;
try{
img_id = await this.getId('img_id');
}catch(err){
console.log('获取图片id失败');
fs.unlink(files.file.path);
reject('获取图片id失败')
}
const imgName = (new Date().getTime() + Math.ceil(Math.random()*10000)).toString(16) + img_id;
const fullName = imgName + path.extname(files.file.name);
const repath = './public/img/' + fullName;
try{
await fs.rename(files.file.path, repath);
gm(repath)
.resize(200, 200, "!")
.write(repath, async (err) => {
// if(err){
// console.log('裁切图片失败');
// reject('裁切图片失败');
// return
// }
resolve(fullName)
})
}catch(err){
console.log('保存图片失败', err);
fs.unlink(files.file.path)
reject('保存图片失败')
}
});
})
}
async qiniu(req, type = 'default'){
return new Promise((resolve, reject) => {
const form = formidable.IncomingForm();
form.uploadDir = './public/img';
form.parse(req, async (err, fields, files) => {
let img_id;
try{
img_id = await this.getId('img_id');
}catch(err){
console.log('获取图片id失败');
fs.unlink(files.file.path);
reject('获取图片id失败')
}
const imgName = (new Date().getTime() + Math.ceil(Math.random()*10000)).toString(16) + img_id;
const extname = path.extname(files.file.name);
const repath = './public/img/' + imgName + extname;
try{
const key = imgName + extname;
await fs.rename(files.file.path, repath);
const token = this.uptoken('node-elm', key);
const qiniuImg = await this.uploadFile(token.toString(), key, repath);
fs.unlink(repath);
resolve(qiniuImg)
}catch(err){
console.log('保存至七牛失败', err);
fs.unlink(files.file.path)
reject('保存至七牛失败')
}
});
})
}
uptoken(bucket, key){
var putPolicy = new qiniu.rs.PutPolicy(bucket+":"+key);
return putPolicy.token();
}
uploadFile(uptoken, key, localFile){
return new Promise((resolve, reject) => {
var extra = new qiniu.io.PutExtra();
qiniu.io.putFile(uptoken, key, localFile, extra, function(err, ret) {
if(!err) {
resolve(ret.key)
} else {
console.log('图片上传至七牛失败', err);
reject(err)
}
});
})
}
}