123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518 |
- var util = require('../../../utils/util.js');
- var api = require('../../../config/api.js');
- var check = require('../../../utils/check.js');
- var area = require('../../../utils/area.js');
- const chooseLocation = requirePlugin('chooseLocation');
- var app = getApp();
- Page({
- data: {
- address: {
- id: 0,
- areaCode: 0,
- address: '',
- name: '',
- tel: '',
- isDefault: 0,
- province: '',
- city: '',
- county: '',
- latitude:'',
- longitude:'',
- 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.addressname=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
- })
- },
- chooseRegion() {
- let that = this;
- this.setData({
- openSelectRegion: !this.data.openSelectRegion
- });
- //设置区域选择数据
- let address = this.data.address;
- if (address.areaCode > 0) {
- let selectRegionList = this.data.selectRegionList;
- console.info(selectRegionList);
- selectRegionList[0].code = address.areaCode.slice(0, 2) + '0000';
- selectRegionList[0].name = address.province;
- selectRegionList[1].code = address.areaCode.slice(0, 4) + '00';
- selectRegionList[1].name = address.city;
- selectRegionList[2].code = address.areaCode;
- selectRegionList[2].name = address.county;
- let regionList = area.getList('county', address.areaCode.slice(0, 4));
- regionList = regionList.map(item => {
- //标记已选择的
- if (address.areaCode === item.code) {
- item.selected = true;
- } else {
- item.selected = false;
- }
- return item;
- })
- this.setData({
- selectRegionList: selectRegionList,
- regionType: 3,
- regionList: regionList
- });
- } else {
- let selectRegionList = [{
- code: 0,
- name: '省份',
- },
- {
- code: 0,
- name: '城市',
- },
- {
- code: 0,
- name: '区县',
- }
- ];
- this.setData({
- selectRegionList: selectRegionList,
- regionType: 1,
- regionList: area.getList('province')
- });
- }
- this.setRegionDoneStatus();
- },
- onLoad: function (options) {
- // 页面初始化 options为页面跳转所带来的参数
- //console.log(options)
- if (options.id && options.id != 0) {
- this.setData({
- addressId: options.id
- });
- this.getAddressDetail();
- }
- //初始化area
- //area.init();
- },
- onReady: function () {
- },
- selectRegionType(event) {
- console.info('selectRegionType');
- let that = this;
- let regionTypeIndex = event.target.dataset.regionTypeIndex;
- let selectRegionList = that.data.selectRegionList;
- //判断是否可点击
- if (regionTypeIndex + 1 == this.data.regionType || (regionTypeIndex - 1 >= 0 && selectRegionList[regionTypeIndex - 1].code <= 0)) {
- return false;
- }
- let selectRegionItem = selectRegionList[regionTypeIndex];
- let code = selectRegionItem.code;
- let regionList;
- if (regionTypeIndex === 0) {
- // 点击省级,取省级
- regionList = area.getList('province');
- } else if (regionTypeIndex === 1) {
- // 点击市级,取市级
- regionList = area.getList('city', code.slice(0, 2));
- } else {
- // 点击县级,取县级
- console.info(code);
- regionList = area.getList('county', code.slice(0, 4));
- console.info(regionList);
- }
- regionList = regionList.map(item => {
- //标记已选择的
- if (that.data.selectRegionList[regionTypeIndex].code == item.code) {
- item.selected = true;
- } else {
- item.selected = false;
- }
- return item;
- })
- this.setData({
- regionList: regionList,
- regionType: regionTypeIndex + 1
- })
- this.setRegionDoneStatus();
- },
- selectRegion(event) {
- let that = this;
- let regionIndex = event.target.dataset.regionIndex;
- let regionItem = this.data.regionList[regionIndex];
- let regionType = this.data.regionType;
- let selectRegionList = this.data.selectRegionList;
- selectRegionList[regionType - 1] = regionItem;
- if (regionType == 3) {
- this.setData({
- selectRegionList: selectRegionList
- })
- let regionList = that.data.regionList.map(item => {
- //标记已选择的
- if (that.data.selectRegionList[that.data.regionType - 1].code == item.code) {
- item.selected = true;
- } else {
- item.selected = false;
- }
- return item;
- })
- this.setData({
- regionList: regionList
- })
- this.setRegionDoneStatus();
- return
- }
- //重置下级区域为空
- selectRegionList.map((item, index) => {
- if (index > regionType - 1) {
- item.code = 0;
- item.name = index == 1 ? '城市' : '区县';
- }
- return item;
- });
- //console.info(selectRegionList);
- this.setData({
- selectRegionList: selectRegionList,
- regionType: regionType + 1
- })
- let code = regionItem.code;
- let regionList = [];
- if (regionType === 1) {
- // 点击省级,取市级
- regionList = area.getList('city', code.slice(0, 2))
- } else {
- // 点击市级,取县级
- regionList = area.getList('county', code.slice(0, 4))
- }
- this.setData({
- regionList: regionList
- })
- this.setRegionDoneStatus();
- },
- doneSelectRegion() {
- if (this.data.selectRegionDone === false) {
- return false;
- }
- let address = this.data.address;
- let selectRegionList = this.data.selectRegionList;
- address.province = selectRegionList[0].name;
- address.city = selectRegionList[1].name;
- address.county = selectRegionList[2].name;
- address.areaCode = selectRegionList[2].code;
- this.setData({
- address: address,
- openSelectRegion: false
- });
- },
- cancelSelectRegion() {
- this.setData({
- openSelectRegion: false,
- regionType: this.data.regionDoneStatus ? 3 : 1
- });
- },
- 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.addressname == '') {
- util.showErrorToast('请选择地址');
- return false;
- }
- if (!check.isValidPhone(address.tel)) {
- util.showErrorToast('手机号不正确');
- return false;
- }
- let that = this;
- let addressString=(address.detail==''?address.addressname:address.addressname+','+address.detail);
- util.request(api.AddressSave, {
- id: address.id,
- name: address.name,
- tel: address.tel,
- province: address.province,
- city: address.city,
- county: address.county,
- areaCode: address.areaCode,
- addressDetail:addressString ,
- 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', 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;
- },
- goMap(){
- // const key = "JA2BZ-XGOEU-UNZVD-46G64-S6MYJ-6BBF4";
- //使用在腾讯位置服务申请的key
- const key = '6TRBZ-XXC6F-BWWJB-N75ME-BQNOF-YKFQY';
- const referer = '鲸致生活LifePlus'; //调用插件的app的名称
- const category = '小区,生活服务,娱乐休闲';
-
- wx.navigateTo({
- url: 'plugin://chooseLocation/index?key=' + key + '&referer=' + referer + '&category=' + category
- });
- },
- onShow: function () {
- // 页面显示
- const location = chooseLocation.getLocation();
- console.log(location);
- if(location!=null){
- let address = this.data.address;
- address.province = location.province;
- address.city = location.city;
- address.county = location.district;
- address.addressname = location.name;
- address.lat=location.latitude;
- address.lng=location.longitude;
-
- this.setData({
- address: address,
- });
- this.getAreaCode()
- }
-
- },
- getAreaCode(){
- let param={
- key:'6TRBZ-XXC6F-BWWJB-N75ME-BQNOF-YKFQY',
- location: this.data.address.lat+','+this.data.address.lng,
- }
- wx.request({
- url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=',
- data:param,
- method: 'GET',
- header: {
- 'Content-Type': 'application/json',
- 'X-JZ-Token': wx.getStorageSync('token')
- },
- success: res=>{
- console.log(res);
- if (res.statusCode == 200) {
- if (res.data.status == 0) {
- let areaCode=res.data.result.ad_info.adcode;
- this.setData({
- ['address.areaCode']:areaCode
- });
- }else{
- wx.showToast({
- title: '获取地区码失败',
- icon: 'none',
- duration: 3000
- });
- }
- }else{
- wx.showToast({
- title: '获取地区码失败',
- icon: 'none',
- duration: 3000
- });
- }
- },
- fail: function(err) {
- reject(err)
- }
- });
- },
- onHide: function () {
- // 页面隐藏
- },
- onUnload: function () {
- // 页面关闭
- chooseLocation.setLocation(null);
- }
- })
|