Authored by 郝肖肖

'mysql-close'

... ... @@ -13,6 +13,9 @@ const Mysql = require('../../../lib/mysql-promise');
const Promise = require('bluebird');
const r = new Router();
const rp = require('request-promise');
const config = require('../../../config/config');
const singleBrandKeyPre = config.singleBrandKeyPre;
const singleSortKeyPre = config.singleSortKeyPre;
const multiAsync=(multi)=>{
return multi.execAsync().then(function(res) {
... ... @@ -73,6 +76,26 @@ const searchDataValues = async(ctx, value)=>{
return multiAsync(multi);
}
const mgets = async(ctx, item) => {
let redis = ctx.redis;
let operation = [];
item = _.uniq(item);
if (item.length <= 0) {
return Promise.resolve({});
}
return redis.mgetAsync(item).then(d => {
let ddata = {};
_.each(item, (el, index) => {
ddata[el] = d[index];
});
return ddata;
});
};
r.get('/', async(ctx) => {
let resData = {};
let q = ctx.request.query,
... ... @@ -180,16 +203,16 @@ r.get('/syncWord', async(ctx) => {
}).then(ddata => {
return Promise.map(ddata, (word) => insertWord(word, mysql), {concurrency: 15});
}).then(d => {
return mysql.close(d);
}).then(d => {
return ctx.body = {
code: 200,
message: 'success',
data: d
};
});
});
r.get('/expand', async(ctx) => {
let resData = {};
let q = ctx.request.query;
... ... @@ -239,28 +262,48 @@ r.get('/expand', async(ctx) => {
break;
}
return mysql.query(`SELECT COUNT(*) as total FROM seo_keywords WHERE 1 = 1 ${wheres}`, conditions).then(d => {
total = d[0] && d[0].total || 0;
conditions.push((page - 1) * limit, limit);
return mysql.query(`SELECT * FROM seo_keywords WHERE 1 = 1 ${wheres} limit ?, ?`, conditions);
}).then(d => {
resData.pager = pager(Math.floor((total - 1) / limit) + 1, ctx.query);
resData.tabs = _.map(d, (elem) => {
return Object.assign({}, elem, {
is_push: elem.is_push ? '是' : '否',
add_time: elem.add_time && moment(elem.add_time * 1000).format('YYYY-MM-DD HH:mm'),
});
});
let d = await mysql.query(`SELECT COUNT(*) as total FROM seo_keywords WHERE 1 = 1 ${wheres}`, conditions);
total = d[0] && d[0].total || 0;
conditions.push((page - 1) * limit, limit);
resData.typeList = typeList;
resData.typeName = typeName;
resData.type = type;
resData.query = query;
d = await mysql.query(`SELECT * FROM seo_keywords WHERE 1 = 1 ${wheres} limit ?, ?`, conditions);
return ctx.render('action/keywords/expand', resData);
d = await mysql.close(d);
let sortIds = [];
let brandIds = [];
resData.pager = pager(Math.floor((total - 1) / limit) + 1, ctx.query);
_.each(d, (val) => {
if (val.sort_id) {
sortIds.push(`${singleSortKeyPre}${val.sort_id}`);
}
if (val.brand_id) {
brandIds.push(`${singleBrandKeyPre}${val.brand_id}`);
}
});
brandIds = await mgets(ctx, brandIds);
sortIds = await mgets(ctx, sortIds);
resData.tabs = _.map(d, (elem) => {
return Object.assign({}, elem, {
brand: brandIds[`${singleBrandKeyPre}${elem.brand_id}`],
sort: sortIds[`${singleSortKeyPre}${elem.sort_id}`],
is_push: elem.is_push ? '是' : '否',
add_time: elem.add_time && moment(elem.add_time * 1000).format('YYYY-MM-DD HH:mm'),
});
});
resData.typeList = typeList;
resData.typeName = typeName;
resData.type = type;
resData.query = query;
return ctx.render('action/keywords/expand', resData);
});
r.post('/expand/del', async(ctx) => {
... ... @@ -284,6 +327,8 @@ r.post('/expand/del', async(ctx) => {
}
return mysql.query(`DELETE FROM seo_keywords WHERE id IN (${marks.join(',')})`, ids).then(d => {
return mysql.close(d);
}).then(d => {
return ctx.body = {
code: 200,
message: 'success',
... ...
... ... @@ -81,16 +81,16 @@
<label><input type="checkbox" data-id={{id}} >{{id}}</label>
</td>
<td>
<input class="values" value="{{keyword}}" disabled>
{{keyword}}
</td>
<td>
<input class="values" value="{{root}}" disabled>
{{root}}
</td>
<td>
<input class="values" value="{{brand}}" disabled>
{{brand}}
</td>
<td>
<input class="values" value="{{sort}}" disabled>
{{sort}}
</td>
<td>
{{yoho_goods_num}}
... ...
... ... @@ -36,6 +36,19 @@ class MysqlPromise {
return [];
});
}
close(data) {
return this.connect().then(d => {
return d.end();
}).then(d => {
this.conn = null;
return data;
}).catch(err => {
this.conn = null;
console.error(`mysql close, err: ${err.message}`);
return data;
});
}
}
module.exports = MysqlPromise;
\ No newline at end of file
... ...