Commit e9e5a9f5 authored by Hsinli's avatar Hsinli

addd

parent bd985926
...@@ -10,11 +10,19 @@ class searchHistoryController extends Controller { ...@@ -10,11 +10,19 @@ class searchHistoryController extends Controller {
async getSearchHistory() { async getSearchHistory() {
const { ctx } = this; const { ctx } = this;
//获取租房2和新房1的搜索记录 //获取租房2和新房1的搜索记录
let inputParams = ctx.params;
const rule = {
city_code: {
type: 'string',
required: true,
}
};
ctx.validate(rule, inputParams);
let ret = {}; let ret = {};
let type = [1, 2]; let type = [1, 2];
for (let i in type) { for (let i in type) {
let tag = type[i] === 1 ? 'new_house' : 'rental_house'; let tag = type[i] === 1 ? 'new_house' : 'rental_house';
ret[tag] = await ctx.service.house.v2.searchHistory.getSearchHistoryList(type[i]); ret[tag] = await ctx.service.house.v2.searchHistory.getSearchHistoryList({ type: type[i], city_code: Number(inputParams.city_code) });
} }
ctx.success(ret); ctx.success(ret);
......
'use strict';
const moment = require('moment');
module.exports = app => {
const { STRING, INTEGER, ENUM, DATE } = app.Sequelize;
const HotSearch = app.realestateModel.define('hot_search', {
id: {
type: INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
text: {
type: STRING,
allowNull: true
},
city: {
type: INTEGER,
allowNull: true
},
type: {
type: INTEGER,
allowNull: true
},
valid: {
type: INTEGER,
allowNull: true
},
created_at: {
type: DATE,
get() {
const date = this.getDataValue('created_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
updated_at: {
type: DATE,
get() {
const date = this.getDataValue('updated_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
deleted_at: {
type: DATE,
get() {
const date = this.getDataValue('deleted_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
}, {
timestamps: false,
tableName: 'hot_search',
});
HotSearch.one = async (data) => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
return await SearchHistory.findOne({
attributes: attributes,
where: where,
});
}
HotSearch.all = async (data) => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const order = data.order ? data.order : [];
return await HotSearch.findAll({
attributes: attributes,
where: where,
order,
});
}
HotSearch.list = async (data = {}) => {
const limit = data.limit ? data.limit : 10;
const page = data.page ? data.page : 1;
const order = data.order ? data.order : [];
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const condition = {
offset: (page - 1) * limit,
limit,
where: where,
order: order,
attributes: attributes,
};
const { count, rows } = await HotSearch.findAndCountAll(condition);
return { page, count, rows };
}
HotSearch.add = async (data) => {
try {
//返回promise对象实力 instance
const res = await HotSearch.create(data);
//从promise 实例中中获得需要的id号,id 必须是自增长,而且必须主键,否则返回null
return res.id;
} catch (error) {
throw (error);
}
}
HotSearch.edit = async (data) => {
const where = data.where;
const params = data.params;
try {
return await HotSearch.update(params, { where: where })
} catch (error) {
throw (error);
}
}
return HotSearch;
};
...@@ -74,7 +74,7 @@ module.exports = app => { ...@@ -74,7 +74,7 @@ module.exports = app => {
router.get('/v2/order/list', loginAuth, 'house.v2.order.getOrderList');//预约列表 router.get('/v2/order/list', loginAuth, 'house.v2.order.getOrderList');//预约列表
//搜索历史 //搜索历史
router.get('/v2/search_history', 'house.v2.searchHistory.getSearchHistory');//搜索历史 router.get('/v2/search_history/:city_code', 'house.v2.searchHistory.getSearchHistory');//搜索历史
router.put('/v2/search_history/:type', 'house.v2.searchHistory.cleanSearchHistory');//用户点击清除搜索记录 router.put('/v2/search_history/:type', 'house.v2.searchHistory.cleanSearchHistory');//用户点击清除搜索记录
//我的 //我的
......
...@@ -32,16 +32,42 @@ class searchHistoryService extends Service { ...@@ -32,16 +32,42 @@ class searchHistoryService extends Service {
* 排序根据搜索时间逆序排列,距离当前时间近的排在前面,重复搜索的关键词取最近一次的搜索时间进行排序 * 排序根据搜索时间逆序排列,距离当前时间近的排在前面,重复搜索的关键词取最近一次的搜索时间进行排序
* 如果没有搜索记录的就不显示搜索词 * 如果没有搜索记录的就不显示搜索词
*/ */
async getSearchHistoryList(type) { async getSearchHistoryList(condition) {
const { ctx } = this; const { ctx } = this;
let type = condition.type;
let cityCode = condition.city_code;
let ret = { let ret = {
results: [], hot_search: {
count: 0 results: [],
count: 0
},
search_history: {
results: [],
count: 0
}
}; };
//不管有没有登录获取城市相关的热门搜索
let hotFilter = {
limit: 10,
attributes: ['text'],
where: {
city: cityCode,
type: type,
valid: 1
},
order: [['created_at', 'desc']]
}
let hotSearch = await ctx.realestateModel.HotSearch.all(hotFilter);
let hotList = hotSearch.map(v => { return v.text });
ret.hot_search = {
results: hotList,
count: hotList.length
}
if (!ctx.appUserId || !ctx.deviceId || !ctx.deviceLoginId || !ctx.userId) { if (!ctx.appUserId || !ctx.deviceId || !ctx.deviceLoginId || !ctx.userId) {
//如果没有登录就返回 //如果没有登录就返回
return ret; return ret;
} }
//用户的搜索记录
let endDate = moment().subtract(6, 'months').format('YYYY-MM-DD HH:mm:ss'); let endDate = moment().subtract(6, 'months').format('YYYY-MM-DD HH:mm:ss');
let filter = { let filter = {
limit: 50, limit: 50,
...@@ -49,13 +75,11 @@ class searchHistoryService extends Service { ...@@ -49,13 +75,11 @@ class searchHistoryService extends Service {
where: { where: {
state: 1, state: 1,
user_id: ctx.userId, user_id: ctx.userId,
house_style: type,
created_at: { $gt: endDate } created_at: { $gt: endDate }
}, },
order: [['created_at', 'desc']], order: [['created_at', 'desc']],
} }
if (type) {
filter.where.house_style = type;
}
let searchHistoryList = await ctx.realestateModel.SearchHistory.findAll(filter); let searchHistoryList = await ctx.realestateModel.SearchHistory.findAll(filter);
let list = []; let list = [];
if (searchHistoryList.length > 0) { if (searchHistoryList.length > 0) {
...@@ -65,7 +89,7 @@ class searchHistoryService extends Service { ...@@ -65,7 +89,7 @@ class searchHistoryService extends Service {
} }
} }
} }
ret = { ret.search_history = {
results: list, results: list,
count: list.length count: list.length
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment