qqmap-wx-jssdk.min.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. var ERROR_CONF = {
  2. KEY_ERR: 311,
  3. KEY_ERR_MSG: 'key格式错误',
  4. PARAM_ERR: 310,
  5. PARAM_ERR_MSG: '请求参数信息有误',
  6. SYSTEM_ERR: 600,
  7. SYSTEM_ERR_MSG: '系统错误',
  8. WX_ERR_CODE: 1000,
  9. WX_OK_CODE: 200
  10. };
  11. var BASE_URL = 'https://apis.map.qq.com/ws/';
  12. var URL_SEARCH = BASE_URL + 'place/v1/search';
  13. var URL_SUGGESTION = BASE_URL + 'place/v1/suggestion';
  14. var URL_GET_GEOCODER = BASE_URL + 'geocoder/v1/';
  15. var URL_CITY_LIST = BASE_URL + 'district/v1/list';
  16. var URL_AREA_LIST = BASE_URL + 'district/v1/getchildren';
  17. var URL_DISTANCE = BASE_URL + 'distance/v1/';
  18. var URL_DIRECTION = BASE_URL + 'direction/v1/';
  19. var MODE = {
  20. driving: 'driving',
  21. transit: 'transit'
  22. };
  23. var EARTH_RADIUS = 6378136.49;
  24. var Utils = {
  25. safeAdd(x, y) {
  26. var lsw = (x & 0xffff) + (y & 0xffff);
  27. var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  28. return (msw << 16) | (lsw & 0xffff)
  29. },
  30. bitRotateLeft(num, cnt) {
  31. return (num << cnt) | (num >>> (32 - cnt))
  32. },
  33. md5cmn(q, a, b, x, s, t) {
  34. return this.safeAdd(this.bitRotateLeft(this.safeAdd(this.safeAdd(a, q), this.safeAdd(x, t)), s), b)
  35. },
  36. md5ff(a, b, c, d, x, s, t) {
  37. return this.md5cmn((b & c) | (~b & d), a, b, x, s, t)
  38. },
  39. md5gg(a, b, c, d, x, s, t) {
  40. return this.md5cmn((b & d) | (c & ~d), a, b, x, s, t)
  41. },
  42. md5hh(a, b, c, d, x, s, t) {
  43. return this.md5cmn(b ^ c ^ d, a, b, x, s, t)
  44. },
  45. md5ii(a, b, c, d, x, s, t) {
  46. return this.md5cmn(c ^ (b | ~d), a, b, x, s, t)
  47. },
  48. binlMD5(x, len) {
  49. x[len >> 5] |= 0x80 << (len % 32);
  50. x[((len + 64) >>> 9 << 4) + 14] = len;
  51. var i;
  52. var olda;
  53. var oldb;
  54. var oldc;
  55. var oldd;
  56. var a = 1732584193;
  57. var b = -271733879;
  58. var c = -1732584194;
  59. var d = 271733878;
  60. for (i = 0; i < x.length; i += 16) {
  61. olda = a;
  62. oldb = b;
  63. oldc = c;
  64. oldd = d;
  65. a = this.md5ff(a, b, c, d, x[i], 7, -680876936);
  66. d = this.md5ff(d, a, b, c, x[i + 1], 12, -389564586);
  67. c = this.md5ff(c, d, a, b, x[i + 2], 17, 606105819);
  68. b = this.md5ff(b, c, d, a, x[i + 3], 22, -1044525330);
  69. a = this.md5ff(a, b, c, d, x[i + 4], 7, -176418897);
  70. d = this.md5ff(d, a, b, c, x[i + 5], 12, 1200080426);
  71. c = this.md5ff(c, d, a, b, x[i + 6], 17, -1473231341);
  72. b = this.md5ff(b, c, d, a, x[i + 7], 22, -45705983);
  73. a = this.md5ff(a, b, c, d, x[i + 8], 7, 1770035416);
  74. d = this.md5ff(d, a, b, c, x[i + 9], 12, -1958414417);
  75. c = this.md5ff(c, d, a, b, x[i + 10], 17, -42063);
  76. b = this.md5ff(b, c, d, a, x[i + 11], 22, -1990404162);
  77. a = this.md5ff(a, b, c, d, x[i + 12], 7, 1804603682);
  78. d = this.md5ff(d, a, b, c, x[i + 13], 12, -40341101);
  79. c = this.md5ff(c, d, a, b, x[i + 14], 17, -1502002290);
  80. b = this.md5ff(b, c, d, a, x[i + 15], 22, 1236535329);
  81. a = this.md5gg(a, b, c, d, x[i + 1], 5, -165796510);
  82. d = this.md5gg(d, a, b, c, x[i + 6], 9, -1069501632);
  83. c = this.md5gg(c, d, a, b, x[i + 11], 14, 643717713);
  84. b = this.md5gg(b, c, d, a, x[i], 20, -373897302);
  85. a = this.md5gg(a, b, c, d, x[i + 5], 5, -701558691);
  86. d = this.md5gg(d, a, b, c, x[i + 10], 9, 38016083);
  87. c = this.md5gg(c, d, a, b, x[i + 15], 14, -660478335);
  88. b = this.md5gg(b, c, d, a, x[i + 4], 20, -405537848);
  89. a = this.md5gg(a, b, c, d, x[i + 9], 5, 568446438);
  90. d = this.md5gg(d, a, b, c, x[i + 14], 9, -1019803690);
  91. c = this.md5gg(c, d, a, b, x[i + 3], 14, -187363961);
  92. b = this.md5gg(b, c, d, a, x[i + 8], 20, 1163531501);
  93. a = this.md5gg(a, b, c, d, x[i + 13], 5, -1444681467);
  94. d = this.md5gg(d, a, b, c, x[i + 2], 9, -51403784);
  95. c = this.md5gg(c, d, a, b, x[i + 7], 14, 1735328473);
  96. b = this.md5gg(b, c, d, a, x[i + 12], 20, -1926607734);
  97. a = this.md5hh(a, b, c, d, x[i + 5], 4, -378558);
  98. d = this.md5hh(d, a, b, c, x[i + 8], 11, -2022574463);
  99. c = this.md5hh(c, d, a, b, x[i + 11], 16, 1839030562);
  100. b = this.md5hh(b, c, d, a, x[i + 14], 23, -35309556);
  101. a = this.md5hh(a, b, c, d, x[i + 1], 4, -1530992060);
  102. d = this.md5hh(d, a, b, c, x[i + 4], 11, 1272893353);
  103. c = this.md5hh(c, d, a, b, x[i + 7], 16, -155497632);
  104. b = this.md5hh(b, c, d, a, x[i + 10], 23, -1094730640);
  105. a = this.md5hh(a, b, c, d, x[i + 13], 4, 681279174);
  106. d = this.md5hh(d, a, b, c, x[i], 11, -358537222);
  107. c = this.md5hh(c, d, a, b, x[i + 3], 16, -722521979);
  108. b = this.md5hh(b, c, d, a, x[i + 6], 23, 76029189);
  109. a = this.md5hh(a, b, c, d, x[i + 9], 4, -640364487);
  110. d = this.md5hh(d, a, b, c, x[i + 12], 11, -421815835);
  111. c = this.md5hh(c, d, a, b, x[i + 15], 16, 530742520);
  112. b = this.md5hh(b, c, d, a, x[i + 2], 23, -995338651);
  113. a = this.md5ii(a, b, c, d, x[i], 6, -198630844);
  114. d = this.md5ii(d, a, b, c, x[i + 7], 10, 1126891415);
  115. c = this.md5ii(c, d, a, b, x[i + 14], 15, -1416354905);
  116. b = this.md5ii(b, c, d, a, x[i + 5], 21, -57434055);
  117. a = this.md5ii(a, b, c, d, x[i + 12], 6, 1700485571);
  118. d = this.md5ii(d, a, b, c, x[i + 3], 10, -1894986606);
  119. c = this.md5ii(c, d, a, b, x[i + 10], 15, -1051523);
  120. b = this.md5ii(b, c, d, a, x[i + 1], 21, -2054922799);
  121. a = this.md5ii(a, b, c, d, x[i + 8], 6, 1873313359);
  122. d = this.md5ii(d, a, b, c, x[i + 15], 10, -30611744);
  123. c = this.md5ii(c, d, a, b, x[i + 6], 15, -1560198380);
  124. b = this.md5ii(b, c, d, a, x[i + 13], 21, 1309151649);
  125. a = this.md5ii(a, b, c, d, x[i + 4], 6, -145523070);
  126. d = this.md5ii(d, a, b, c, x[i + 11], 10, -1120210379);
  127. c = this.md5ii(c, d, a, b, x[i + 2], 15, 718787259);
  128. b = this.md5ii(b, c, d, a, x[i + 9], 21, -343485551);
  129. a = this.safeAdd(a, olda);
  130. b = this.safeAdd(b, oldb);
  131. c = this.safeAdd(c, oldc);
  132. d = this.safeAdd(d, oldd)
  133. }
  134. return [a, b, c, d]
  135. },
  136. binl2rstr(input) {
  137. var i;
  138. var output = '';
  139. var length32 = input.length * 32;
  140. for (i = 0; i < length32; i += 8) {
  141. output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xff)
  142. }
  143. return output
  144. },
  145. rstr2binl(input) {
  146. var i;
  147. var output = [];
  148. output[(input.length >> 2) - 1] = undefined;
  149. for (i = 0; i < output.length; i += 1) {
  150. output[i] = 0
  151. }
  152. var length8 = input.length * 8;
  153. for (i = 0; i < length8; i += 8) {
  154. output[i >> 5] |= (input.charCodeAt(i / 8) & 0xff) << (i % 32)
  155. }
  156. return output
  157. },
  158. rstrMD5(s) {
  159. return this.binl2rstr(this.binlMD5(this.rstr2binl(s), s.length * 8))
  160. },
  161. rstrHMACMD5(key, data) {
  162. var i;
  163. var bkey = this.rstr2binl(key);
  164. var ipad = [];
  165. var opad = [];
  166. var hash;
  167. ipad[15] = opad[15] = undefined;
  168. if (bkey.length > 16) {
  169. bkey = this.binlMD5(bkey, key.length * 8)
  170. }
  171. for (i = 0; i < 16; i += 1) {
  172. ipad[i] = bkey[i] ^ 0x36363636;
  173. opad[i] = bkey[i] ^ 0x5c5c5c5c
  174. }
  175. hash = this.binlMD5(ipad.concat(this.rstr2binl(data)), 512 + data.length * 8);
  176. return this.binl2rstr(this.binlMD5(opad.concat(hash), 512 + 128))
  177. },
  178. rstr2hex(input) {
  179. var hexTab = '0123456789abcdef';
  180. var output = '';
  181. var x;
  182. var i;
  183. for (i = 0; i < input.length; i += 1) {
  184. x = input.charCodeAt(i);
  185. output += hexTab.charAt((x >>> 4) & 0x0f) + hexTab.charAt(x & 0x0f)
  186. }
  187. return output
  188. },
  189. str2rstrUTF8(input) {
  190. return unescape(encodeURIComponent(input))
  191. },
  192. rawMD5(s) {
  193. return this.rstrMD5(this.str2rstrUTF8(s))
  194. },
  195. hexMD5(s) {
  196. return this.rstr2hex(this.rawMD5(s))
  197. },
  198. rawHMACMD5(k, d) {
  199. return this.rstrHMACMD5(this.str2rstrUTF8(k), str2rstrUTF8(d))
  200. },
  201. hexHMACMD5(k, d) {
  202. return this.rstr2hex(this.rawHMACMD5(k, d))
  203. },
  204. md5(string, key, raw) {
  205. if (!key) {
  206. if (!raw) {
  207. return this.hexMD5(string)
  208. }
  209. return this.rawMD5(string)
  210. }
  211. if (!raw) {
  212. return this.hexHMACMD5(key, string)
  213. }
  214. return this.rawHMACMD5(key, string)
  215. },
  216. getSig(requestParam, sk, feature, mode) {
  217. var sig = null;
  218. var requestArr = [];
  219. Object.keys(requestParam).sort().forEach(function(key) {
  220. requestArr.push(key + '=' + requestParam[key])
  221. });
  222. if (feature == 'search') {
  223. sig = '/ws/place/v1/search?' + requestArr.join('&') + sk
  224. }
  225. if (feature == 'suggest') {
  226. sig = '/ws/place/v1/suggestion?' + requestArr.join('&') + sk
  227. }
  228. if (feature == 'reverseGeocoder') {
  229. sig = '/ws/geocoder/v1/?' + requestArr.join('&') + sk
  230. }
  231. if (feature == 'geocoder') {
  232. sig = '/ws/geocoder/v1/?' + requestArr.join('&') + sk
  233. }
  234. if (feature == 'getCityList') {
  235. sig = '/ws/district/v1/list?' + requestArr.join('&') + sk
  236. }
  237. if (feature == 'getDistrictByCityId') {
  238. sig = '/ws/district/v1/getchildren?' + requestArr.join('&') + sk
  239. }
  240. if (feature == 'calculateDistance') {
  241. sig = '/ws/distance/v1/?' + requestArr.join('&') + sk
  242. }
  243. if (feature == 'direction') {
  244. sig = '/ws/direction/v1/' + mode + '?' + requestArr.join('&') + sk
  245. }
  246. sig = this.md5(sig);
  247. return sig
  248. },
  249. location2query(data) {
  250. if (typeof data == 'string') {
  251. return data
  252. }
  253. var query = '';
  254. for (var i = 0; i < data.length; i++) {
  255. var d = data[i];
  256. if (!!query) {
  257. query += ';'
  258. }
  259. if (d.location) {
  260. query = query + d.location.lat + ',' + d.location.lng
  261. }
  262. if (d.latitude && d.longitude) {
  263. query = query + d.latitude + ',' + d.longitude
  264. }
  265. }
  266. return query
  267. },
  268. rad(d) {
  269. return d * Math.PI / 180.0
  270. },
  271. getEndLocation(location) {
  272. var to = location.split(';');
  273. var endLocation = [];
  274. for (var i = 0; i < to.length; i++) {
  275. endLocation.push({
  276. lat: parseFloat(to[i].split(',')[0]),
  277. lng: parseFloat(to[i].split(',')[1])
  278. })
  279. }
  280. return endLocation
  281. },
  282. getDistance(latFrom, lngFrom, latTo, lngTo) {
  283. var radLatFrom = this.rad(latFrom);
  284. var radLatTo = this.rad(latTo);
  285. var a = radLatFrom - radLatTo;
  286. var b = this.rad(lngFrom) - this.rad(lngTo);
  287. var distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLatFrom) * Math.cos(
  288. radLatTo) * Math.pow(Math.sin(b / 2), 2)));
  289. distance = distance * EARTH_RADIUS;
  290. distance = Math.round(distance * 10000) / 10000;
  291. return parseFloat(distance.toFixed(0))
  292. },
  293. getWXLocation(success, fail, complete) {
  294. wx.getLocation({
  295. type: 'gcj02',
  296. success: success,
  297. fail: fail,
  298. complete: complete
  299. })
  300. },
  301. getLocationParam(location) {
  302. if (typeof location == 'string') {
  303. var locationArr = location.split(',');
  304. if (locationArr.length === 2) {
  305. location = {
  306. latitude: location.split(',')[0],
  307. longitude: location.split(',')[1]
  308. }
  309. } else {
  310. location = {}
  311. }
  312. }
  313. return location
  314. },
  315. polyfillParam(param) {
  316. param.success = param.success || function() {};
  317. param.fail = param.fail || function() {};
  318. param.complete = param.complete || function() {}
  319. },
  320. checkParamKeyEmpty(param, key) {
  321. if (!param[key]) {
  322. var errconf = this.buildErrorConfig(ERROR_CONF.PARAM_ERR, ERROR_CONF.PARAM_ERR_MSG + key + '参数格式有误');
  323. param.fail(errconf);
  324. param.complete(errconf);
  325. return true
  326. }
  327. return false
  328. },
  329. checkKeyword(param) {
  330. return !this.checkParamKeyEmpty(param, 'keyword')
  331. },
  332. checkLocation(param) {
  333. var location = this.getLocationParam(param.location);
  334. if (!location || !location.latitude || !location.longitude) {
  335. var errconf = this.buildErrorConfig(ERROR_CONF.PARAM_ERR, ERROR_CONF.PARAM_ERR_MSG + ' location参数格式有误');
  336. param.fail(errconf);
  337. param.complete(errconf);
  338. return false
  339. }
  340. return true
  341. },
  342. buildErrorConfig(errCode, errMsg) {
  343. return {
  344. status: errCode,
  345. message: errMsg
  346. }
  347. },
  348. handleData(param, data, feature) {
  349. if (feature == 'search') {
  350. var searchResult = data.data;
  351. var searchSimplify = [];
  352. for (var i = 0; i < searchResult.length; i++) {
  353. searchSimplify.push({
  354. id: searchResult[i].id || null,
  355. title: searchResult[i].title || null,
  356. latitude: searchResult[i].location && searchResult[i].location.lat || null,
  357. longitude: searchResult[i].location && searchResult[i].location.lng || null,
  358. address: searchResult[i].address || null,
  359. category: searchResult[i].category || null,
  360. tel: searchResult[i].tel || null,
  361. adcode: searchResult[i].ad_info && searchResult[i].ad_info.adcode || null,
  362. city: searchResult[i].ad_info && searchResult[i].ad_info.city || null,
  363. district: searchResult[i].ad_info && searchResult[i].ad_info.district || null,
  364. province: searchResult[i].ad_info && searchResult[i].ad_info.province || null
  365. })
  366. }
  367. param.success(data, {
  368. searchResult: searchResult,
  369. searchSimplify: searchSimplify
  370. })
  371. } else if (feature == 'suggest') {
  372. var suggestResult = data.data;
  373. var suggestSimplify = [];
  374. for (var i = 0; i < suggestResult.length; i++) {
  375. suggestSimplify.push({
  376. adcode: suggestResult[i].adcode || null,
  377. address: suggestResult[i].address || null,
  378. category: suggestResult[i].category || null,
  379. city: suggestResult[i].city || null,
  380. district: suggestResult[i].district || null,
  381. id: suggestResult[i].id || null,
  382. latitude: suggestResult[i].location && suggestResult[i].location.lat || null,
  383. longitude: suggestResult[i].location && suggestResult[i].location.lng || null,
  384. province: suggestResult[i].province || null,
  385. title: suggestResult[i].title || null,
  386. type: suggestResult[i].type || null
  387. })
  388. }
  389. param.success(data, {
  390. suggestResult: suggestResult,
  391. suggestSimplify: suggestSimplify
  392. })
  393. } else if (feature == 'reverseGeocoder') {
  394. var reverseGeocoderResult = data.result;
  395. var reverseGeocoderSimplify = {
  396. address: reverseGeocoderResult.address || null,
  397. latitude: reverseGeocoderResult.location && reverseGeocoderResult.location.lat || null,
  398. longitude: reverseGeocoderResult.location && reverseGeocoderResult.location.lng || null,
  399. adcode: reverseGeocoderResult.ad_info && reverseGeocoderResult.ad_info.adcode || null,
  400. city: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.city ||
  401. null,
  402. district: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component
  403. .district || null,
  404. nation: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component
  405. .nation || null,
  406. province: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component
  407. .province || null,
  408. street: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component
  409. .street || null,
  410. street_number: reverseGeocoderResult.address_component && reverseGeocoderResult
  411. .address_component.street_number || null,
  412. recommend: reverseGeocoderResult.formatted_addresses && reverseGeocoderResult
  413. .formatted_addresses.recommend || null,
  414. rough: reverseGeocoderResult.formatted_addresses && reverseGeocoderResult.formatted_addresses
  415. .rough || null
  416. };
  417. if (reverseGeocoderResult.pois) {
  418. var pois = reverseGeocoderResult.pois;
  419. var poisSimplify = [];
  420. for (var i = 0; i < pois.length; i++) {
  421. poisSimplify.push({
  422. id: pois[i].id || null,
  423. title: pois[i].title || null,
  424. latitude: pois[i].location && pois[i].location.lat || null,
  425. longitude: pois[i].location && pois[i].location.lng || null,
  426. address: pois[i].address || null,
  427. category: pois[i].category || null,
  428. adcode: pois[i].ad_info && pois[i].ad_info.adcode || null,
  429. city: pois[i].ad_info && pois[i].ad_info.city || null,
  430. district: pois[i].ad_info && pois[i].ad_info.district || null,
  431. province: pois[i].ad_info && pois[i].ad_info.province || null
  432. })
  433. }
  434. param.success(data, {
  435. reverseGeocoderResult: reverseGeocoderResult,
  436. reverseGeocoderSimplify: reverseGeocoderSimplify,
  437. pois: pois,
  438. poisSimplify: poisSimplify
  439. })
  440. } else {
  441. param.success(data, {
  442. reverseGeocoderResult: reverseGeocoderResult,
  443. reverseGeocoderSimplify: reverseGeocoderSimplify
  444. })
  445. }
  446. } else if (feature == 'geocoder') {
  447. var geocoderResult = data.result;
  448. var geocoderSimplify = {
  449. title: geocoderResult.title || null,
  450. latitude: geocoderResult.location && geocoderResult.location.lat || null,
  451. longitude: geocoderResult.location && geocoderResult.location.lng || null,
  452. adcode: geocoderResult.ad_info && geocoderResult.ad_info.adcode || null,
  453. province: geocoderResult.address_components && geocoderResult.address_components.province ||
  454. null,
  455. city: geocoderResult.address_components && geocoderResult.address_components.city || null,
  456. district: geocoderResult.address_components && geocoderResult.address_components.district ||
  457. null,
  458. street: geocoderResult.address_components && geocoderResult.address_components.street || null,
  459. street_number: geocoderResult.address_components && geocoderResult.address_components
  460. .street_number || null,
  461. level: geocoderResult.level || null
  462. };
  463. param.success(data, {
  464. geocoderResult: geocoderResult,
  465. geocoderSimplify: geocoderSimplify
  466. })
  467. } else if (feature == 'getCityList') {
  468. var provinceResult = data.result[0];
  469. var cityResult = data.result[1];
  470. var districtResult = data.result[2];
  471. param.success(data, {
  472. provinceResult: provinceResult,
  473. cityResult: cityResult,
  474. districtResult: districtResult
  475. })
  476. } else if (feature == 'getDistrictByCityId') {
  477. var districtByCity = data.result[0];
  478. param.success(data, districtByCity)
  479. } else if (feature == 'calculateDistance') {
  480. var calculateDistanceResult = data.result.elements;
  481. var distance = [];
  482. for (var i = 0; i < calculateDistanceResult.length; i++) {
  483. distance.push(calculateDistanceResult[i].distance)
  484. }
  485. param.success(data, {
  486. calculateDistanceResult: calculateDistanceResult,
  487. distance: distance
  488. })
  489. } else if (feature == 'direction') {
  490. var direction = data.result.routes;
  491. param.success(data, direction)
  492. } else {
  493. param.success(data)
  494. }
  495. },
  496. buildWxRequestConfig(param, options, feature) {
  497. var that = this;
  498. options.header = {
  499. "content-type": "application/json"
  500. };
  501. options.method = 'GET';
  502. options.success = function(res) {
  503. var data = res.data;
  504. if (data.status === 0) {
  505. that.handleData(param, data, feature)
  506. } else {
  507. param.fail(data)
  508. }
  509. };
  510. options.fail = function(res) {
  511. res.statusCode = ERROR_CONF.WX_ERR_CODE;
  512. param.fail(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg))
  513. };
  514. options.complete = function(res) {
  515. var statusCode = +res.statusCode;
  516. switch (statusCode) {
  517. case ERROR_CONF.WX_ERR_CODE: {
  518. param.complete(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg));
  519. break
  520. }
  521. case ERROR_CONF.WX_OK_CODE: {
  522. var data = res.data;
  523. if (data.status === 0) {
  524. param.complete(data)
  525. } else {
  526. param.complete(that.buildErrorConfig(data.status, data.message))
  527. }
  528. break
  529. }
  530. default: {
  531. param.complete(that.buildErrorConfig(ERROR_CONF.SYSTEM_ERR, ERROR_CONF.SYSTEM_ERR_MSG))
  532. }
  533. }
  534. };
  535. return options
  536. },
  537. locationProcess(param, locationsuccess, locationfail, locationcomplete) {
  538. var that = this;
  539. locationfail = locationfail || function(res) {
  540. res.statusCode = ERROR_CONF.WX_ERR_CODE;
  541. param.fail(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg))
  542. };
  543. locationcomplete = locationcomplete || function(res) {
  544. if (res.statusCode == ERROR_CONF.WX_ERR_CODE) {
  545. param.complete(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg))
  546. }
  547. };
  548. if (!param.location) {
  549. that.getWXLocation(locationsuccess, locationfail, locationcomplete)
  550. } else if (that.checkLocation(param)) {
  551. var location = Utils.getLocationParam(param.location);
  552. locationsuccess(location)
  553. }
  554. }
  555. };
  556. class QQMapWX {
  557. constructor(options) {
  558. if (!options.key) {
  559. throw Error('key值不能为空')
  560. }
  561. this.key = options.key
  562. };
  563. search(options) {
  564. var that = this;
  565. options = options || {};
  566. Utils.polyfillParam(options);
  567. if (!Utils.checkKeyword(options)) {
  568. return
  569. }
  570. var requestParam = {
  571. keyword: options.keyword,
  572. orderby: options.orderby || '_distance',
  573. page_size: options.page_size || 10,
  574. page_index: options.page_index || 1,
  575. output: 'json',
  576. key: that.key
  577. };
  578. if (options.address_format) {
  579. requestParam.address_format = options.address_format
  580. }
  581. if (options.filter) {
  582. requestParam.filter = options.filter
  583. }
  584. var distance = options.distance || "1000";
  585. var auto_extend = options.auto_extend || 1;
  586. var region = null;
  587. var rectangle = null;
  588. if (options.region) {
  589. region = options.region
  590. }
  591. if (options.rectangle) {
  592. rectangle = options.rectangle
  593. }
  594. var locationsuccess = function(result) {
  595. if (region && !rectangle) {
  596. requestParam.boundary = "region(" + region + "," + auto_extend + "," + result.latitude + "," +
  597. result.longitude + ")";
  598. if (options.sig) {
  599. requestParam.sig = Utils.getSig(requestParam, options.sig, 'search')
  600. }
  601. } else if (rectangle && !region) {
  602. requestParam.boundary = "rectangle(" + rectangle + ")";
  603. if (options.sig) {
  604. requestParam.sig = Utils.getSig(requestParam, options.sig, 'search')
  605. }
  606. } else {
  607. requestParam.boundary = "nearby(" + result.latitude + "," + result.longitude + "," + distance +
  608. "," + auto_extend + ")";
  609. if (options.sig) {
  610. requestParam.sig = Utils.getSig(requestParam, options.sig, 'search')
  611. }
  612. }
  613. wx.request(Utils.buildWxRequestConfig(options, {
  614. url: URL_SEARCH,
  615. data: requestParam
  616. }, 'search'))
  617. };
  618. Utils.locationProcess(options, locationsuccess)
  619. };
  620. getSuggestion(options) {
  621. var that = this;
  622. options = options || {};
  623. Utils.polyfillParam(options);
  624. if (!Utils.checkKeyword(options)) {
  625. return
  626. }
  627. var requestParam = {
  628. keyword: options.keyword,
  629. region: options.region || '全国',
  630. region_fix: options.region_fix || 0,
  631. policy: options.policy || 0,
  632. page_size: options.page_size || 10,
  633. page_index: options.page_index || 1,
  634. get_subpois: options.get_subpois || 0,
  635. output: 'json',
  636. key: that.key
  637. };
  638. if (options.address_format) {
  639. requestParam.address_format = options.address_format
  640. }
  641. if (options.filter) {
  642. requestParam.filter = options.filter
  643. }
  644. if (options.location) {
  645. var locationsuccess = function(result) {
  646. requestParam.location = result.latitude + ',' + result.longitude;
  647. if (options.sig) {
  648. requestParam.sig = Utils.getSig(requestParam, options.sig, 'suggest')
  649. }
  650. wx.request(Utils.buildWxRequestConfig(options, {
  651. url: URL_SUGGESTION,
  652. data: requestParam
  653. }, "suggest"))
  654. };
  655. Utils.locationProcess(options, locationsuccess)
  656. } else {
  657. if (options.sig) {
  658. requestParam.sig = Utils.getSig(requestParam, options.sig, 'suggest')
  659. }
  660. wx.request(Utils.buildWxRequestConfig(options, {
  661. url: URL_SUGGESTION,
  662. data: requestParam
  663. }, "suggest"))
  664. }
  665. };
  666. reverseGeocoder(options) {
  667. var that = this;
  668. options = options || {};
  669. Utils.polyfillParam(options);
  670. var requestParam = {
  671. coord_type: options.coord_type || 5,
  672. get_poi: options.get_poi || 0,
  673. output: 'json',
  674. key: that.key
  675. };
  676. if (options.poi_options) {
  677. requestParam.poi_options = options.poi_options
  678. }
  679. var locationsuccess = function(result) {
  680. requestParam.location = result.latitude + ',' + result.longitude;
  681. if (options.sig) {
  682. requestParam.sig = Utils.getSig(requestParam, options.sig, 'reverseGeocoder')
  683. }
  684. wx.request(Utils.buildWxRequestConfig(options, {
  685. url: URL_GET_GEOCODER,
  686. data: requestParam
  687. }, 'reverseGeocoder'))
  688. };
  689. Utils.locationProcess(options, locationsuccess)
  690. };
  691. geocoder(options) {
  692. var that = this;
  693. options = options || {};
  694. Utils.polyfillParam(options);
  695. if (Utils.checkParamKeyEmpty(options, 'address')) {
  696. return
  697. }
  698. var requestParam = {
  699. address: options.address,
  700. output: 'json',
  701. key: that.key
  702. };
  703. if (options.region) {
  704. requestParam.region = options.region
  705. }
  706. if (options.sig) {
  707. requestParam.sig = Utils.getSig(requestParam, options.sig, 'geocoder')
  708. }
  709. wx.request(Utils.buildWxRequestConfig(options, {
  710. url: URL_GET_GEOCODER,
  711. data: requestParam
  712. }, 'geocoder'))
  713. };
  714. getCityList(options) {
  715. var that = this;
  716. options = options || {};
  717. Utils.polyfillParam(options);
  718. var requestParam = {
  719. output: 'json',
  720. key: that.key
  721. };
  722. if (options.sig) {
  723. requestParam.sig = Utils.getSig(requestParam, options.sig, 'getCityList')
  724. }
  725. wx.request(Utils.buildWxRequestConfig(options, {
  726. url: URL_CITY_LIST,
  727. data: requestParam
  728. }, 'getCityList'))
  729. };
  730. getDistrictByCityId(options) {
  731. var that = this;
  732. options = options || {};
  733. Utils.polyfillParam(options);
  734. if (Utils.checkParamKeyEmpty(options, 'id')) {
  735. return
  736. }
  737. var requestParam = {
  738. id: options.id || '',
  739. output: 'json',
  740. key: that.key
  741. };
  742. if (options.sig) {
  743. requestParam.sig = Utils.getSig(requestParam, options.sig, 'getDistrictByCityId')
  744. }
  745. wx.request(Utils.buildWxRequestConfig(options, {
  746. url: URL_AREA_LIST,
  747. data: requestParam
  748. }, 'getDistrictByCityId'))
  749. };
  750. calculateDistance(options) {
  751. var that = this;
  752. options = options || {};
  753. Utils.polyfillParam(options);
  754. if (Utils.checkParamKeyEmpty(options, 'to')) {
  755. return
  756. }
  757. var requestParam = {
  758. mode: options.mode || 'walking',
  759. to: Utils.location2query(options.to),
  760. output: 'json',
  761. key: that.key
  762. };
  763. if (options.from) {
  764. options.location = options.from
  765. }
  766. if (requestParam.mode == 'straight') {
  767. var locationsuccess = function(result) {
  768. var locationTo = Utils.getEndLocation(requestParam.to);
  769. var data = {
  770. message: "query ok",
  771. result: {
  772. elements: []
  773. },
  774. status: 0
  775. };
  776. for (var i = 0; i < locationTo.length; i++) {
  777. data.result.elements.push({
  778. distance: Utils.getDistance(result.latitude, result.longitude, locationTo[i]
  779. .lat, locationTo[i].lng),
  780. duration: 0,
  781. from: {
  782. lat: result.latitude,
  783. lng: result.longitude
  784. },
  785. to: {
  786. lat: locationTo[i].lat,
  787. lng: locationTo[i].lng
  788. }
  789. })
  790. }
  791. var calculateResult = data.result.elements;
  792. var distanceResult = [];
  793. for (var i = 0; i < calculateResult.length; i++) {
  794. distanceResult.push(calculateResult[i].distance)
  795. }
  796. return options.success(data, {
  797. calculateResult: calculateResult,
  798. distanceResult: distanceResult
  799. })
  800. };
  801. Utils.locationProcess(options, locationsuccess)
  802. } else {
  803. var locationsuccess = function(result) {
  804. requestParam.from = result.latitude + ',' + result.longitude;
  805. if (options.sig) {
  806. requestParam.sig = Utils.getSig(requestParam, options.sig, 'calculateDistance')
  807. }
  808. wx.request(Utils.buildWxRequestConfig(options, {
  809. url: URL_DISTANCE,
  810. data: requestParam
  811. }, 'calculateDistance'))
  812. };
  813. Utils.locationProcess(options, locationsuccess)
  814. }
  815. };
  816. direction(options) {
  817. var that = this;
  818. options = options || {};
  819. Utils.polyfillParam(options);
  820. if (Utils.checkParamKeyEmpty(options, 'to')) {
  821. return
  822. }
  823. var requestParam = {
  824. output: 'json',
  825. key: that.key
  826. };
  827. if (typeof options.to == 'string') {
  828. requestParam.to = options.to
  829. } else {
  830. requestParam.to = options.to.latitude + ',' + options.to.longitude
  831. }
  832. var SET_URL_DIRECTION = null;
  833. options.mode = options.mode || MODE.driving;
  834. SET_URL_DIRECTION = URL_DIRECTION + options.mode;
  835. if (options.from) {
  836. options.location = options.from
  837. }
  838. if (options.mode == MODE.driving) {
  839. if (options.from_poi) {
  840. requestParam.from_poi = options.from_poi
  841. }
  842. if (options.heading) {
  843. requestParam.heading = options.heading
  844. }
  845. if (options.speed) {
  846. requestParam.speed = options.speed
  847. }
  848. if (options.accuracy) {
  849. requestParam.accuracy = options.accuracy
  850. }
  851. if (options.road_type) {
  852. requestParam.road_type = options.road_type
  853. }
  854. if (options.to_poi) {
  855. requestParam.to_poi = options.to_poi
  856. }
  857. if (options.from_track) {
  858. requestParam.from_track = options.from_track
  859. }
  860. if (options.waypoints) {
  861. requestParam.waypoints = options.waypoints
  862. }
  863. if (options.policy) {
  864. requestParam.policy = options.policy
  865. }
  866. if (options.plate_number) {
  867. requestParam.plate_number = options.plate_number
  868. }
  869. }
  870. if (options.mode == MODE.transit) {
  871. if (options.departure_time) {
  872. requestParam.departure_time = options.departure_time
  873. }
  874. if (options.policy) {
  875. requestParam.policy = options.policy
  876. }
  877. }
  878. var locationsuccess = function(result) {
  879. requestParam.from = result.latitude + ',' + result.longitude;
  880. if (options.sig) {
  881. requestParam.sig = Utils.getSig(requestParam, options.sig, 'direction', options.mode)
  882. }
  883. wx.request(Utils.buildWxRequestConfig(options, {
  884. url: SET_URL_DIRECTION,
  885. data: requestParam
  886. }, 'direction'))
  887. };
  888. Utils.locationProcess(options, locationsuccess)
  889. }
  890. };
  891. export default QQMapWX