-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.js
311 lines (266 loc) · 12.6 KB
/
request.js
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
var evt = require('events');
var random = require("random-js")(); // uses the nativeMath engine
request.prototype = new evt.EventEmitter();
var fs = require('fs');
var xml = require('xml2js');
var a ='k'
function request(){
console.log('request constructor');
function readConfig(){
config = JSON.parse(fs.readFileSync('config.json','utf8'));
console.log(config);
return config;
}
this.config = readConfig();
}
var queue_order = [];
request.prototype.run_request = function( res ){
console.log('run_request', res);
var evt = require('events');
var datatime = require('node-datetime');
var Client = require('ftp');
var c = new Client();
fs.exists('log_file_request.txt',(exists) =>{
if (exists){
var fd = fs.openSync('log_file_request.txt', 'r')
fs.readFile('log_file_request.txt',{encoding:'utf-8'},(err,data) => {
if (err) throw err;
var arr = data.split('\n')
var length = arr.length;
if (length > 7000){
logStream = fs.createWriteStream('log_file_request.txt',{encoding:'utf-8'});
arr.slice(arr.length - 4000,arr.length).forEach((item,index) => {
logStream.write(item.toString() +'\n');
}
)
emt.emit('logComplite')
}else{
logStream = fs.createWriteStream('log_file_request.txt',{encoding:'utf-8',flags:'a'})
emt.emit('logComplite')
}
})
}else{
fs.createWriteStream('log_file_request.txt',{encoding:'utf-8',flags:'a'})
emt.emit('logComplite')
}
})
var emt = new evt.EventEmitter();
var c = new Client();
var fileCount;
var closeCount=0;
// var config = readConfig();
var ftpconfig = {
host: this.config.host,
user: this.config.user,
password: this.config.password,
};
c.on('error',(error)=>{
logStream.write(datatime.create().format('Y-m-d H:M:S')+': Неудалось подключиться к FTP серверу, проверьте настройки конфигурации. Request!'+ error + '\n')
console.log('Ошибка соединения',error);
queue_order.push(res)
})
emt.on('logComplite',() => {
try{
c.connect(ftpconfig);
// c.status((err,status) => {
// console.log(err,status);
// })
}catch(err){
logStream.write(datatime.create().format('Y-m-d H:M:S')+': Неудалось подключиться к ftp серверу \n');
}
})
c.on('close',()=>{
console.log('Соединение с ftp успешно закрыто')
})
c.on('ready',function(){
c.pwd( (err,dir)=>{
try{
if (err) throw err;
if (dir.indexOf('ftpOrders')>0){
console.log(dir)
emt.emit('cwdComplete')
// if queue_order
}else{
c.cwd('ftpOrders',function(err,currentDir){
try{
if (err) throw err;
emt.emit('cwdComplete');
}catch(err){
console.log(datatime.create().format('Y-m-d H:M:S')+ ': Неудалось изменить папку на FTP', err)
}
})
}
}catch(error){
console.log(datatime.create().format('Y-m-d H:M:S')+ ': Неудалось получить текущую директорию на FTP', error)
}
});
});
// c.on('greeting', (msg) => {
// console.log('Приветствие',msg);
// })
emt.on('fileComplete',(filename) =>{
console.log(filename, " Файл готов к копированию")
c.put('Orders/' + filename, filename, function(err) {
try{
if (err) throw err;
logStream.write(datatime.create().format('Y-m-d H:M:S') + " Заказ добавлен на FTP \n")
c.end();
//Удаление локальных файлов
fs.unlink('./Orders/'+filename,err=> {
try{
if (err) throw err
}catch(error){
logStream.write(datatime.create().format('Y-m-d H:M:S') + 'Неудалось удалить локальный файл ' + file +' ' + error+ '\n');
}
})
// fs.readdir('./Orders',(err,files)=>{
// console.log('Количество локальных файлов', files.length)
// if (files.length > 200){
// files.forEach( (file, i)=> {
// console.log(file)
// if (i < 80){
// fs.unlink('./Orders/'+file,err=> {
// try{
// if (err) throw err
// }catch(error){
// logStream.write(datatime.create().format('Y-m-d H:M:S') + 'Неудалось удалить локальный файл ' + file +' ' + error+ '\n');
// }
// })
// }
// else{
// }
// })
// }
// files.forEach( file => {
// var oldDate = datatime.create(file.slice(6,16));
// var nowDate = datatime.create();
// console.log(nowDate.getTime() - oldDate.getTime())
// if (file.indexOf('.xml')>0 && (nowDate.getTime()-oldDate.getTime())>172800000){
// fs.unlink('./Orders/'+file,err=> {
// try{
// if (err) throw err
// }catch(error){
// logStream.write(datatime.create().format('Y-m-d H:M:S') + 'Неудалось удалить локальный файл ' + file +' ' + error+ '\n');
// }
// })
// }
// })
// })
}catch(err){
logStream.write("Неудалось скопировать файл "+filename+' на FTP сервер.\n')
}
})
})
var dbclient = require('./dbclient')
var builder = new xml.Builder( { rootName : 'Order', headless : true });
emt.on('cwdComplete',() =>{
try{
// var str = '<root>\n';
console.log('Полученный заказ')
console.log(res);
console.log('Length =',res.length);
let order = {
order_id: res.order_id,
date: datatime.create().format('Y-m-d H:M:S'),
store_id: res.store_id,
store_name: res.store_name,
customer_id: res.customer_id,
firstname: res.firstname,
lastname: res.lastname,
telephone: res.telephone,
shipping_company: res.shipping_company,
email: res.email,
comment: res.comment,
// payment_city: res.payment_city,
// payment_address_1: res.payment_address_1,
// payment_address_2: res.payment_address_2,
// payment_custom_field: res.payment_custom_field,
// payment_postcode: res.payment_postcode,
shipping_city: res.shipping_city,
shipping_address_1: res.shipping_address_1,
// shipping_address_2: res.shipping_address_2,
// shipping_custom_field: res.shipping_custom_field,
customer_address: res.customer_address,
comment_for_address: res.comment_for_address,
shipping_postcode: res.shipping_postcode,
products: res.products,
totals: res.totals,
}
// console.log('Order ', order);
// order.forEach((element, i) => {
var x = builder.buildObject(order);
// str = str + x + '\n';
// console.log(order.lastname)
// if (i ==order.length - 1){
// str = str + '</root>\n';
var data = datatime.create().format('Y-m-d H:M:S');
// Тест работы добавление заказа
var value = random.integer(1, 1000000);
// var filename = 'order_'+value+'.xml';
var filename = 'order_'+res.order_id+'.xml';
// console.log(x);
fs.writeFile('Orders/' + filename,x,(err) =>{
try{
if (err) throw err;
emt.emit('fileComplete',filename)
}catch(err){
logStream.write(datatime.create().format('Y-m-d H:M:S')+'Неудалось сформировать XML файл. '+err+'\n')
}
})
}catch(error){
logStream.write(datatime.create().format('Y-m-d H:M:S')+'Неудалось сформировать XML файл. \n')
}
}
)
}
request.prototype.exportTree = function(){
var dbclient = require('./dbclient');
var xml = require('xml2js');
var builder = new xml.Builder( { rootName : 'categoryList', headless : true });
dbclient.exportTree().then( (res) =>{
var x = builder.buildObject(res);
console.log(x);
})
}
request.prototype.createXmlFileOrder = function( res ){
var builder = new xml.Builder( { rootName : 'Order', headless : true });
let order = {
order_id: res.order_id,
date: datatime.create().format('Y-m-d H:M:S'),
store_id: res.store_id,
store_name: res.store_name,
customer_id: res.customer_id,
firstname: res.firstname,
lastname: res.lastname,
telephone: res.telephone,
shipping_company: res.shipping_company,
email: res.email,
comment: res.comment,
// payment_city: res.payment_city,
// payment_address_1: res.payment_address_1,
// payment_address_2: res.payment_address_2,
// payment_custom_field: res.payment_custom_field,
// payment_postcode: res.payment_postcode,
shipping_city: res.shipping_city,
shipping_address_1: res.shipping_address_1,
// shipping_address_2: res.shipping_address_2,
// shipping_custom_field: res.shipping_custom_field,
customer_address: res.customer_address,
comment_for_address: res.comment_for_address,
shipping_postcode: res.shipping_postcode,
products: res.products,
totals: res.totals,
}
var x = builder.buildObject(order);
var data = datatime.create().format('Y-m-d H:M:S');
var filename = 'order_'+res.order_id+'.xml';
fs.writeFile('Orders/' + filename,x,(err) =>{
try{
if (err) throw err;
return filename;
}catch(err){
logStream.write(datatime.create().format('Y-m-d H:M:S')+'Неудалось сформировать XML файл. '+err+'\n')
}
})
}
module.exports = new request();