123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- 'use strict';
- import Cities from '../../models/v1/cities'
- import pinyin from "pinyin"
- import AddressComponent from '../../prototype/addressComponent'
- class CityHandle extends AddressComponent{
- constructor(){
- super()
- this.getCity = this.getCity.bind(this);
- this.getExactAddress = this.getExactAddress.bind(this);
- this.pois = this.pois.bind(this);
- }
- async getCity(req, res, next){
- const type = req.query.type;
- let cityInfo;
- try{
- switch (type){
- case 'guess':
- const city = await this.getCityName(req);
- cityInfo = await Cities.cityGuess(city);
- break;
- case 'hot':
- cityInfo = await Cities.cityHot();
- break;
- case 'group':
- cityInfo = await Cities.cityGroup();
- break;
- default:
- res.json({
- name: 'ERROR_QUERY_TYPE',
- message: '参数错误',
- })
- return
- }
- res.send(cityInfo);
- }catch(err){
- res.send({
- name: 'ERROR_DATA',
- message: '获取数据失败',
- });
- }
- }
- async getCityById(req, res, next){
- const cityid = req.params.id;
- if (isNaN(cityid)) {
- res.json({
- name: 'ERROR_PARAM_TYPE',
- message: '参数错误',
- })
- return
- }
- try{
- const cityInfo = await Cities.getCityById(cityid);
- res.send(cityInfo);
- }catch(err){
- res.send({
- name: 'ERROR_DATA',
- message: '获取数据失败',
- });
- }
- }
- async getCityName(req){
- let cityInfo;
- try{
- cityInfo = await this.guessPosition(req);
- }catch(err){
- console.error('获取IP位置信息失败');
- res.send({
- name: 'ERROR_DATA',
- message: '获取数据失败',
- });
- return
- }
- /*
- 汉字转换成拼音
- */
- const pinyinArr = pinyin(cityInfo.city, {
- style: pinyin.STYLE_NORMAL,
- });
- let cityName = '';
- pinyinArr.forEach(item => {
- cityName += item[0];
- })
- return cityName
- }
- async getExactAddress(req, res, next){
- try{
- const position = await this.geocoder(req)
- res.send(position);
- }catch(err){
- console.log('获取精确位置信息失败');
- res.send({
- name: 'ERROR_DATA',
- message: '获取精确位置信息失败',
- });
- }
- }
- async pois(req, res, next){
- const geohash = req.params.geohash;
- try{
- if (geohash.indexOf(',') == -1) {
- throw new Error('参数错误')
- }
- }catch(err){
- console.log('参数错误');
- res.send({
- status: 0,
- type: 'ERROR_PARAMS',
- message: '参数错误',
- })
- return
- }
- const poisArr = geohash.split(',');
- try{
- const result = await this.getpois(poisArr[0], poisArr[1]);
- const address = {
- address: result.result.address,
- city: result.result.address_component.province,
- geohash,
- latitude: poisArr[0],
- longitude: poisArr[1],
- name: result.result.formatted_addresses.recommend,
- }
- res.send(address);
- }catch(err){
- console.log('getpois返回信息失败');
- res.send({
- status: 0,
- type: 'ERROR_DATA',
- message: '获取数据失败',
- })
- }
- }
- }
- export default new CityHandle()
|