webservice.js 10 KB

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