1
0

qqmap-wx-jssdk.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /**
  2. * 微信小程序JavaScriptSDK
  3. *
  4. * @version 1.0
  5. * @date 2017-01-10
  6. * @author jaysonzhou@tencent.com
  7. */
  8. var ERROR_CONF = {
  9. KEY_ERR: 311,
  10. KEY_ERR_MSG: 'key格式错误',
  11. PARAM_ERR: 310,
  12. PARAM_ERR_MSG: '请求参数信息有误',
  13. SYSTEM_ERR: 600,
  14. SYSTEM_ERR_MSG: '系统错误',
  15. WX_ERR_CODE: 1000,
  16. WX_OK_CODE: 200
  17. };
  18. var BASE_URL = 'https://apis.map.qq.com/ws/';
  19. var URL_SEARCH = BASE_URL + 'place/v1/search';
  20. var URL_SUGGESTION = BASE_URL + 'place/v1/suggestion';
  21. var URL_GET_GEOCODER = BASE_URL + 'geocoder/v1/';
  22. var URL_CITY_LIST = BASE_URL + 'district/v1/list';
  23. var URL_AREA_LIST = BASE_URL + 'district/v1/getchildren';
  24. var URL_DISTANCE = BASE_URL + 'distance/v1/';
  25. var Utils = {
  26. /**
  27. * 得到终点query字符串
  28. * @param {Array|String} 检索数据
  29. */
  30. location2query(data) {
  31. if (typeof data == 'string') {
  32. return data;
  33. }
  34. var query = '';
  35. for (var i = 0; i < data.length; i++) {
  36. var d = data[i];
  37. if (!!query) {
  38. query += ';';
  39. }
  40. if (d.location) {
  41. query = query + d.location.lat + ',' + d.location.lng;
  42. }
  43. if (d.latitude && d.longitude) {
  44. query = query + d.latitude + ',' + d.longitude;
  45. }
  46. }
  47. return query;
  48. },
  49. /**
  50. * 使用微信接口进行定位
  51. */
  52. getWXLocation(success, fail, complete) {
  53. wx.getFuzzyLocation({
  54. type: 'gcj02',
  55. success: success,
  56. fail: fail,
  57. complete: complete
  58. });
  59. },
  60. /**
  61. * 获取location参数
  62. */
  63. getLocationParam(location) {
  64. if (typeof location == 'string') {
  65. var locationArr = location.split(',');
  66. if (locationArr.length === 2) {
  67. location = {
  68. latitude: location.split(',')[0],
  69. longitude: location.split(',')[1]
  70. };
  71. } else {
  72. location = {};
  73. }
  74. }
  75. return location;
  76. },
  77. /**
  78. * 回调函数默认处理
  79. */
  80. polyfillParam(param) {
  81. param.success = param.success || function () { };
  82. param.fail = param.fail || function () { };
  83. param.complete = param.complete || function () { };
  84. },
  85. /**
  86. * 验证param对应的key值是否为空
  87. *
  88. * @param {Object} param 接口参数
  89. * @param {String} key 对应参数的key
  90. */
  91. checkParamKeyEmpty(param, key) {
  92. if (!param[key]) {
  93. var errconf = this.buildErrorConfig(ERROR_CONF.PARAM_ERR, ERROR_CONF.PARAM_ERR_MSG + key +'参数格式有误');
  94. param.fail(errconf);
  95. param.complete(errconf);
  96. return true;
  97. }
  98. return false;
  99. },
  100. /**
  101. * 验证参数中是否存在检索词keyword
  102. *
  103. * @param {Object} param 接口参数
  104. */
  105. checkKeyword(param){
  106. return !this.checkParamKeyEmpty(param, 'keyword');
  107. },
  108. /**
  109. * 验证location值
  110. *
  111. * @param {Object} param 接口参数
  112. */
  113. checkLocation(param) {
  114. var location = this.getLocationParam(param.location);
  115. if (!location || !location.latitude || !location.longitude) {
  116. var errconf = this.buildErrorConfig(ERROR_CONF.PARAM_ERR, ERROR_CONF.PARAM_ERR_MSG + ' location参数格式有误')
  117. param.fail(errconf);
  118. param.complete(errconf);
  119. return false;
  120. }
  121. return true;
  122. },
  123. /**
  124. * 构造错误数据结构
  125. * @param {Number} errCode 错误码
  126. * @param {Number} errMsg 错误描述
  127. */
  128. buildErrorConfig(errCode, errMsg) {
  129. return {
  130. status: errCode,
  131. message: errMsg
  132. };
  133. },
  134. /**
  135. * 构造微信请求参数,公共属性处理
  136. *
  137. * @param {Object} param 接口参数
  138. * @param {Object} param 配置项
  139. */
  140. buildWxRequestConfig(param, options) {
  141. var that = this;
  142. options.header = { "content-type": "application/json" };
  143. options.method = 'GET';
  144. options.success = function (res) {
  145. var data = res.data;
  146. if (data.status === 0) {
  147. param.success(data);
  148. } else {
  149. param.fail(data);
  150. }
  151. };
  152. options.fail = function (res) {
  153. res.statusCode = ERROR_CONF.WX_ERR_CODE;
  154. param.fail(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, result.errMsg));
  155. };
  156. options.complete = function (res) {
  157. var statusCode = +res.statusCode;
  158. switch(statusCode) {
  159. case ERROR_CONF.WX_ERR_CODE: {
  160. param.complete(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg));
  161. break;
  162. }
  163. case ERROR_CONF.WX_OK_CODE: {
  164. var data = res.data;
  165. if (data.status === 0) {
  166. param.complete(data);
  167. } else {
  168. param.complete(that.buildErrorConfig(data.status, data.message));
  169. }
  170. break;
  171. }
  172. default:{
  173. param.complete(that.buildErrorConfig(ERROR_CONF.SYSTEM_ERR, ERROR_CONF.SYSTEM_ERR_MSG));
  174. }
  175. }
  176. }
  177. return options;
  178. },
  179. /**
  180. * 处理用户参数是否传入坐标进行不同的处理
  181. */
  182. locationProcess(param, locationsuccess, locationfail, locationcomplete) {
  183. var that = this;
  184. locationfail = locationfail || function (res) {
  185. res.statusCode = ERROR_CONF.WX_ERR_CODE;
  186. param.fail(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg));
  187. };
  188. locationcomplete = locationcomplete || function (res) {
  189. if (res.statusCode == ERROR_CONF.WX_ERR_CODE) {
  190. param.complete(that.buildErrorConfig(ERROR_CONF.WX_ERR_CODE, res.errMsg));
  191. }
  192. };
  193. if (!param.location) {
  194. that.getWXLocation(locationsuccess, locationfail, locationcomplete);
  195. } else if (that.checkLocation(param)) {
  196. var location = Utils.getLocationParam(param.location);
  197. locationsuccess(location);
  198. }
  199. }
  200. }
  201. class QQMapWX {
  202. /**
  203. * 构造函数
  204. *
  205. * @param {Object} options 接口参数,key 为必选参数
  206. */
  207. constructor(options) {
  208. if (!options.key) {
  209. throw Error('key值不能为空');
  210. }
  211. this.key = options.key;
  212. }
  213. /**
  214. * POI周边检索
  215. *
  216. * @param {Object} options 接口参数对象
  217. *
  218. * 参数对象结构可以参考
  219. * @see http://lbs.qq.com/webservice_v1/guide-search.html
  220. */
  221. search(options) {
  222. var that = this;
  223. options = options || {};
  224. Utils.polyfillParam(options);
  225. if (!Utils.checkKeyword(options)) {
  226. return;
  227. }
  228. var requestParam = {
  229. keyword: options.keyword,
  230. orderby: options.orderby || '_distance',
  231. page_size: options.page_size || 10,
  232. page_index: options.page_index || 1,
  233. output: 'json',
  234. key: that.key
  235. };
  236. if (options.address_format) {
  237. requestParam.address_format = options.address_format;
  238. }
  239. if (options.filter) {
  240. requestParam.filter = options.filter;
  241. }
  242. var distance = options.distance || "1000";
  243. var auto_extend = options.auto_extend || 1;
  244. var locationsuccess = function (result) {
  245. requestParam.boundary = "nearby(" + result.latitude + "," + result.longitude + "," + distance + "," + auto_extend +")";
  246. wx.request(Utils.buildWxRequestConfig(options, {
  247. url: URL_SEARCH,
  248. data: requestParam
  249. }));
  250. }
  251. Utils.locationProcess(options, locationsuccess);
  252. }
  253. /**
  254. * sug模糊检索
  255. *
  256. * @param {Object} options 接口参数对象
  257. *
  258. * 参数对象结构可以参考
  259. * http://lbs.qq.com/webservice_v1/guide-suggestion.html
  260. */
  261. getSuggestion(options) {
  262. var that = this;
  263. options = options || {};
  264. Utils.polyfillParam(options);
  265. if (!Utils.checkKeyword(options)) {
  266. return;
  267. }
  268. var requestParam = {
  269. keyword: options.keyword,
  270. region: options.region || '全国',
  271. region_fix: options.region_fix || 0,
  272. policy: options.policy || 0,
  273. output: 'json',
  274. key: that.key
  275. };
  276. wx.request(Utils.buildWxRequestConfig(options, {
  277. url: URL_SUGGESTION,
  278. data: requestParam
  279. }));
  280. }
  281. /**
  282. * 逆地址解析
  283. *
  284. * @param {Object} options 接口参数对象
  285. *
  286. * 请求参数结构可以参考
  287. * http://lbs.qq.com/webservice_v1/guide-gcoder.html
  288. */
  289. reverseGeocoder(options) {
  290. var that = this;
  291. options = options || {};
  292. Utils.polyfillParam(options);
  293. var requestParam = {
  294. coord_type: options.coord_type || 5,
  295. get_poi: options.get_poi || 0,
  296. output: 'json',
  297. key: that.key
  298. };
  299. if (options.poi_options) {
  300. requestParam.poi_options = options.poi_options
  301. }
  302. var locationsuccess = function (result) {
  303. requestParam.location = result.latitude + ',' + result.longitude;
  304. wx.request(Utils.buildWxRequestConfig(options, {
  305. url: URL_GET_GEOCODER,
  306. data: requestParam
  307. }));
  308. };
  309. Utils.locationProcess(options, locationsuccess);
  310. }
  311. /**
  312. * 地址解析
  313. *
  314. * @param {Object} options 接口参数对象
  315. *
  316. * 请求参数结构可以参考
  317. * http://lbs.qq.com/webservice_v1/guide-geocoder.html
  318. */
  319. geocoder(options) {
  320. var that = this;
  321. options = options || {};
  322. Utils.polyfillParam(options);
  323. if (Utils.checkParamKeyEmpty(options, 'address')) {
  324. return;
  325. }
  326. var requestParam = {
  327. address: options.address,
  328. output: 'json',
  329. key: that.key
  330. };
  331. wx.request(Utils.buildWxRequestConfig(options, {
  332. url: URL_GET_GEOCODER,
  333. data: requestParam
  334. }));
  335. }
  336. /**
  337. * 获取城市列表
  338. *
  339. * @param {Object} options 接口参数对象
  340. *
  341. * 请求参数结构可以参考
  342. * http://lbs.qq.com/webservice_v1/guide-region.html
  343. */
  344. getCityList(options) {
  345. var that = this;
  346. options = options || {};
  347. Utils.polyfillParam(options);
  348. var requestParam = {
  349. output: 'json',
  350. key: that.key
  351. };
  352. wx.request(Utils.buildWxRequestConfig(options, {
  353. url: URL_CITY_LIST,
  354. data: requestParam
  355. }));
  356. }
  357. /**
  358. * 获取对应城市ID的区县列表
  359. *
  360. * @param {Object} options 接口参数对象
  361. *
  362. * 请求参数结构可以参考
  363. * http://lbs.qq.com/webservice_v1/guide-region.html
  364. */
  365. getDistrictByCityId(options) {
  366. var that = this;
  367. options = options || {};
  368. Utils.polyfillParam(options);
  369. if (Utils.checkParamKeyEmpty(options, 'id')) {
  370. return;
  371. }
  372. var requestParam = {
  373. id: options.id || '',
  374. output: 'json',
  375. key: that.key
  376. };
  377. wx.request(Utils.buildWxRequestConfig(options, {
  378. url: URL_AREA_LIST,
  379. data: requestParam
  380. }));
  381. }
  382. /**
  383. * 用于单起点到多终点的路线距离(非直线距离)计算:
  384. * 支持两种距离计算方式:步行和驾车。
  385. * 起点到终点最大限制直线距离10公里。
  386. *
  387. * @param {Object} options 接口参数对象
  388. *
  389. * 请求参数结构可以参考
  390. * http://lbs.qq.com/webservice_v1/guide-distance.html
  391. */
  392. calculateDistance(options) {
  393. var that = this;
  394. options = options || {};
  395. Utils.polyfillParam(options);
  396. if (Utils.checkParamKeyEmpty(options, 'to')) {
  397. return;
  398. }
  399. var requestParam = {
  400. mode: options.mode || 'walking',
  401. to: Utils.location2query(options.to),
  402. output: 'json',
  403. key: that.key
  404. };
  405. var locationsuccess = function (result) {
  406. requestParam.from = result.latitude + ',' + result.longitude;
  407. wx.request(Utils.buildWxRequestConfig(options, {
  408. url: URL_DISTANCE,
  409. data: requestParam
  410. }));
  411. }
  412. if (options.from) {
  413. options.location = options.from;
  414. }
  415. Utils.locationProcess(options, locationsuccess);
  416. }
  417. }
  418. module.exports = QQMapWX;