switchcity.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. const city = require('../../utils/util.js');
  2. const appInstance = getApp();
  3. Page({
  4. data: {
  5. searchLetter: [],
  6. showLetter: "",
  7. winHeight: 0,
  8. cityList: [],
  9. isShowLetter: false,
  10. scrollTop: 0,//置顶高度
  11. scrollTopId: '',//置顶id
  12. city: "定位中",
  13. currentCityCode: '',
  14. hotcityList: [{ cityCode: 110000, city: '北京市' }, { cityCode: 310000, city: '上海市' }, { cityCode: 440100, city: '广州市' }, { cityCode: 440300, city: '深圳市' }, { cityCode: 330100, city: '杭州市' }, { cityCode: 320100, city: '南京市' }, { cityCode: 420100, city: '武汉市' }, { cityCode: 120000, city: '天津市' }, { cityCode: 610100, city: '西安市' },],
  15. commonCityList: [{ cityCode: 110000, city: '北京市' }, { cityCode: 310000, city: '上海市' }],
  16. countyList: [{ cityCode: 110000, county: 'A区' }, { cityCode: 310000, county: 'B区' }, { cityCode: 440100, county: 'C区' }, { cityCode: 440300, county: 'D区' }, { cityCode: 330100, county: 'E县' }, { cityCode: 320100, county: 'F县' }, { cityCode: 420100, county: 'G县' }],
  17. inputName: '',
  18. completeList: [],
  19. county: '',
  20. condition: false,
  21. },
  22. //选择城市
  23. bindCity: function (e) {
  24. this.setData({
  25. condition: true, //选择区县修改为true
  26. city: e.currentTarget.dataset.city,
  27. currentCityCode: e.currentTarget.dataset.code,
  28. scrollTop: 0,
  29. completeList: [],
  30. })
  31. this.selectCounty() //获取当前城市的区名称
  32. appInstance.globalData.defaultCity = this.data.city
  33. appInstance.globalData.defaultCounty = ''
  34. },
  35. bindCounty: function (e) //设置当前区域
  36. {
  37. this.setData({ county: e.currentTarget.dataset.city })
  38. appInstance.globalData.defaultCounty = this.data.county
  39. wx.switchTab({
  40. url: '../index/index'
  41. })
  42. },
  43. onLoad: function (options) {
  44. const searchLetter = city.searchLetter;
  45. const cityList = city.cityList();
  46. const sysInfo = wx.getSystemInfoSync();
  47. const winHeight = sysInfo.windowHeight;
  48. const itemH = winHeight / searchLetter.length;
  49. let tempArr = [];
  50. searchLetter.map(
  51. (item, index) => {
  52. let temp = {};
  53. temp.name = item;
  54. temp.tHeight = index * itemH;
  55. temp.bHeight = (index + 1) * itemH;
  56. tempArr.push(temp)
  57. }
  58. );
  59. this.setData({
  60. winHeight: winHeight,
  61. itemH: itemH,
  62. searchLetter: tempArr,
  63. cityList: cityList
  64. });
  65. this.getLocation();
  66. },
  67. //定位当前城市的函数
  68. getLocation: function () {
  69. this.setData({
  70. county: ''
  71. })
  72. const that = this;
  73. wx.getLocation({
  74. type: 'wgs84',
  75. success: function (res) {
  76. //当前的经度和纬度
  77. let latitude = res.latitude
  78. let longitude = res.longitude
  79. wx.request({
  80. url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${appInstance.globalData.tencentMapKey}`,
  81. success: res => {
  82. that.setData({
  83. city: res.data.result.ad_info.city,
  84. currentCityCode: res.data.result.ad_info.adcode,
  85. county: res.data.result.ad_info.district
  86. })
  87. }
  88. })
  89. }
  90. })
  91. },
  92. //获取当前选择城市的区县
  93. selectCounty: function () {
  94. let code = this.data.currentCityCode
  95. const that = this;
  96. wx.request({
  97. url: `https://apis.map.qq.com/ws/district/v1/getchildren?&id=${code}&key=${appInstance.globalData.tencentMapKey}`,
  98. success: function (res) {
  99. that.setData({
  100. countyList: res.data.result[0],
  101. })
  102. },
  103. fail: function () {
  104. console.log("请求区县失败,请重试");
  105. }
  106. })
  107. },
  108. //重新定位城市
  109. reGetLocation: function () {
  110. appInstance.globalData.defaultCity = this.data.city
  111. appInstance.globalData.defaultCounty = this.data.county
  112. //返回首页
  113. wx.switchTab({
  114. url: '../index/index'
  115. })
  116. },
  117. //点击热门城市回到顶部
  118. hotCity: function () {
  119. this.setData({
  120. scrollTop: 0,
  121. })
  122. },
  123. bindBlur: function (e) {
  124. this.setData({
  125. inputName: ''
  126. })
  127. },
  128. //获取查询框输入,并执行自动查询方法
  129. bindKeyInput: function (e) {
  130. this.setData({
  131. inputName: e.detail.value
  132. })
  133. this.auto()
  134. },
  135. auto: function () {
  136. let inputSd = this.data.inputName.trim()
  137. let sd = inputSd.toLowerCase()
  138. let num = sd.length
  139. const cityList = city.cityObjs
  140. let finalCityList = []
  141. let temp = cityList.filter(
  142. item => {
  143. let text = item.short.slice(0, num).toLowerCase()
  144. return (text && text == sd)
  145. }
  146. )
  147. //在城市数据中,添加简拼到“shorter”属性,就可以实现简拼搜索
  148. let tempShorter = cityList.filter(
  149. itemShorter => {
  150. if (itemShorter.shorter) {
  151. let textShorter = itemShorter.shorter.slice(0, num).toLowerCase()
  152. return (textShorter && textShorter == sd)
  153. }
  154. return
  155. }
  156. )
  157. let tempChinese = cityList.filter(
  158. itemChinese => {
  159. let textChinese = itemChinese.city.slice(0, num)
  160. return (textChinese && textChinese == sd)
  161. }
  162. )
  163. if (temp[0]) {
  164. temp.map(
  165. item => {
  166. let testObj = {};
  167. testObj.city = item.city
  168. testObj.code = item.code
  169. finalCityList.push(testObj)
  170. }
  171. )
  172. this.setData({
  173. completeList: finalCityList,
  174. })
  175. } else if (tempShorter[0]) {
  176. tempShorter.map(
  177. item => {
  178. let testObj = {};
  179. testObj.city = item.city
  180. testObj.code = item.code
  181. finalCityList.push(testObj)
  182. }
  183. );
  184. this.setData({
  185. completeList: finalCityList,
  186. })
  187. } else if (tempChinese[0]) {
  188. tempChinese.map(
  189. item => {
  190. let testObj = {};
  191. testObj.city = item.city
  192. testObj.code = item.code
  193. finalCityList.push(testObj)
  194. })
  195. this.setData({
  196. completeList: finalCityList,
  197. })
  198. } else {
  199. return
  200. }
  201. },
  202. /**
  203. * 用户点击右上角分享
  204. */
  205. onShareAppMessage: function () {
  206. return {
  207. title: '很赞的全国城市切换器~',
  208. desc: '分享个小程序,希望你喜欢☺️~',
  209. success: function (res) {
  210. // 转发成功
  211. wx.showToast({
  212. title: "分享成功",
  213. duration: 1000,
  214. icon: "success"
  215. })
  216. }
  217. }
  218. }
  219. })