client-search
Version:
客户端搜索
965 lines (948 loc) • 46.9 kB
JavaScript
'use strict';
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var lodash = require('lodash');
var qs = require('qs');
var moment = _interopDefault(require('moment'));
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
/* global Reflect, Promise */
var extendStatics = function(d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
function __extends(d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function __awaiter(thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
function __generator(thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
}
var API;
(function (API) {
API["AmapOpsURL"] = "/v3/assistant/inputtips?key=b403e838708e55f7f871baa0a5ea3ffe&";
API["BathSearch"] = "https://restapi.amap.com/v3/batch?key=b403e838708e55f7f871baa0a5ea3ffe";
API["SearchHouse"] = "/prod-plus/es/houses";
API["regeo"] = "https://restapi.amap.com/v3/geocode/regeo?key=b403e838708e55f7f871baa0a5ea3ffe&radius=1000&extensions=all";
API["geo"] = "https://restapi.amap.com/v3/geocode/regeo?key=b403e838708e55f7f871baa0a5ea3ffe&radius=1000&extensions=all";
API["AmapDistrict"] = "https://restapi.amap.com/v3/config/district?subdistrict=0&key=b403e838708e55f7f871baa0a5ea3ffe";
})(API || (API = {}));
var AMapQuery;
(function (AMapQuery) {
AMapQuery["normalType"] = "190102|190103|190104";
AMapQuery["allType"] = "010000|010100|010101|010102|010103|010104|010105|010107|010108|010109|010110|010111|010112|010200|010300|010400|010401|010500|010600|010700|010800|010900|010901|011000|011100|050000|050100|050101|050102|050103|050104|050105|050106|050107|050108|050109|050110|050111|050112|050113|050114|050115|050116|050117|050118|050119|050120|050121|050122|050123|050200|050201|050202|050203|050204|050205|050206|050207|050208|050209|050210|050211|050212|050213|050214|050215|050216|050217|050300|050301|050302|050303|050304|050305|050306|050307|050308|050309|050310|050311|050400|050500|050501|050502|050503|050504|050600|050700|050800|050900|060000|060100|060101|060102|060103|060200|060201|060202|060300|060301|060302|060303|060304|060305|060306|060307|060308|060400|060401|060402|060403|060404|060405|060406|060407|060408|060409|060411|060413|060414|060415|060500|060501|060502|060600|060601|060602|060603|060604|060605|060606|060700|060701|060702|060703|060704|060705|060706|060800|060900|060901|060902|060903|060904|060905|060906|060907|061000|061001|061100|061101|061102|061103|061104|061200|061201|061202|061203|061204|061205|061206|061207|061208|061209|061210|061211|061212|061213|061214|061300|061301|061302|061400|061401|070000|070100|070200|070201|070202|070203|070300|070301|070302|070303|070304|070305|070306|070400|070401|070500|070501|070600|070601|070603|070604|070605|070606|070607|070608|070609|070610|070700|070701|070702|070703|070704|070705|070706|070800|070900|071000|071100|071200|071300|071400|071500|071600|071700|071800|071801|071900|071901|071902|071903|072000|072001|080000|080100|080101|080102|080103|080104|080105|080106|080107|080108|080109|080110|080111|080112|080113|080114|080115|080116|080117|080118|080119|080200|080201|080202|080300|080301|080302|080303|080304|080305|080306|080307|080308|080400|080401|080402|080500|080501|080502|080503|080504|080505|080600|080601|080602|080603|090000|090100|090101|090102|090200|090201|090202|090203|090204|090205|090206|090207|090208|090209|090210|090211|090300|090400|090500|090600|090601|090602|090700|090701|090702|100000|100100|100101|100102|100103|100104|100105|100200|100201|110000|110100|110101|110102|110103|110104|110105|110106|110200|110201|110202|110203|110204|110205|110206|110207|110208|110209|120000|120100|120200|120201|120202|120203|120300|120301|120302|120303|120304|130000|130100|130101|130102|130103|130104|130105|130106|130107|130200|130201|130202|130300|130400|130401|130402|130403|130404|130405|130406|130407|130408|130409|130500|130501|130502|130503|130504|130505|130506|130600|130601|130602|130603|130604|130605|130606|130700|130701|130702|130703|140000|140100|140101|140102|140200|140201|140300|140400|140500|140600|140700|140800|140900|141000|141100|141101|141102|141103|141104|141105|141200|141201|141202|141203|141204|141205|141206|141207|141300|141400|141500|150000|150100|150101|150102|150104|150105|150106|150107|150200|150201|150202|150203|150204|150205|150206|150207|150208|150209|150210|150300|150301|150302|150303|150304|150400|150500|150501|150600|150700|150701|150702|150703|150800|150900|150903|150904|150905|150906|150907|150908|150909|151000|151100|151200|151300|160400|160401|160402|160403|160404|160405|160406|160407|160408|160500|160501|160600|170000|170100|170200|180000|180100|180101|180102|180103|180104|180200|180201|180202|180203|180300|180301|180302|180400|180500|190000|190100|190101|190105|190106|190107|190108|190109|190200|190201|190202|190203|190204|190205|190300|190301|190302|190303|190304|190305|190306|190307|190308|190309|190310|190311|190400|190401|190402|190403|190500|190600|190700";
})(AMapQuery || (AMapQuery = {}));
function adapterTips(response) {
return response.map(adapterTip);
}
function adapterTip(response) {
if (!response)
return [];
if (!response.body)
return [];
if (!Array.isArray(response.body.tips))
return [];
return response.body.tips
.map(function (tip) {
var adapterValue = {
adcode: lodash.isString(tip.adcode) ? tip.adcode : '',
address: lodash.isString(tip.address) ? tip.address : '',
city: lodash.isString(tip.city) ? tip.city : '',
district: lodash.isString(tip.district) ? tip.district : '',
id: lodash.isString(tip.id) ? tip.id : '',
location: lodash.isString(tip.location) ? tip.location : '',
name: lodash.isString(tip.name) ? tip.name : '',
typecode: lodash.isString(tip.typecode) ? tip.typecode : '',
};
if (!adapterValue.location) {
return null;
}
return adapterValue;
})
.filter(function (v) { return !!v; });
}
function adapterAddressComponent(response) {
if (!response)
return null;
var addressComponent = null;
if (response.regeocode) {
addressComponent = regeo();
}
if (response.geocodes) {
addressComponent = reo();
}
function regeo() {
if (!response.regeocode)
return null;
if (lodash.isString(response.regeocode.addressComponent))
return null;
if (lodash.isArray(response.regeocode.addressComponent))
return null;
if (lodash.isNumber(response.regeocode.addressComponent))
return null;
if (lodash.isEmpty(response.regeocode.addressComponent))
return null;
return response.regeocode.addressComponent;
}
function reo() {
if (!lodash.isArray(response.geocodes))
return null;
if (!response.geocodes.length)
return null;
return response.geocodes[0];
}
if (!addressComponent)
return null;
return {
adcode: lodash.isString(addressComponent.adcode) ? addressComponent.adcode : '',
building: lodash.isString(addressComponent.building) ? addressComponent.building : '',
businessAreas: addressComponent.businessAreas,
city: lodash.isString(addressComponent.city) ? addressComponent.city : '',
citycode: lodash.isString(addressComponent.citycode) ? addressComponent.citycode : '',
country: lodash.isString(addressComponent.country) ? addressComponent.country : '',
district: lodash.isString(addressComponent.district) ? addressComponent.district : '',
neighborhood: lodash.isString(addressComponent.neighborhood) ? addressComponent.neighborhood : '',
province: lodash.isString(addressComponent.province) ? addressComponent.province : '',
streetNumber: lodash.isString(addressComponent.streetNumber) ? addressComponent.streetNumber : '',
towncode: lodash.isString(addressComponent.towncode) ? addressComponent.towncode : '',
township: lodash.isString(addressComponent.township) ? addressComponent.township : '',
};
}
function adapterDistrict(response) {
if (!response)
return [];
if (!response.districts || !response.districts.length) {
return [];
}
return response.districts.map(function (district) {
var coordinate = district.center;
if (!lodash.isString(coordinate)) {
return null;
}
var _a = coordinate.split(','), longitude = _a[0], latitude = _a[1];
return {
latitude: latitude,
longitude: longitude,
};
});
}
var Service = (function () {
function Service(fetch) {
this.fetch = fetch;
}
Service.prototype.fetchHouseList = function (body) {
if (body === void 0) { body = {}; }
var defaultResponse = {
current: 0,
hasNextPage: false,
list: [],
pages: 0,
size: 0,
total: 0,
};
var queryData = __assign({}, body);
if (body.beginDate) {
queryData.beginDate = moment(body.beginDate).format('YYYY-MM-DD');
}
if (body.endDate) {
queryData.endDate = moment(body.endDate).format('YYYY-MM-DD');
}
if (!body.limitImage) {
queryData.limitImage = 5;
}
if (!body.pageSize) {
queryData.pageSize = 10;
}
var sortKeys = ['rankingSort', 'starsSort', 'priceSort'];
if (!sortKeys.some(function (key) { return Reflect.has(queryData, key); }) && !queryData.latitude) {
queryData.rankingSort = 0;
}
return this.fetch(API.SearchHouse + '?' + qs.stringify(queryData))
.then(function (response) {
if (!response || !response.data) {
return defaultResponse;
}
return response.data;
})
.catch(function () { return defaultResponse; });
};
Service.prototype.batchSearchAddress = function (keywords) {
return __awaiter(this, void 0, void 0, function () {
function ops(poiType) {
var querystring = qs.stringify({ datatype: 'poi', keywords: keywords, type: poiType });
return {
url: API.AmapOpsURL + querystring,
};
}
var response, result, returnResult, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4, this.fetch(API.BathSearch, {
body: JSON.stringify({ ops: [ops(AMapQuery.normalType), ops(AMapQuery.allType)] }),
method: 'post',
})];
case 1:
response = (_a.sent());
result = adapterTips(response);
returnResult = [];
if (result[0]) {
returnResult.push.apply(returnResult, result[0]
.filter(function (v) { return !!v; })
.map(function (v) { return (__assign({}, v, { istrative: true })); }));
}
returnResult.push.apply(returnResult, result[1]);
return [2, returnResult];
case 2:
error_1 = _a.sent();
return [2, []];
case 3: return [2];
}
});
});
};
Service.prototype.georegeo = function (location) {
return __awaiter(this, void 0, void 0, function () {
var result, error_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4, this.fetch(API.regeo + "&location=" + location.longitude + "," + location.latitude)];
case 1:
result = _a.sent();
return [2, adapterAddressComponent(result)];
case 2:
error_2 = _a.sent();
return [2, null];
case 3: return [2];
}
});
});
};
Service.prototype.districtToCoordinates = function (keyword) {
return __awaiter(this, void 0, void 0, function () {
var result, coord, error_3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!keyword)
return [2, null];
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4, this.fetch(API.AmapDistrict + "&keywords=" + keyword)];
case 2:
result = _a.sent();
coord = adapterDistrict(result)[0];
return [2, coord];
case 3:
error_3 = _a.sent();
return [2, null];
case 4: return [2];
}
});
});
};
return Service;
}());
var Controller = (function () {
function Controller(option) {
this.params = {};
this.houseList = [];
this.hasNext = true;
this.option = option;
this.service = new Service(this.fetch.bind(this));
}
Controller.prototype.fetch = function (input, init) {
return __awaiter(this, void 0, void 0, function () {
var url, response, isSuccess, data;
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if (!this.option.fetch) {
throw new Error('提供 fetch polyfill');
}
if (!this.option.prefix_api) {
throw new Error('提供接口前缀');
}
url = input;
if (!/^\w+:\/\//.test(url)) {
url = this.option.prefix_api + url;
}
if (init) {
init.headers = __assign({}, init.headers, (_a = {}, _a['content-type'] = 'application/json', _a));
}
return [4, this.option.fetch(url, init)];
case 1:
response = _b.sent();
isSuccess = (response.status >= 200 && response.status < 300) || response.status === 304;
return [4, response.json()];
case 2:
data = _b.sent();
if (!isSuccess || data.success === false) {
return [2, Promise.reject(data)];
}
return [2, Promise.resolve(data)];
}
});
});
};
Controller.prototype.fetchHouseListWithParams = function (immediate) {
if (immediate === void 0) { immediate = false; }
return __awaiter(this, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!immediate && !this.option.immediate && !this.params.beginDate && !this.params.endDate) {
return [2, []];
}
return [4, this.service.fetchHouseList(this.params)];
case 1:
data = _a.sent();
if (!data) {
return [2, []];
}
if (!this.params.pageNum || this.params.pageNum <= 1) {
this.houseList = data.list;
}
else {
this.houseList = this.houseList.concat(data.list);
}
this.hasNext = data.hasNextPage;
return [2, data.list];
}
});
});
};
return Controller;
}());
var ClientSearch = (function (_super) {
__extends(ClientSearch, _super);
function ClientSearch(option) {
var _this = _super.call(this, option) || this;
_this.option = option;
_this.params = {
pageNum: 0,
pageSize: 10,
};
_this.houseList = [];
_this.mapHouseList = [];
_this.recommendHouseList = [];
_this.geoLocationCityName = '';
_this.mapCoord = null;
_this.hasNext = true;
_this.inputKeywork = '';
return _this;
}
Object.defineProperty(ClientSearch.prototype, "houseListIsEmpty", {
get: function () {
return this.houseList.length;
},
enumerable: true,
configurable: true
});
ClientSearch.prototype.setCurrentGeoLocation = function (geo) {
return __awaiter(this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
this.currentGeoLocation = geo || {};
_a = this.currentGeoLocation;
return [4, this.getCoordCityName(geo)];
case 1:
_a.cityName = _b.sent();
return [2];
}
});
});
};
ClientSearch.prototype.onInputKeyword = function (keyword) {
return __awaiter(this, void 0, void 0, function () {
var _a, addressResult, houseList, tuples;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4, Promise.all([
this.service.batchSearchAddress(keyword),
this.service.fetchHouseList({ keyword: keyword, pageSize: 5 }),
])];
case 1:
_a = _b.sent(), addressResult = _a[0], houseList = _a[1];
tuples = [[], [], {}];
tuples[0] = addressResult;
tuples[1] = houseList.list;
tuples[2] = houseList;
this.inputKeywork = keyword;
return [2, tuples];
}
});
});
};
ClientSearch.prototype.searchByGeolocation = function (params) {
return __awaiter(this, void 0, void 0, function () {
var _a, houseList;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
this.clearParams([
'areaCode',
'areaName',
'longitude',
'latitude',
'keyword',
'secondLatitude',
'secondLongitude',
'cityName',
'cityCode',
]);
this.clearFilter();
this.params.pageNum = 1;
this.params.latitude = params.latitude;
this.params.longitude = params.longitude;
this.params.distance = 6;
_a = this;
return [4, this.getCoordCityName(params)];
case 1:
_a.geoLocationCityName = _b.sent();
return [4, this.fetchHouseListWithParams()];
case 2:
houseList = _b.sent();
if (!houseList.length) {
this.searchRecommendHouses();
}
return [2, houseList];
}
});
});
};
ClientSearch.prototype.searchByKeyword = function (keyword) {
return __awaiter(this, void 0, void 0, function () {
var houseList;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.clearParams([
'areaCode',
'areaName',
'longitude',
'latitude',
'keyword',
'secondLatitude',
'secondLongitude',
'cityName',
'cityCode',
]);
this.clearFilter();
this.params.pageNum = 1;
this.params.keyword = keyword;
this.params.keywordDummy = keyword;
return [4, this.fetchHouseListWithParams()];
case 1:
houseList = _a.sent();
if (!houseList.length) {
this.searchRecommendHouses();
}
return [2, houseList];
}
});
});
};
ClientSearch.prototype.searchByDate = function (date) {
return __awaiter(this, void 0, void 0, function () {
var houseList;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.params.pageNum = 1;
this.params.beginDate = date.beginDate;
this.params.endDate = date.endDate;
return [4, this.fetchHouseListWithParams(true)];
case 1:
houseList = _a.sent();
if (!!houseList.length) return [3, 3];
return [4, this.searchRecommendHouses()];
case 2:
_a.sent();
_a.label = 3;
case 3: return [2, houseList];
}
});
});
};
ClientSearch.prototype.searchByFilter = function (filterCondition, byMap) {
if (byMap === void 0) { byMap = false; }
return __awaiter(this, void 0, void 0, function () {
var houseList;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.clearFilter();
this.params.pageNum = 1;
this.params = __assign({}, this.params, filterCondition);
return [4, this.fetchHouseListWithParams(true)];
case 1:
houseList = _a.sent();
if (!byMap) return [3, 3];
return [4, this.searchMap()];
case 2:
_a.sent();
_a.label = 3;
case 3:
if (!houseList.length) {
this.searchRecommendHouses();
}
return [2, houseList];
}
});
});
};
ClientSearch.prototype.searchByLocation = function (locationParams, byMap) {
if (byMap === void 0) { byMap = false; }
return __awaiter(this, void 0, void 0, function () {
var houseList;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.clearParams(['areaCode', 'areaName']);
this.params.pageNum = 1;
this.params = __assign({}, this.params, locationParams);
return [4, this.fetchHouseListWithParams(true)];
case 1:
houseList = _a.sent();
if (!byMap) return [3, 3];
return [4, this.searchMap()];
case 2:
_a.sent();
_a.label = 3;
case 3:
if (!houseList.length) {
this.searchRecommendHouses();
}
return [2, houseList];
}
});
});
};
ClientSearch.prototype.searchBySort = function (sortParams) {
return __awaiter(this, void 0, void 0, function () {
var houseList;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.clearParams(['starsSort', 'distanceSort', 'priceSort', 'rankingSort']);
this.params.pageNum = 1;
this.params = __assign({}, this.params, sortParams);
return [4, this.fetchHouseListWithParams(true)];
case 1:
houseList = _a.sent();
if (!houseList.length) {
this.searchRecommendHouses();
}
return [2, houseList];
}
});
});
};
ClientSearch.prototype.searchByCity = function (cityParams) {
return __awaiter(this, void 0, void 0, function () {
var houseList;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.clearFilter();
this.clearParams(['longitude', 'latitude', 'secondLatitude', 'secondLongitude', 'areaCode', 'areaName', 'keyword']);
this.geoLocationCityName = cityParams.cityName || '';
this.params.pageNum = 1;
this.params = __assign({}, this.params, cityParams);
return [4, this.fetchHouseListWithParams()];
case 1:
houseList = _a.sent();
if (!houseList.length) {
this.searchRecommendHouses();
}
return [2, houseList];
}
});
});
};
ClientSearch.prototype.searchByAreaCode = function (areaCode, byMap) {
if (byMap === void 0) { byMap = false; }
return __awaiter(this, void 0, void 0, function () {
var houseList;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.clearFilter();
this.clearParams([
'longitude',
'latitude',
'secondLatitude',
'secondLongitude',
'areaCode',
'areaName',
'keyword',
'cityName',
'cityCode',
]);
this.params.pageNum = 1;
this.params.areaCode = areaCode;
if (!byMap) return [3, 2];
return [4, this.searchMap()];
case 1:
_a.sent();
_a.label = 2;
case 2: return [4, this.fetchHouseListWithParams()];
case 3:
houseList = _a.sent();
if (!houseList.length) {
this.searchRecommendHouses();
}
return [2, houseList];
}
});
});
};
ClientSearch.prototype.immediatelySearchByAreaCode = function (areaCode, byMap) {
if (byMap === void 0) { byMap = false; }
return __awaiter(this, void 0, void 0, function () {
var houseList;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.clearFilter();
this.clearParams([
'longitude',
'latitude',
'secondLatitude',
'secondLongitude',
'areaCode',
'areaName',
'keyword',
'cityName',
'cityCode',
]);
this.params.pageNum = 1;
this.params.areaCode = areaCode;
if (!byMap) return [3, 2];
return [4, this.searchMap()];
case 1:
_a.sent();
_a.label = 2;
case 2: return [4, this.fetchHouseListWithParams(true)];
case 3:
houseList = _a.sent();
if (!houseList.length) {
this.searchRecommendHouses();
}
return [2, houseList];
}
});
});
};
ClientSearch.prototype.searchMap = function () {
return __awaiter(this, void 0, void 0, function () {
var getCoord, currentCoord, coord, response, hosueList, error_1;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
getCoord = function () { return __awaiter(_this, void 0, void 0, function () {
var coord;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
coord = null;
if (!this.params.areaCode) return [3, 3];
return [4, this.service.districtToCoordinates(this.params.areaCode)];
case 1:
coord = _a.sent();
if (coord)
return [2, coord];
return [4, this.service.districtToCoordinates(this.params.areaName)];
case 2:
coord = _a.sent();
if (coord)
return [2, coord];
return [2, null];
case 3:
if (this.params.longitude && this.params.latitude) {
return [2, { longitude: this.params.longitude, latitude: this.params.latitude }];
}
if (!this.params.cityName) return [3, 5];
return [4, this.service.districtToCoordinates(this.params.cityName)];
case 4:
coord = _a.sent();
return [2, coord];
case 5: return [2, this.currentGeoLocation || null];
}
});
}); };
_a.label = 1;
case 1:
_a.trys.push([1, 6, , 7]);
return [4, getCoord()];
case 2:
currentCoord = _a.sent();
coord = __assign({}, currentCoord);
if (this.params.cityName) {
coord.cityName = this.params.cityName;
}
if (!!coord.cityName) return [3, 4];
return [4, this.service.georegeo(coord)];
case 3:
response = _a.sent();
if (response && lodash.isString(response.city)) {
coord.cityName = response.city;
}
if (!coord.cityName && response && lodash.isString(response.province)) {
coord.cityName = response.province;
}
_a.label = 4;
case 4: return [4, this.service.fetchHouseList(__assign({}, coord, { allTagIds: this.params.allTagIds, bedNumberGreaterThanEqual: this.params.bedNumberGreaterThanEqual, bizTagIds: this.params.bizTagIds, customTagIds: this.params.customTagIds, distance: 6, pageSize: 30, priceGreaterThanEqual: this.params.priceGreaterThanEqual, priceLessThanEqual: this.params.priceLessThanEqual, roomNumberGreaterThanEqual: this.params.roomNumberGreaterThanEqual, tenantNumberGreaterThanEqual: this.params.tenantNumberGreaterThanEqual }))];
case 5:
hosueList = _a.sent();
this.mapHouseList = hosueList.list;
this.mapCoord = coord;
return [2, [coord, hosueList.list]];
case 6:
error_1 = _a.sent();
return [2, [null, []]];
case 7: return [2];
}
});
});
};
ClientSearch.prototype.searchMapWithCoord = function (coord) {
return __awaiter(this, void 0, void 0, function () {
var hosueList, error_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4, this.service.fetchHouseList(__assign({}, coord, { allTagIds: this.params.allTagIds, bedNumberGreaterThanEqual: this.params.bedNumberGreaterThanEqual, bizTagIds: this.params.bizTagIds, customTagIds: this.params.customTagIds, distance: 6, pageSize: 30, priceGreaterThanEqual: this.params.priceGreaterThanEqual, priceLessThanEqual: this.params.priceLessThanEqual, roomNumberGreaterThanEqual: this.params.roomNumberGreaterThanEqual, tenantNumberGreaterThanEqual: this.params.tenantNumberGreaterThanEqual }))];
case 1:
hosueList = _a.sent();
this.mapHouseList = hosueList.list;
return [2, [coord, hosueList.list]];
case 2:
error_2 = _a.sent();
return [2, [null, []]];
case 3: return [2];
}
});
});
};
ClientSearch.prototype.searchByTagIds = function (bizTagIds, params) {
return __awaiter(this, void 0, void 0, function () {
var houseList, error_3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.clearFilter();
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
this.params = __assign({}, this.params, params, { allTagIds: bizTagIds, bizTagIds: bizTagIds });
return [4, this.fetchHouseListWithParams(true)];
case 2:
houseList = _a.sent();
if (!houseList.length) {
this.searchRecommendHouses();
}
return [2, houseList];
case 3:
error_3 = _a.sent();
return [2, []];
case 4: return [2];
}
});
});
};
ClientSearch.prototype.loadDataByPageNum = function (pageNum) {
this.params.pageNum = pageNum;
return this.fetchHouseListWithParams(true);
};
ClientSearch.prototype.loadNextPageData = function () {
if (!this.hasNext)
return;
return this.loadDataByPageNum((this.params.pageNum || 1) + 1);
};
ClientSearch.prototype.loadFirstPageData = function () {
return this.loadDataByPageNum(1);
};
ClientSearch.prototype.searchRecommendHouses = function (location) {
return __awaiter(this, void 0, void 0, function () {
var houseList, response, response, response, userCityInfo, response, response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
houseList = [];
if (!location) return [3, 2];
return [4, this.service.fetchHouseList({
cityCode: this.params.cityCode,
pageSize: 10,
recommend: true,
})];
case 1:
response = _a.sent();
houseList.unshift.apply(houseList, response.list);
_a.label = 2;
case 2:
if (!(!houseList.length && this.params.cityCode)) return [3, 4];
return [4, this.service.fetchHouseList({
cityCode: this.params.cityCode,
pageSize: 10,
recommend: true,
})];
case 3:
response = _a.sent();
houseList.unshift.apply(houseList, response.list);
_a.label = 4;
case 4:
if (!(!houseList.length && this.currentGeoLocation)) return [3, 6];
return [4, this.service.fetchHouseList(__assign({}, this.currentGeoLocation, { distance: 6, pageSize: 10, recommend: true }))];
case 5:
response = _a.sent();
houseList.unshift.apply(houseList, response.list);
_a.label = 6;
case 6:
if (!(!houseList.length && this.currentGeoLocation)) return [3, 9];
return [4, this.service.georegeo(this.currentGeoLocation)];
case 7:
userCityInfo = _a.sent();
if (!(userCityInfo && lodash.isString(userCityInfo.city))) return [3, 9];
return [4, this.service.fetchHouseList({ cityName: userCityInfo.city, recommend: true })];
case 8:
response = _a.sent();
houseList.unshift.apply(houseList, response.list);
_a.label = 9;
case 9:
if (!!houseList.length) return [3, 11];
return [4, this.service.fetchHouseList({ recommend: true })];
case 10:
response = _a.sent();
houseList.unshift.apply(houseList, response.list);
_a.label = 11;
case 11:
this.recommendHouseList = houseList;
return [2, houseList];
}
});
});
};
ClientSearch.prototype.getCoordCityName = function (coord) {
return __awaiter(this, void 0, void 0, function () {
var response;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, this.service.georegeo(coord)];
case 1:
response = _a.sent();
if (response && lodash.isString(response.city) && response.city) {
return [2, response.city];
}
if (response && lodash.isString(response.province) && response.province) {
return [2, response.province];
}
return [2, ''];
}
});
});
};
ClientSearch.prototype.clearParams = function (keys) {
var _this = this;
if (!keys) {
this.params = {};
return;
}
keys.forEach(function (key) {
if (typeof Reflect !== 'undefined') {
Reflect.deleteProperty(_this.params, key);
return;
}
try {
delete _this.params[key];
}
catch (error) {
_this.params[key] = undefined;
}
});
};
ClientSearch.prototype.clearFilter = function () {
this.clearParams([
'bedNumberGreaterThanEqual',
'roomNumberGreaterThanEqual',
'tenantNumberGreaterThanEqual',
'priceGreaterThanEqual',
'priceLessThanEqual',
'facilities',
'bedNumbers',
'roomNumbers',
'tenantNumbers',
'tenantNumber',
'allTagIds',
'bizTagIds',
'customTagIds',
]);
};
return ClientSearch;
}(Controller));
module.exports = ClientSearch;