index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. originalList: [], // 原始数据
  15. },
  16. /**
  17. * 生命周期函数--监听页面加载
  18. */
  19. onLoad: function (options) {
  20. let _this = this;
  21. },
  22. onReady: function () {
  23. this.loadData();
  24. },
  25. // 基础搜索功能
  26. searchMt(e) {
  27. // this.search = e.detail.value;
  28. // console.log(e.detail.value);
  29. if (e.detail.value) {
  30. this.setData({
  31. search: e.detail.value
  32. })
  33. this.handleList();
  34. } else {
  35. this.setData({
  36. search: '',
  37. list: this.data.originalList
  38. });
  39. }
  40. // console.log(this.data.originalList);
  41. },
  42. handleList() {
  43. let list = [];
  44. this.data.originalList.forEach(item => {
  45. // if (item.name.indexOf(this.data.search) > -1) {
  46. // list.push(item);
  47. // }
  48. if (item.title == '热门城市') {
  49. list.push(item);
  50. } else {
  51. let itemList = [];
  52. item.item.forEach(v => {
  53. if (v.name.indexOf(this.data.search) > -1) {
  54. itemList.push(v);
  55. }
  56. });
  57. if (itemList.length) {
  58. list.push({
  59. title: item.title,
  60. item: itemList
  61. });
  62. }
  63. }
  64. });
  65. // console.log(list);
  66. this.setData({
  67. list
  68. });
  69. },
  70. //选择城市
  71. bindtap(e) {
  72. let cityName = e.currentTarget.dataset.detail.name ? e.currentTarget.dataset.detail.name : ''
  73. let cityId = e.currentTarget.dataset.cityid
  74. // console.log(e.target.dataset.detail.name)
  75. console.log(e.currentTarget.dataset)
  76. wx.reLaunch({
  77. url: `/pages/home/index/index?cityName=${cityName}&cityId=${cityId}`,
  78. // url: '/pages/home/index/index?id=' + e.currentTarget.dataset.id,
  79. })
  80. },
  81. loadData: function () {
  82. app._post_form('hotCityList', '', {}, (res)=> {
  83. // console.log(res)
  84. if(res && res.msg === 'success') {
  85. let citys = res.city, hotCitys = res.hot;
  86. // fommat city data
  87. this.handleFormatCityData({array: hotCitys, type: 'hot'})
  88. this.handleFormatCityData({ array: citys, type: 'normal' })
  89. }
  90. })
  91. },
  92. /**
  93. * format city data
  94. * @params array [array] 城市数组
  95. * @params type [string] 城市类型(热门 | 普通)
  96. */
  97. handleFormatCityData({array,type}) {
  98. if(!array.length) return
  99. switch(type) {
  100. case 'hot':
  101. let arrayItem = {
  102. title: '热门城市',
  103. type,
  104. item: []
  105. },
  106. subItems = [];
  107. array.forEach(item=>{
  108. let subItem = {};
  109. subItem.key = '热门'
  110. subItem.name = item.name
  111. subItem.id = item.id
  112. subItem.cityId = item.cityId
  113. subItems.push(subItem)
  114. })
  115. arrayItem.item = subItems
  116. // console.log(arrayItem)
  117. this.setData({
  118. list: [arrayItem]
  119. })
  120. break;
  121. default:
  122. let _array = [];
  123. array.forEach(item => {
  124. let _arrayItem = {}
  125. _arrayItem.title = item.letter
  126. let arrayItem = []
  127. try {
  128. item.city.forEach(ite=>{
  129. let subItem = {};
  130. subItem.key = item.letter
  131. subItem.name = ite.name
  132. subItem.id = ite.id
  133. subItem.cityId = ite.cityId
  134. arrayItem.push(subItem)
  135. })
  136. _arrayItem.item = arrayItem
  137. // console.log(_arrayItem)
  138. }
  139. catch(e) {
  140. // console.log(e)
  141. }
  142. _array.push(_arrayItem)
  143. })
  144. let list = [...this.data.list, ..._array];
  145. this.setData({
  146. list: list,
  147. originalList: list,
  148. })
  149. break;
  150. }
  151. },
  152. //重新定位
  153. relocation() {
  154. }
  155. })