addAddress.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // pages/addAddress/addAddress.js
  2. const date = new Date()
  3. const years = []
  4. const months = []
  5. const days = []
  6. for (let i = 1990; i <= date.getFullYear(); i++) {
  7. years.push(i)
  8. }
  9. for (let i = 1; i <= 12; i++) {
  10. months.push(i)
  11. }
  12. for (let i = 1; i <= 31; i++) {
  13. days.push(i)
  14. }
  15. const getReq = require('./../../config.js').getReq
  16. Page({
  17. /**
  18. * 页面的初始数据
  19. */
  20. data: {
  21. checked: true,
  22. areas: [],
  23. tree: [],
  24. cityIndex: 0,
  25. districtIndex: 0,
  26. region: [],
  27. years: years,
  28. year: date.getFullYear(),
  29. months: months,
  30. month: 2,
  31. days: days,
  32. day: 2,
  33. value: [0, 0, 0],
  34. oldval: [0, 0, 0],
  35. citys: [],
  36. districts: []
  37. },
  38. /**
  39. * 生命周期函数--监听页面加载
  40. */
  41. onLoad: function (options) {
  42. this.getDatas()
  43. },
  44. toggleChecked() {
  45. this.setData({
  46. checked: !this.data.checked
  47. })
  48. },
  49. bindRegionChange: function (e) {
  50. // console.log('picker发送选择改变,携带值为', e.detail.value)
  51. this.setData({
  52. region: e.detail.value
  53. })
  54. },
  55. bindChange: function (e) {
  56. console.log(e)
  57. const val = e.detail.value
  58. // if (districtIndex != this.data.oldval[1] && cityIndex != 0) {
  59. // districtIndex = 0
  60. // }
  61. if (this.data.oldval[0] != val[0]) {
  62. val[1] = 0
  63. val[2] = 0
  64. } else { //若省份column未做滑动,地级市做了滑动则定位区县第一位
  65. if (this.data.oldval[1] != val[1]) {
  66. val[2] = 0
  67. }
  68. }
  69. let cityIndex = val[0]
  70. let districtIndex = val[1]
  71. this.setData({
  72. oldval: val,
  73. citys: this.data.tree[val[0]].children,
  74. districts: this.data.tree[val[0]].children[val[1]].children
  75. })
  76. // this.setData({
  77. // oldval: val,
  78. // citys: this.data.tree[val[0]],
  79. // districts: this.data.tree[val[0].children[val[1]]
  80. // })
  81. },
  82. getDatas() {
  83. wx.showLoading({
  84. title: '加载中',
  85. })
  86. var self = this
  87. getReq({
  88. act: 'app_update',
  89. op: 'area',
  90. curpage: 1
  91. }, function (res) {
  92. wx.hideLoading()
  93. if (res.code == 200) {
  94. console.log(res.datas.areas)
  95. let tree = self.arrayToTree(res.datas.areas)
  96. console.log(tree)
  97. self.setData({
  98. areas: res.datas.areas,
  99. tree
  100. })
  101. }
  102. else {
  103. wx.showToast({
  104. icon: 'none',
  105. title: res.message,
  106. duration: 2000
  107. })
  108. }
  109. })
  110. },
  111. arrayToTree(array) {
  112. let result = [], hash = {}, children = 'children'
  113. array.forEach((item, index) => {
  114. hash[item.aid] = item
  115. })
  116. array.forEach((item, index) => {
  117. let pid = item.pid
  118. if (pid == 0) {
  119. result.push(item)
  120. }
  121. else {
  122. let hashvp = hash[item.pid]
  123. if (!hashvp[children]) {
  124. hashvp[children] = []
  125. }
  126. hashvp[children].push(item)
  127. }
  128. })
  129. return result
  130. },
  131. /**
  132. * 生命周期函数--监听页面初次渲染完成
  133. */
  134. onReady: function () {
  135. },
  136. /**
  137. * 生命周期函数--监听页面显示
  138. */
  139. onShow: function () {
  140. },
  141. /**
  142. * 生命周期函数--监听页面隐藏
  143. */
  144. onHide: function () {
  145. },
  146. /**
  147. * 生命周期函数--监听页面卸载
  148. */
  149. onUnload: function () {
  150. },
  151. /**
  152. * 页面相关事件处理函数--监听用户下拉动作
  153. */
  154. onPullDownRefresh: function () {
  155. },
  156. /**
  157. * 页面上拉触底事件的处理函数
  158. */
  159. onReachBottom: function () {
  160. },
  161. /**
  162. * 用户点击右上角分享
  163. */
  164. onShareAppMessage: function () {
  165. }
  166. })