123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- // pages/addAddress/addAddress.js
- const date = new Date()
- const years = []
- const months = []
- const days = []
- for (let i = 1990; i <= date.getFullYear(); i++) {
- years.push(i)
- }
- for (let i = 1; i <= 12; i++) {
- months.push(i)
- }
- for (let i = 1; i <= 31; i++) {
- days.push(i)
- }
- const getReq = require('./../../config.js').getReq
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- checked: true,
- areas: [],
- tree: [],
- cityIndex: 0,
- districtIndex: 0,
- region: [],
- years: years,
- year: date.getFullYear(),
- months: months,
- month: 2,
- days: days,
- day: 2,
- value: [0, 0, 0],
- oldval: [0, 0, 0],
- citys: [],
- districts: []
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- this.getDatas()
- },
- toggleChecked() {
- this.setData({
- checked: !this.data.checked
- })
- },
- bindRegionChange: function (e) {
- // console.log('picker发送选择改变,携带值为', e.detail.value)
- this.setData({
- region: e.detail.value
- })
- },
- bindChange: function (e) {
- console.log(e)
- const val = e.detail.value
- // if (districtIndex != this.data.oldval[1] && cityIndex != 0) {
- // districtIndex = 0
- // }
- if (this.data.oldval[0] != val[0]) {
- val[1] = 0
- val[2] = 0
- } else { //若省份column未做滑动,地级市做了滑动则定位区县第一位
- if (this.data.oldval[1] != val[1]) {
- val[2] = 0
- }
- }
- let cityIndex = val[0]
- let districtIndex = val[1]
- this.setData({
- oldval: val,
- citys: this.data.tree[val[0]].children,
- districts: this.data.tree[val[0]].children[val[1]].children
- })
- // this.setData({
- // oldval: val,
- // citys: this.data.tree[val[0]],
- // districts: this.data.tree[val[0].children[val[1]]
- // })
- },
- getDatas() {
- wx.showLoading({
- title: '加载中',
- })
- var self = this
- getReq({
- act: 'app_update',
- op: 'area',
- curpage: 1
- }, function (res) {
- wx.hideLoading()
- if (res.code == 200) {
- console.log(res.datas.areas)
- let tree = self.arrayToTree(res.datas.areas)
- console.log(tree)
- self.setData({
- areas: res.datas.areas,
- tree
- })
- }
- else {
- wx.showToast({
- icon: 'none',
- title: res.message,
- duration: 2000
- })
- }
- })
- },
- arrayToTree(array) {
- let result = [], hash = {}, children = 'children'
- array.forEach((item, index) => {
- hash[item.aid] = item
- })
- array.forEach((item, index) => {
- let pid = item.pid
- if (pid == 0) {
- result.push(item)
- }
- else {
- let hashvp = hash[item.pid]
- if (!hashvp[children]) {
- hashvp[children] = []
- }
- hashvp[children].push(item)
- }
- })
- return result
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {
- }
- })
|