1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
| var util = require('../../../utils/util.js'); var api = require('../../../config/api.js'); var area = require('../../../config/area.js');
var app = getApp(); Page({ data: { address: { id: 0, provinceId: 0, cityId: 0, areaId: 0, address: '', name: '', mobile: '', isDefault: 0, provinceName: '', cityName: '', areaName: '' }, addressId: 0, openSelectRegion: false, regionType: 1, selectRegionDone: false, szxqList: [], szxq: { id: "", name: "请选择小区" }, szdsList: [], szds: { id: "", name: "" }, fanghao: "", animationAddressMenu: {}, addressMenuIsShow: false, value: [0, 0, 0], provinces: [], citys: [], areas: [], areaInfo: '', areaJson: {} }, bindinputMobile(event) { let address = this.data.address; address.mobile = event.detail.value; this.setData({ address: address }); }, bindinputName(event) { let address = this.data.address; address.name = event.detail.value; this.setData({ address: address }); }, bindinputAddress(event) { let address = this.data.address; address.address = event.detail.value; this.setData({ address: address }); }, bindIsDefault() { let address = this.data.address; address.isDefault = !address.isDefault; this.setData({ address: address }); }, getAddressDetail() { let that = this; util.request(api.AddressDetail, { id: that.data.addressId }).then(function(res) { if (res.errno === 0) { if (res.data) { that.setData({ address: res.data }); } } }); },
wxChooseAddress() { let that = this; let address = this.data.address; // 用户已经同意小程序使用地址功能 wx.chooseAddress({ success: function(res) { address.provinceId = 99999; address.cityId = 88888; address.areaId = 77777; address.name = res.userName; address.mobile = res.telNumber; address.provinceName = res.provinceName; address.cityName = res.cityName; address.areaName = res.countyName; address.address = res.provinceName + res.cityName + res.countyName + res.detailInfo;
that.setData({ address: address, }); } }); },
wxAddress() { let that = this; // 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.address" 这个 scope wx.getSetting({ success(res) { if (!res.authSetting['scope.address']) { wx.authorize({ scope: 'scope.address', success() { that.wxChooseAddress(); } }) } else { that.wxChooseAddress(); } } }) },
onLoad: function(options) { let that = this; // 页面初始化 options为页面跳转所带来的参数 console.log(options); if (options.id && options.id != 0) { this.setData({ addressId: options.id }); this.getAddressDetail(); } else { that.wxAddress(); } },
onReady: function() {
},
cancelAddress() { wx.navigateBack(); }, saveAddress() { console.log(this.data.address); let address = this.data.address;
if (address.name == '') { util.showErrorToast('请输入姓名');
return false; }
if (address.mobile == '') { util.showErrorToast('请输入手机号码'); return false; }
if (address.areaId == 0) { util.showErrorToast('请输入省市区'); return false; }
if (address.address == '') { util.showErrorToast('请输入详细地址'); return false; }
if (!check.isValidPhone(address.mobile)) { util.showErrorToast('手机号不正确'); return false; } }, onShow: function() {
// 获取所在栋数 var animation = wx.createAnimation({ duration: 500, timingFunction: 'linear', }) this.animation = animation
util.request("https://www.xaibox.com/czbb/interface/dataInfo.php", JSON.stringify({ "param_key": { "info_mode": "getcity_jd" }, "secret_key": "047709aaa7df22205d818bf4c1707458" }), "post").then(response => { console.log(response)
that.setData({ szxqList: response.data.rs_data }) that.setData({ szxq: response.data.data[0] }) that.setData({ szds: response.data.data[0]['buildingList']['0'] }) that.setData({ szdsList: response.data.data[0]['buildingList'] })
}) // 获取所在地区
that.setData({ provinces: areajs, citys: areajs[0].children, areas: areajs[0].children ? areajs[0].children[0].children : [], areaJson: { provinces: { id: 40, name: "广东省" }, citys: { id: 4006, name: "河源市" }, areas: { id: 400602, name: "源城区" } } })
var areas = that.data.areaJson.areas.name == null ? "" : that.data.areaJson.areas.name var areaInfo = that.data.areaJson.provinces.name + '·' + that.data.areaJson.citys.name + '·' + areas
that.setData({ areaInfo: areaInfo, }) },
// 点击所在地区弹出选择框 select: function(e) { // 如果已经显示,不在执行显示动画 if (this.data.addressMenuIsShow) { return false } else { // 执行显示动画 this.startAddressAnimation(true) } },
// 处理省市县联动逻辑 cityChange: function(e) { // console.log(this.data.provinces) var value = e.detail.value var provinces = this.data.provinces var citys = this.data.citys var areas = this.data.areas var provinceNum = value[0] var cityNum = value[1] var countyNum = value[2] var that = this; // console.log(provinces) // 如果省份选择项和之前不一样,表示滑动了省份,此时市默认是省的第一组数据, if (this.data.value[0] != provinceNum) { var id = provinces[provinceNum].id // console.log(citys[cityNum]) this.setData({ value: [provinceNum, 0, 0], citys: provinces[provinceNum].children, areas: provinces[provinceNum].children ? provinces[provinceNum].children[0].children : [], areaJson: { provinces: { id: provinces[provinceNum].code, name: provinces[provinceNum].area }, citys: { id: provinces[provinceNum].children[0].code, name: provinces[provinceNum].children[0].area }, areas: { id: citys[cityNum].children.length > 0 ? citys[cityNum].children[0].code : null, name: citys[cityNum].children.length > 0 ? citys[cityNum].children[0].area : null } } })
} else if (this.data.value[1] != cityNum) { // 滑动选择了第二项数据,即市,此时区显示省市对应的第一组数据 var id = citys[cityNum].id
this.setData({ value: [provinceNum, cityNum, 0], areas: citys[cityNum].children, areaJson: { provinces: { id: provinces[provinceNum].code, name: provinces[provinceNum].area }, citys: { id: citys[cityNum].code, name: citys[cityNum].area }, areas: { id: citys[cityNum].children.length > 0 ? citys[cityNum].children[0].code : null, name: citys[cityNum].children.length > 0 ? citys[cityNum].children[0].area : null } } }) } else { // 滑动选择了区
this.setData({ value: [provinceNum, cityNum, countyNum], areaJson: { provinces: { id: provinces[provinceNum].code, name: provinces[provinceNum].area }, citys: { id: citys[cityNum].code, name: citys[cityNum].area }, areas: { id: areas[countyNum].code, name: areas[countyNum].area } } }) // console.log(that.data.areaJson) } },
// 执行动画 startAddressAnimation: function(isShow) { if (isShow) { // vh是用来表示尺寸的单位,高度全屏是100vh this.animation.translateY(0 + 'vh').step() } else { this.animation.translateY(40 + 'vh').step() } this.setData({ animationAddressMenu: this.animation.export(), addressMenuIsShow: isShow, }) }, // 点击地区选择取消按钮 cityCancel: function(e) { this.startAddressAnimation(false) }, // 点击地区选择确定按钮 citySure: function(e) { var that = this var city = that.data.city var value = that.data.value this.startAddressAnimation(false) // console.log(that.data.areaJson) var areas = that.data.areaJson.areas.name == null ? "" : that.data.areaJson.areas.name // 将选择的城市信息显示到输入框 var areaInfo = that.data.areaJson.provinces.name + '·' + that.data.areaJson.citys.name + '·' + areas
that.setData({ areaInfo: areaInfo, }) }, onHide: function() { // 页面隐藏
}, onUnload: function() { // 页面关闭
} });
|