webservice.js 10 KB

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