switchcity.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. const city = require('../../utils/city.js');
  2. const cityObjs = require('../../utils/city.js');
  3. const config = require('../../utils/config.js');
  4. const appInstance = getApp();
  5. const app = getApp()
  6. Page({
  7. data: {
  8. searchLetter: [],
  9. showLetter: "",
  10. winHeight: 0,
  11. cityList: [],
  12. isShowLetter: false,
  13. scrollTop: 0,//置顶高度
  14. scrollTopId: '',//置顶id
  15. city: "定位中",
  16. currentCityCode: '',
  17. hotcityList: [{ cityCode: 110000, city: '北京市' },
  18. { cityCode: 310000, city: '上海市' },
  19. { cityCode: 120000, city: '天津市' },
  20. { cityCode: 320500, city: '苏州市' },
  21. { cityCode: 330100, city: '杭州市' },
  22. { cityCode: 320100, city: '南京市' },
  23. { cityCode: 420100, city: '武汉市' },
  24. { cityCode: 440100, city: '广州市' },
  25. { cityCode: 440300, city: '深圳市' },],
  26. commonCityList: [{ cityCode: 110000, city: '北京市' },
  27. { cityCode: 310000, city: '上海市' }],
  28. countyList: [{ cityCode: 110000, county: 'A区' },
  29. { cityCode: 310000, county: 'B区' },
  30. { cityCode: 440100, county: 'C区' },
  31. { cityCode: 440300, county: 'D区' },
  32. { cityCode: 330100, county: 'E县' },
  33. { cityCode: 320100, county: 'F县' },
  34. { cityCode: 420100, county: 'G县' }],
  35. inputName: '',
  36. completeList: [],
  37. county: '',
  38. condition: false,
  39. },
  40. onLoad: function () {
  41. // 生命周期函数--监听页面加载
  42. const searchLetter = city.searchLetter;
  43. const cityList = city.cityList();
  44. const sysInfo = wx.getSystemInfoSync();
  45. const winHeight = sysInfo.windowHeight;
  46. const itemH = winHeight / searchLetter.length;
  47. let tempArr = [];
  48. searchLetter.map(
  49. (item, index) => {
  50. let temp = {};
  51. temp.name = item;
  52. temp.tHeight = index * itemH;
  53. temp.bHeight = (index + 1) * itemH;
  54. tempArr.push(temp)
  55. }
  56. );
  57. this.setData({
  58. winHeight: winHeight,
  59. itemH: itemH,
  60. searchLetter: tempArr,
  61. cityList: cityList
  62. });
  63. this.getLocation();
  64. let _this = this;
  65. _this.setData({
  66. navH: app.globalData.navHeight
  67. })
  68. },
  69. onReady: function () {
  70. // 生命周期函数--监听页面初次渲染完成
  71. },
  72. onShow: function () {
  73. // 生命周期函数--监听页面显示
  74. },
  75. onHide: function () {
  76. // 生命周期函数--监听页面隐藏
  77. },
  78. onUnload: function () {
  79. // 生命周期函数--监听页面卸载
  80. },
  81. onPullDownRefresh: function () {
  82. // 页面相关事件处理函数--监听用户下拉动作
  83. },
  84. onReachBottom: function () {
  85. // 页面上拉触底事件的处理函数
  86. },
  87. clickLetter: function (e) {
  88. const showLetter = e.currentTarget.dataset.letter;
  89. this.setData({
  90. toastShowLetter: showLetter,
  91. isShowLetter: true,
  92. scrollTopId: showLetter,
  93. })
  94. const that = this;
  95. setTimeout(function () {
  96. that.setData({
  97. isShowLetter: false
  98. })
  99. }, 500)
  100. },
  101. reGetLocation: function () {
  102. appInstance.globalData.defaultCity = this.data.city
  103. appInstance.globalData.defaultCounty = this.data.county
  104. //返回首页
  105. wx.switchTab({
  106. url: '../index/index'
  107. })
  108. },
  109. //选择城市
  110. bindCity: function (e) {
  111. this.setData({
  112. condition: true,
  113. city: e.currentTarget.dataset.city,
  114. currentCityCode: e.currentTarget.dataset.code,
  115. scrollTop: 0,
  116. completeList: [],
  117. })
  118. this.selectCounty()
  119. appInstance.globalData.defaultCity = this.data.city
  120. appInstance.globalData.defaultCounty = ''
  121. },
  122. bindCounty: function (e) {
  123. this.setData({ county: e.currentTarget.dataset.city })
  124. appInstance.globalData.defaultCounty = this.data.county
  125. var pages = getCurrentPages();
  126. var currPage = pages[pages.length - 1];
  127. var prevPage = pages[pages.length - 2];
  128. prevPage.setData({
  129. city: this.data.city,
  130. county: e.currentTarget.dataset.city
  131. })
  132. wx.navigateBack({
  133. delta: 1
  134. })
  135. },
  136. //点击热门城市回到顶部
  137. hotCity: function () {
  138. this.setData({
  139. scrollTop: 0,
  140. })
  141. },
  142. bindScroll: function (e) {
  143. },
  144. selectCounty: function () {
  145. var code = "";
  146. var codet = String(this.data.currentCityCode).substring(0,3)
  147. if (codet == '110' || codet == '120' || codet == '500' || codet == '310') {
  148. code = codet + "000"
  149. } else {
  150. code = String(this.data.currentCityCode)
  151. }
  152. const that = this;
  153. wx.request({
  154. url: `https://apis.map.qq.com/ws/district/v1/getchildren?&id=${code}&key=${config.key}`,
  155. success: function (res) {
  156. that.setData({
  157. countyList: res.data.result[0],
  158. })
  159. },
  160. fail() {
  161. app.timeOut()
  162. }
  163. })
  164. },
  165. getLocation: function () {
  166. this.setData({
  167. county: ''
  168. })
  169. const that = this;
  170. wx.getLocation({
  171. type: 'wgs84',
  172. success: function (res) {
  173. let latitude = res.latitude
  174. let longitude = res.longitude
  175. wx.request({
  176. url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${config.key}`,
  177. success: res => {
  178. that.setData({
  179. city: res.data.result.ad_info.city,
  180. currentCityCode: res.data.result.ad_info.adcode,
  181. county: res.data.result.ad_info.district
  182. })
  183. that.selectCounty();
  184. },
  185. fail() {
  186. app.timeOut()
  187. }
  188. })
  189. },
  190. fail() {
  191. app.timeOut()
  192. }
  193. })
  194. },
  195. bindBlur: function (e) {
  196. this.setData({
  197. inputName: ''
  198. })
  199. },
  200. bindKeyInput: function (e) {
  201. this.setData({
  202. inputName: e.detail.value
  203. })
  204. this.auto()
  205. },
  206. auto: function () {
  207. let inputSd = this.data.inputName.trim()
  208. let sd = inputSd.toLowerCase()
  209. let num = sd.length
  210. const cityList = cityObjs.cityObjs
  211. let finalCityList = []
  212. let temp = cityList.filter(
  213. item => {
  214. let text = item.short.slice(0, num).toLowerCase()
  215. return (text && text == sd)
  216. }
  217. )
  218. //在城市数据中,添加简拼到“shorter”属性,就可以实现简拼搜索
  219. let tempShorter = cityList.filter(
  220. itemShorter => {
  221. if (itemShorter.shorter) {
  222. let textShorter = itemShorter.shorter.slice(0, num).toLowerCase()
  223. return (textShorter && textShorter == sd)
  224. }
  225. return
  226. }
  227. )
  228. let tempChinese = cityList.filter(
  229. itemChinese => {
  230. let textChinese = itemChinese.city.slice(0, num)
  231. return (textChinese && textChinese == sd)
  232. }
  233. )
  234. if (temp[0]) {
  235. temp.map(
  236. item => {
  237. let testObj = {};
  238. testObj.city = item.city
  239. testObj.code = item.code
  240. finalCityList.push(testObj)
  241. }
  242. )
  243. this.setData({
  244. completeList: finalCityList,
  245. })
  246. } else if (tempShorter[0]) {
  247. tempShorter.map(
  248. item => {
  249. let testObj = {};
  250. testObj.city = item.city
  251. testObj.code = item.code
  252. finalCityList.push(testObj)
  253. }
  254. );
  255. this.setData({
  256. completeList: finalCityList,
  257. })
  258. } else if (tempChinese[0]) {
  259. tempChinese.map(
  260. item => {
  261. let testObj = {};
  262. testObj.city = item.city
  263. testObj.code = item.code
  264. finalCityList.push(testObj)
  265. })
  266. this.setData({
  267. completeList: finalCityList,
  268. })
  269. } else {
  270. return
  271. }
  272. },
  273. })