userinfo_edit.js 4.37 KB
var e = getApp(), s = e.globalData.setting, a = e.request, i = require("../../../utils/md5.js"), t = require("../../../utils/common.js");

Page({
    data: {
        url: s.url,
        resourceUrl: s.resourceUrl,
        user: null,
        type: "",
        canGetCode: !1
    },
    onLoad: function(s) {
        var a = this;
        this.setBarTitle(s.type), e.getUserInfo(function(e) {
            a.setData({
                user: e,
                type: s.type
            });
        });
    },
    setBarTitle: function(e) {
        var s = "修改个人信息";
        "nickname" == e ? s = "修改昵称" : "mobile" == e ? s = "修改手机" : "email" == e ? s = "修改邮箱" : "password" == e ? s = "修改密码" : "paypwd" == e ? s = "修改支付密码" : "sex" == e && (s = "修改性别"), 
        wx.setNavigationBarTitle({
            title: s
        });
    },
    formSubmit: function(s) {
        var a = this.data.type;
        if (a) {
            var i = s.detail.value;
            "nickname" == a ? this.submitNickname(i) : "mobile" == a ? this.submitMobile(i) : "email" == a ? this.submitEmail(i) : "password" == a ? this.submitPassword(i) : "paypwd" == a ? this.submitPaypwd(i) : "sex" == a ? this.submitSex(i) : e.confirmBox("处理类型出错:" + a);
        }
    },
    submitNickname: function(s) {
        if (!s.nickname) return e.showWarning("请输入昵称"), !1;
        this.requestUpdateUser({
            nickname: s.nickname
        });
    },
    submitMobile: function(s) {
        return s.mobile ? s.mobile_code ? void this.requestUpdateUser({
            mobile: s.mobile,
            mobile_code: s.mobile_code
        }) : (e.showWarning("请输入验证码"), !1) : (e.showWarning("请输入手机号"), !1);
    },
    submitEmail: function(s) {
        return s.email ? s.email.indexOf("@") < 0 ? (e.showWarning("邮箱格式不正确"), !1) : void this.requestUpdateUser({
            email: s.email
        }) : (e.showWarning("请输入邮箱"), !1);
    },
    submitPassword: function(s) {
        return this.data.user.password && !s.old_password ? (e.showWarning("请输入旧密码"), !1) : s.new_password ? s.confirm_password ? s.new_password.length < 6 || s.new_password.length > 18 ? (e.showWarning("密码长度不合法"), 
        !1) : s.new_password !== s.confirm_password ? (e.showWarning("两次密码不一致"), !1) : void a.post("/api/user/password", {
            data: {
                old_password: i("TPSHOP" + s.old_password),
                new_password: i("TPSHOP" + s.new_password)
            },
            success: function(s) {
                e.showSuccess("修改成功", function() {
                    wx.navigateBack();
                });
            }
        }) : (e.showWarning("请输入确认密码"), !1) : (e.showWarning("请输入新密码"), !1);
    },
    submitPaypwd: function(s) {
        return s.paypwd_mobile ? s.paypwd_code ? s.paypwd ? s.paypwd_confirm ? s.paypwd.length < 6 || s.paypwd.length > 18 ? (e.showWarning("密码长度不合法"), 
        !1) : s.paypwd !== s.paypwd_confirm ? (e.showWarning("两次密码不一致"), !1) : void a.post("/api/user/paypwd", {
            data: {
                new_password: i("TPSHOP" + s.paypwd),
                mobile: s.paypwd_mobile,
                paypwd_code: s.paypwd_code
            },
            success: function(s) {
                e.showSuccess("修改成功", function() {
                    wx.navigateBack();
                });
            }
        }) : (e.showWarning("请输入确认密码"), !1) : (e.showWarning("请输入新密码"), !1) : (e.showWarning("请输入验证码"), 
        !1) : (e.showWarning("请输入手机号"), !1);
    },
    submitSex: function(s) {
        if (0 == this.data.user.sex) return e.showWarning("请选择性别"), !1;
        this.requestUpdateUser({
            sex: this.data.user.sex
        });
    },
    changeGender: function(e) {
        var s = "boy" == e.currentTarget.dataset.gender ? 1 : 2;
        this.setData({
            "user.sex": s
        });
    },
    requestUpdateUser: function(e) {
        a.post("/api/user/updateUserInfo", {
            data: e,
            success: function(e) {
                wx.navigateBack();
            }
        });
    },
    setMobile: function(e) {
        this.data.user.mobile = e.detail.value;
    },
    getCode: function(e) {
        t.sendSmsCode(this.data.user.mobile);
    }
});