index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.id
  28. // console.log(e.target.dataset.detail.name)
  29. // console.log(e)
  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('two/citylist', '', null, (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. subItems.push(subItem)
  67. })
  68. arrayItem.item = subItems
  69. // console.log(arrayItem)
  70. this.setData({
  71. list: [arrayItem]
  72. })
  73. break;
  74. default:
  75. let _array = [];
  76. array.forEach(item => {
  77. let _arrayItem = {}
  78. _arrayItem.title = item.letter
  79. let arrayItem = []
  80. try {
  81. item.city.forEach(ite=>{
  82. let subItem = {};
  83. subItem.key = item.letter
  84. subItem.name = ite.name
  85. subItem.id=ite.id
  86. arrayItem.push(subItem)
  87. })
  88. _arrayItem.item = arrayItem
  89. // console.log(_arrayItem)
  90. }
  91. catch(e) {
  92. // console.log(e)
  93. }
  94. _array.push(_arrayItem)
  95. })
  96. this.setData({
  97. list: [...this.data.list, ..._array]
  98. })
  99. break;
  100. }
  101. },
  102. //重新定位
  103. relocation() {
  104. }
  105. })