123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504 |
- var ERROR_CONF = {
- KEY_ERR: 311,
- KEY_ERR_MSG: 'key格式错误',
- PARAM_ERR: 310,
- PARAM_ERR_MSG: '请求参数信息有误',
- SYSTEM_ERR: 600,
- SYSTEM_ERR_MSG: '系统错误',
- WX_ERR_CODE: 1000,
- WX_OK_CODE: 200
- };
- var BASE_URL = 'https://apis.map.qq.com/ws/';
- var URL_SEARCH = BASE_URL + 'place/v1/search';
- var URL_SUGGESTION = BASE_URL + 'place/v1/suggestion';
- var URL_GET_GEOCODER = BASE_URL + 'geocoder/v1/';
- var URL_CITY_LIST = BASE_URL + 'district/v1/list';
- var URL_AREA_LIST = BASE_URL + 'district/v1/getchildren';
- var URL_DISTANCE = BASE_URL + 'distance/v1/';
- var Utils = {
-
- location2query(data) {
- if (typeof data == 'string') {
- return data;
- }
- var query = '';
- for (var i = 0; i < data.length; i++) {
- var d = data[i];
- if (!!query) {
- query += ';';
- }
- if (d.location) {
- query = query + d.location.lat + ',' + d.location.lng;
- }
- if (d.latitude && d.longitude) {
- query = query + d.latitude + ',' + d.longitude;
- }
- }
- return query;
- },
-
- getWXLocation(success, fail, complete) {
- uni.getLocation({
- type: 'gcj02',
- success: success,
- fail: fail,
- complete: complete
- });
- },
-
- getLocationParam(location) {
- if (typeof location == 'string') {
- var locationArr = location.split(',');
- if (locationArr.length === 2) {
- location = {
- latitude: location.split(',')[0],
- longitude: location.split(',')[1]
- };
- } else {
- location = {};
- }
- }
- return location;
- },
-
- polyfillParam(param) {
- param.success = param.success || function () {};
- param.fail = param.fail || function () {};
- param.complete = param.complete || function () {};
- },
-
- checkParamKeyEmpty(param, key) {
- if (!param[key]) {
- var errconf = this.buildErrorConfig(ERROR_CONF.PARAM_ERR, ERROR_CONF.PARAM_ERR_MSG + key + '参数格式有误');
- param.fail(errconf);
- param.complete(errconf);
- return true;
- }
- return false;
- },
-
- checkKeyword(param) {
- return !this.checkParamKeyEmpty(param, 'keyword');
- },
-
- checkLocation(param) {
- var location = this.getLocationParam(param.location);
- if (!location || !location.latitude || !location.longitude) {
- var errconf = this.buildErrorConfig(ERROR_CONF.PARAM_ERR, ERROR_CONF.PARAM_ERR_MSG + ' location参数格式有误');
- param.fail(errconf);
- param.complete(errconf);
- return false;
- }
- return true;
- },
-
- buildErrorConfig(errCode, errMsg) {
- return {
- status: errCode,
- message: errMsg
- };
- },
-
- buildWxRequestConfig(param, options) {
- var that = this;
- options.header = {
- 'content-type': 'application/json'
- };
- options.method = 'GET';
- options.success = function (res) {
- var data = res.data;
- if (data.status === 0) {
- param.success(data);
- } else {
- param.fail(data);
- }
- };
- options.fail = function (res) {
- res.statusCode = ERROR_CONF.WX_ERR_CODE;
- param.fail(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, result.errMsg));
- };
- options.complete = function (res) {
- var statusCode = +res.statusCode;
- switch (statusCode) {
- case ERROR_CONF.WX_ERR_CODE: {
- param.complete(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg));
- break;
- }
- case ERROR_CONF.WX_OK_CODE: {
- var data = res.data;
- if (data.status === 0) {
- param.complete(data);
- } else {
- param.complete(that.buildErrorConfig(data.status, data.message));
- }
- break;
- }
- default: {
- param.complete(that.buildErrorConfig(ERROR_CONF.SYSTEM_ERR, ERROR_CONF.SYSTEM_ERR_MSG));
- }
- }
- };
- return options;
- },
-
- locationProcess(param, locationsuccess, locationfail, locationcomplete) {
- var that = this;
- locationfail =
- locationfail ||
- function (res) {
- res.statusCode = ERROR_CONF.WX_ERR_CODE;
- param.fail(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg));
- };
- locationcomplete =
- locationcomplete ||
- function (res) {
- if (res.statusCode == ERROR_CONF.WX_ERR_CODE) {
- param.complete(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg));
- }
- };
- if (!param.location) {
- that.getWXLocation(locationsuccess, locationfail, locationcomplete);
- } else if (that.checkLocation(param)) {
- var location = Utils.getLocationParam(param.location);
- locationsuccess(location);
- }
- }
- };
- class QQMapWX {
-
- constructor(options) {
- if (!options.key) {
- throw Error('key值不能为空');
- }
- this.key = options.key;
- }
-
- search(options) {
- var that = this;
- options = options || {};
- Utils.polyfillParam(options);
- if (!Utils.checkKeyword(options)) {
- return;
- }
- var requestParam = {
- keyword: options.keyword,
- orderby: options.orderby || '_distance',
- page_size: options.page_size || 10,
- page_index: options.page_index || 1,
- output: 'json',
- key: that.key
- };
- if (options.address_format) {
- requestParam.address_format = options.address_format;
- }
- if (options.filter) {
- requestParam.filter = options.filter;
- }
- var distance = options.distance || '1000';
- var auto_extend = options.auto_extend || 1;
- var locationsuccess = function (result) {
- requestParam.boundary = 'nearby(' + result.latitude + ',' + result.longitude + ',' + distance + ',' + auto_extend + ')';
- uni.request(
- Utils.buildWxRequestConfig(options, {
- url: URL_SEARCH,
- data: requestParam
- })
- );
- };
- Utils.locationProcess(options, locationsuccess);
- }
-
- getSuggestion(options) {
- var that = this;
- options = options || {};
- Utils.polyfillParam(options);
- if (!Utils.checkKeyword(options)) {
- return;
- }
- var requestParam = {
- keyword: options.keyword,
- region: options.region || '全国',
- region_fix: options.region_fix || 0,
- policy: options.policy || 0,
- output: 'json',
- key: that.key
- };
- uni.request(
- Utils.buildWxRequestConfig(options, {
- url: URL_SUGGESTION,
- data: requestParam
- })
- );
- }
-
- reverseGeocoder(options) {
- var that = this;
- options = options || {};
- Utils.polyfillParam(options);
- var requestParam = {
- coord_type: options.coord_type || 5,
- get_poi: options.get_poi || 0,
- output: 'json',
- key: that.key
- };
- if (options.poi_options) {
- requestParam.poi_options = options.poi_options;
- }
- var locationsuccess = function (result) {
- requestParam.location = result.latitude + ',' + result.longitude;
- uni.request(
- Utils.buildWxRequestConfig(options, {
- url: URL_GET_GEOCODER,
- data: requestParam
- })
- );
- };
- Utils.locationProcess(options, locationsuccess);
- }
-
- geocoder(options) {
- var that = this;
- options = options || {};
- Utils.polyfillParam(options);
- if (Utils.checkParamKeyEmpty(options, 'address')) {
- return;
- }
- var requestParam = {
- address: options.address,
- output: 'json',
- key: that.key
- };
- uni.request(
- Utils.buildWxRequestConfig(options, {
- url: URL_GET_GEOCODER,
- data: requestParam
- })
- );
- }
-
- getCityList(options) {
- var that = this;
- options = options || {};
- Utils.polyfillParam(options);
- var requestParam = {
- output: 'json',
- key: that.key
- };
- uni.request(
- Utils.buildWxRequestConfig(options, {
- url: URL_CITY_LIST,
- data: requestParam
- })
- );
- }
-
- getDistrictByCityId(options) {
- var that = this;
- options = options || {};
- Utils.polyfillParam(options);
- if (Utils.checkParamKeyEmpty(options, 'id')) {
- return;
- }
- var requestParam = {
- id: options.id || '',
- output: 'json',
- key: that.key
- };
- uni.request(
- Utils.buildWxRequestConfig(options, {
- url: URL_AREA_LIST,
- data: requestParam
- })
- );
- }
-
- calculateDistance(options) {
- var that = this;
- options = options || {};
- Utils.polyfillParam(options);
- if (Utils.checkParamKeyEmpty(options, 'to')) {
- return;
- }
- var requestParam = {
- mode: options.mode || 'walking',
- to: Utils.location2query(options.to),
- output: 'json',
- key: that.key
- };
- var locationsuccess = function (result) {
- requestParam.from = result.latitude + ',' + result.longitude;
- uni.request(
- Utils.buildWxRequestConfig(options, {
- url: URL_DISTANCE,
- data: requestParam
- })
- );
- };
- if (options.from) {
- options.location = options.from;
- }
- Utils.locationProcess(options, locationsuccess);
- }
- }
- module.exports = QQMapWX;
|