var util = require('../../../utils/util.js'); var api = require('../../../config/api.js'); var check = require('../../../utils/check.js'); var area = require('../../../utils/area.js'); var map = require('../../../utils/map.js'); const chooseLocation = requirePlugin('chooseLocation'); var app = getApp(); Page({ data: { address: { id: 0, areaCode: 0, address: '', name: '', tel: '', isDefault: 0, province: '', city: '', county: '', lat: '', lng: '', addressDetail: '', detail: '', addressName: '' }, addressId: 0, openSelectRegion: false, selectRegionList: [{ code: 0, name: '省份' }, { code: 0, name: '城市' }, { code: 0, name: '区县' } ], regionType: 1, regionList: [], selectRegionDone: false }, bindinputMobile(event) { let address = this.data.address; address.tel = 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.detail = event.detail.value.replace(/[,,]/g, ''); this.setData({ address: address }); // return value.replace(/[,,]/g,''); }, 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 }); let addressDetailList = res.data.addressDetail.split(','); let address = that.data.address; address.address = addressDetailList[0]; address.detail = (addressDetailList.length > 1 ? addressDetailList[1] : ''); that.setData({ address: address }); } } }); }, setRegionDoneStatus() { let that = this; let doneStatus = that.data.selectRegionList.every(item => { return item.code != 0; }); that.setData({ selectRegionDone: doneStatus }) }, onLoad: function (options) { // 页面初始化 options为页面跳转所带来的参数 //console.log(options) if (options.id && options.id != 0) { this.setData({ addressId: options.id }); this.getAddressDetail(); } //初始化area //area.init(); }, cancelAddress() { wx.navigateBack(); }, saveAddress() { console.log(this.data.address) let address = this.data.address; if (address.name == '') { util.showErrorToast('请输入姓名'); return false; } if (address.tel == '') { util.showErrorToast('请输入手机号码'); return false; } if (address.areaCode == 0) { util.showErrorToast('请输入省市区'); return false; } if (address.address == '') { util.showErrorToast('请选择地址'); return false; } if (!check.isValidPhone(address.tel)) { util.showErrorToast('手机号不正确'); return false; } let that = this; let addressString = (address.detail == '' ? address.address : address.address + ',' + address.detail); util.request(api.AddressSave, { shopId: app.globalData.shopId ? app.globalData.shopId : '', id: address.id, name: address.name, tel: address.tel, province: address.province, city: address.city, county: address.county, areaCode: address.areaCode, addressDetail: addressString, addressName: address.addressName, isDefault: address.isDefault, lat: Number(address.lat).toFixed(5), lng: Number(address.lng).toFixed(5), }, 'POST').then(function (res) { if (res.errno === 0) { //返回之前,先取出上一页对象,并设置addressId var pages = getCurrentPages(); var prevPage = pages[pages.length - 2]; console.log(prevPage); if (prevPage.route == "pages/index/index") { var city = address.city.indexOf("市") < 0 ? address.province : address.city; var addresInfo = { city: city, lng: res.data.lng, lat: res.data.lat, shopId: res.data.shopId, address: res.data.addressDetail } wx.setStorageSync('addressInfo', address); } else if (prevPage.route == "pages/ucenter/serviceDetail/serviceDetail") { wx.setStorageSync('addressInfo', addresInfo); } else if (prevPage.route == "pages/checkout/checkout") { prevPage.setData({ addressId: res.data.id }) try { wx.setStorageSync('addressId', res.data); } catch (e) { } console.log("set address"); } wx.navigateBack(); } else { wx.showModal({ title: '提醒', showCancel: false, content: res.errmsg }) } }); }, deleteAddress(event) { console.log(event.target) let that = this; wx.showModal({ title: '', content: '确定要删除地址?', success: function (res) { if (res.confirm) { let addressId = event.target.dataset.addressId; util.request(api.AddressDelete, { id: addressId }, 'POST').then(function (res) { if (res.errno === 0) { wx.navigateBack({ delta: 1, success: function () {} }) // that.getAddressList(); // wx.removeStorage({ // key: 'addressId', // success: function(res) {}, // }) } }); console.log('用户点击确定') } } }) return false; }, goMapOld() { // const key = "JA2BZ-XGOEU-UNZVD-46G64-S6MYJ-6BBF4"; //使用在腾讯位置服务申请的key const key = '6TRBZ-XXC6F-BWWJB-N75ME-BQNOF-YKFQY'; const referer = '鲸致生活LifePlus'; //调用插件的app的名称 const category = '小区,生活服务,娱乐休闲'; if (this.data.address.lat) { const location = JSON.stringify({ latitude: Number(this.data.address.lat), longitude: Number(this.data.address.lng) }); wx.navigateTo({ url: 'plugin://chooseLocation/index?key=' + key + '&referer=' + referer + '&category=' + category + '&location=' + location }); } else wx.navigateTo({ url: 'plugin://chooseLocation/index?key=' + key + '&referer=' + referer + '&category=' + category }); }, goMap() { app.globalData.mapSelectFinish = false; if (this.data.address.lng && this.data.address.lat) { let city = (this.data.address.city == '市辖区' ? this.data.address.province : this.data.address.city) wx.navigateTo({ url: '/pages/map/mapContent/mapContent?lng=' + this.data.address.lng + '&lat=' + this.data.address.lat + '&addressname=' + this.data.address.address + '&city=' + city, }); } else { wx.navigateTo({ url: '/pages/map/mapContent/mapContent', }); } }, // onShow: function () { // // 页面显示 // const location = chooseLocation.getLocation(); // console.log(location); // if (location != null) { // let address = this.data.address; // address.province = location.province ? location.province : location.city; // address.city = location.city; // address.county = location.district; // address.addressName = location.name; // address.address = location.address; // address.lat = location.latitude; // address.lng = location.longitude; // this.setData({ // address: address, // }); // this.getAreaCode() // } // }, onShow: function () { let that = this; if (app.globalData.mapSelectFinish) { let poi = app.globalData.mapSelectPoi; let location = poi.location; let address = that.data.address; address.lng = location.split(',')[0]; address.lat = location.split(',')[1]; address.addressName = poi.name; address.address = poi.address; address.areaCode = poi.areaCode; address.province = poi.province; address.city = poi.city; address.county = poi.county; if (!address.city) { that.getAreaCode(address.lat, address.lng).then(res => { address.areaCode = res.areaCode; address.province = res.province; address.city = res.city; address.county = res.county; that.setData({ address: address, selectAddress: poi.address }); }).catch(() => { wx.showToast({ title: '获取地区码失败', icon: 'none', duration: 3000 }); }) } else that.setData({ address: address, selectAddress: poi.address }); } else { //这里分2种情况 1,有城市 标识选中原有地址进来 保持不变 //2,新增地址 初始化进入此页面时 需要调用getlocation2 // if (that.data.addressId) return;//从已有地址进入 // map.getLocation2().then(() => { // that.getAreaCode(app.globalData.lat, app.globalData.lng).then(res => { // res.isDefault=that.data.address.isDefault; // // console.log('globalData',app.globalData.lat,app.globalData.lng) // // console.log('areaCode',res) // that.setData({ // address: res, // selectAddress: res // }); // }).catch(err => { // wx.showToast({ // title: '获取地区码失败', // icon: 'none', // duration: 3000 // }); // }); // }) } }, getAreaCodeOld() { // let param = { // key: '6TRBZ-XXC6F-BWWJB-N75ME-BQNOF-YKFQY', // location: this.data.address.lat + ',' + this.data.address.lng, // } let param ={ location: this.data.address.lng + ',' + this.data.address.lat, extensions: 'all', key: api.gdKey, radius: 3000, }; wx.request({ url: api.gdUrl, data: param, method: 'GET', header: { 'Content-Type': 'application/json' }, success: res => { console.log(res); if (res.data.infocode == "10000") { if (res.data.info == "OK") { // let ad_info = res.data.result.ad_info; // let areaCode = res.data.result.ad_info.adcode; // this.setData({ // ['address.areaCode']: areaCode, // ['address.province']: ad_info.province, // ['address.city']: ad_info.city, // ['address.county']: ad_info.district, // }); let regeocode = res.data.regeocode.addressComponent; let cityName=(regeocode.city==null||regeocode.city.length==0)?regeocode.province:regeocode.city; let district= (regeocode.district==null||regeocode.district.length==0)?'':regeocode.district; this.setData({ ['address.areaCode']: regeocode.adcode, ['address.province']:regeocode.province, ['address.city']: cityName, ['address.county']: district }); } else { wx.showToast({ title: '获取地区码失败', icon: 'none', duration: 3000 }); } } else { wx.showToast({ title: '获取地区码失败', icon: 'none', duration: 3000 }); } }, fail: function (err) { reject(err) } }); }, getAreaCode(lat, lng) { return new Promise(function (resolve, reject) { let param ={ location: lng+ ',' +lat, extensions: 'all', key: api.gdKey, radius: 3000, }; wx.request({ url: api.gdUrl, data: param, method: 'GET', header: { 'Content-Type': 'application/json' }, success: res => { console.log(res); if (res.data.infocode == "10000") { if (res.data.info == "OK") { // console.info(res.data) let regeocode = res.data.regeocode.addressComponent; let cityName=(regeocode.city==null||regeocode.city.length==0)?regeocode.province:regeocode.city; let district= (regeocode.district==null||regeocode.district.length==0)?'':regeocode.district; let address = { lat: lat, lng: lng, areaCode: regeocode.adcode, province:regeocode.province, city: cityName, county: district, address: res.data.regeocode.formatted_address, addressName: regeocode.township //纯粹做显示 } console.log(address); resolve(address); } else { reject(false) } } else { reject(false) } }, fail: function (err) { reject(false); } }); }) }, onHide: function () { // 页面隐藏 }, onUnload: function () { // 页面关闭 chooseLocation.setLocation(null); } })