const app = getApp()
const baseUrl = app.globalData.baseUrl
// page/component/new-pages/user/address/address.js
Page({
  data: {
    address: {
      username: "",
      phone: "",
      address: "",
    },
  },
  onLoad() {
    var self = this
    // 获取地址信息
    wx.request({
      url: baseUrl + "/user/getById",
      method: "GET",
      data: {
        id: 3,
      },
      success(res) {
        self.setData({
          address: res.data,
        })
      },
    })
    wx.getStorage({
      key: "address",
      success: function (res) {
        self.setData({
          address: res.data,
        })
      },
    })
  },
  formSubmit(e) {
    console.log(e, "e")
    const value = e.detail.value
    if (value.username && value.phone && value.address) {
      //保存地址
      wx.request({
        url: baseUrl + "/user/addOrUpdate",
        method: "POST",
        data: {
          ...value,
          openId: "123123dasdas",
        },
        success(res) {
          console.log(res)
        },
      })
      wx.setStorage({
        key: "address",
        data: value,
        success() {
          wx.navigateBack()
        },
      })
    } else {
      wx.showModal({
        title: "提示",
        content: "请填写完整资料",
        showCancel: false,
      })
    }
  },
})
