add_address.js 5.72 KB
var s = function(s) {
    return s && s.__esModule ? s : {
        default: s
    };
}(require("../../../utils/regions/Regions.js")), e = getApp(), a = e.globalData.setting,os=a,t = e.request;
var ut = require("../../../utils/util.js"), oo = e.globalData;

Page({
    data: {
        url: a.url,
        resourceUrl: a.resourceUrl,
        iurl: a.imghost,
        address: null,
        isnew:0,
        is_op_co:false,//switch开关
        disabled:0
    },
    onLoad: function(s) {
        var a = this,th=a;
        var is_e = ut.isEmptyObject(s);
        this.setData({
            address: s, isnew:is_e,
        }), e.getUserInfo(function(s) {
            a.setData({
                user: s
            });
            th.initRegions();
        })
    },
    
  onReachBottom: function () {
    if (this.data.total <= 6) return;
    if (this.data.ismore) return;
    this.requestAddressList();
  },

  requestAddressList: function () {
    var e = this;
    t.get(e.data.url + "/api/weshop/useraddress/page", {
      data: { store_id: os.stoid, user_id: oo.user_id, page: e.data.curpage, pageSize: 6 },
      success: function (t) {
        e.data.curpage++;
        var arr1 = e.data.addresses;
        var arr2 = t.data.data.pageData;
        var arr3 = [...arr1, ...arr2];

        var ismore = 0;
        if (arr3.length == t.data.data.total) ismore = 1

        e.setData({
          addresses: arr3, total: t.data.data.total, ismore: ismore
        }), wx.stopPullDownRefresh();
      }
    });
  },
    initRegions: function() {
        var e = this;
        new s.default(this, "regions", {
            endAreaLevelCall: function(s, a, t) {
                console.log("initRegions");
                console.log(t);                        
                Object.assign(e.data.address, t), e.setData({
                    "disabled": 0,
                    "address.province_name": e.data.address.province_name,
                    "address.city_name": e.data.address.city_name,
                    "address.district_name": e.data.address.district_name,
                    "address.twon_name": e.data.address.twon_name,
                    "address.more_address": 
                      e.data.address.province_name + "-" + e.data.address.city_name + "-" + e.data.address.district_name
                       +"-"+e.data.address.twon_name,
                });
                console.log(e.data.address);
            }
        });
    },
  //-----------添加或者编辑-----------
  submitAddress: function (s) {
    var a = this.data.address, b = a, th = this;
    Object.assign(a, s.detail.value), a.is_default = Number(a.is_default);
    a.store_id = os.stoid;

     //判断用户名是否为空
    var cs = th.check(a.consignee);
    if (cs != 1) {
      return getApp().my_warnning("收货姓名不能为空", 0, th);
    }

    var ob = ut.check_mobile(a.mobile);
    if (ob.code != 1) {
      return getApp().my_warnning(ob.title, 0, th);
    }

    if (a.more_address == null || a.more_address == undefined || a.more_address == "") {
      getApp().my_warnning("请选择地区", 0, th);
      return false;
    }
    //判断地址是否为空
    var add = th.check(a.address);
    if (add != 1) {
      return getApp().my_warnning("详细地址不能为空", 0, th);
    }

    //判断是不是空对象
    //--------如果是修改地址----------
    if (!th.data.isnew) {
      if (a.is_default == 1) {
        t.put("/api/weshop/useraddress/update", {
          data: { user_id: oo.user_id, is_default: 0 },
          success: function (s) {
            t.put("/api/weshop/useraddress/updateById", {
              data: a,
              success: function (s) {
                e.showSuccess(s.data.msg, function () {
                  wx.navigateBack();
                });
              }
            });
          }
        });

      } else {
        t.put("/api/weshop/useraddress/updateById", {
          data: a,
          success: function (s) {
            e.showSuccess(s.data.msg, function () {
              wx.navigateBack();
            });
          }
        });
      }

    } else {
      a['user_id'] = oo.user_id;
      a['store_id'] = os.stoid;


      if (a.is_default == 1) {
        t.put("/api/weshop/useraddress/update", {
          data: { user_id: oo.user_id, is_default: 0 },
          success: function (s) {

            //-----新增地址-------
            t.post("/api/weshop/useraddress/save", {
              data: a,
              success: function (s) {
                e.showSuccess(s.data.msg, function () {
                  wx.navigateBack();
                });
              }
            });

          }
        })
      } else {

        //-----新增地址-------
        t.post("/api/weshop/useraddress/save", {
          data: a,
          success: function (s) {
            e.showSuccess(s.data.msg, function () {
              wx.navigateBack();
            });
          }
        });
      }



    }
  },

    //---------删除收货地址------------
    deleteAddress: function() {
        var s = this.data.address.address_id;
        s && wx.showModal({
            title: "确定删除?",
            success: function(a) {
                a.confirm && t.delete("/api/weshop/useraddress/del/" + oo.user_id+"/"+s, {
                    success: function(s) {
                        e.showSuccess("删除成功", function() {
                            wx.navigateBack();
                        }, 500);
                    }
                });
            }
        });
    },
    //是否是默认地址
  bind_is_op_co:function(e){
        var i = e.detail;
        this.setData({
          is_op_co:i
        })
        console.log(i)
  },
 check:function(as){
    var code = 1;
    if(as===''){
         code = -1;
     }
      return code;
}
});