qqmap-wx-jssdk.min.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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(radLatTo) * Math.pow(Math.sin(b / 2), 2)));
  288. distance = distance * EARTH_RADIUS;
  289. distance = Math.round(distance * 10000) / 10000;
  290. return parseFloat(distance.toFixed(0));
  291. },
  292. getWXLocation(success, fail, complete) {
  293. uni.getLocation({
  294. type: 'gcj02',
  295. success: success,
  296. fail: fail,
  297. complete: complete
  298. });
  299. },
  300. getLocationParam(location) {
  301. if (typeof location == 'string') {
  302. var locationArr = location.split(',');
  303. if (locationArr.length === 2) {
  304. location = {
  305. latitude: location.split(',')[0],
  306. longitude: location.split(',')[1]
  307. };
  308. } else {
  309. location = {};
  310. }
  311. }
  312. return location;
  313. },
  314. polyfillParam(param) {
  315. param.success = param.success || function () {};
  316. param.fail = param.fail || function () {};
  317. param.complete = param.complete || function () {};
  318. },
  319. checkParamKeyEmpty(param, key) {
  320. if (!param[key]) {
  321. var errconf = this.buildErrorConfig(ERROR_CONF.PARAM_ERR, ERROR_CONF.PARAM_ERR_MSG + key + '参数格式有误');
  322. param.fail(errconf);
  323. param.complete(errconf);
  324. return true;
  325. }
  326. return false;
  327. },
  328. checkKeyword(param) {
  329. return !this.checkParamKeyEmpty(param, 'keyword');
  330. },
  331. checkLocation(param) {
  332. var location = this.getLocationParam(param.location);
  333. if (!location || !location.latitude || !location.longitude) {
  334. var errconf = this.buildErrorConfig(ERROR_CONF.PARAM_ERR, ERROR_CONF.PARAM_ERR_MSG + ' location参数格式有误');
  335. param.fail(errconf);
  336. param.complete(errconf);
  337. return false;
  338. }
  339. return true;
  340. },
  341. buildErrorConfig(errCode, errMsg) {
  342. return {
  343. status: errCode,
  344. message: errMsg
  345. };
  346. },
  347. handleData(param, data, feature) {
  348. if (feature == 'search') {
  349. var searchResult = data.data;
  350. var searchSimplify = [];
  351. for (var i = 0; i < searchResult.length; i++) {
  352. searchSimplify.push({
  353. id: searchResult[i].id || null,
  354. title: searchResult[i].title || null,
  355. latitude: searchResult[i].location && searchResult[i].location.lat || null,
  356. longitude: searchResult[i].location && searchResult[i].location.lng || null,
  357. address: searchResult[i].address || null,
  358. category: searchResult[i].category || null,
  359. tel: searchResult[i].tel || null,
  360. adcode: searchResult[i].ad_info && searchResult[i].ad_info.adcode || null,
  361. city: searchResult[i].ad_info && searchResult[i].ad_info.city || null,
  362. district: searchResult[i].ad_info && searchResult[i].ad_info.district || null,
  363. province: searchResult[i].ad_info && searchResult[i].ad_info.province || null
  364. });
  365. }
  366. param.success(data, {
  367. searchResult: searchResult,
  368. searchSimplify: searchSimplify
  369. });
  370. } else if (feature == 'suggest') {
  371. var suggestResult = data.data;
  372. var suggestSimplify = [];
  373. for (var i = 0; i < suggestResult.length; i++) {
  374. suggestSimplify.push({
  375. adcode: suggestResult[i].adcode || null,
  376. address: suggestResult[i].address || null,
  377. category: suggestResult[i].category || null,
  378. city: suggestResult[i].city || null,
  379. district: suggestResult[i].district || null,
  380. id: suggestResult[i].id || null,
  381. latitude: suggestResult[i].location && suggestResult[i].location.lat || null,
  382. longitude: suggestResult[i].location && suggestResult[i].location.lng || null,
  383. province: suggestResult[i].province || null,
  384. title: suggestResult[i].title || null,
  385. type: suggestResult[i].type || null
  386. });
  387. }
  388. param.success(data, {
  389. suggestResult: suggestResult,
  390. suggestSimplify: suggestSimplify
  391. });
  392. } else if (feature == 'reverseGeocoder') {
  393. var reverseGeocoderResult = data.result;
  394. var reverseGeocoderSimplify = {
  395. address: reverseGeocoderResult.address || null,
  396. latitude: reverseGeocoderResult.location && reverseGeocoderResult.location.lat || null,
  397. longitude: reverseGeocoderResult.location && reverseGeocoderResult.location.lng || null,
  398. adcode: reverseGeocoderResult.ad_info && reverseGeocoderResult.ad_info.adcode || null,
  399. city: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.city || null,
  400. district: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.district || null,
  401. nation: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.nation || null,
  402. province: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.province || null,
  403. street: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.street || null,
  404. street_number: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.street_number || null,
  405. recommend: reverseGeocoderResult.formatted_addresses && reverseGeocoderResult.formatted_addresses.recommend || null,
  406. rough: reverseGeocoderResult.formatted_addresses && reverseGeocoderResult.formatted_addresses.rough || null
  407. };
  408. if (reverseGeocoderResult.pois) {
  409. var pois = reverseGeocoderResult.pois;
  410. var poisSimplify = [];
  411. for (var i = 0; i < pois.length; i++) {
  412. poisSimplify.push({
  413. id: pois[i].id || null,
  414. title: pois[i].title || null,
  415. latitude: pois[i].location && pois[i].location.lat || null,
  416. longitude: pois[i].location && pois[i].location.lng || null,
  417. address: pois[i].address || null,
  418. category: pois[i].category || null,
  419. adcode: pois[i].ad_info && pois[i].ad_info.adcode || null,
  420. city: pois[i].ad_info && pois[i].ad_info.city || null,
  421. district: pois[i].ad_info && pois[i].ad_info.district || null,
  422. province: pois[i].ad_info && pois[i].ad_info.province || null
  423. });
  424. }
  425. param.success(data, {
  426. reverseGeocoderResult: reverseGeocoderResult,
  427. reverseGeocoderSimplify: reverseGeocoderSimplify,
  428. pois: pois,
  429. poisSimplify: poisSimplify
  430. });
  431. } else {
  432. param.success(data, {
  433. reverseGeocoderResult: reverseGeocoderResult,
  434. reverseGeocoderSimplify: reverseGeocoderSimplify
  435. });
  436. }
  437. } else if (feature == 'geocoder') {
  438. var geocoderResult = data.result;
  439. var geocoderSimplify = {
  440. title: geocoderResult.title || null,
  441. latitude: geocoderResult.location && geocoderResult.location.lat || null,
  442. longitude: geocoderResult.location && geocoderResult.location.lng || null,
  443. adcode: geocoderResult.ad_info && geocoderResult.ad_info.adcode || null,
  444. province: geocoderResult.address_components && geocoderResult.address_components.province || null,
  445. city: geocoderResult.address_components && geocoderResult.address_components.city || null,
  446. district: geocoderResult.address_components && geocoderResult.address_components.district || null,
  447. street: geocoderResult.address_components && geocoderResult.address_components.street || null,
  448. street_number: geocoderResult.address_components && geocoderResult.address_components.street_number || null,
  449. level: geocoderResult.level || null
  450. };
  451. param.success(data, {
  452. geocoderResult: geocoderResult,
  453. geocoderSimplify: geocoderSimplify
  454. });
  455. } else if (feature == 'getCityList') {
  456. var provinceResult = data.result[0];
  457. var cityResult = data.result[1];
  458. var districtResult = data.result[2];
  459. param.success(data, {
  460. provinceResult: provinceResult,
  461. cityResult: cityResult,
  462. districtResult: districtResult
  463. });
  464. } else if (feature == 'getDistrictByCityId') {
  465. var districtByCity = data.result[0];
  466. param.success(data, districtByCity);
  467. } else if (feature == 'calculateDistance') {
  468. var calculateDistanceResult = data.result.elements;
  469. var distance = [];
  470. for (var i = 0; i < calculateDistanceResult.length; i++) {
  471. distance.push(calculateDistanceResult[i].distance);
  472. }
  473. param.success(data, {
  474. calculateDistanceResult: calculateDistanceResult,
  475. distance: distance
  476. });
  477. } else if (feature == 'direction') {
  478. var direction = data.result.routes;
  479. param.success(data, direction);
  480. } else {
  481. param.success(data);
  482. }
  483. },
  484. buildWxRequestConfig(param, options, feature) {
  485. var that = this;
  486. options.header = {
  487. "content-type": "application/json"
  488. };
  489. options.method = 'GET';
  490. options.success = function (res) {
  491. var data = res.data;
  492. if (data.status === 0) {
  493. that.handleData(param, data, feature);
  494. } else {
  495. param.fail(data);
  496. }
  497. };
  498. options.fail = function (res) {
  499. res.statusCode = ERROR_CONF.WX_ERR_CODE;
  500. param.fail(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg));
  501. };
  502. options.complete = function (res) {
  503. var statusCode = +res.statusCode;
  504. switch (statusCode) {
  505. case ERROR_CONF.WX_ERR_CODE:
  506. {
  507. param.complete(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg));
  508. break;
  509. }
  510. case ERROR_CONF.WX_OK_CODE:
  511. {
  512. var data = res.data;
  513. if (data.status === 0) {
  514. param.complete(data);
  515. } else {
  516. param.complete(that.buildErrorConfig(data.status, data.message));
  517. }
  518. break;
  519. }
  520. default:
  521. {
  522. param.complete(that.buildErrorConfig(ERROR_CONF.SYSTEM_ERR, ERROR_CONF.SYSTEM_ERR_MSG));
  523. }
  524. }
  525. };
  526. return options;
  527. },
  528. locationProcess(param, locationsuccess, locationfail, locationcomplete) {
  529. var that = this;
  530. locationfail = locationfail || function (res) {
  531. res.statusCode = ERROR_CONF.WX_ERR_CODE;
  532. param.fail(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg));
  533. };
  534. locationcomplete = locationcomplete || function (res) {
  535. if (res.statusCode == ERROR_CONF.WX_ERR_CODE) {
  536. param.complete(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg));
  537. }
  538. };
  539. if (!param.location) {
  540. that.getWXLocation(locationsuccess, locationfail, locationcomplete);
  541. } else if (that.checkLocation(param)) {
  542. var location = Utils.getLocationParam(param.location);
  543. locationsuccess(location);
  544. }
  545. }
  546. };
  547. class QQMapWX {
  548. constructor(options) {
  549. if (!options.key) {
  550. throw Error('key值不能为空');
  551. }
  552. this.key = options.key;
  553. }
  554. search(options) {
  555. var that = this;
  556. options = options || {};
  557. Utils.polyfillParam(options);
  558. if (!Utils.checkKeyword(options)) {
  559. return;
  560. }
  561. var requestParam = {
  562. keyword: options.keyword,
  563. orderby: options.orderby || '_distance',
  564. page_size: options.page_size || 10,
  565. page_index: options.page_index || 1,
  566. output: 'json',
  567. key: that.key
  568. };
  569. if (options.address_format) {
  570. requestParam.address_format = options.address_format;
  571. }
  572. if (options.filter) {
  573. requestParam.filter = options.filter;
  574. }
  575. var distance = options.distance || "1000";
  576. var auto_extend = options.auto_extend || 1;
  577. var region = null;
  578. var rectangle = null;
  579. if (options.region) {
  580. region = options.region;
  581. }
  582. if (options.rectangle) {
  583. rectangle = options.rectangle;
  584. }
  585. var locationsuccess = function (result) {
  586. if (region && !rectangle) {
  587. requestParam.boundary = "region(" + region + "," + auto_extend + "," + result.latitude + "," + result.longitude + ")";
  588. if (options.sig) {
  589. requestParam.sig = Utils.getSig(requestParam, options.sig, 'search');
  590. }
  591. } else if (rectangle && !region) {
  592. requestParam.boundary = "rectangle(" + rectangle + ")";
  593. if (options.sig) {
  594. requestParam.sig = Utils.getSig(requestParam, options.sig, 'search');
  595. }
  596. } else {
  597. requestParam.boundary = "nearby(" + result.latitude + "," + result.longitude + "," + distance + "," + auto_extend + ")";
  598. if (options.sig) {
  599. requestParam.sig = Utils.getSig(requestParam, options.sig, 'search');
  600. }
  601. }
  602. uni.request(Utils.buildWxRequestConfig(options, {
  603. url: URL_SEARCH,
  604. data: requestParam
  605. }, 'search'));
  606. };
  607. Utils.locationProcess(options, locationsuccess);
  608. }
  609. getSuggestion(options) {
  610. var that = this;
  611. options = options || {};
  612. Utils.polyfillParam(options);
  613. if (!Utils.checkKeyword(options)) {
  614. return;
  615. }
  616. var requestParam = {
  617. keyword: options.keyword,
  618. region: options.region || '全国',
  619. region_fix: options.region_fix || 0,
  620. policy: options.policy || 0,
  621. page_size: options.page_size || 10,
  622. page_index: options.page_index || 1,
  623. get_subpois: options.get_subpois || 0,
  624. output: 'json',
  625. key: that.key
  626. };
  627. if (options.address_format) {
  628. requestParam.address_format = options.address_format;
  629. }
  630. if (options.filter) {
  631. requestParam.filter = options.filter;
  632. }
  633. if (options.location) {
  634. var locationsuccess = function (result) {
  635. requestParam.location = result.latitude + ',' + result.longitude;
  636. if (options.sig) {
  637. requestParam.sig = Utils.getSig(requestParam, options.sig, 'suggest');
  638. }
  639. uni.request(Utils.buildWxRequestConfig(options, {
  640. url: URL_SUGGESTION,
  641. data: requestParam
  642. }, "suggest"));
  643. };
  644. Utils.locationProcess(options, locationsuccess);
  645. } else {
  646. if (options.sig) {
  647. requestParam.sig = Utils.getSig(requestParam, options.sig, 'suggest');
  648. }
  649. uni.request(Utils.buildWxRequestConfig(options, {
  650. url: URL_SUGGESTION,
  651. data: requestParam
  652. }, "suggest"));
  653. }
  654. }
  655. reverseGeocoder(options) {
  656. var that = this;
  657. options = options || {};
  658. Utils.polyfillParam(options);
  659. var requestParam = {
  660. coord_type: options.coord_type || 5,
  661. get_poi: options.get_poi || 0,
  662. output: 'json',
  663. key: that.key
  664. };
  665. if (options.poi_options) {
  666. requestParam.poi_options = options.poi_options;
  667. }
  668. var locationsuccess = function (result) {
  669. requestParam.location = result.latitude + ',' + result.longitude;
  670. if (options.sig) {
  671. requestParam.sig = Utils.getSig(requestParam, options.sig, 'reverseGeocoder');
  672. }
  673. uni.request(Utils.buildWxRequestConfig(options, {
  674. url: URL_GET_GEOCODER,
  675. data: requestParam
  676. }, 'reverseGeocoder'));
  677. };
  678. Utils.locationProcess(options, locationsuccess);
  679. }
  680. geocoder(options) {
  681. var that = this;
  682. options = options || {};
  683. Utils.polyfillParam(options);
  684. if (Utils.checkParamKeyEmpty(options, 'address')) {
  685. return;
  686. }
  687. var requestParam = {
  688. address: options.address,
  689. output: 'json',
  690. key: that.key
  691. };
  692. if (options.region) {
  693. requestParam.region = options.region;
  694. }
  695. if (options.sig) {
  696. requestParam.sig = Utils.getSig(requestParam, options.sig, 'geocoder');
  697. }
  698. uni.request(Utils.buildWxRequestConfig(options, {
  699. url: URL_GET_GEOCODER,
  700. data: requestParam
  701. }, 'geocoder'));
  702. }
  703. getCityList(options) {
  704. var that = this;
  705. options = options || {};
  706. Utils.polyfillParam(options);
  707. var requestParam = {
  708. output: 'json',
  709. key: that.key
  710. };
  711. if (options.sig) {
  712. requestParam.sig = Utils.getSig(requestParam, options.sig, 'getCityList');
  713. }
  714. uni.request(Utils.buildWxRequestConfig(options, {
  715. url: URL_CITY_LIST,
  716. data: requestParam
  717. }, 'getCityList'));
  718. }
  719. getDistrictByCityId(options) {
  720. var that = this;
  721. options = options || {};
  722. Utils.polyfillParam(options);
  723. if (Utils.checkParamKeyEmpty(options, 'id')) {
  724. return;
  725. }
  726. var requestParam = {
  727. id: options.id || '',
  728. output: 'json',
  729. key: that.key
  730. };
  731. if (options.sig) {
  732. requestParam.sig = Utils.getSig(requestParam, options.sig, 'getDistrictByCityId');
  733. }
  734. uni.request(Utils.buildWxRequestConfig(options, {
  735. url: URL_AREA_LIST,
  736. data: requestParam
  737. }, 'getDistrictByCityId'));
  738. }
  739. calculateDistance(options) {
  740. var that = this;
  741. options = options || {};
  742. Utils.polyfillParam(options);
  743. if (Utils.checkParamKeyEmpty(options, 'to')) {
  744. return;
  745. }
  746. var requestParam = {
  747. mode: options.mode || 'walking',
  748. to: Utils.location2query(options.to),
  749. output: 'json',
  750. key: that.key
  751. };
  752. if (options.from) {
  753. options.location = options.from;
  754. }
  755. if (requestParam.mode == 'straight') {
  756. var locationsuccess = function (result) {
  757. var locationTo = Utils.getEndLocation(requestParam.to);
  758. var data = {
  759. message: "query ok",
  760. result: {
  761. elements: []
  762. },
  763. status: 0
  764. };
  765. for (var i = 0; i < locationTo.length; i++) {
  766. data.result.elements.push({
  767. distance: Utils.getDistance(result.latitude, result.longitude, locationTo[i].lat, locationTo[i].lng),
  768. duration: 0,
  769. from: {
  770. lat: result.latitude,
  771. lng: result.longitude
  772. },
  773. to: {
  774. lat: locationTo[i].lat,
  775. lng: locationTo[i].lng
  776. }
  777. });
  778. }
  779. var calculateResult = data.result.elements;
  780. var distanceResult = [];
  781. for (var i = 0; i < calculateResult.length; i++) {
  782. distanceResult.push(calculateResult[i].distance);
  783. }
  784. return options.success(data, {
  785. calculateResult: calculateResult,
  786. distanceResult: distanceResult
  787. });
  788. };
  789. Utils.locationProcess(options, locationsuccess);
  790. } else {
  791. var locationsuccess = function (result) {
  792. requestParam.from = result.latitude + ',' + result.longitude;
  793. if (options.sig) {
  794. requestParam.sig = Utils.getSig(requestParam, options.sig, 'calculateDistance');
  795. }
  796. uni.request(Utils.buildWxRequestConfig(options, {
  797. url: URL_DISTANCE,
  798. data: requestParam
  799. }, 'calculateDistance'));
  800. };
  801. Utils.locationProcess(options, locationsuccess);
  802. }
  803. }
  804. direction(options) {
  805. var that = this;
  806. options = options || {};
  807. Utils.polyfillParam(options);
  808. if (Utils.checkParamKeyEmpty(options, 'to')) {
  809. return;
  810. }
  811. var requestParam = {
  812. output: 'json',
  813. key: that.key
  814. };
  815. if (typeof options.to == 'string') {
  816. requestParam.to = options.to;
  817. } else {
  818. requestParam.to = options.to.latitude + ',' + options.to.longitude;
  819. }
  820. var SET_URL_DIRECTION = null;
  821. options.mode = options.mode || MODE.driving;
  822. SET_URL_DIRECTION = URL_DIRECTION + options.mode;
  823. if (options.from) {
  824. options.location = options.from;
  825. }
  826. if (options.mode == MODE.driving) {
  827. if (options.from_poi) {
  828. requestParam.from_poi = options.from_poi;
  829. }
  830. if (options.heading) {
  831. requestParam.heading = options.heading;
  832. }
  833. if (options.speed) {
  834. requestParam.speed = options.speed;
  835. }
  836. if (options.accuracy) {
  837. requestParam.accuracy = options.accuracy;
  838. }
  839. if (options.road_type) {
  840. requestParam.road_type = options.road_type;
  841. }
  842. if (options.to_poi) {
  843. requestParam.to_poi = options.to_poi;
  844. }
  845. if (options.from_track) {
  846. requestParam.from_track = options.from_track;
  847. }
  848. if (options.waypoints) {
  849. requestParam.waypoints = options.waypoints;
  850. }
  851. if (options.policy) {
  852. requestParam.policy = options.policy;
  853. }
  854. if (options.plate_number) {
  855. requestParam.plate_number = options.plate_number;
  856. }
  857. }
  858. if (options.mode == MODE.transit) {
  859. if (options.departure_time) {
  860. requestParam.departure_time = options.departure_time;
  861. }
  862. if (options.policy) {
  863. requestParam.policy = options.policy;
  864. }
  865. }
  866. var locationsuccess = function (result) {
  867. requestParam.from = result.latitude + ',' + result.longitude;
  868. if (options.sig) {
  869. requestParam.sig = Utils.getSig(requestParam, options.sig, 'direction', options.mode);
  870. }
  871. uni.request(Utils.buildWxRequestConfig(options, {
  872. url: SET_URL_DIRECTION,
  873. data: requestParam
  874. }, 'direction'));
  875. };
  876. Utils.locationProcess(options, locationsuccess);
  877. }
  878. }
  879. ;
  880. module.exports = QQMapWX;