address-management.js 2.28 KB
import {
  getBindtapData,
  getObjByListKeyValue
} from '../../utils/util';

let app = getApp();
Page({
  data: {
    authorizeVisible: false,
    receiverList: [], // 收货地址
    defaultReceiver: null, // 默认收货地址
  },
  onShareAppMessage() {},
  showAuth() {
    this.setData({
      authorizeVisible: true
    })
  },
  onLoad(options) {},
  onShow() {
    this.initData();
  },
  initData() {
    app.queryIndex().then((result) => {
      this.queryReceiver();
    })
  },
  queryReceiver() {
    return new Promise((resolve, reject) => {
      app.post({
        url: app.api.receiver,
        data: {}
      }).then((result2) => {
        let defaultReceiver = getObjByListKeyValue(1, "defaultAddress", result2);
        this.setData({
          indexInfo: app.globalData.indexInfo,
          receiverList: result2,
          defaultReceiver: defaultReceiver
        })
        resolve();
      })
    });
  },

  // 编辑收货地址
  onAddressEditHandler(evt) {
    console.log("edit");
    let curData = getBindtapData(evt);
    app.globalData.addressEditInfo = curData;
    app.router.push({
      path: "addressEdit"
    })
  },
  // 添加收货地址
  onAddressAddHandler(evt) {
    app.globalData.addressEditInfo = null;
    app.router.push({
      path: "addressEdit"
    })
  },
  /**
   * 设置为默认地址
   */
  onSetDefaultHandler(evt) {
    let _this = this;
    let curData = getBindtapData(evt);
    wx.showModal({
      title: '提示',
      content: '设置为默认地址?',
      success(res) {
        if (res.confirm) {
          curData.defaultAddress = 1;
          app.post({
            url: app.api.receiverSave,
            data: curData
          }).then((result) => {
            _this.queryReceiver().then((result) => {
              wx.showModal({
                content: '操作成功',
                showCancel: false,
                success(res) {}
              })
            })
          })
        }
      }
    })
  },
  // 隐藏蒙层
  hideMask() {
    this.setData({
      authorizeVisible: false,
    })
  },
  // 子组件事件
  evtcomp(evt) {
    let {
      name,
      data
    } = evt.detail;
    switch (name) {

      // 隐藏弹窗
      case "_evt_hide_mask":
        this.hideMask();
        break;

      default:
        break;
    }
  },
})