Showing
2 changed files
with
2 additions
and
463 deletions
utils/qqmap-wx-jssdk.js
deleted
100644 → 0
| 1 | -/** | ||
| 2 | - * 微信小程序JavaScriptSDK | ||
| 3 | - * | ||
| 4 | - * @version 1.0 | ||
| 5 | - * @date 2017-01-10 | ||
| 6 | - * @author jaysonzhou@tencent.com | ||
| 7 | - */ | ||
| 8 | - | ||
| 9 | -var ERROR_CONF = { | ||
| 10 | - KEY_ERR: 311, | ||
| 11 | - KEY_ERR_MSG: 'key格式错误', | ||
| 12 | - PARAM_ERR: 310, | ||
| 13 | - PARAM_ERR_MSG: '请求参数信息有误', | ||
| 14 | - SYSTEM_ERR: 600, | ||
| 15 | - SYSTEM_ERR_MSG: '系统错误', | ||
| 16 | - WX_ERR_CODE: 1000, | ||
| 17 | - WX_OK_CODE: 200 | ||
| 18 | -}; | ||
| 19 | -var BASE_URL = 'https://apis.map.qq.com/ws/'; | ||
| 20 | -var URL_SEARCH = BASE_URL + 'place/v1/search'; | ||
| 21 | -var URL_SUGGESTION = BASE_URL + 'place/v1/suggestion'; | ||
| 22 | -var URL_GET_GEOCODER = BASE_URL + 'geocoder/v1/'; | ||
| 23 | -var URL_CITY_LIST = BASE_URL + 'district/v1/list'; | ||
| 24 | -var URL_AREA_LIST = BASE_URL + 'district/v1/getchildren'; | ||
| 25 | -var URL_DISTANCE = BASE_URL + 'distance/v1/'; | ||
| 26 | -var Utils = { | ||
| 27 | - /** | ||
| 28 | - * 得到终点query字符串 | ||
| 29 | - * @param {Array|String} 检索数据 | ||
| 30 | - */ | ||
| 31 | - location2query(data) { | ||
| 32 | - if (typeof data == 'string') { | ||
| 33 | - return data; | ||
| 34 | - } | ||
| 35 | - var query = ''; | ||
| 36 | - for (var i = 0; i < data.length; i++) { | ||
| 37 | - var d = data[i]; | ||
| 38 | - if (!!query) { | ||
| 39 | - query += ';'; | ||
| 40 | - } | ||
| 41 | - if (d.location) { | ||
| 42 | - query = query + d.location.lat + ',' + d.location.lng; | ||
| 43 | - } | ||
| 44 | - if (d.latitude && d.longitude) { | ||
| 45 | - query = query + d.latitude + ',' + d.longitude; | ||
| 46 | - } | ||
| 47 | - } | ||
| 48 | - return query; | ||
| 49 | - }, | ||
| 50 | - | ||
| 51 | - /** | ||
| 52 | - * 使用微信接口进行定位 | ||
| 53 | - */ | ||
| 54 | - getWXLocation(success, fail, complete) { | ||
| 55 | - wx.getLocation({ | ||
| 56 | - type: 'gcj02', | ||
| 57 | - success: success, | ||
| 58 | - fail: fail, | ||
| 59 | - complete: complete | ||
| 60 | - }); | ||
| 61 | - }, | ||
| 62 | - | ||
| 63 | - /** | ||
| 64 | - * 获取location参数 | ||
| 65 | - */ | ||
| 66 | - getLocationParam(location) { | ||
| 67 | - if (typeof location == 'string') { | ||
| 68 | - var locationArr = location.split(','); | ||
| 69 | - if (locationArr.length === 2) { | ||
| 70 | - location = { | ||
| 71 | - latitude: location.split(',')[0], | ||
| 72 | - longitude: location.split(',')[1] | ||
| 73 | - }; | ||
| 74 | - } else { | ||
| 75 | - location = {}; | ||
| 76 | - } | ||
| 77 | - } | ||
| 78 | - return location; | ||
| 79 | - }, | ||
| 80 | - | ||
| 81 | - /** | ||
| 82 | - * 回调函数默认处理 | ||
| 83 | - */ | ||
| 84 | - polyfillParam(param) { | ||
| 85 | - param.success = param.success || function () { }; | ||
| 86 | - param.fail = param.fail || function () { }; | ||
| 87 | - param.complete = param.complete || function () { }; | ||
| 88 | - }, | ||
| 89 | - | ||
| 90 | - /** | ||
| 91 | - * 验证param对应的key值是否为空 | ||
| 92 | - * | ||
| 93 | - * @param {Object} param 接口参数 | ||
| 94 | - * @param {String} key 对应参数的key | ||
| 95 | - */ | ||
| 96 | - checkParamKeyEmpty(param, key) { | ||
| 97 | - if (!param[key]) { | ||
| 98 | - var errconf = this.buildErrorConfig(ERROR_CONF.PARAM_ERR, ERROR_CONF.PARAM_ERR_MSG + key +'参数格式有误'); | ||
| 99 | - param.fail(errconf); | ||
| 100 | - param.complete(errconf); | ||
| 101 | - return true; | ||
| 102 | - } | ||
| 103 | - return false; | ||
| 104 | - }, | ||
| 105 | - | ||
| 106 | - /** | ||
| 107 | - * 验证参数中是否存在检索词keyword | ||
| 108 | - * | ||
| 109 | - * @param {Object} param 接口参数 | ||
| 110 | - */ | ||
| 111 | - checkKeyword(param){ | ||
| 112 | - return !this.checkParamKeyEmpty(param, 'keyword'); | ||
| 113 | - }, | ||
| 114 | - | ||
| 115 | - /** | ||
| 116 | - * 验证location值 | ||
| 117 | - * | ||
| 118 | - * @param {Object} param 接口参数 | ||
| 119 | - */ | ||
| 120 | - checkLocation(param) { | ||
| 121 | - var location = this.getLocationParam(param.location); | ||
| 122 | - if (!location || !location.latitude || !location.longitude) { | ||
| 123 | - var errconf = this.buildErrorConfig(ERROR_CONF.PARAM_ERR, ERROR_CONF.PARAM_ERR_MSG + ' location参数格式有误') | ||
| 124 | - param.fail(errconf); | ||
| 125 | - param.complete(errconf); | ||
| 126 | - return false; | ||
| 127 | - } | ||
| 128 | - return true; | ||
| 129 | - }, | ||
| 130 | - | ||
| 131 | - /** | ||
| 132 | - * 构造错误数据结构 | ||
| 133 | - * @param {Number} errCode 错误码 | ||
| 134 | - * @param {Number} errMsg 错误描述 | ||
| 135 | - */ | ||
| 136 | - buildErrorConfig(errCode, errMsg) { | ||
| 137 | - return { | ||
| 138 | - status: errCode, | ||
| 139 | - message: errMsg | ||
| 140 | - }; | ||
| 141 | - }, | ||
| 142 | - | ||
| 143 | - /** | ||
| 144 | - * 构造微信请求参数,公共属性处理 | ||
| 145 | - * | ||
| 146 | - * @param {Object} param 接口参数 | ||
| 147 | - * @param {Object} param 配置项 | ||
| 148 | - */ | ||
| 149 | - buildWxRequestConfig(param, options) { | ||
| 150 | - var that = this; | ||
| 151 | - options.header = { "content-type": "application/json" }; | ||
| 152 | - options.method = 'GET'; | ||
| 153 | - options.success = function (res) { | ||
| 154 | - var data = res.data; | ||
| 155 | - if (data.status === 0) { | ||
| 156 | - param.success(data); | ||
| 157 | - } else { | ||
| 158 | - param.fail(data); | ||
| 159 | - } | ||
| 160 | - }; | ||
| 161 | - options.fail = function (res) { | ||
| 162 | - res.statusCode = ERROR_CONF.WX_ERR_CODE; | ||
| 163 | - param.fail(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, result.errMsg)); | ||
| 164 | - }; | ||
| 165 | - options.complete = function (res) { | ||
| 166 | - var statusCode = +res.statusCode; | ||
| 167 | - switch(statusCode) { | ||
| 168 | - case ERROR_CONF.WX_ERR_CODE: { | ||
| 169 | - param.complete(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg)); | ||
| 170 | - break; | ||
| 171 | - } | ||
| 172 | - case ERROR_CONF.WX_OK_CODE: { | ||
| 173 | - var data = res.data; | ||
| 174 | - if (data.status === 0) { | ||
| 175 | - param.complete(data); | ||
| 176 | - } else { | ||
| 177 | - param.complete(that.buildErrorConfig(data.status, data.message)); | ||
| 178 | - } | ||
| 179 | - break; | ||
| 180 | - } | ||
| 181 | - default:{ | ||
| 182 | - param.complete(that.buildErrorConfig(ERROR_CONF.SYSTEM_ERR, ERROR_CONF.SYSTEM_ERR_MSG)); | ||
| 183 | - } | ||
| 184 | - | ||
| 185 | - } | ||
| 186 | - } | ||
| 187 | - return options; | ||
| 188 | - }, | ||
| 189 | - | ||
| 190 | - /** | ||
| 191 | - * 处理用户参数是否传入坐标进行不同的处理 | ||
| 192 | - */ | ||
| 193 | - locationProcess(param, locationsuccess, locationfail, locationcomplete) { | ||
| 194 | - var that = this; | ||
| 195 | - locationfail = locationfail || function (res) { | ||
| 196 | - res.statusCode = ERROR_CONF.WX_ERR_CODE; | ||
| 197 | - param.fail(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg)); | ||
| 198 | - }; | ||
| 199 | - locationcomplete = locationcomplete || function (res) { | ||
| 200 | - if (res.statusCode == ERROR_CONF.WX_ERR_CODE) { | ||
| 201 | - param.complete(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg)); | ||
| 202 | - } | ||
| 203 | - }; | ||
| 204 | - if (!param.location) { | ||
| 205 | - that.getWXLocation(locationsuccess, locationfail, locationcomplete); | ||
| 206 | - } else if (that.checkLocation(param)) { | ||
| 207 | - var location = Utils.getLocationParam(param.location); | ||
| 208 | - locationsuccess(location); | ||
| 209 | - } | ||
| 210 | - } | ||
| 211 | -} | ||
| 212 | - | ||
| 213 | - | ||
| 214 | -class QQMapWX { | ||
| 215 | - | ||
| 216 | - /** | ||
| 217 | - * 构造函数 | ||
| 218 | - * | ||
| 219 | - * @param {Object} options 接口参数,key 为必选参数 | ||
| 220 | - */ | ||
| 221 | - constructor(options) { | ||
| 222 | - if (!options.key) { | ||
| 223 | - throw Error('key值不能为空'); | ||
| 224 | - } | ||
| 225 | - this.key = options.key; | ||
| 226 | - } | ||
| 227 | - | ||
| 228 | - /** | ||
| 229 | - * POI周边检索 | ||
| 230 | - * | ||
| 231 | - * @param {Object} options 接口参数对象 | ||
| 232 | - * | ||
| 233 | - * 参数对象结构可以参考 | ||
| 234 | - * @see http://lbs.qq.com/webservice_v1/guide-search.html | ||
| 235 | - */ | ||
| 236 | - search(options) { | ||
| 237 | - var that = this; | ||
| 238 | - options = options || {}; | ||
| 239 | - | ||
| 240 | - Utils.polyfillParam(options); | ||
| 241 | - | ||
| 242 | - if (!Utils.checkKeyword(options)) { | ||
| 243 | - return; | ||
| 244 | - } | ||
| 245 | - | ||
| 246 | - var requestParam = { | ||
| 247 | - keyword: options.keyword, | ||
| 248 | - orderby: options.orderby || '_distance', | ||
| 249 | - page_size: options.page_size || 10, | ||
| 250 | - page_index: options.page_index || 1, | ||
| 251 | - output: 'json', | ||
| 252 | - key: that.key | ||
| 253 | - }; | ||
| 254 | - | ||
| 255 | - if (options.address_format) { | ||
| 256 | - requestParam.address_format = options.address_format; | ||
| 257 | - } | ||
| 258 | - | ||
| 259 | - if (options.filter) { | ||
| 260 | - requestParam.filter = options.filter; | ||
| 261 | - } | ||
| 262 | - | ||
| 263 | - var distance = options.distance || "1000"; | ||
| 264 | - var auto_extend = options.auto_extend || 1; | ||
| 265 | - | ||
| 266 | - var locationsuccess = function (result) { | ||
| 267 | - requestParam.boundary = "nearby(" + result.latitude + "," + result.longitude + "," + distance + "," + auto_extend +")"; | ||
| 268 | - wx.request(Utils.buildWxRequestConfig(options, { | ||
| 269 | - url: URL_SEARCH, | ||
| 270 | - data: requestParam | ||
| 271 | - })); | ||
| 272 | - } | ||
| 273 | - Utils.locationProcess(options, locationsuccess); | ||
| 274 | - } | ||
| 275 | - | ||
| 276 | - /** | ||
| 277 | - * sug模糊检索 | ||
| 278 | - * | ||
| 279 | - * @param {Object} options 接口参数对象 | ||
| 280 | - * | ||
| 281 | - * 参数对象结构可以参考 | ||
| 282 | - * http://lbs.qq.com/webservice_v1/guide-suggestion.html | ||
| 283 | - */ | ||
| 284 | - getSuggestion(options) { | ||
| 285 | - var that = this; | ||
| 286 | - options = options || {}; | ||
| 287 | - Utils.polyfillParam(options); | ||
| 288 | - | ||
| 289 | - if (!Utils.checkKeyword(options)) { | ||
| 290 | - return; | ||
| 291 | - } | ||
| 292 | - | ||
| 293 | - var requestParam = { | ||
| 294 | - keyword: options.keyword, | ||
| 295 | - region: options.region || '全国', | ||
| 296 | - region_fix: options.region_fix || 0, | ||
| 297 | - policy: options.policy || 0, | ||
| 298 | - output: 'json', | ||
| 299 | - key: that.key | ||
| 300 | - }; | ||
| 301 | - wx.request(Utils.buildWxRequestConfig(options, { | ||
| 302 | - url: URL_SUGGESTION, | ||
| 303 | - data: requestParam | ||
| 304 | - })); | ||
| 305 | - } | ||
| 306 | - | ||
| 307 | - /** | ||
| 308 | - * 逆地址解析 | ||
| 309 | - * | ||
| 310 | - * @param {Object} options 接口参数对象 | ||
| 311 | - * | ||
| 312 | - * 请求参数结构可以参考 | ||
| 313 | - * http://lbs.qq.com/webservice_v1/guide-gcoder.html | ||
| 314 | - */ | ||
| 315 | - reverseGeocoder(options) { | ||
| 316 | - var that = this; | ||
| 317 | - options = options || {}; | ||
| 318 | - Utils.polyfillParam(options); | ||
| 319 | - var requestParam = { | ||
| 320 | - coord_type: options.coord_type || 5, | ||
| 321 | - get_poi: options.get_poi || 0, | ||
| 322 | - output: 'json', | ||
| 323 | - key: that.key | ||
| 324 | - }; | ||
| 325 | - if (options.poi_options) { | ||
| 326 | - requestParam.poi_options = options.poi_options | ||
| 327 | - } | ||
| 328 | - | ||
| 329 | - var locationsuccess = function (result) { | ||
| 330 | - requestParam.location = result.latitude + ',' + result.longitude; | ||
| 331 | - wx.request(Utils.buildWxRequestConfig(options, { | ||
| 332 | - url: URL_GET_GEOCODER, | ||
| 333 | - data: requestParam | ||
| 334 | - })); | ||
| 335 | - }; | ||
| 336 | - Utils.locationProcess(options, locationsuccess); | ||
| 337 | - } | ||
| 338 | - | ||
| 339 | - /** | ||
| 340 | - * 地址解析 | ||
| 341 | - * | ||
| 342 | - * @param {Object} options 接口参数对象 | ||
| 343 | - * | ||
| 344 | - * 请求参数结构可以参考 | ||
| 345 | - * http://lbs.qq.com/webservice_v1/guide-geocoder.html | ||
| 346 | - */ | ||
| 347 | - geocoder(options) { | ||
| 348 | - var that = this; | ||
| 349 | - options = options || {}; | ||
| 350 | - Utils.polyfillParam(options); | ||
| 351 | - | ||
| 352 | - if (Utils.checkParamKeyEmpty(options, 'address')) { | ||
| 353 | - return; | ||
| 354 | - } | ||
| 355 | - | ||
| 356 | - var requestParam = { | ||
| 357 | - address: options.address, | ||
| 358 | - output: 'json', | ||
| 359 | - key: that.key | ||
| 360 | - }; | ||
| 361 | - | ||
| 362 | - wx.request(Utils.buildWxRequestConfig(options, { | ||
| 363 | - url: URL_GET_GEOCODER, | ||
| 364 | - data: requestParam | ||
| 365 | - })); | ||
| 366 | - } | ||
| 367 | - | ||
| 368 | - | ||
| 369 | - /** | ||
| 370 | - * 获取城市列表 | ||
| 371 | - * | ||
| 372 | - * @param {Object} options 接口参数对象 | ||
| 373 | - * | ||
| 374 | - * 请求参数结构可以参考 | ||
| 375 | - * http://lbs.qq.com/webservice_v1/guide-region.html | ||
| 376 | - */ | ||
| 377 | - getCityList(options) { | ||
| 378 | - var that = this; | ||
| 379 | - options = options || {}; | ||
| 380 | - Utils.polyfillParam(options); | ||
| 381 | - var requestParam = { | ||
| 382 | - output: 'json', | ||
| 383 | - key: that.key | ||
| 384 | - }; | ||
| 385 | - | ||
| 386 | - wx.request(Utils.buildWxRequestConfig(options, { | ||
| 387 | - url: URL_CITY_LIST, | ||
| 388 | - data: requestParam | ||
| 389 | - })); | ||
| 390 | - } | ||
| 391 | - | ||
| 392 | - /** | ||
| 393 | - * 获取对应城市ID的区县列表 | ||
| 394 | - * | ||
| 395 | - * @param {Object} options 接口参数对象 | ||
| 396 | - * | ||
| 397 | - * 请求参数结构可以参考 | ||
| 398 | - * http://lbs.qq.com/webservice_v1/guide-region.html | ||
| 399 | - */ | ||
| 400 | - getDistrictByCityId(options) { | ||
| 401 | - var that = this; | ||
| 402 | - options = options || {}; | ||
| 403 | - Utils.polyfillParam(options); | ||
| 404 | - | ||
| 405 | - if (Utils.checkParamKeyEmpty(options, 'id')) { | ||
| 406 | - return; | ||
| 407 | - } | ||
| 408 | - | ||
| 409 | - var requestParam = { | ||
| 410 | - id: options.id || '', | ||
| 411 | - output: 'json', | ||
| 412 | - key: that.key | ||
| 413 | - }; | ||
| 414 | - | ||
| 415 | - wx.request(Utils.buildWxRequestConfig(options, { | ||
| 416 | - url: URL_AREA_LIST, | ||
| 417 | - data: requestParam | ||
| 418 | - })); | ||
| 419 | - } | ||
| 420 | - | ||
| 421 | - /** | ||
| 422 | - * 用于单起点到多终点的路线距离(非直线距离)计算: | ||
| 423 | - * 支持两种距离计算方式:步行和驾车。 | ||
| 424 | - * 起点到终点最大限制直线距离10公里。 | ||
| 425 | - * | ||
| 426 | - * @param {Object} options 接口参数对象 | ||
| 427 | - * | ||
| 428 | - * 请求参数结构可以参考 | ||
| 429 | - * http://lbs.qq.com/webservice_v1/guide-distance.html | ||
| 430 | - */ | ||
| 431 | - calculateDistance(options) { | ||
| 432 | - var that = this; | ||
| 433 | - options = options || {}; | ||
| 434 | - Utils.polyfillParam(options); | ||
| 435 | - | ||
| 436 | - if (Utils.checkParamKeyEmpty(options, 'to')) { | ||
| 437 | - return; | ||
| 438 | - } | ||
| 439 | - | ||
| 440 | - var requestParam = { | ||
| 441 | - mode: options.mode || 'walking', | ||
| 442 | - to: Utils.location2query(options.to), | ||
| 443 | - output: 'json', | ||
| 444 | - key: that.key | ||
| 445 | - }; | ||
| 446 | - | ||
| 447 | - var locationsuccess = function (result) { | ||
| 448 | - requestParam.from = result.latitude + ',' + result.longitude; | ||
| 449 | - wx.request(Utils.buildWxRequestConfig(options, { | ||
| 450 | - url: URL_DISTANCE, | ||
| 451 | - data: requestParam | ||
| 452 | - })); | ||
| 453 | - } | ||
| 454 | - if (options.from) { | ||
| 455 | - options.location = options.from; | ||
| 456 | - } | ||
| 457 | - | ||
| 458 | - Utils.locationProcess(options, locationsuccess); | ||
| 459 | - } | ||
| 460 | -} | ||
| 461 | - | ||
| 462 | -module.exports = QQMapWX; |
-
Please register or login to post a comment