qqmap-wx-jssdk.min.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. //引入vue
  2. // import Vue from "vue"
  3. // // 下载vue-jsonp引入 这里我是在mian.js引入
  4. // Vue.use(VueJsonp)
  5. // //必须实例化
  6. // var ve = new Vue()
  7. import { VueJsonp } from 'vue-jsonp'
  8. var vm=''
  9. /**
  10. * 这里是重写部分
  11. */
  12. var wx = {
  13. request(obj){
  14. obj.data.output = 'jsonp'
  15. ve.$jsonp(obj.url,obj.data)
  16. .then(json => {
  17. if(json.status == 0){
  18. obj.success(json)
  19. }else {
  20. obj.fail(json)
  21. }
  22. })
  23. .catch(err => {
  24. obj.fail(err)
  25. })
  26. }
  27. }
  28. var ERROR_CONF = {
  29. KEY_ERR: 311,
  30. KEY_ERR_MSG: 'key格式错误',
  31. PARAM_ERR: 310,
  32. PARAM_ERR_MSG: '请求参数信息有误',
  33. SYSTEM_ERR: 600,
  34. SYSTEM_ERR_MSG: '系统错误',
  35. WX_ERR_CODE: 1000,
  36. WX_OK_CODE: 200
  37. };
  38. var BASE_URL = 'https://apis.map.qq.com/ws/';
  39. var URL_SEARCH = BASE_URL + 'place/v1/search';
  40. var URL_SUGGESTION = BASE_URL + 'place/v1/suggestion';
  41. var URL_GET_GEOCODER = BASE_URL + 'geocoder/v1/';
  42. var URL_CITY_LIST = BASE_URL + 'district/v1/list';
  43. var URL_AREA_LIST = BASE_URL + 'district/v1/getchildren';
  44. var URL_DISTANCE = BASE_URL + 'distance/v1/';
  45. var URL_DIRECTION = BASE_URL + 'direction/v1/';
  46. var MODE = {
  47. driving: 'driving',
  48. transit: 'transit'
  49. };
  50. var EARTH_RADIUS = 6378136.49;
  51. var Utils = {
  52. /**
  53. * md5加密方法
  54. * 版权所有©2011 Sebastian Tschan,https://blueimp.net
  55. */
  56. safeAdd(x, y) {
  57. var lsw = (x & 0xffff) + (y & 0xffff);
  58. var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  59. return (msw << 16) | (lsw & 0xffff);
  60. },
  61. bitRotateLeft(num, cnt) {
  62. return (num << cnt) | (num >>> (32 - cnt));
  63. },
  64. md5cmn(q, a, b, x, s, t) {
  65. return this.safeAdd(this.bitRotateLeft(this.safeAdd(this.safeAdd(a, q), this.safeAdd(x, t)), s), b);
  66. },
  67. md5ff(a, b, c, d, x, s, t) {
  68. return this.md5cmn((b & c) | (~b & d), a, b, x, s, t);
  69. },
  70. md5gg(a, b, c, d, x, s, t) {
  71. return this.md5cmn((b & d) | (c & ~d), a, b, x, s, t);
  72. },
  73. md5hh(a, b, c, d, x, s, t) {
  74. return this.md5cmn(b ^ c ^ d, a, b, x, s, t);
  75. },
  76. md5ii(a, b, c, d, x, s, t) {
  77. return this.md5cmn(c ^ (b | ~d), a, b, x, s, t);
  78. },
  79. binlMD5(x, len) {
  80. /* append padding */
  81. x[len >> 5] |= 0x80 << (len % 32);
  82. x[((len + 64) >>> 9 << 4) + 14] = len;
  83. var i;
  84. var olda;
  85. var oldb;
  86. var oldc;
  87. var oldd;
  88. var a = 1732584193;
  89. var b = -271733879;
  90. var c = -1732584194;
  91. var d = 271733878;
  92. for (i = 0; i < x.length; i += 16) {
  93. olda = a;
  94. oldb = b;
  95. oldc = c;
  96. oldd = d;
  97. a = this.md5ff(a, b, c, d, x[i], 7, -680876936);
  98. d = this.md5ff(d, a, b, c, x[i + 1], 12, -389564586);
  99. c = this.md5ff(c, d, a, b, x[i + 2], 17, 606105819);
  100. b = this.md5ff(b, c, d, a, x[i + 3], 22, -1044525330);
  101. a = this.md5ff(a, b, c, d, x[i + 4], 7, -176418897);
  102. d = this.md5ff(d, a, b, c, x[i + 5], 12, 1200080426);
  103. c = this.md5ff(c, d, a, b, x[i + 6], 17, -1473231341);
  104. b = this.md5ff(b, c, d, a, x[i + 7], 22, -45705983);
  105. a = this.md5ff(a, b, c, d, x[i + 8], 7, 1770035416);
  106. d = this.md5ff(d, a, b, c, x[i + 9], 12, -1958414417);
  107. c = this.md5ff(c, d, a, b, x[i + 10], 17, -42063);
  108. b = this.md5ff(b, c, d, a, x[i + 11], 22, -1990404162);
  109. a = this.md5ff(a, b, c, d, x[i + 12], 7, 1804603682);
  110. d = this.md5ff(d, a, b, c, x[i + 13], 12, -40341101);
  111. c = this.md5ff(c, d, a, b, x[i + 14], 17, -1502002290);
  112. b = this.md5ff(b, c, d, a, x[i + 15], 22, 1236535329);
  113. a = this.md5gg(a, b, c, d, x[i + 1], 5, -165796510);
  114. d = this.md5gg(d, a, b, c, x[i + 6], 9, -1069501632);
  115. c = this.md5gg(c, d, a, b, x[i + 11], 14, 643717713);
  116. b = this.md5gg(b, c, d, a, x[i], 20, -373897302);
  117. a = this.md5gg(a, b, c, d, x[i + 5], 5, -701558691);
  118. d = this.md5gg(d, a, b, c, x[i + 10], 9, 38016083);
  119. c = this.md5gg(c, d, a, b, x[i + 15], 14, -660478335);
  120. b = this.md5gg(b, c, d, a, x[i + 4], 20, -405537848);
  121. a = this.md5gg(a, b, c, d, x[i + 9], 5, 568446438);
  122. d = this.md5gg(d, a, b, c, x[i + 14], 9, -1019803690);
  123. c = this.md5gg(c, d, a, b, x[i + 3], 14, -187363961);
  124. b = this.md5gg(b, c, d, a, x[i + 8], 20, 1163531501);
  125. a = this.md5gg(a, b, c, d, x[i + 13], 5, -1444681467);
  126. d = this.md5gg(d, a, b, c, x[i + 2], 9, -51403784);
  127. c = this.md5gg(c, d, a, b, x[i + 7], 14, 1735328473);
  128. b = this.md5gg(b, c, d, a, x[i + 12], 20, -1926607734);
  129. a = this.md5hh(a, b, c, d, x[i + 5], 4, -378558);
  130. d = this.md5hh(d, a, b, c, x[i + 8], 11, -2022574463);
  131. c = this.md5hh(c, d, a, b, x[i + 11], 16, 1839030562);
  132. b = this.md5hh(b, c, d, a, x[i + 14], 23, -35309556);
  133. a = this.md5hh(a, b, c, d, x[i + 1], 4, -1530992060);
  134. d = this.md5hh(d, a, b, c, x[i + 4], 11, 1272893353);
  135. c = this.md5hh(c, d, a, b, x[i + 7], 16, -155497632);
  136. b = this.md5hh(b, c, d, a, x[i + 10], 23, -1094730640);
  137. a = this.md5hh(a, b, c, d, x[i + 13], 4, 681279174);
  138. d = this.md5hh(d, a, b, c, x[i], 11, -358537222);
  139. c = this.md5hh(c, d, a, b, x[i + 3], 16, -722521979);
  140. b = this.md5hh(b, c, d, a, x[i + 6], 23, 76029189);
  141. a = this.md5hh(a, b, c, d, x[i + 9], 4, -640364487);
  142. d = this.md5hh(d, a, b, c, x[i + 12], 11, -421815835);
  143. c = this.md5hh(c, d, a, b, x[i + 15], 16, 530742520);
  144. b = this.md5hh(b, c, d, a, x[i + 2], 23, -995338651);
  145. a = this.md5ii(a, b, c, d, x[i], 6, -198630844);
  146. d = this.md5ii(d, a, b, c, x[i + 7], 10, 1126891415);
  147. c = this.md5ii(c, d, a, b, x[i + 14], 15, -1416354905);
  148. b = this.md5ii(b, c, d, a, x[i + 5], 21, -57434055);
  149. a = this.md5ii(a, b, c, d, x[i + 12], 6, 1700485571);
  150. d = this.md5ii(d, a, b, c, x[i + 3], 10, -1894986606);
  151. c = this.md5ii(c, d, a, b, x[i + 10], 15, -1051523);
  152. b = this.md5ii(b, c, d, a, x[i + 1], 21, -2054922799);
  153. a = this.md5ii(a, b, c, d, x[i + 8], 6, 1873313359);
  154. d = this.md5ii(d, a, b, c, x[i + 15], 10, -30611744);
  155. c = this.md5ii(c, d, a, b, x[i + 6], 15, -1560198380);
  156. b = this.md5ii(b, c, d, a, x[i + 13], 21, 1309151649);
  157. a = this.md5ii(a, b, c, d, x[i + 4], 6, -145523070);
  158. d = this.md5ii(d, a, b, c, x[i + 11], 10, -1120210379);
  159. c = this.md5ii(c, d, a, b, x[i + 2], 15, 718787259);
  160. b = this.md5ii(b, c, d, a, x[i + 9], 21, -343485551);
  161. a = this.safeAdd(a, olda);
  162. b = this.safeAdd(b, oldb);
  163. c = this.safeAdd(c, oldc);
  164. d = this.safeAdd(d, oldd);
  165. }
  166. return [a, b, c, d];
  167. },
  168. binl2rstr(input) {
  169. var i;
  170. var output = '';
  171. var length32 = input.length * 32;
  172. for (i = 0; i < length32; i += 8) {
  173. output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xff);
  174. }
  175. return output;
  176. },
  177. rstr2binl(input) {
  178. var i;
  179. var output = [];
  180. output[(input.length >> 2) - 1] = undefined;
  181. for (i = 0; i < output.length; i += 1) {
  182. output[i] = 0;
  183. }
  184. var length8 = input.length * 8;
  185. for (i = 0; i < length8; i += 8) {
  186. output[i >> 5] |= (input.charCodeAt(i / 8) & 0xff) << (i % 32);
  187. }
  188. return output;
  189. },
  190. rstrMD5(s) {
  191. return this.binl2rstr(this.binlMD5(this.rstr2binl(s), s.length * 8));
  192. },
  193. rstrHMACMD5(key, data) {
  194. var i;
  195. var bkey = this.rstr2binl(key);
  196. var ipad = [];
  197. var opad = [];
  198. var hash;
  199. ipad[15] = opad[15] = undefined;
  200. if (bkey.length > 16) {
  201. bkey = this.binlMD5(bkey, key.length * 8);
  202. }
  203. for (i = 0; i < 16; i += 1) {
  204. ipad[i] = bkey[i] ^ 0x36363636;
  205. opad[i] = bkey[i] ^ 0x5c5c5c5c;
  206. }
  207. hash = this.binlMD5(ipad.concat(this.rstr2binl(data)), 512 + data.length * 8);
  208. return this.binl2rstr(this.binlMD5(opad.concat(hash), 512 + 128));
  209. },
  210. rstr2hex(input) {
  211. var hexTab = '0123456789abcdef';
  212. var output = '';
  213. var x;
  214. var i;
  215. for (i = 0; i < input.length; i += 1) {
  216. x = input.charCodeAt(i);
  217. output += hexTab.charAt((x >>> 4) & 0x0f) + hexTab.charAt(x & 0x0f);
  218. }
  219. return output;
  220. },
  221. str2rstrUTF8(input) {
  222. return unescape(encodeURIComponent(input));
  223. },
  224. rawMD5(s) {
  225. return this.rstrMD5(this.str2rstrUTF8(s));
  226. },
  227. hexMD5(s) {
  228. return this.rstr2hex(this.rawMD5(s));
  229. },
  230. rawHMACMD5(k, d) {
  231. return this.rstrHMACMD5(this.str2rstrUTF8(k), str2rstrUTF8(d));
  232. },
  233. hexHMACMD5(k, d) {
  234. return this.rstr2hex(this.rawHMACMD5(k, d));
  235. },
  236. md5(string, key, raw) {
  237. if (!key) {
  238. if (!raw) {
  239. return this.hexMD5(string);
  240. }
  241. return this.rawMD5(string);
  242. }
  243. if (!raw) {
  244. return this.hexHMACMD5(key, string);
  245. }
  246. return this.rawHMACMD5(key, string);
  247. },
  248. /**
  249. * 得到md5加密后的sig参数
  250. * @param {Object} requestParam 接口参数
  251. * @param {String} sk签名字符串
  252. * @param {String} featrue 方法名
  253. * @return 返回加密后的sig参数
  254. */
  255. getSig(requestParam, sk, feature, mode) {
  256. var sig = null;
  257. var requestArr = [];
  258. Object.keys(requestParam).sort().forEach(function(key){
  259. requestArr.push(key + '=' + requestParam[key]);
  260. });
  261. if (feature == 'search') {
  262. sig = '/ws/place/v1/search?' + requestArr.join('&') + sk;
  263. }
  264. if (feature == 'suggest') {
  265. sig = '/ws/place/v1/suggestion?' + requestArr.join('&') + sk;
  266. }
  267. if (feature == 'reverseGeocoder') {
  268. sig = '/ws/geocoder/v1/?' + requestArr.join('&') + sk;
  269. }
  270. if (feature == 'geocoder') {
  271. sig = '/ws/geocoder/v1/?' + requestArr.join('&') + sk;
  272. }
  273. if (feature == 'getCityList') {
  274. sig = '/ws/district/v1/list?' + requestArr.join('&') + sk;
  275. }
  276. if (feature == 'getDistrictByCityId') {
  277. sig = '/ws/district/v1/getchildren?' + requestArr.join('&') + sk;
  278. }
  279. if (feature == 'calculateDistance') {
  280. sig = '/ws/distance/v1/?' + requestArr.join('&') + sk;
  281. }
  282. if (feature == 'direction') {
  283. sig = '/ws/direction/v1/' + mode + '?' + requestArr.join('&') + sk;
  284. }
  285. sig = this.md5(sig);
  286. return sig;
  287. },
  288. /**
  289. * 得到终点query字符串
  290. * @param {Array|String} 检索数据
  291. */
  292. location2query(data) {
  293. if (typeof data == 'string') {
  294. return data;
  295. }
  296. var query = '';
  297. for (var i = 0; i < data.length; i++) {
  298. var d = data[i];
  299. if (!!query) {
  300. query += ';';
  301. }
  302. if (d.location) {
  303. query = query + d.location.lat + ',' + d.location.lng;
  304. }
  305. if (d.latitude && d.longitude) {
  306. query = query + d.latitude + ',' + d.longitude;
  307. }
  308. }
  309. return query;
  310. },
  311. /**
  312. * 计算角度
  313. */
  314. rad(d) {
  315. return d * Math.PI / 180.0;
  316. },
  317. /**
  318. * 处理终点location数组
  319. * @return 返回终点数组
  320. */
  321. getEndLocation(location){
  322. var to = location.split(';');
  323. var endLocation = [];
  324. for (var i = 0; i < to.length; i++) {
  325. endLocation.push({
  326. lat: parseFloat(to[i].split(',')[0]),
  327. lng: parseFloat(to[i].split(',')[1])
  328. })
  329. }
  330. return endLocation;
  331. },
  332. /**
  333. * 计算两点间直线距离
  334. * @param a 表示纬度差
  335. * @param b 表示经度差
  336. * @return 返回的是距离,单位m
  337. */
  338. getDistance(latFrom, lngFrom, latTo, lngTo) {
  339. var radLatFrom = this.rad(latFrom);
  340. var radLatTo = this.rad(latTo);
  341. var a = radLatFrom - radLatTo;
  342. var b = this.rad(lngFrom) - this.rad(lngTo);
  343. 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)));
  344. distance = distance * EARTH_RADIUS;
  345. distance = Math.round(distance * 10000) / 10000;
  346. return parseFloat(distance.toFixed(0));
  347. },
  348. /**
  349. * 使用微信接口进行定位
  350. */
  351. getWXLocation(success, fail, complete) {
  352. wx.getLocation({
  353. type: 'gcj02',
  354. success: success,
  355. fail: fail,
  356. complete: complete
  357. });
  358. },
  359. /**
  360. * 获取location参数
  361. */
  362. getLocationParam(location) {
  363. if (typeof location == 'string') {
  364. var locationArr = location.split(',');
  365. if (locationArr.length === 2) {
  366. location = {
  367. latitude: location.split(',')[0],
  368. longitude: location.split(',')[1]
  369. };
  370. } else {
  371. location = {};
  372. }
  373. }
  374. return location;
  375. },
  376. /**
  377. * 回调函数默认处理
  378. */
  379. polyfillParam(param) {
  380. param.success = param.success || function () { };
  381. param.fail = param.fail || function () { };
  382. param.complete = param.complete || function () { };
  383. },
  384. /**
  385. * 验证param对应的key值是否为空
  386. *
  387. * @param {Object} param 接口参数
  388. * @param {String} key 对应参数的key
  389. */
  390. checkParamKeyEmpty(param, key) {
  391. if (!param[key]) {
  392. var errconf = this.buildErrorConfig(ERROR_CONF.PARAM_ERR, ERROR_CONF.PARAM_ERR_MSG + key +'参数格式有误');
  393. param.fail(errconf);
  394. param.complete(errconf);
  395. return true;
  396. }
  397. return false;
  398. },
  399. /**
  400. * 验证参数中是否存在检索词keyword
  401. *
  402. * @param {Object} param 接口参数
  403. */
  404. checkKeyword(param){
  405. return !this.checkParamKeyEmpty(param, 'keyword');
  406. },
  407. /**
  408. * 验证location值
  409. *
  410. * @param {Object} param 接口参数
  411. */
  412. checkLocation(param) {
  413. var location = this.getLocationParam(param.location);
  414. if (!location || !location.latitude || !location.longitude) {
  415. var errconf = this.buildErrorConfig(ERROR_CONF.PARAM_ERR, ERROR_CONF.PARAM_ERR_MSG + ' location参数格式有误');
  416. param.fail(errconf);
  417. param.complete(errconf);
  418. return false;
  419. }
  420. return true;
  421. },
  422. /**
  423. * 构造错误数据结构
  424. * @param {Number} errCode 错误码
  425. * @param {Number} errMsg 错误描述
  426. */
  427. buildErrorConfig(errCode, errMsg) {
  428. return {
  429. status: errCode,
  430. message: errMsg
  431. };
  432. },
  433. /**
  434. *
  435. * 数据处理函数
  436. * 根据传入参数不同处理不同数据
  437. * @param {String} feature 功能名称
  438. * search 地点搜索
  439. * suggest关键词提示
  440. * reverseGeocoder逆地址解析
  441. * geocoder地址解析
  442. * getCityList获取城市列表:父集
  443. * getDistrictByCityId获取区县列表:子集
  444. * calculateDistance距离计算
  445. * @param {Object} param 接口参数
  446. * @param {Object} data 数据
  447. */
  448. handleData(param,data,feature){
  449. if (feature == 'search') {
  450. var searchResult = data.data;
  451. var searchSimplify = [];
  452. for (var i = 0; i < searchResult.length; i++) {
  453. searchSimplify.push({
  454. id: searchResult[i].id || null,
  455. title: searchResult[i].title || null,
  456. latitude: searchResult[i].location && searchResult[i].location.lat || null,
  457. longitude: searchResult[i].location && searchResult[i].location.lng || null,
  458. address: searchResult[i].address || null,
  459. category: searchResult[i].category || null,
  460. tel: searchResult[i].tel || null,
  461. adcode: searchResult[i].ad_info && searchResult[i].ad_info.adcode || null,
  462. city: searchResult[i].ad_info && searchResult[i].ad_info.city || null,
  463. district: searchResult[i].ad_info && searchResult[i].ad_info.district || null,
  464. province: searchResult[i].ad_info && searchResult[i].ad_info.province || null
  465. })
  466. }
  467. param.success(data, {
  468. searchResult: searchResult,
  469. searchSimplify: searchSimplify
  470. })
  471. } else if (feature == 'suggest') {
  472. var suggestResult = data.data;
  473. var suggestSimplify = [];
  474. for (var i = 0; i < suggestResult.length; i++) {
  475. suggestSimplify.push({
  476. adcode: suggestResult[i].adcode || null,
  477. address: suggestResult[i].address || null,
  478. category: suggestResult[i].category || null,
  479. city: suggestResult[i].city || null,
  480. district: suggestResult[i].district || null,
  481. id: suggestResult[i].id || null,
  482. latitude: suggestResult[i].location && suggestResult[i].location.lat || null,
  483. longitude: suggestResult[i].location && suggestResult[i].location.lng || null,
  484. province: suggestResult[i].province || null,
  485. title: suggestResult[i].title || null,
  486. type: suggestResult[i].type || null
  487. })
  488. }
  489. param.success(data, {
  490. suggestResult: suggestResult,
  491. suggestSimplify: suggestSimplify
  492. })
  493. } else if (feature == 'reverseGeocoder') {
  494. var reverseGeocoderResult = data.result;
  495. var reverseGeocoderSimplify = {
  496. address: reverseGeocoderResult.address || null,
  497. latitude: reverseGeocoderResult.location && reverseGeocoderResult.location.lat || null,
  498. longitude: reverseGeocoderResult.location && reverseGeocoderResult.location.lng || null,
  499. adcode: reverseGeocoderResult.ad_info && reverseGeocoderResult.ad_info.adcode || null,
  500. city: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.city || null,
  501. district: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.district || null,
  502. nation: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.nation || null,
  503. province: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.province || null,
  504. street: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.street || null,
  505. street_number: reverseGeocoderResult.address_component && reverseGeocoderResult.address_component.street_number || null,
  506. recommend: reverseGeocoderResult.formatted_addresses && reverseGeocoderResult.formatted_addresses.recommend || null,
  507. rough: reverseGeocoderResult.formatted_addresses && reverseGeocoderResult.formatted_addresses.rough || null
  508. };
  509. if (reverseGeocoderResult.pois) {//判断是否返回周边poi
  510. var pois = reverseGeocoderResult.pois;
  511. var poisSimplify = [];
  512. for (var i = 0;i < pois.length;i++) {
  513. poisSimplify.push({
  514. id: pois[i].id || null,
  515. title: pois[i].title || null,
  516. latitude: pois[i].location && pois[i].location.lat || null,
  517. longitude: pois[i].location && pois[i].location.lng || null,
  518. address: pois[i].address || null,
  519. category: pois[i].category || null,
  520. adcode: pois[i].ad_info && pois[i].ad_info.adcode || null,
  521. city: pois[i].ad_info && pois[i].ad_info.city || null,
  522. district: pois[i].ad_info && pois[i].ad_info.district || null,
  523. province: pois[i].ad_info && pois[i].ad_info.province || null
  524. })
  525. }
  526. param.success(data,{
  527. reverseGeocoderResult: reverseGeocoderResult,
  528. reverseGeocoderSimplify: reverseGeocoderSimplify,
  529. pois: pois,
  530. poisSimplify: poisSimplify
  531. })
  532. } else {
  533. param.success(data, {
  534. reverseGeocoderResult: reverseGeocoderResult,
  535. reverseGeocoderSimplify: reverseGeocoderSimplify
  536. })
  537. }
  538. } else if (feature == 'geocoder') {
  539. var geocoderResult = data.result;
  540. var geocoderSimplify = {
  541. title: geocoderResult.title || null,
  542. latitude: geocoderResult.location && geocoderResult.location.lat || null,
  543. longitude: geocoderResult.location && geocoderResult.location.lng || null,
  544. adcode: geocoderResult.ad_info && geocoderResult.ad_info.adcode || null,
  545. province: geocoderResult.address_components && geocoderResult.address_components.province || null,
  546. city: geocoderResult.address_components && geocoderResult.address_components.city || null,
  547. district: geocoderResult.address_components && geocoderResult.address_components.district || null,
  548. street: geocoderResult.address_components && geocoderResult.address_components.street || null,
  549. street_number: geocoderResult.address_components && geocoderResult.address_components.street_number || null,
  550. level: geocoderResult.level || null
  551. };
  552. param.success(data,{
  553. geocoderResult: geocoderResult,
  554. geocoderSimplify: geocoderSimplify
  555. });
  556. } else if (feature == 'getCityList') {
  557. var provinceResult = data.result[0];
  558. var cityResult = data.result[1];
  559. var districtResult = data.result[2];
  560. param.success(data,{
  561. provinceResult: provinceResult,
  562. cityResult: cityResult,
  563. districtResult: districtResult
  564. });
  565. } else if (feature == 'getDistrictByCityId') {
  566. var districtByCity = data.result[0];
  567. param.success(data, districtByCity);
  568. } else if (feature == 'calculateDistance') {
  569. var calculateDistanceResult = data.result.elements;
  570. var distance = [];
  571. for (var i = 0; i < calculateDistanceResult.length; i++){
  572. distance.push(calculateDistanceResult[i].distance);
  573. }
  574. param.success(data, {
  575. calculateDistanceResult: calculateDistanceResult,
  576. distance: distance
  577. });
  578. } else if (feature == 'direction') {
  579. var direction = data.result.routes;
  580. param.success(data,direction);
  581. } else {
  582. param.success(data);
  583. }
  584. },
  585. /**
  586. * 构造微信请求参数,公共属性处理
  587. *
  588. * @param {Object} param 接口参数
  589. * @param {Object} param 配置项
  590. * @param {String} feature 方法名
  591. */
  592. buildWxRequestConfig(param, options, feature) {
  593. var that = this;
  594. options.header = { "content-type": "application/json" };
  595. options.method = 'GET';
  596. options.success = function (res) {
  597. var data = res;
  598. if (data.status === 0) {
  599. that.handleData(param, data, feature);
  600. } else {
  601. param.fail(data);
  602. }
  603. };
  604. options.fail = function (res) {
  605. res.statusCode = ERROR_CONF.WX_ERR_CODE;
  606. param.fail(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg));
  607. };
  608. options.complete = function (res) {
  609. var statusCode = +res.statusCode;
  610. switch(statusCode) {
  611. case ERROR_CONF.WX_ERR_CODE: {
  612. param.complete(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg));
  613. break;
  614. }
  615. case ERROR_CONF.WX_OK_CODE: {
  616. var data = res.data;
  617. if (data.status === 0) {
  618. param.complete(data);
  619. } else {
  620. param.complete(that.buildErrorConfig(data.status, data.message));
  621. }
  622. break;
  623. }
  624. default:{
  625. param.complete(that.buildErrorConfig(ERROR_CONF.SYSTEM_ERR, ERROR_CONF.SYSTEM_ERR_MSG));
  626. }
  627. }
  628. };
  629. return options;
  630. },
  631. /**
  632. * 处理用户参数是否传入坐标进行不同的处理
  633. */
  634. locationProcess(param, locationsuccess, locationfail, locationcomplete) {
  635. var that = this;
  636. locationfail = locationfail || function (res) {
  637. res.statusCode = ERROR_CONF.WX_ERR_CODE;
  638. param.fail(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg));
  639. };
  640. locationcomplete = locationcomplete || function (res) {
  641. if (res.statusCode == ERROR_CONF.WX_ERR_CODE) {
  642. param.complete(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg));
  643. }
  644. };
  645. if (!param.location) {
  646. that.getWXLocation(locationsuccess, locationfail, locationcomplete);
  647. } else if (that.checkLocation(param)) {
  648. var location = Utils.getLocationParam(param.location);
  649. locationsuccess(location);
  650. }
  651. }
  652. };
  653. class QQMapWX {
  654. /**
  655. * 构造函数
  656. *
  657. * @param {Object} options 接口参数,key 为必选参数
  658. */
  659. constructor(options) {
  660. if (!options.key) {
  661. throw Error('key值不能为空');
  662. }
  663. vm = options.vm
  664. this.key = options.key;
  665. };
  666. /**
  667. * POI周边检索
  668. *
  669. * @param {Object} options 接口参数对象
  670. *
  671. * 参数对象结构可以参考
  672. * @see http://lbs.qq.com/webservice_v1/guide-search.html
  673. */
  674. search(options) {
  675. var that = this;
  676. options = options || {};
  677. Utils.polyfillParam(options);
  678. if (!Utils.checkKeyword(options)) {
  679. return;
  680. }
  681. var requestParam = {
  682. keyword: options.keyword,
  683. orderby: options.orderby || '_distance',
  684. page_size: options.page_size || 10,
  685. page_index: options.page_index || 1,
  686. output: 'json',
  687. key: that.key
  688. };
  689. if (options.address_format) {
  690. requestParam.address_format = options.address_format;
  691. }
  692. if (options.filter) {
  693. requestParam.filter = options.filter;
  694. }
  695. var distance = options.distance || "1000";
  696. var auto_extend = options.auto_extend || 1;
  697. var region = null;
  698. var rectangle = null;
  699. //判断城市限定参数
  700. if (options.region) {
  701. region = options.region;
  702. }
  703. //矩形限定坐标(暂时只支持字符串格式)
  704. if (options.rectangle) {
  705. rectangle = options.rectangle;
  706. }
  707. var locationsuccess = function (result) {
  708. if (region && !rectangle) {
  709. //城市限定参数拼接
  710. requestParam.boundary = "region(" + region + "," + auto_extend + "," + result.latitude + "," + result.longitude + ")";
  711. if (options.sig) {
  712. requestParam.sig = Utils.getSig(requestParam, options.sig, 'search');
  713. }
  714. } else if (rectangle && !region) {
  715. //矩形搜索
  716. requestParam.boundary = "rectangle(" + rectangle + ")";
  717. if (options.sig) {
  718. requestParam.sig = Utils.getSig(requestParam, options.sig, 'search');
  719. }
  720. } else {
  721. requestParam.boundary = "nearby(" + result.latitude + "," + result.longitude + "," + distance + "," + auto_extend + ")";
  722. if (options.sig) {
  723. requestParam.sig = Utils.getSig(requestParam, options.sig, 'search');
  724. }
  725. }
  726. wx.request(Utils.buildWxRequestConfig(options, {
  727. url: URL_SEARCH,
  728. data: requestParam
  729. }, 'search'));
  730. };
  731. Utils.locationProcess(options, locationsuccess);
  732. };
  733. /**
  734. * sug模糊检索
  735. *
  736. * @param {Object} options 接口参数对象
  737. *
  738. * 参数对象结构可以参考
  739. * http://lbs.qq.com/webservice_v1/guide-suggestion.html
  740. */
  741. getSuggestion(options) {
  742. var that = this;
  743. options = options || {};
  744. Utils.polyfillParam(options);
  745. if (!Utils.checkKeyword(options)) {
  746. return;
  747. }
  748. var requestParam = {
  749. keyword: options.keyword,
  750. region: options.region || '全国',
  751. region_fix: options.region_fix || 0,
  752. policy: options.policy || 0,
  753. page_size: options.page_size || 10,//控制显示条数
  754. page_index: options.page_index || 1,//控制页数
  755. get_subpois : options.get_subpois || 0,//返回子地点
  756. output: 'json',
  757. key: that.key
  758. };
  759. //长地址
  760. if (options.address_format) {
  761. requestParam.address_format = options.address_format;
  762. }
  763. //过滤
  764. if (options.filter) {
  765. requestParam.filter = options.filter;
  766. }
  767. //排序
  768. if (options.location) {
  769. var locationsuccess = function (result) {
  770. requestParam.location = result.latitude + ',' + result.longitude;
  771. if (options.sig) {
  772. requestParam.sig = Utils.getSig(requestParam, options.sig, 'suggest');
  773. }
  774. wx.request(Utils.buildWxRequestConfig(options, {
  775. url: URL_SUGGESTION,
  776. data: requestParam
  777. }, "suggest"));
  778. };
  779. Utils.locationProcess(options, locationsuccess);
  780. } else {
  781. if (options.sig) {
  782. requestParam.sig = Utils.getSig(requestParam, options.sig, 'suggest');
  783. }
  784. wx.request(Utils.buildWxRequestConfig(options, {
  785. url: URL_SUGGESTION,
  786. data: requestParam
  787. }, "suggest"));
  788. }
  789. };
  790. /**
  791. * 逆地址解析
  792. *
  793. * @param {Object} options 接口参数对象
  794. *
  795. * 请求参数结构可以参考
  796. * http://lbs.qq.com/webservice_v1/guide-gcoder.html
  797. */
  798. reverseGeocoder(options) {
  799. var that = this;
  800. options = options || {};
  801. Utils.polyfillParam(options);
  802. var requestParam = {
  803. coord_type: options.coord_type || 5,
  804. get_poi: options.get_poi || 0,
  805. output: 'json',
  806. key: that.key
  807. };
  808. if (options.poi_options) {
  809. requestParam.poi_options = options.poi_options
  810. }
  811. var locationsuccess = function (result) {
  812. requestParam.location = result.latitude + ',' + result.longitude;
  813. if (options.sig) {
  814. requestParam.sig = Utils.getSig(requestParam, options.sig, 'reverseGeocoder');
  815. }
  816. wx.request(Utils.buildWxRequestConfig(options, {
  817. url: URL_GET_GEOCODER,
  818. data: requestParam
  819. }, 'reverseGeocoder'));
  820. };
  821. Utils.locationProcess(options, locationsuccess);
  822. };
  823. /**
  824. * 地址解析
  825. *
  826. * @param {Object} options 接口参数对象
  827. *
  828. * 请求参数结构可以参考
  829. * http://lbs.qq.com/webservice_v1/guide-geocoder.html
  830. */
  831. geocoder(options) {
  832. var that = this;
  833. options = options || {};
  834. Utils.polyfillParam(options);
  835. if (Utils.checkParamKeyEmpty(options, 'address')) {
  836. return;
  837. }
  838. var requestParam = {
  839. address: options.address,
  840. output: 'json',
  841. key: that.key
  842. };
  843. //城市限定
  844. if (options.region) {
  845. requestParam.region = options.region;
  846. }
  847. if (options.sig) {
  848. requestParam.sig = Utils.getSig(requestParam, options.sig, 'geocoder');
  849. }
  850. wx.request(Utils.buildWxRequestConfig(options, {
  851. url: URL_GET_GEOCODER,
  852. data: requestParam
  853. },'geocoder'));
  854. };
  855. /**
  856. * 获取城市列表
  857. *
  858. * @param {Object} options 接口参数对象
  859. *
  860. * 请求参数结构可以参考
  861. * http://lbs.qq.com/webservice_v1/guide-region.html
  862. */
  863. getCityList(options) {
  864. var that = this;
  865. options = options || {};
  866. Utils.polyfillParam(options);
  867. var requestParam = {
  868. output: 'json',
  869. key: that.key
  870. };
  871. if (options.sig) {
  872. requestParam.sig = Utils.getSig(requestParam, options.sig, 'getCityList');
  873. }
  874. wx.request(Utils.buildWxRequestConfig(options, {
  875. url: URL_CITY_LIST,
  876. data: requestParam
  877. },'getCityList'));
  878. };
  879. /**
  880. * 获取对应城市ID的区县列表
  881. *
  882. * @param {Object} options 接口参数对象
  883. *
  884. * 请求参数结构可以参考
  885. * http://lbs.qq.com/webservice_v1/guide-region.html
  886. */
  887. getDistrictByCityId(options) {
  888. var that = this;
  889. options = options || {};
  890. Utils.polyfillParam(options);
  891. if (Utils.checkParamKeyEmpty(options, 'id')) {
  892. return;
  893. }
  894. var requestParam = {
  895. id: options.id || '',
  896. output: 'json',
  897. key: that.key
  898. };
  899. if (options.sig) {
  900. requestParam.sig = Utils.getSig(requestParam, options.sig, 'getDistrictByCityId');
  901. }
  902. wx.request(Utils.buildWxRequestConfig(options, {
  903. url: URL_AREA_LIST,
  904. data: requestParam
  905. },'getDistrictByCityId'));
  906. };
  907. /**
  908. * 用于单起点到多终点的路线距离(非直线距离)计算:
  909. * 支持两种距离计算方式:步行和驾车。
  910. * 起点到终点最大限制直线距离10公里。
  911. *
  912. * 新增直线距离计算。
  913. *
  914. * @param {Object} options 接口参数对象
  915. *
  916. * 请求参数结构可以参考
  917. * http://lbs.qq.com/webservice_v1/guide-distance.html
  918. */
  919. calculateDistance(options) {
  920. var that = this;
  921. options = options || {};
  922. Utils.polyfillParam(options);
  923. if (Utils.checkParamKeyEmpty(options, 'to')) {
  924. return;
  925. }
  926. var requestParam = {
  927. mode: options.mode || 'walking',
  928. to: Utils.location2query(options.to),
  929. output: 'json',
  930. key: that.key
  931. };
  932. if (options.from) {
  933. options.location = options.from;
  934. }
  935. //计算直线距离
  936. if(requestParam.mode == 'straight'){
  937. var locationsuccess = function (result) {
  938. var locationTo = Utils.getEndLocation(requestParam.to);//处理终点坐标
  939. var data = {
  940. message:"query ok",
  941. result:{
  942. elements:[]
  943. },
  944. status:0
  945. };
  946. for (var i = 0; i < locationTo.length; i++) {
  947. data.result.elements.push({//将坐标存入
  948. distance: Utils.getDistance(result.latitude, result.longitude, locationTo[i].lat, locationTo[i].lng),
  949. duration:0,
  950. from:{
  951. lat: result.latitude,
  952. lng:result.longitude
  953. },
  954. to:{
  955. lat: locationTo[i].lat,
  956. lng: locationTo[i].lng
  957. }
  958. });
  959. }
  960. var calculateResult = data.result.elements;
  961. var distanceResult = [];
  962. for (var i = 0; i < calculateResult.length; i++) {
  963. distanceResult.push(calculateResult[i].distance);
  964. }
  965. return options.success(data,{
  966. calculateResult: calculateResult,
  967. distanceResult: distanceResult
  968. });
  969. };
  970. Utils.locationProcess(options, locationsuccess);
  971. } else {
  972. var locationsuccess = function (result) {
  973. requestParam.from = result.latitude + ',' + result.longitude;
  974. if (options.sig) {
  975. requestParam.sig = Utils.getSig(requestParam, options.sig, 'calculateDistance');
  976. }
  977. wx.request(Utils.buildWxRequestConfig(options, {
  978. url: URL_DISTANCE,
  979. data: requestParam
  980. },'calculateDistance'));
  981. };
  982. Utils.locationProcess(options, locationsuccess);
  983. }
  984. };
  985. /**
  986. * 路线规划:
  987. *
  988. * @param {Object} options 接口参数对象
  989. *
  990. * 请求参数结构可以参考
  991. * https://lbs.qq.com/webservice_v1/guide-road.html
  992. */
  993. direction(options) {
  994. var that = this;
  995. options = options || {};
  996. Utils.polyfillParam(options);
  997. if (Utils.checkParamKeyEmpty(options, 'to')) {
  998. return;
  999. }
  1000. var requestParam = {
  1001. output: 'json',
  1002. key: that.key
  1003. };
  1004. //to格式处理
  1005. if (typeof options.to == 'string') {
  1006. requestParam.to = options.to;
  1007. } else {
  1008. requestParam.to = options.to.latitude + ',' + options.to.longitude;
  1009. }
  1010. //初始化局部请求域名
  1011. var SET_URL_DIRECTION = null;
  1012. //设置默认mode属性
  1013. options.mode = options.mode || MODE.driving;
  1014. //设置请求域名
  1015. SET_URL_DIRECTION = URL_DIRECTION + options.mode;
  1016. if (options.from) {
  1017. options.location = options.from;
  1018. }
  1019. if (options.mode == MODE.driving) {
  1020. if (options.from_poi) {
  1021. requestParam.from_poi = options.from_poi;
  1022. }
  1023. if (options.heading) {
  1024. requestParam.heading = options.heading;
  1025. }
  1026. if (options.speed) {
  1027. requestParam.speed = options.speed;
  1028. }
  1029. if (options.accuracy) {
  1030. requestParam.accuracy = options.accuracy;
  1031. }
  1032. if (options.road_type) {
  1033. requestParam.road_type = options.road_type;
  1034. }
  1035. if (options.to_poi) {
  1036. requestParam.to_poi = options.to_poi;
  1037. }
  1038. if (options.from_track) {
  1039. requestParam.from_track = options.from_track;
  1040. }
  1041. if (options.waypoints) {
  1042. requestParam.waypoints = options.waypoints;
  1043. }
  1044. if (options.policy) {
  1045. requestParam.policy = options.policy;
  1046. }
  1047. if (options.plate_number) {
  1048. requestParam.plate_number = options.plate_number;
  1049. }
  1050. }
  1051. if (options.mode == MODE.transit) {
  1052. if (options.departure_time) {
  1053. requestParam.departure_time = options.departure_time;
  1054. }
  1055. if (options.policy) {
  1056. requestParam.policy = options.policy;
  1057. }
  1058. }
  1059. var locationsuccess = function (result) {
  1060. requestParam.from = result.latitude + ',' + result.longitude;
  1061. if (options.sig) {
  1062. requestParam.sig = Utils.getSig(requestParam, options.sig, 'direction',options.mode);
  1063. }
  1064. wx.request(Utils.buildWxRequestConfig(options, {
  1065. url: SET_URL_DIRECTION,
  1066. data: requestParam
  1067. }, 'direction'));
  1068. };
  1069. Utils.locationProcess(options, locationsuccess);
  1070. }
  1071. };
  1072. module.exports = QQMapWX;