address-edit.js 3.3 KB
import {
  getBindtapData,
  getObjByListKeyValue,
  checkMobile
} from '../../utils/util';

let app = getApp();
Page({
  data: {
    authorizeVisible: false,
    value: "",
    checked: false,
    addressEditInfo: {
      receiverCode: "",
      receiverName: "",
      receiverPhone: "",
      receiverAddress: "",
      defaultAddress: 1,
    }
  },
  onShareAppMessage() {},
  showAuth() {
    this.setData({
      authorizeVisible: true
    })
  },
  onLoad(options) {
    // this.setData({
    //   options: app.globalData.addressEditInfo
    // })
    this.initData();
  },
  initData() {
    let addressEditInfo = app.globalData.addressEditInfo;
    this.setData({
      addressEditInfo
    })
  },

  /**
   * 显示提示
   */
  showTips(tips) {
    wx.showToast({
      title: tips,
      icon: "none"
    })
  },

  /**
   * 检查提交
   */
  checkSubmit() {
    return new Promise((resolve, reject) => {
      let addressEditInfo = this.data.addressEditInfo;
      if (!addressEditInfo.receiverName) {
        this.showTips("请输入收货人姓名")
        reject();
      } else if (!addressEditInfo.receiverPhone) {
        this.showTips("请输入收货电话")
        reject();
      } else if (!checkMobile(addressEditInfo.receiverPhone)) {
        this.showTips("请输入正确收货电话")
        reject();
      } else if (!addressEditInfo.receiverAddress) {
        this.showTips("请输入收货地址")
        reject();
      } else {
        resolve()
      }
    });
  },

  /**
   * 表单提交
   * @param {*} evt
   */
  onSubmitHandler(evt) {
    this.checkSubmit().then((result) => {
      let {
        addressEditInfo,
        checked
      } = this.data;
      addressEditInfo.defaultAddress = checked ? 1 : 0;
      app.post({
        url: app.api.receiverSave,
        data: addressEditInfo
      }).then((result2) => {
        wx.showModal({
          content: '操作成功',
          showCancel: false,
          success(res) {
            wx.navigateBack({
              delta: 1
            });
          }
        })
      })
    });
  },

  /**
   * 删除表单
   * @param {*} evt
   */
  onDeleteHandler(evt) {
    let {
      addressEditInfo,
    } = this.data;
    app.post({
      url: app.api.receiverDelete,
      data: {
        receiverCode: addressEditInfo.receiverCode
      }
    }).then((result) => {
      wx.showModal({
        content: '删除成功',
        showCancel: false,
        success(res) {
          wx.navigateBack({
            delta: 1
          });
        }
      })
    })
  },

  bindReceiverNameInput(e) {
    this.setData({
      "addressEditInfo.receiverName": e.detail.value
    });
  },
  bindReceiverPhoneInput(e) {
    this.setData({
      "addressEditInfo.receiverPhone": e.detail.value
    });
  },
  bindReceiverAddressInput(e) {
    this.setData({
      "addressEditInfo.receiverAddress": e.detail.value
    });
  },

  onChange(event) {
    let checked = event.detail
    this.setData({
      checked: event.detail,
    });
  },

  // 隐藏蒙层
  hideMask() {
    this.setData({
      authorizeVisible: false,
    })
  },
  // 子组件事件
  evtcomp(evt) {
    let {
      name,
      data
    } = evt.detail;
    switch (name) {

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

      default:
        break;
    }
  },

})