Commit ff5bccaa authored by 李尚科's avatar 李尚科

add controller v2

parent 4bd643a3
Pipeline #8591 passed with stage
in 3 seconds
'use strict';
const Controller = require('egg').Controller;
class RentalHouseController extends Controller {
//租房首页信息
async home() {
const { ctx } = this;
const input_params = ctx.query;
const city_code = input_params.city_code ? input_params.city_code : 330100;
//banners
const headers = {
cookie: ctx.request.headers.cookie
}
const banners_result = await ctx.helper.send_request(this.config.NODE_BASE_URL + '/51app/api/block/all', { alias: '51fangc_rental_home_banners' }, { method: 'GET', dataType: 'json', headers: headers });
let banners = [];
if (banners_result.status === 200) {
banners = banners_result.data.results;
}
const developers = await ctx.service.house.option.getDevelopers(city_code);//品牌信息
ctx.success({ banners, developers });
}
//租房列表
async getRentalHouses() {
const { ctx } = this;
const input_params = ctx.request.body;
const rule = {
brand: { type: 'string', required: false },//品牌
area_code: { type: 'object', required: false },//区域
price: { type: 'object', required: false },//价格
house_type: { type: 'string', required: false },//房型
name: { type: 'string', required: false },//楼盘名称
page: { type: 'string', required: false },
page_size: { type: 'string', required: false },
};
ctx.validate(rule, input_params);
const ret = await ctx.service.house.v2.rentalHouse.getRentalHousesByFilter(input_params);
ctx.success({ input_params, results: ret.results, count: ret.count });
}
//租房详情
async getRentalHouse() {
const { ctx } = this;
const input_params = ctx.params;
if (!input_params.rental_house_id) {
ctx.failed('rental_house_id error');
}
const rental_house_id = input_params.rental_house_id;
const ret = await ctx.service.house.v2.rentalHouse.getRentalHouse(rental_house_id);
ctx.success(ret);
}
}
module.exports = RentalHouseController;
......@@ -46,4 +46,12 @@ module.exports = app => {
//房产v2
//租房列表
router.get('/v2/rental_house/list', 'house.v2.rentalHouse.getRentalHouses');//租房列表
router.post('/v2/rental_house/list', 'house.v2.rentalHouse.getRentalHouses');//租房列表
router.get('/v2/rental_house/info/:rental_house_id', 'house.v2.rentalHouse.getRentalHouse');//住房详情
};
'use strict';
const Service = require('egg').Service;
const moment = require('moment');
class FootPrintService extends Service {
/**
* 添加足迹
* @param {object} inputParams {id: XX, type: XX, remark: XX}
*/
async addFootPrint(inputParams) {
const { ctx } = this;
const user_id = ctx.userId;
const app_user_id = ctx.appUserId;
const app_type_id = ctx.appTypeId;
const app_id = ctx.app_id;
if (!user_id || !app_user_id) {
return false;
}
const data = {
user_id: user_id,
app_type_id: app_type_id,
app_id: app_id,
house_style: inputParams.type,
connectId: inputParams.id,
state: 1,
remark: inputParams.remark || '',
}
let ret = await ctx.realestateModel.footPrint.add(data);
return { id: ret.id };
}
/**
* 获取用户足迹列表 保留最近6个月的记录,条数不超过50条
* @param {object} condition
*/
async getFootPrintList() {
const { ctx } = this;
const user_id = ctx.userId;
const app_type_id = ctx.appTypeId;
if (!user_id || !app_type_id) {
return {
results: [],
count: 0
};
}
const foot_prints_rows = await ctx.realestateModel.FootPrint.list({ page: 1, limit: 50, where: { user_id: user_id, app_type_id: app_type_id, state: 1, order: [['id', 'desc']] } });
const foot_prints = foot_prints_rows.rows;
const p_houses = [];
for (let i in foot_prints) {
let foot_print = foot_prints[i];
if (foot_print.house_style === 1) {//获取房源信息
p_houses[i] = ctx.realestateModel.NewHouse.one(foot_print.connect_id);
} else {
p_houses[i] = ctx.realestateModel.RentalHouse.one(foot_print.connect_id);
}
}
const houses = await Promise.all(p_houses).then(result => {//等待所有异步内容获取完成
return result;
}).catch(error => {
ctx.failed(error);
});
const now_time = moment(new Date()).format('X');
//处理足迹数据
const foot_print_records = [];
for (let j in houses) {
const house = houses[j];
const foot_print = foot_prints[j];
const type = foot_print.house_style === 1 ? 'new_house' : 'rental_house';
const create_time = moment(foot_print.created_at).format('X');
let time = moment(foot_print.created_at).format('YYYY-MM-DD');
const du_time = now_time - create_time;
if (du_time < 86400) {
const hours = parseInt(du_time / 3600);
const mins = parseInt(du_time / 60);
time = hours ? `${hours}小时前` : (mins > 5 ? `${mins}分钟前` : '刚刚');
}
foot_print_records.push({
id: foot_print.id,
house_id: house.id,
name: house.name,
time: time,
type: type,
});
}
return { results: foot_print_records, count: foot_prints_results.rowCount };
}
/**
* 我的-获取用户足迹个数
*/
async getFootPrintCount() {
const { ctx, service } = this;
if (!ctx.userId) {
return { count: 0 };
}
const filter = {
pageIndex: 1,
pageSize: 50,
queryConditions: [{
key: "state",
value: 1,
operator: "equal"
}, {
key: "userId",
value: ctx.userId,
operator: "equal"
},
],
orderConditions: [{
key: 'createdAt',
orderSequence: 'desc',
},],
}
const foot_prints_results = await service.houseCommon.footPrint.all(filter);
let ret = {
count: foot_prints_results.rowCount
};
return ret;
}
}
module.exports = FootPrintService;
......@@ -17,110 +17,39 @@ const HOUSE_TYPE = [
class RentalHouseService extends Service {
async getRentalHouseByFilter_V2(condition) {
async getRentalHousesByFilter(condition) {
const { ctx } = this;
const { brand, price, house_type, area_code, name, } = condition;
let where = {};
if (brand) {
const residentials = await ctx.service.house.residential.getResidentialByDeveloper({ developer_id: brand });
const residentials_ids = R.pluck('id', residentials);
}
if (area_code) {
let { brand, name, price, house_type, area_code, page, page_size } = condition;
}
if (house_type) {
page = page ? page : 1;
let limit = page_size ? page_size : 30;
let where = { status: 'online', valid: 1 };
if (brand) {
where.developer_id = brand;
}
if (name) {
where.name = { $like: `%${name}%` };
}
}
async getRentalHousesByFilter(condition) {
const { ctx } = this;
const queryConditions = [];
if (condition.brand) {
queryConditions.push({
key: 'developerid',
value: parseInt(condition.brand),
operator: 'equal',
});
}
if (condition.price && condition.price.max) {
queryConditions.push({
key: 'price',
value: condition.price.min,
operator: 'greaterEqual',
});
queryConditions.push({
key: 'price',
value: condition.price.max,
operator: 'lessEqual',
});
if (price && price.max) {
where.price = { $between: [price.min, price.max] };
}
if (condition.house_type) {
queryConditions.push({
key: 'type',
value: parseInt(condition.house_type),
operator: 'equal',
});
}
if (condition.area_code) {//城市\区域\商圈筛选
if (condition.area_code.hasOwnProperty('city_code') && condition.area_code.city_code !== '') {
queryConditions.push({
key: 'city',
value: condition.area_code.city_code,
operator: 'equal',
});
}
if (condition.area_code.hasOwnProperty('district_code') && condition.area_code.district_code !== '') {
queryConditions.push({
key: 'county',
value: condition.area_code.district_code,
operator: 'equal',
});
}
if (condition.area_code.hasOwnProperty('bizcircle_code') && condition.area_code.bizcircle_code !== '') {
queryConditions.push({
key: 'region',
value: condition.area_code.bizcircle_code,
operator: 'equal',
});
}
}
if (condition.name) {
await ctx.service.house.searchHistory.addSearchHistory({ type: 2, key_word: condition.name });//增加搜索历史
queryConditions.push({
key: 'name',
value: condition.name,
operator: 'contains',
});
if (area_code) {
}
queryConditions.push({
key: "state",
value: 0,
operator: "notEqual"
});
const pageSize = parseInt(condition.page_size) ? parseInt(condition.page_size) : 30;
const page = parseInt(condition.page) ? parseInt(condition.page) : 1;
const filter = {
pageSize: pageSize,
pageIndex: page,
queryConditions: queryConditions,
orderConditions: [{
key: 'price',
orderSequence: 'asc',
},],
if (house_type) {
const house_types = await ctx.realestateModel.RentalHouseType.all({ where: { type: house_type, status: 'online', valid: 1 } });
const rental_houses_ids = R.pluck('rental_house_id', house_types);
where.id = { $in: rental_houses_ids };
}
const rental_houses_results = await ctx.service.houseCommon.rentalHouse.all(filter);
let rental_houses = await this.formatRentalHouseBasic(rental_houses_results.results);
const rental_houses_results = await ctx.realestateModel.RentalHouse.list({ page: page, limit: limit, where: where, order: [['order_id', 'asc']] });
let rental_houses = await this.formatRentalHouseBasic(rental_houses_results.rows);
rental_houses = R.project(['id', 'image', 'name', 'address', 'price', 'tags', 'favourable_info'])(rental_houses);
return { results: rental_houses, count: rental_houses_results.rowCount };
return { page: page, count: rental_houses_results.count, results: rental_houses };
}
//房源详细信息
......@@ -128,90 +57,30 @@ class RentalHouseService extends Service {
const { ctx } = this;
//获取房源基本信息
let house_basic_result = await ctx.service.houseCommon.rentalHouse.one(rental_house_id);
if (!house_basic_result || !house_basic_result.id) {
ctx.failed('house not found');
const rental_house_info = await ctx.realestateModel.RentalHouse.one({ where: { id: rental_house_id } });
if (!rental_house_info || !rental_house_info.id) {
ctx.failed('rental house not found');
}
rental_house_id = house_basic_result.id;
//获取房源图片信息
const p_house_images = this.getRentalHouseImages(rental_house_id);
//获取房源类型
const p_house_types = this.formatRentHouseTypes(house_basic_result.rentalHouseType);
//楼盘图片信息和房型信息
const house_types_rets = await ctx.realestateModel.RentalHouseType.all({ where: { rental_house_id: rental_house_id, status: 'online', valid: 1 } });
const p_house_images = ctx.service.houseImage.getAll({ type: 1, connect_id: rental_house_id }, ['id', 'path', 'description']);
const p_house_types = this.formatRentHouseTypes(house_types_rets);
const p_ret = await Promise.all([p_house_images, p_house_types]).then(result => {//等待所有异步内容获取完成
return result;
}).catch(error => {
ctx.failed(error);
});
const house_images = p_ret[0];
const house_types = p_ret[1];
//处理房源基本信息格式
let house_basic = await this.formatRentalHouseBasic([house_basic_result]);
let house_basic = await this.formatRentalHouseBasic([rental_house_info]);
house_basic = house_basic[0];
//是否关注
const collectionFilter = { type: 2, id: rental_house_id }
const collection = await ctx.service.house.collection.getCollection(collectionFilter);
//TODO添加用户足迹,是否关注过房源
ctx.service.house.footPrint.addFootPrint({ id: rental_house_id, type: 2 });//添加用户足迹记录
const city = await ctx.blockModel.City.one({ where: { code: house_basic_result.residential.city } });
const county = await ctx.blockModel.HouseDistrict.one({ where: { id: house_basic_result.residential.county } });
const region = await ctx.blockModel.HouseBizcircle.one({ where: { id: house_basic_result.residential.region } });
const city_name = (city && city.name) ? city.name : '';
const county_name = (county && county.name) ? county.name : '';
const region_name = (region && region.name) ? region.name : '';
let address = city_name + county_name + region_name + house_basic.address;
let gps_result = await ctx.helper.getGPS(address, city_name);
if (!gps_result.lat || !gps_result.lng) {
address = city_name + county_name + region_name;
gps_result = await ctx.helper.getGPS(address, city_name);
}
let longitude = 0;
let latitude = 0;
if (gps_result) {
longitude = gps_result.lng;
latitude = gps_result.lat;
}
house_basic.longitude = longitude;
house_basic.latitude = latitude;
return { house_basic, house_images, house_types, collection: collection.rowCount > 0 ? true : false, };
}
//获取房源画册
async getRentalHouseImages(rental_house_id) {
const { ctx } = this;
const house_image_filter = {
pageSize: 10000,
queryConditions: [
{
key: 'state',
value: 1,
operator: 'equal'
},
{
key: 'type',
value: 0,
operator: 'equal'
},
{
key: 'connectId',
value: rental_house_id,
operator: 'equal'
},
],
};
const house_images_results = await ctx.service.houseCommon.houseImage.all(house_image_filter);
const house_images = R.project(['id', 'path', 'description'])(house_images_results.results);
return house_images;
return { house_basic, house_images, house_types, collection: false, };
}
//格式化户型数据
......@@ -225,31 +94,11 @@ class RentalHouseService extends Service {
for (let i in house_types) {
const house_type = house_types[i];
if (house_type.state === 0) {
if (house_type.status === 'offline' || house_type.valid === 0) {
continue;
}
const house_type_image_filter = {
pageSize: 10000,
queryConditions: [
{
key: 'state',
value: 1,
operator: 'equal'
},
{
key: 'type',
value: 3,
operator: 'equal',
},
{
key: 'connectId',
value: house_type.id,
operator: 'equal',
}
],
};
const house_type_images_results = await ctx.service.houseCommon.houseImage.all(house_type_image_filter);
const house_type_images_results = await ctx.service.houseImage.getAll({ type: 3, connect_id: house_type.id });
const type = house_type.type || 1;
const house_type_value = HOUSE_TYPE[type];
......
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