Nav apraksta

choose_city.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. var qs = parseQueryString(window.location.href);
  2. // var hostUrl = '../..';
  3. var hostUrl = BASEURL;
  4. var returnUrl = qs.returnUrl;
  5. var token = qs.token;
  6. var addressName = '';
  7. var local_city = localStorage.getItem("city");
  8. if(local_city){
  9. //已经定位
  10. }else{
  11. //还未定位
  12. getAddress();
  13. }
  14. checkToken();
  15. $('#back').click(function() {
  16. // window.webkit.messageHandlers.PopRootMessageHandler.postMessage({name:"1"});
  17. if (document.referrer.includes('fund_h5_api.html')) {
  18. window.location.href = document.referrer;
  19. } else {
  20. redirectToFailedReturnUrl('', 'failed', 1020, '退出公积金查询页');
  21. }
  22. });
  23. //搜索
  24. function searchCity () {
  25. window.location.href = "choice_city.html?token=" + token;
  26. }
  27. $('.current-city').click(function() {
  28. var flag = false;
  29. var cityName = $('.current-city span').text();
  30. var cityCode = $('.current-city span').attr('data-code');
  31. tips.find(function(item, index) {
  32. if(item.region == $('.current-city span').text()){
  33. flag = true;
  34. if (document.referrer.includes('&name=')) {
  35. window.location.href = document.referrer.replace(/&name=[^&]+&code=[^&]+/, '&name=' + cityName + '&code=' + cityCode);
  36. } else {
  37. window.location.href = './fund_h5_api.html?token=' + token + '&name=' + cityName + '&code=' + cityCode;
  38. }
  39. // window.location.href = './fund_h5_api.html?token=' + token + '&name=' + cityName + '&code=' + cityCode;
  40. }
  41. })
  42. if(!flag){
  43. window.location.href="no_open_city.html?cityName=" + $('.current-city span').text();
  44. }
  45. // if (document.referrer.includes('&name=')) {
  46. // window.location.href = document.referrer.replace(/&name=[^&]+&code=[^&]+/, '&name=' + cityName + '&code=' + cityCode);
  47. // }
  48. })
  49. function redirectToFailedReturnUrl(tid, result, failCode, reason) {
  50. var linkString = returnUrl.includes('?') ? '&' : '?';
  51. returnUrl += linkString + "result=" + result + "&tid=" + tid + "&token=" + token + "&failCode=" + failCode + "&reason=" + reason;
  52. window.location.href = returnUrl;
  53. }
  54. function showMsg(msg) {
  55. var msgBox = $('.alert-info');
  56. msgBox.children('p').text(msg);
  57. msgBox.show();
  58. setTimeout(function() {
  59. msgBox.hide();
  60. }, 30000);
  61. }
  62. function checkToken() {
  63. var getTokenInfo = $.ajax({
  64. type: 'GET',
  65. url: hostUrl + '/token?token=' + token + "&time=" + Math.round(Date.now() / 1000),
  66. timeout: 30000,
  67. contentType: 'application/json;charset=utf-8'
  68. });
  69. getTokenInfo.done(function(data) {
  70. if (data.returnUrl && !returnUrl) {
  71. returnUrl = data.returnUrl;
  72. }
  73. if (data.tid && data.region && data.loginLabel && data.inputData) {
  74. window.location.href = './fund_h5_api.html?token=' + token + "&time=" + Math.round(Date.now() / 1000);
  75. return;
  76. }
  77. $('body').show();
  78. getCityListAndRender();
  79. });
  80. getTokenInfo.fail(function(err) {
  81. $('body').show();
  82. showMsg('token校验失败:' + JSON.stringify(err.responseJSON.error));
  83. });
  84. }
  85. function getCityListAndRender() {
  86. $.ajax({
  87. type: "GET",
  88. url: hostUrl + '/city_list?type=fund&sort=true&token=' + token + '&time=' + Math.round(Date.now() / 1000),
  89. contentType: 'application/json;charset=utf-8',
  90. success: function(data) {
  91. return $(".city-list").CityPicker(data);
  92. },
  93. error: function(err) {
  94. return showMsg('获取城市列表失败:' + JSON.stringify(err.responseJSON.error));
  95. }
  96. });
  97. }
  98. //自动获取地址
  99. function getAddress () {
  100. var code = '';
  101. var map = new BMap.Map("allmap");
  102. var point = new BMap.Point(116.331398,39.897445);
  103. map.centerAndZoom(point,12);
  104. var geolocation = new BMap.Geolocation();
  105. // 开启SDK辅助定位
  106. geolocation.enableSDKLocation();
  107. geolocation.getCurrentPosition(function(r){
  108. if(this.getStatus() == BMAP_STATUS_SUCCESS){
  109. var mk = new BMap.Marker(r.point);
  110. map.addOverlay(mk);
  111. map.panTo(r.point);
  112. // alert('您的位置:'+r.point.lng+','+r.point.lat);
  113. // alert(r.address.city)
  114. addressName = r.address.city;
  115. localStorage.setItem("city",addressName);
  116. }
  117. else {
  118. // alert('failed'+this.getStatus());
  119. }
  120. //页面首次定位渲染
  121. if (addressName.indexOf("市") != -1) {
  122. addressName = addressName.replace('市', '');
  123. console.log(addressName)
  124. tips.find(function(item, index) {
  125. if(item.region == addressName){
  126. $('.current-city span').text(addressName).attr('data-code', item.regionCode);
  127. }
  128. })
  129. }
  130. });
  131. }