cities.js 5.2 KB

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