cities.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // lionfish_comshop/pages/position/cities.js
  2. var app = getApp(),
  3. cities = [];
  4. var QQMapWX = require("../../utils/qqmap-wx-jssdk.min.js")
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. cities: [],
  11. localCity: {}
  12. },
  13. /**
  14. * 生命周期函数--监听页面加载
  15. */
  16. onLoad: function(options) {
  17. var shopname = wx.getStorageSync('shopname');
  18. if (shopname) wx.setNavigationBarTitle({ title: shopname });
  19. // let city = wx.getStorageSync('city');
  20. // if (city && city.districtName){
  21. // console.log(city)
  22. // this.setData({ localCity: city })
  23. // } else {
  24. this.getMapKey();
  25. // }
  26. this.getData();
  27. },
  28. getMapKey: function(){
  29. var tx_map_key = wx.getStorageSync('tx_map_key');
  30. if (tx_map_key) {
  31. this.getCity();
  32. } else {
  33. new Promise(function (resolve, reject) {
  34. app.util.request({
  35. 'url': 'entry/wxapp/index',
  36. 'data': {
  37. controller: 'index.get_community_config'
  38. },
  39. dataType: 'json',
  40. success: function (res) {
  41. if (res.data.code == 0) {
  42. wx.setStorage({
  43. key: "shopname",
  44. data: res.data.shoname
  45. })
  46. if (res.data.shoname) {
  47. wx.setNavigationBarTitle({
  48. title: res.data.shoname
  49. });
  50. }
  51. wx.setStorage({
  52. key: "tx_map_key",
  53. data: res.data.tx_map_key
  54. })
  55. wx.setStorage({
  56. key: "shop_index_share_title",
  57. data: res.data.shop_index_share_title
  58. })
  59. resolve(res);
  60. }
  61. }
  62. })
  63. })
  64. .then(that.getCity())
  65. }
  66. },
  67. /**
  68. * 获取定位城市
  69. */
  70. getCity: function(){
  71. var token = wx.getStorageSync('token');
  72. var tx_map_key = wx.getStorageSync('tx_map_key');
  73. var that = this;
  74. var demo = new QQMapWX({
  75. key: tx_map_key
  76. });
  77. wx.getLocation({
  78. type: 'gcj02', //编码方式,
  79. success: function (res) {
  80. var latitude = res.latitude;
  81. var longitude = res.longitude;
  82. that.setData({
  83. latitude: res.latitude,
  84. longitude: res.longitude
  85. })
  86. wx.setStorage({
  87. key: "latitude",
  88. data: res.latitude
  89. })
  90. wx.setStorage({
  91. key: "longitude",
  92. data: res.longitude
  93. })
  94. demo.reverseGeocoder({
  95. //腾讯地图api 逆解析方法 首先设计经纬度
  96. location: {
  97. latitude: res.latitude,
  98. longitude: res.longitude
  99. }, //逆解析成功回调函数
  100. success: function (res) {
  101. that.setData({
  102. localCity: {
  103. districtName: res.result.address_component.city
  104. }
  105. })
  106. wx.setStorage({
  107. key: 'city',
  108. data: that.data.localCity,
  109. })
  110. }
  111. })
  112. }
  113. })
  114. },
  115. /**
  116. * 获取数据
  117. */
  118. getData: function() {
  119. var that = this;
  120. wx.showLoading({
  121. title: "加载中...",
  122. mask: true
  123. });
  124. var city = app.globalData.city;
  125. if (cities.length) {
  126. this.setData({
  127. cities: cities,
  128. localCity: city
  129. }), wx.hideLoading()
  130. } else {
  131. // 请求数据
  132. var token = wx.getStorageSync('token');
  133. var community = wx.getStorageSync('community');
  134. app.util.request({
  135. 'url': 'entry/wxapp/index',
  136. 'data': {
  137. controller: 'community.get_city_list',
  138. token: token
  139. },
  140. dataType: 'json',
  141. success: function (res) {
  142. if(res.data.code == 0){
  143. var cityArr = [], keyArr = [];
  144. res.data.data && res.data.data.forEach(function (item) {
  145. var firstLetter = item.firstLetter,
  146. pos = keyArr.indexOf(firstLetter);
  147. if (pos < 0) {
  148. keyArr.push(firstLetter);
  149. cityArr.push({
  150. key: firstLetter,
  151. list: []
  152. });
  153. pos = keyArr.length - 1
  154. }
  155. cityArr[pos].list.push({
  156. name: item.districtName,
  157. key: firstLetter,
  158. city: item
  159. });
  160. });
  161. cityArr.sort(function (x, y) {
  162. if (x.key > y.key) return 1
  163. else if (x.key < y.key) return -1
  164. else return 0
  165. })
  166. cities = cityArr;
  167. that.setData({
  168. cities: cities
  169. });
  170. }
  171. wx.hideLoading();
  172. }
  173. })
  174. }
  175. },
  176. /**
  177. * 选择城市
  178. */
  179. chooseCity: function(e) {
  180. var currentPages = getCurrentPages(), a = 1;
  181. currentPages[currentPages.length - 2].route.indexOf("/position/search") > -1 && (a = 2);
  182. var city = e.currentTarget.dataset.city;
  183. app.globalData.changeCity = city.city_id || 0;
  184. wx.setStorage({
  185. key: "city",
  186. data: { districtName: city.districtName }
  187. })
  188. wx.setStorage({
  189. key: "city_id",
  190. data: city.city_id
  191. })
  192. wx.navigateBack({
  193. delta: a
  194. });
  195. }
  196. })