...
|
...
|
@@ -6,6 +6,7 @@ const md5 = require('md5'); |
|
|
const moment = require('moment');
|
|
|
const pager = require('../utils/pager');
|
|
|
const seoModel = require('../models/seoModel');
|
|
|
const xlsx = require('xlsx');
|
|
|
|
|
|
let r = new Router();
|
|
|
|
...
|
...
|
@@ -149,7 +150,6 @@ const tdk = { |
|
|
|
|
|
ctx.response.body = result;
|
|
|
},
|
|
|
|
|
|
edit: async(ctx, next) => {
|
|
|
let result = {code: 500, message: '非法参数'};
|
|
|
|
...
|
...
|
@@ -217,6 +217,76 @@ const tdk = { |
|
|
}
|
|
|
|
|
|
ctx.response.body = result;
|
|
|
},
|
|
|
upload: async(ctx, next) => {
|
|
|
// console.log(ctx.request.body);
|
|
|
let result = {code: 500, message: '非法参数'};
|
|
|
if (ctx.request.body._files) {
|
|
|
let file = ctx.request.body._files.up_excel;
|
|
|
const workbook = xlsx.readFile(file.path);
|
|
|
// console.log('workbook=',workbook);
|
|
|
const sheetNames = workbook.Props.SheetNames;
|
|
|
const worksheet = workbook.Sheets[sheetNames[0]];
|
|
|
|
|
|
let json_data = xlsx.utils.sheet_to_json(worksheet);
|
|
|
|
|
|
let post_data = [];
|
|
|
let success = 0;
|
|
|
|
|
|
// 处理EXCEL数据,并插入redis
|
|
|
let createRedisData = async function (ctx, data, index) {
|
|
|
if (index < data.length) {
|
|
|
let obj = data[index];
|
|
|
let type = obj.type || '';
|
|
|
let key = obj.key || '';
|
|
|
let title = obj.title || '';
|
|
|
let keywords = obj.keywords || '';
|
|
|
let description = obj.description || '';
|
|
|
// console.log(obj, index);
|
|
|
|
|
|
if (_.find(TYPE_LIST, ['type', type]) && key) {
|
|
|
// console.log('type: ', type, key);
|
|
|
let hashKey = key;
|
|
|
if (type === 'url') {
|
|
|
key = _.replace(key, /http[s]?:\/\//, '');
|
|
|
hashKey = md5(key);
|
|
|
}
|
|
|
|
|
|
//先删除掉相同值的字段
|
|
|
let exist = await ctx.redis.multi([['lrem', `tdk:${type}:links`, 1, hashKey]]).execAsync();
|
|
|
|
|
|
//插入List, 插入数据
|
|
|
let res = await ctx.redis.multi([
|
|
|
['lpushx', `tdk:${type}:links`, hashKey],
|
|
|
['hmset', `tdk:${type}:${hashKey}`, 'key', key, 'title', title, 'keywords', keywords,
|
|
|
'description', description, 'modify_time', Date.parse(new Date()) / 1000]
|
|
|
]).execAsync();
|
|
|
|
|
|
if (res[1]) {
|
|
|
if (!res[0]) {
|
|
|
ctx.redis.lpush(`tdk:${type}:links`, hashKey);
|
|
|
}
|
|
|
}
|
|
|
success += 1;
|
|
|
return createRedisData(ctx, data, index + 1);
|
|
|
|
|
|
} else {
|
|
|
return createRedisData(ctx, data, index + 1);
|
|
|
}
|
|
|
} else {
|
|
|
result = {
|
|
|
code: 200,
|
|
|
data: data,
|
|
|
message: `导入完成${success}条`
|
|
|
};
|
|
|
console.log(result);
|
|
|
ctx.response.body = result;
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
return createRedisData(ctx, json_data, 0);
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
...
|
...
|
@@ -720,6 +790,7 @@ r.get('/tdk', tdk.index); |
|
|
r.post('/tdk/add', tdk.add);
|
|
|
r.post('/tdk/edit', tdk.edit);
|
|
|
r.post('/tdk/delete', tdk.delete);
|
|
|
r.post('/tdk/upload', tdk.upload);
|
|
|
|
|
|
// 词根管理
|
|
|
r.get('/rootwords', rootWords.index);
|
...
|
...
|
|