webservice.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  2. import { isUndefined, isNil, isEqual, forOwn } from "lodash";
  3. // M: Mandatory,必选
  4. // api调用限流说明: https://lbs.amap.com/api/webservice/guide/tools/flowlevel
  5. export var services = {
  6. // 地理/逆地理编码: https://lbs.amap.com/api/webservice/guide/api/georegeo
  7. geocode_geo: {
  8. name: '地理编码',
  9. method: 'GET',
  10. url: 'https://restapi.amap.com/v3/geocode/geo',
  11. parameters: {
  12. key: undefined, // M
  13. address: undefined, // M
  14. city: undefined, // M
  15. batch: false,
  16. sig: undefined,
  17. output: 'JSON',
  18. callback: null
  19. }
  20. },
  21. geocode_regeo: {
  22. name: '逆地理编码',
  23. method: 'GET',
  24. url: 'https://restapi.amap.com/v3/geocode/regeo',
  25. parameters: {
  26. key: undefined, // M
  27. location: undefined, // M
  28. poitype: undefined,
  29. radius: 1000,
  30. extensions: 'base',
  31. batch: false,
  32. roadlevel: undefined,
  33. sig: undefined,
  34. output: 'JSON',
  35. callback: null,
  36. homeorcorp: 0
  37. }
  38. },
  39. // 路径规划: https://lbs.amap.com/api/webservice/guide/api/direction
  40. direction_walking: {
  41. name: '步行路径规划',
  42. method: 'GET',
  43. url: 'https://restapi.amap.com/v3/direction/walking',
  44. parameters: {
  45. key: undefined, // M
  46. origin: undefined, // M
  47. destination: undefined, // M
  48. sig: undefined,
  49. output: 'JSON',
  50. callback: null
  51. }
  52. },
  53. direction_transit_integrated: {
  54. name: '公交路径规划',
  55. method: 'GET',
  56. url: 'https://restapi.amap.com/v3/direction/transit/integrated',
  57. parameters: {
  58. key: undefined // M
  59. }
  60. },
  61. direction_driving: {
  62. name: '驾车路径规划',
  63. method: 'GET',
  64. url: 'https://restapi.amap.com/v3/direction/driving',
  65. parameters: {
  66. key: undefined // M
  67. }
  68. },
  69. direction_bicycling: {
  70. name: '骑行路径规划',
  71. method: 'GET',
  72. url: 'https://restapi.amap.com/v4/direction/bicycling',
  73. parameters: {
  74. key: undefined // M
  75. }
  76. },
  77. direction_truck: {
  78. name: '货车路径规划',
  79. method: 'GET',
  80. url: 'https://restapi.amap.com/v4/direction/truck',
  81. parameters: {
  82. key: undefined // M
  83. }
  84. },
  85. distance: {
  86. name: '距离测量',
  87. method: 'GET',
  88. url: 'https://restapi.amap.com/v3/distance',
  89. parameters: {
  90. key: undefined // M
  91. }
  92. },
  93. // 行政区域查询: https://lbs.amap.com/api/webservice/guide/api/district
  94. config_district: {
  95. name: '行政区域查询',
  96. method: 'GET',
  97. url: 'https://restapi.amap.com/v3/config/district',
  98. parameters: {
  99. key: undefined // M
  100. }
  101. },
  102. // 搜索POI: https://lbs.amap.com/api/webservice/guide/api/search
  103. place_text: {
  104. name: '关键字搜索',
  105. method: 'GET',
  106. url: 'https://restapi.amap.com/v3/place/text',
  107. parameters: {
  108. key: undefined // M
  109. }
  110. },
  111. place_around: {
  112. name: '周边搜索',
  113. method: 'GET',
  114. url: 'https://restapi.amap.com/v3/place/around',
  115. parameters: {
  116. key: undefined // M
  117. }
  118. },
  119. place_polygon: {
  120. name: '多边形搜索',
  121. method: 'GET',
  122. url: 'https://restapi.amap.com/v3/place/polygon',
  123. parameters: {
  124. key: undefined // M
  125. }
  126. },
  127. place_detail: {
  128. name: 'ID查询',
  129. method: 'GET',
  130. url: 'https://restapi.amap.com/v3/place/detail',
  131. parameters: {
  132. key: undefined // M
  133. }
  134. },
  135. // IP定位: https://lbs.amap.com/api/webservice/guide/api/ipconfig
  136. ip: {
  137. name: 'IP定位',
  138. method: 'GET',
  139. url: 'https://restapi.amap.com/v3/ip',
  140. parameters: {
  141. key: undefined // M
  142. }
  143. },
  144. // 抓路服务: https://lbs.amap.com/api/webservice/guide/api/autograsp
  145. autograsp: {
  146. name: '抓路服务',
  147. method: 'GET',
  148. url: 'https://restapi.amap.com/v3/autograsp',
  149. parameters: {
  150. key: undefined // M
  151. }
  152. },
  153. // 批量请求接口: https://lbs.amap.com/api/webservice/guide/api/batchrequest
  154. batch: {
  155. name: '批量请求接口',
  156. method: 'POST',
  157. url: 'https://restapi.amap.com/v3/batch',
  158. parameters: {
  159. key: undefined // M
  160. },
  161. body: {
  162. "ops": [{
  163. "url": "/v3/place/around?offset=10&page=1&key=<您的key>&location=116.50394379585519,39.278209477408794&output=json&radius=100000&types=080000"
  164. }, {
  165. "url": "/v3/place/around?offset=10&page=1&key=<您的key>&location=118.50394379585519,39.278209477408794&output=json&radius=100000&types=080000"
  166. }]
  167. }
  168. },
  169. // 静态地图: https://lbs.amap.com/api/webservice/guide/api/staticmaps
  170. staticmap: {
  171. name: '静态地图',
  172. method: 'GET',
  173. url: 'https://restapi.amap.com/v3/staticmap',
  174. parameters: {
  175. key: undefined // M
  176. }
  177. },
  178. // 坐标转换: https://lbs.amap.com/api/webservice/guide/api/convert
  179. assistant_coordinate_convert: {
  180. name: '坐标转换',
  181. method: 'GET',
  182. url: 'https://restapi.amap.com/v3/assistant/coordinate/convert',
  183. parameters: {
  184. key: undefined // M
  185. }
  186. },
  187. // 天气查询: https://lbs.amap.com/api/webservice/guide/api/weatherinfo
  188. weather_weatherInfo: {
  189. name: '天气查询',
  190. method: 'GET',
  191. url: 'https://restapi.amap.com/v3/weather/weatherInfo',
  192. parameters: {
  193. key: undefined // M
  194. }
  195. },
  196. // 输入提示: https://lbs.amap.com/api/webservice/guide/api/inputtips
  197. assistant_inputtips: {
  198. name: '输入提示',
  199. method: 'GET',
  200. url: 'https://restapi.amap.com/v3/assistant/inputtips',
  201. parameters: {
  202. key: undefined // M
  203. }
  204. },
  205. // 交通态势: https://lbs.amap.com/api/webservice/guide/api/trafficstatus
  206. traffic_status_rectangle: {
  207. name: '矩形区域交通态势',
  208. method: 'GET',
  209. url: 'https://restapi.amap.com/v3/traffic/status/rectangle',
  210. parameters: {
  211. key: undefined // M
  212. }
  213. },
  214. traffic_status_circle: {
  215. name: '圆形区域交通态势',
  216. method: 'GET',
  217. url: 'https://restapi.amap.com/v3/traffic/status/circle',
  218. parameters: {
  219. key: undefined // M
  220. }
  221. },
  222. traffic_status_road: {
  223. name: '指定线路交通态势',
  224. method: 'GET',
  225. url: 'https://restapi.amap.com/v3/traffic/status/road',
  226. parameters: {
  227. key: undefined // M
  228. }
  229. },
  230. // 地理围栏: https://lbs.amap.com/api/webservice/guide/api/geofence_service
  231. geofence_meta_post: {
  232. name: '创建围栏',
  233. method: 'POST',
  234. url: 'https://restapi.amap.com/v4/geofence/meta',
  235. parameters: {
  236. key: undefined // M
  237. },
  238. body: {
  239. name: undefined //M
  240. }
  241. },
  242. geofence_meta_get: {
  243. name: '查询围栏',
  244. method: 'GET',
  245. url: 'https://restapi.amap.com/v4/geofence/meta',
  246. parameters: {
  247. key: undefined // M
  248. }
  249. },
  250. geofence_meta_patch: {
  251. name: '更新围栏',
  252. method: 'POST ', // PATCH
  253. url: 'https://restapi.amap.com/v4/geofence/meta',
  254. parameters: {
  255. key: undefined, // M
  256. gid: undefined, // M
  257. method: undefined // 'patch'
  258. },
  259. body: {
  260. name: undefined //M
  261. }
  262. },
  263. geofence_meta_patch2: {
  264. name: '围栏启动&停止',
  265. method: 'POST ', // PATCH
  266. url: 'https://restapi.amap.com/v4/geofence/meta',
  267. parameters: {
  268. key: undefined, // M
  269. gid: undefined, //M
  270. method: undefined //'patch'
  271. },
  272. body: {
  273. name: undefined //M
  274. }
  275. },
  276. geofence_meta_delete: {
  277. name: '删除围栏',
  278. method: 'DELETE ', // POST
  279. url: 'https://restapi.amap.com/v4/geofence/meta',
  280. parameters: {
  281. key: undefined, // M
  282. gid: undefined, //M
  283. method: undefined //'delete'
  284. },
  285. body: {
  286. name: undefined //M
  287. }
  288. },
  289. geofence_status: {
  290. name: '围栏设备监控',
  291. method: 'GET',
  292. url: 'https://restapi.amap.com/v4/geofence/status',
  293. parameters: {
  294. key: undefined, // M
  295. diu: undefined, // M
  296. uid: undefined,
  297. locations: undefined, // M
  298. sig: undefined
  299. }
  300. },
  301. // 轨迹纠偏: https://lbs.amap.com/api/webservice/guide/api/grasproad
  302. grasproad_driving: {
  303. name: '轨迹纠偏',
  304. method: 'POST',
  305. url: 'https://restapi.amap.com/v4/grasproad/driving',
  306. parameters: {
  307. key: undefined // M
  308. },
  309. body: {
  310. x: undefined, // M
  311. y: undefined, // M
  312. ag: undefined, // M
  313. tm: undefined, // M
  314. sp: undefined // M
  315. }
  316. }
  317. };
  318. export var infocode = {
  319. "10000": {
  320. info: 'OK',
  321. desc: '请求正常',
  322. suggest: '请求正常'
  323. },
  324. "10001": {
  325. info: 'INVALID_USER_KEY',
  326. desc: 'key不正确或过期',
  327. suggest: '开发者发起请求时,传入的key不正确或者过期 '
  328. },
  329. "10002": {
  330. info: 'SERVICE_NOT_AVAILABLE',
  331. desc: '没有权限使用相应的服务或者请求接口的路径拼写错误',
  332. suggest: '1.开发者没有权限使用相应的服务,例如:开发者申请了WEB定位功能的key,却使用该key访问逆地理编码功能时,就会返回该错误。反之亦然。2.开发者请求接口的路径拼写错误。例如:正确的https://restapi.amap.com/v3/ip在程序中被拼装写了https://restapi.amap.com/vv3/ip"'
  333. }
  334. };
  335. export var getRequest = function getRequest(serviceId, parameters, body) {
  336. if (!serviceId) return null;
  337. var service = services[serviceId];
  338. var option = {};
  339. var name = service.name,
  340. url = service.url,
  341. method = service.method,
  342. dfParameters = service.parameters,
  343. dfBody = service.body;
  344. var destParameters = _extends({}, dfParameters || {}, parameters || {});
  345. // 组建url中参数
  346. var paramArr = [];
  347. forOwn(destParameters, function (value, key) {
  348. if (!isNil(value)) {
  349. paramArr.push(key + '=' + value);
  350. }
  351. });
  352. if (paramArr.length > 0) {
  353. url += '?' + paramArr.join('&');
  354. }
  355. // 组建option
  356. if (method === 'GET') {
  357. option = {
  358. credentials: 'include',
  359. method: method,
  360. headers: { 'Content-Type': 'application/json' }
  361. };
  362. } else if (method === 'POST' || method === 'PUT' || method === 'PATCH') {
  363. var destBody = _extends({}, dfBody || {}, body || {});
  364. option = {
  365. credentials: 'include',
  366. method: method,
  367. headers: { 'Content-Type': 'application/json' },
  368. body: destBody
  369. };
  370. } else {
  371. console.log('error! not support method=' + method + ' of ' + name);
  372. }
  373. return {
  374. url: url,
  375. option: option
  376. };
  377. };