product_cache.js
1.41 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
/**
*
* @author: chenfeng<feng.chen@yoho.cn>
* @date: 16/10/19
*/
'use strict';
import Router from 'koa-router';
import xlsx from 'node-xlsx';
import fs from 'fs';
import {
ProductCache
} from '../../models'
const r = new Router();
const productCache = {
async query(ctx) {
await ctx.render('action/product_cache');
},
async clear(ctx) {
let excelFile = ctx.request.body._files && ctx.request.body._files.excelFile;
let type = ctx.request.body.type;
if (excelFile && type) {
let sheets = xlsx.parse(fs.readFileSync(excelFile.path));
if (sheets.length) {
let rows = sheets[0].data;
if (rows.length > 1) {
let data = [];
for (var i = 1; i < rows.length; i++) {
var row = rows[i];
if (row.length) {
data.push(row[0]);
}
}
if (type === '1') {
ProductCache.removePriceCache(data)
} else if (type === '2') {
ProductCache.removeProductCache(data)
}
}
}
}
ctx.body = {
code: 200
}
}
}
r.get('/query', productCache.query);
r.post('/clear', productCache.clear);
export default r;