index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // pages/home/pages/location/index.js
  2. var ampFile = require("../../../utils/amap-wx.js");
  3. var app = getApp();
  4. var util = require("../../../utils/util.js")
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. city: [],//热门城市
  11. currentCity: '武汉市',//当前城市
  12. search: '',
  13. list:[],// city list
  14. },
  15. /**
  16. * 生命周期函数--监听页面加载
  17. */
  18. onLoad: function (options) {
  19. let _this = this;
  20. },
  21. onReady: function () {
  22. this.loadData();
  23. },
  24. //选择城市
  25. bindtap(e) {
  26. let cityName = e.currentTarget.dataset.detail.name ? e.currentTarget.dataset.detail.name : ''
  27. let cityId = e.currentTarget.dataset.cityid
  28. // console.log(e.target.dataset.detail.name)
  29. console.log(e.currentTarget.dataset)
  30. wx.reLaunch({
  31. url: `/pages/home/index/index?cityName=${cityName}&cityId=${cityId}`,
  32. // url: '/pages/home/index/index?id=' + e.currentTarget.dataset.id,
  33. })
  34. },
  35. loadData: function () {
  36. app._post_form('hotCityList', '', {}, (res)=> {
  37. // console.log(res)
  38. if(res && res.msg === 'success') {
  39. let citys = res.city, hotCitys = res.hot;
  40. // fommat city data
  41. this.handleFormatCityData({array: hotCitys, type: 'hot'})
  42. this.handleFormatCityData({ array: citys, type: 'normal' })
  43. }
  44. })
  45. },
  46. /**
  47. * format city data
  48. * @params array [array] 城市数组
  49. * @params type [string] 城市类型(热门 | 普通)
  50. */
  51. handleFormatCityData({array,type}) {
  52. if(!array.length) return
  53. switch(type) {
  54. case 'hot':
  55. let arrayItem = {
  56. title: '热门城市',
  57. type,
  58. item: []
  59. },
  60. subItems = [];
  61. array.forEach(item=>{
  62. let subItem = {};
  63. subItem.key = '热门'
  64. subItem.name = item.name
  65. subItem.id = item.id
  66. subItem.cityId = item.cityId
  67. subItems.push(subItem)
  68. })
  69. arrayItem.item = subItems
  70. // console.log(arrayItem)
  71. this.setData({
  72. list: [arrayItem]
  73. })
  74. break;
  75. default:
  76. let _array = [];
  77. array.forEach(item => {
  78. let _arrayItem = {}
  79. _arrayItem.title = item.letter
  80. let arrayItem = []
  81. try {
  82. item.city.forEach(ite=>{
  83. let subItem = {};
  84. subItem.key = item.letter
  85. subItem.name = ite.name
  86. subItem.id = ite.id
  87. subItem.cityId = ite.cityId
  88. arrayItem.push(subItem)
  89. })
  90. _arrayItem.item = arrayItem
  91. // console.log(_arrayItem)
  92. }
  93. catch(e) {
  94. // console.log(e)
  95. }
  96. _array.push(_arrayItem)
  97. })
  98. this.setData({
  99. list: [...this.data.list, ..._array]
  100. })
  101. break;
  102. }
  103. },
  104. //重新定位
  105. relocation() {
  106. }
  107. })