amap-wx.130.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. function AMapWX(a) {
  2. this.key = a.key;
  3. this.requestConfig = {
  4. key: a.key,
  5. s: "rsx",
  6. platform: "WXJS",
  7. appname: a.key,
  8. sdkversion: "1.2.0",
  9. logversion: "2.0"
  10. };
  11. this.MeRequestConfig = {
  12. key: a.key,
  13. serviceName: "https://restapi.amap.com/rest/me"
  14. }
  15. }
  16. AMapWX.prototype.getWxLocation = function(a, b) {
  17. uni.getLocation({
  18. type: "gcj02",
  19. success: function(c) {
  20. console.log("定位成功")
  21. c = c.longitude + "," + c.latitude;
  22. uni.setStorage({
  23. key: "userLocation",
  24. data: c
  25. });
  26. b(c)
  27. },
  28. fail: function(c) {
  29. console.log("定位失败")
  30. uni.getStorage({
  31. key: "userLocation",
  32. success: function(d) {
  33. d.data && b(d.data)
  34. }
  35. });
  36. a.fail({
  37. errCode: "0",
  38. errMsg: c.errMsg || ""
  39. })
  40. }
  41. })
  42. };
  43. AMapWX.prototype.getMEKeywordsSearch = function(a) {
  44. if (!a.options) return a.fail({
  45. errCode: "0",
  46. errMsg: "\u7f3a\u5c11\u5fc5\u8981\u53c2\u6570"
  47. });
  48. var b = a.options,
  49. c = this.MeRequestConfig,
  50. d = {
  51. key: c.key,
  52. s: "rsx",
  53. platform: "WXJS",
  54. appname: a.key,
  55. sdkversion: "1.2.0",
  56. logversion: "2.0"
  57. };
  58. b.layerId && (d.layerId = b.layerId);
  59. b.keywords && (d.keywords = b.keywords);
  60. b.city && (d.city = b.city);
  61. b.filter && (d.filter = b.filter);
  62. b.sortrule && (d.sortrule = b.sortrule);
  63. b.pageNum && (d.pageNum = b.pageNum);
  64. b.pageSize && (d.pageSize = b.pageSize);
  65. b.sig && (d.sig =
  66. b.sig);
  67. uni.request({
  68. url: c.serviceName + "/cpoint/datasearch/local",
  69. data: d,
  70. method: "GET",
  71. header: {
  72. "content-type": "application/json"
  73. },
  74. success: function(e) {
  75. (e = e.data) && e.status && "1" === e.status && 0 === e.code ? a.success(e.data) : a.fail({
  76. errCode: "0",
  77. errMsg: e
  78. })
  79. },
  80. fail: function(e) {
  81. a.fail({
  82. errCode: "0",
  83. errMsg: e.errMsg || ""
  84. })
  85. }
  86. })
  87. };
  88. AMapWX.prototype.getMEIdSearch = function(a) {
  89. if (!a.options) return a.fail({
  90. errCode: "0",
  91. errMsg: "\u7f3a\u5c11\u5fc5\u8981\u53c2\u6570"
  92. });
  93. var b = a.options,
  94. c = this.MeRequestConfig,
  95. d = {
  96. key: c.key,
  97. s: "rsx",
  98. platform: "WXJS",
  99. appname: a.key,
  100. sdkversion: "1.2.0",
  101. logversion: "2.0"
  102. };
  103. b.layerId && (d.layerId = b.layerId);
  104. b.id && (d.id = b.id);
  105. b.sig && (d.sig = b.sig);
  106. uni.request({
  107. url: c.serviceName + "/cpoint/datasearch/id",
  108. data: d,
  109. method: "GET",
  110. header: {
  111. "content-type": "application/json"
  112. },
  113. success: function(e) {
  114. (e = e.data) && e.status && "1" === e.status &&
  115. 0 === e.code ? a.success(e.data) : a.fail({
  116. errCode: "0",
  117. errMsg: e
  118. })
  119. },
  120. fail: function(e) {
  121. a.fail({
  122. errCode: "0",
  123. errMsg: e.errMsg || ""
  124. })
  125. }
  126. })
  127. };
  128. AMapWX.prototype.getMEPolygonSearch = function(a) {
  129. if (!a.options) return a.fail({
  130. errCode: "0",
  131. errMsg: "\u7f3a\u5c11\u5fc5\u8981\u53c2\u6570"
  132. });
  133. var b = a.options,
  134. c = this.MeRequestConfig,
  135. d = {
  136. key: c.key,
  137. s: "rsx",
  138. platform: "WXJS",
  139. appname: a.key,
  140. sdkversion: "1.2.0",
  141. logversion: "2.0"
  142. };
  143. b.layerId && (d.layerId = b.layerId);
  144. b.keywords && (d.keywords = b.keywords);
  145. b.polygon && (d.polygon = b.polygon);
  146. b.filter && (d.filter = b.filter);
  147. b.sortrule && (d.sortrule = b.sortrule);
  148. b.pageNum && (d.pageNum = b.pageNum);
  149. b.pageSize && (d.pageSize = b.pageSize);
  150. b.sig && (d.sig = b.sig);
  151. uni.request({
  152. url: c.serviceName + "/cpoint/datasearch/polygon",
  153. data: d,
  154. method: "GET",
  155. header: {
  156. "content-type": "application/json"
  157. },
  158. success: function(e) {
  159. (e = e.data) && e.status && "1" === e.status && 0 === e.code ? a.success(e.data) : a.fail({
  160. errCode: "0",
  161. errMsg: e
  162. })
  163. },
  164. fail: function(e) {
  165. a.fail({
  166. errCode: "0",
  167. errMsg: e.errMsg || ""
  168. })
  169. }
  170. })
  171. };
  172. AMapWX.prototype.getMEaroundSearch = function(a) {
  173. if (!a.options) return a.fail({
  174. errCode: "0",
  175. errMsg: "\u7f3a\u5c11\u5fc5\u8981\u53c2\u6570"
  176. });
  177. var b = a.options,
  178. c = this.MeRequestConfig,
  179. d = {
  180. key: c.key,
  181. s: "rsx",
  182. platform: "WXJS",
  183. appname: a.key,
  184. sdkversion: "1.2.0",
  185. logversion: "2.0"
  186. };
  187. b.layerId && (d.layerId = b.layerId);
  188. b.keywords && (d.keywords = b.keywords);
  189. b.center && (d.center = b.center);
  190. b.radius && (d.radius = b.radius);
  191. b.filter && (d.filter = b.filter);
  192. b.sortrule && (d.sortrule = b.sortrule);
  193. b.pageNum && (d.pageNum = b.pageNum);
  194. b.pageSize &&
  195. (d.pageSize = b.pageSize);
  196. b.sig && (d.sig = b.sig);
  197. uni.request({
  198. url: c.serviceName + "/cpoint/datasearch/around",
  199. data: d,
  200. method: "GET",
  201. header: {
  202. "content-type": "application/json"
  203. },
  204. success: function(e) {
  205. (e = e.data) && e.status && "1" === e.status && 0 === e.code ? a.success(e.data) : a.fail({
  206. errCode: "0",
  207. errMsg: e
  208. })
  209. },
  210. fail: function(e) {
  211. a.fail({
  212. errCode: "0",
  213. errMsg: e.errMsg || ""
  214. })
  215. }
  216. })
  217. };
  218. AMapWX.prototype.getGeo = function(a) {
  219. var b = this.requestConfig,
  220. c = a.options;
  221. b = {
  222. key: this.key,
  223. extensions: "all",
  224. s: b.s,
  225. platform: b.platform,
  226. appname: this.key,
  227. sdkversion: b.sdkversion,
  228. logversion: b.logversion
  229. };
  230. c.address && (b.address = c.address);
  231. c.city && (b.city = c.city);
  232. c.batch && (b.batch = c.batch);
  233. c.sig && (b.sig = c.sig);
  234. uni.request({
  235. url: "https://restapi.amap.com/v3/geocode/geo",
  236. data: b,
  237. method: "GET",
  238. header: {
  239. "content-type": "application/json"
  240. },
  241. success: function(d) {
  242. (d = d.data) && d.status && "1" === d.status ? a.success(d) : a.fail({
  243. errCode: "0",
  244. errMsg: d
  245. })
  246. },
  247. fail: function(d) {
  248. a.fail({
  249. errCode: "0",
  250. errMsg: d.errMsg || ""
  251. })
  252. }
  253. })
  254. };
  255. AMapWX.prototype.getRegeo = function(a) {
  256. function b(d) {
  257. var e = c.requestConfig;
  258. uni.request({
  259. url: "https://restapi.amap.com/v3/geocode/regeo",
  260. data: {
  261. key: c.key,
  262. location: d,
  263. extensions: "all",
  264. s: e.s,
  265. platform: e.platform,
  266. appname: c.key,
  267. sdkversion: e.sdkversion,
  268. logversion: e.logversion
  269. },
  270. method: "GET",
  271. header: {
  272. "content-type": "application/json"
  273. },
  274. success: function(g) {
  275. if (g.data.status && "1" == g.data.status) {
  276. g = g.data.regeocode;
  277. var h = g.addressComponent,
  278. f = [],
  279. k = g.roads[0].name + "\u9644\u8fd1",
  280. m = d.split(",")[0],
  281. n = d.split(",")[1];
  282. if (g.pois &&
  283. g.pois[0]) {
  284. k = g.pois[0].name + "\u9644\u8fd1";
  285. var l = g.pois[0].location;
  286. l && (m = parseFloat(l.split(",")[0]), n = parseFloat(l.split(",")[1]))
  287. }
  288. h.provice && f.push(h.provice);
  289. h.city && f.push(h.city);
  290. h.district && f.push(h.district);
  291. h.streetNumber && h.streetNumber.street && h.streetNumber.number ? (f.push(h
  292. .streetNumber.street), f.push(h.streetNumber.number)) : f.push(g.roads[0]
  293. .name);
  294. f = f.join("");
  295. a.success([{
  296. iconPath: a.iconPath,
  297. width: a.iconWidth,
  298. height: a.iconHeight,
  299. name: f,
  300. desc: k,
  301. longitude: m,
  302. latitude: n,
  303. id: 0,
  304. regeocodeData: g
  305. }])
  306. } else a.fail({
  307. errCode: g.data.infocode,
  308. errMsg: g.data.info
  309. })
  310. },
  311. fail: function(g) {
  312. a.fail({
  313. errCode: "0",
  314. errMsg: g.errMsg || ""
  315. })
  316. }
  317. })
  318. }
  319. var c = this;
  320. a.location ? b(a.location) : c.getWxLocation(a, function(d) {
  321. b(d)
  322. })
  323. };
  324. AMapWX.prototype.getWeather = function(a) {
  325. function b(g) {
  326. var h = "base";
  327. a.type && "forecast" == a.type && (h = "all");
  328. uni.request({
  329. url: "https://restapi.amap.com/v3/weather/weatherInfo",
  330. data: {
  331. key: d.key,
  332. city: g,
  333. extensions: h,
  334. s: e.s,
  335. platform: e.platform,
  336. appname: d.key,
  337. sdkversion: e.sdkversion,
  338. logversion: e.logversion
  339. },
  340. method: "GET",
  341. header: {
  342. "content-type": "application/json"
  343. },
  344. success: function(f) {
  345. if (f.data.status && "1" == f.data.status)
  346. if (f.data.lives) {
  347. if ((f = f.data.lives) && 0 < f.length) {
  348. f = f[0];
  349. var k = {
  350. city: {
  351. text: "\u57ce\u5e02",
  352. data: f.city
  353. },
  354. weather: {
  355. text: "\u5929\u6c14",
  356. data: f.weather
  357. },
  358. temperature: {
  359. text: "\u6e29\u5ea6",
  360. data: f.temperature
  361. },
  362. winddirection: {
  363. text: "\u98ce\u5411",
  364. data: f.winddirection + "\u98ce"
  365. },
  366. windpower: {
  367. text: "\u98ce\u529b",
  368. data: f.windpower + "\u7ea7"
  369. },
  370. humidity: {
  371. text: "\u6e7f\u5ea6",
  372. data: f.humidity + "%"
  373. }
  374. };
  375. k.liveData = f;
  376. a.success(k)
  377. }
  378. } else f.data.forecasts && f.data.forecasts[0] && a.success({
  379. forecast: f.data.forecasts[0]
  380. });
  381. else a.fail({
  382. errCode: f.data.infocode,
  383. errMsg: f.data.info
  384. })
  385. },
  386. fail: function(f) {
  387. a.fail({
  388. errCode: "0",
  389. errMsg: f.errMsg || ""
  390. })
  391. }
  392. })
  393. }
  394. function c(g) {
  395. uni.request({
  396. url: "https://restapi.amap.com/v3/geocode/regeo",
  397. data: {
  398. key: d.key,
  399. location: g,
  400. extensions: "all",
  401. s: e.s,
  402. platform: e.platform,
  403. appname: d.key,
  404. sdkversion: e.sdkversion,
  405. logversion: e.logversion
  406. },
  407. method: "GET",
  408. header: {
  409. "content-type": "application/json"
  410. },
  411. success: function(h) {
  412. if (h.data.status && "1" == h.data.status) {
  413. h = h.data.regeocode;
  414. if (h.addressComponent) var f = h.addressComponent.adcode;
  415. else h.aois && 0 < h.aois.length && (f = h.aois[0].adcode);
  416. b(f)
  417. } else a.fail({
  418. errCode: h.data.infocode,
  419. errMsg: h.data.info
  420. })
  421. },
  422. fail: function(h) {
  423. a.fail({
  424. errCode: "0",
  425. errMsg: h.errMsg || ""
  426. })
  427. }
  428. })
  429. }
  430. var d = this,
  431. e = d.requestConfig;
  432. a.city ? b(a.city) : d.getWxLocation(a, function(g) {
  433. c(g)
  434. })
  435. };
  436. AMapWX.prototype.getPoiAround = function(a) {
  437. function b(e) {
  438. e = {
  439. key: c.key,
  440. location: e,
  441. s: d.s,
  442. platform: d.platform,
  443. appname: c.key,
  444. sdkversion: d.sdkversion,
  445. logversion: d.logversion
  446. };
  447. a.querytypes && (e.types = a.querytypes);
  448. a.querykeywords && (e.keywords = a.querykeywords);
  449. uni.request({
  450. url: "https://restapi.amap.com/v3/place/around",
  451. data: e,
  452. method: "GET",
  453. header: {
  454. "content-type": "application/json"
  455. },
  456. success: function(g) {
  457. if (g.data.status && "1" == g.data.status) {
  458. if ((g = g.data) && g.pois) {
  459. for (var h = [], f = 0; f < g.pois.length; f++) {
  460. var k = 0 ==
  461. f ? a.iconPathSelected : a.iconPath;
  462. h.push({
  463. latitude: parseFloat(g.pois[f].location.split(",")[1]),
  464. longitude: parseFloat(g.pois[f].location.split(",")[0]),
  465. iconPath: k,
  466. width: 22,
  467. height: 32,
  468. id: f,
  469. name: g.pois[f].name,
  470. address: g.pois[f].address
  471. })
  472. }
  473. a.success({
  474. markers: h,
  475. poisData: g.pois
  476. })
  477. }
  478. } else a.fail({
  479. errCode: g.data.infocode,
  480. errMsg: g.data.info
  481. })
  482. },
  483. fail: function(g) {
  484. a.fail({
  485. errCode: "0",
  486. errMsg: g.errMsg || ""
  487. })
  488. }
  489. })
  490. }
  491. var c = this,
  492. d = c.requestConfig;
  493. a.location ? b(a.location) : c.getWxLocation(a, function(e) {
  494. b(e)
  495. })
  496. };
  497. AMapWX.prototype.getStaticmap = function(a) {
  498. function b(e) {
  499. c.push("location=" + e);
  500. a.zoom && c.push("zoom=" + a.zoom);
  501. a.size && c.push("size=" + a.size);
  502. a.scale && c.push("scale=" + a.scale);
  503. a.markers && c.push("markers=" + a.markers);
  504. a.labels && c.push("labels=" + a.labels);
  505. a.paths && c.push("paths=" + a.paths);
  506. a.traffic && c.push("traffic=" + a.traffic);
  507. e = "https://restapi.amap.com/v3/staticmap?" + c.join("&");
  508. a.success({
  509. url: e
  510. })
  511. }
  512. var c = [];
  513. c.push("key=" + this.key);
  514. var d = this.requestConfig;
  515. c.push("s=" + d.s);
  516. c.push("platform=" + d.platform);
  517. c.push("appname=" + d.appname);
  518. c.push("sdkversion=" + d.sdkversion);
  519. c.push("logversion=" + d.logversion);
  520. a.location ? b(a.location) : this.getWxLocation(a, function(e) {
  521. b(e)
  522. })
  523. };
  524. AMapWX.prototype.getInputtips = function(a) {
  525. var b = Object.assign({}, this.requestConfig);
  526. a.location && (b.location = a.location);
  527. a.keywords && (b.keywords = a.keywords);
  528. a.type && (b.type = a.type);
  529. a.city && (b.city = a.city);
  530. a.citylimit && (b.citylimit = a.citylimit);
  531. uni.request({
  532. url: "https://restapi.amap.com/v3/assistant/inputtips",
  533. data: b,
  534. method: "GET",
  535. header: {
  536. "content-type": "application/json"
  537. },
  538. success: function(c) {
  539. c && c.data && c.data.tips && a.success({
  540. tips: c.data.tips
  541. })
  542. },
  543. fail: function(c) {
  544. a.fail({
  545. errCode: "0",
  546. errMsg: c.errMsg ||
  547. ""
  548. })
  549. }
  550. })
  551. };
  552. AMapWX.prototype.getDrivingRoute = function(a) {
  553. var b = Object.assign({}, this.requestConfig);
  554. a.origin && (b.origin = a.origin);
  555. a.destination && (b.destination = a.destination);
  556. a.strategy && (b.strategy = a.strategy);
  557. a.waypoints && (b.waypoints = a.waypoints);
  558. a.avoidpolygons && (b.avoidpolygons = a.avoidpolygons);
  559. a.avoidroad && (b.avoidroad = a.avoidroad);
  560. uni.request({
  561. url: "https://restapi.amap.com/v3/direction/driving",
  562. data: b,
  563. method: "GET",
  564. header: {
  565. "content-type": "application/json"
  566. },
  567. success: function(c) {
  568. c && c.data && c.data.route && a.success({
  569. paths: c.data.route.paths,
  570. taxi_cost: c.data.route.taxi_cost || ""
  571. })
  572. },
  573. fail: function(c) {
  574. a.fail({
  575. errCode: "0",
  576. errMsg: c.errMsg || ""
  577. })
  578. }
  579. })
  580. };
  581. AMapWX.prototype.getWalkingRoute = function(a) {
  582. var b = Object.assign({}, this.requestConfig);
  583. a.origin && (b.origin = a.origin);
  584. a.destination && (b.destination = a.destination);
  585. uni.request({
  586. url: "https://restapi.amap.com/v3/direction/walking",
  587. data: b,
  588. method: "GET",
  589. header: {
  590. "content-type": "application/json"
  591. },
  592. success: function(c) {
  593. c && c.data && c.data.route && a.success({
  594. paths: c.data.route.paths
  595. })
  596. },
  597. fail: function(c) {
  598. a.fail({
  599. errCode: "0",
  600. errMsg: c.errMsg || ""
  601. })
  602. }
  603. })
  604. };
  605. AMapWX.prototype.getTransitRoute = function(a) {
  606. var b = Object.assign({}, this.requestConfig);
  607. a.origin && (b.origin = a.origin);
  608. a.destination && (b.destination = a.destination);
  609. a.strategy && (b.strategy = a.strategy);
  610. a.city && (b.city = a.city);
  611. a.cityd && (b.cityd = a.cityd);
  612. uni.request({
  613. url: "https://restapi.amap.com/v3/direction/transit/integrated",
  614. data: b,
  615. method: "GET",
  616. header: {
  617. "content-type": "application/json"
  618. },
  619. success: function(c) {
  620. c && c.data && c.data.route && (c = c.data.route, a.success({
  621. distance: c.distance || "",
  622. taxi_cost: c.taxi_cost ||
  623. "",
  624. transits: c.transits
  625. }))
  626. },
  627. fail: function(c) {
  628. a.fail({
  629. errCode: "0",
  630. errMsg: c.errMsg || ""
  631. })
  632. }
  633. })
  634. };
  635. AMapWX.prototype.getRidingRoute = function(a) {
  636. var b = Object.assign({}, this.requestConfig);
  637. a.origin && (b.origin = a.origin);
  638. a.destination && (b.destination = a.destination);
  639. uni.request({
  640. url: "https://restapi.amap.com/v3/direction/riding",
  641. data: b,
  642. method: "GET",
  643. header: {
  644. "content-type": "application/json"
  645. },
  646. success: function(c) {
  647. c && c.data && c.data.route && a.success({
  648. paths: c.data.route.paths
  649. })
  650. },
  651. fail: function(c) {
  652. a.fail({
  653. errCode: "0",
  654. errMsg: c.errMsg || ""
  655. })
  656. }
  657. })
  658. };
  659. module.exports.AMapWX = AMapWX;