addOrEditAddress.js 6.11 KB
// packagePersonal/pages/addOrEditAddress/addOrEditAddress.js
const filter = require('../../../utils/filter.js');
import util from '../../../utils/util.js'
import api from '../../restapi'
import {
  getDetailedAdrress,
  getCoord
} from '../../../utils/mapapi.js'
Page(filter.identityFilter({

  /**
   * 页面的初始数据
   */
  data: {
    obj: {
      name: '',
      phone: '',
      province: '',
      address: ''
    }
  },
  input(e) {
    let target = e.currentTarget.dataset.target
    let value = e.detail.value
    this.setData({
      [`obj.${target}`]: value
    })
    this.check()
  },
  change(e) {
    this.setData({
      isDefault: e.detail.value
    })
  },
  click(e) {
    let _this = this
    _this.setData({
      changeAddressFlag:true
    })
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        wx.chooseLocation({
          latitude: res.latitude,
          longitude: res.longitude,
          success(_res_) {
            console.log(_res_)
            getDetailedAdrress(_res_.longitude, _res_.latitude, info => {
              console.log(info)
              let detail = {
                province: `${info.ad_info.province||''}${info.ad_info.city == info.ad_info.province?'':info.ad_info.city||''}${info.ad_info.district||''}`,
                address: `${info.address_component.street}${_res_.name}`,
                areaName: info.ad_info.district,
                cityName: info.ad_info.city,
                provinceName: info.ad_info.province,
                lat: info.ad_info.location.lat,
                lng: info.ad_info.location.lng
              }
              let obj = {
                ..._this.data.obj,
                ...detail
              }
              _this.setData({
                obj: obj
              })
              _this.check()
            })
          },
          complete(){
            
          }
        })
      }
    })

  },
  check() {
    let obj = this.data.obj
    let flag = Object.values(obj).findIndex(i => !i)
    this.setData({
      flag: flag == -1
    })
  },
  done() {
    if (!this.data.flag) return
    let obj = this.data.obj
    let data = {
      name: obj.name,
      phone: obj.phone,
      provinceName: obj.provinceName,
      cityName: obj.cityName,
      areaName: obj.areaName,
      address: obj.address,
      isDefault: this.data.isDefault ? 'Y' : 'N',
      lat: obj.lat,
      lng: obj.lng
    }
    if (!util.checkPhone(obj.phone)) return
    let isEdit = this.data.isEdit
    if (isEdit) data.id = this.data.id
    let target = !isEdit ? 'insertUserAddressList' : 'updateUserAddressList'
    api[target](data, res => {
      if (res.returnCode == '0') {
        util.showToast(isEdit ? '修改成功' : '添加成功')
        this.setData({
          flag: false
        })
        setTimeout(() => {
          util.navigateBack()
        }, 1500)
      } else {
        util.showToast(res.message)
      }
    })
  },
  del() {
    let id = this.data.id
    let _this = this
    wx.showModal({
      content: '确定删除地址吗?',
      success(res) {
        if (res.confirm) {
          console.log('删除')
          api.delUserAddress(id, _res_ => {
            if (_res_.returnCode == '0') {
              util.showToast('删除成功')
              _this.setData({
                flag: false
              })
              setTimeout(() => {
                util.navigateBack()
              }, 1500)
            } else {
              util.showToast(_res_.message)
            }
          })
        }
      }
    })
  },
  onLogin() {
    if (this.data.id && !this.data.changeAddressFlag) this.getUserAddress()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if (options.id) {
      util.showLoading('加载中')
      this.setData({
        isEdit: true,
        id: options.id
      })
      wx.setNavigationBarTitle({
        title: '修改地址'
      })
      this.getUserAddress()
    }
    util.getData(this, res => {
      console.log(res)
      if (res) {
        let data = res.data
        getCoord(`${data.provinceName}${data.cityName}${data.countyName}${data.detailInfo}`, _res_ => {
          if (_res_.status == 0) {
            let obj = {
              address: data.detailInfo,
              areaName: data.countyName,
              cityName: data.cityName,
              lat: _res_.result.location.lat,
              lng: _res_.result.location.lng,
              name: data.userName,
              phone: data.telNumber,
              provinceName: data.provinceName,
              province: `${data.provinceName}${data.cityName ==data.provinceName?'': data.cityName}${data.countyName}`
            }
            this.setData({
              obj:obj,
              isDefault:res.length < 1
            })
            this.check()
          }
        })
      }
    })
  },
  getUserAddress() {
    api.getUserAddress(this.data.id, res => {
      console.log(res)
      if (res.returnCode == '0') {
        let data = res.data
        let obj = {
          name: data.name,
          phone: data.phone,
          province: `${data.provinceName}${data.cityName ==data.provinceName?'': data.cityName}${data.areaName}`,
          address: data.address,
          lat: data.lat,
          lng: data.lng,
          areaName: data.areaName,
          cityName: data.cityName,
          provinceName: data.provinceName
        }
        this.setData({
          obj: obj,
          isDefault: data.isDefault == 'Y',
          flag: true
        })
        util.hideLoading()
      } else {
        util.showToast(res.message)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
}))