Vue.config.devtools = true;
var v = new Vue({
    el:".index-box",
    data:{
        userName:"",
        oldUserName:"",
        headerImg:"",
        oldPhone:"",
        phone:"",
        code:"",
        isLogin: 0,
        memToken: '',
        memHeadImg: '',
        userJf:-1
    },
    created:function (){
        var that = this;
        if (!checkWebLogin()) {
            // $('.navlogorebox').trigger('click')
            return false;
        }
        // if(!checkLogin()){
        //     goLogin();
        //     return false;
        // }
        // if($.cookie("memToken")){
        //     that.memToken = $.cookie("memToken");
        //     that.isLogin=1;
        // }
        // if($.cookie("memHeadImg")){
        //     that.memHeadImg = $.cookie("memHeadImg");
        // }
        that.getUserInfo();
        // that.findUserIntegral();
    },
    mounted:function (){
        setTimeout(() => {
            $(".loading-item").fadeOut(200);
        },500)
    },
    methods:{
        //获取验证码
        editVerification (){
            if (this.oldPhone == this.phone) {
                layer.msg('手机号没有更改，不需要验证码验证');
                return;
            }
            if (this.phone == "") {
                layer.msg('请填写手机号');
                return;
            }
            if (!isMobile(this.phone)) {
                layer.msg('请填写正确的手机号码');
                return;
            }
            sendYzm(this.phone, 'editVerification');
        },

        findUserIntegral (){
            var that = this;
            $.ajax({
                type : 'POST',
                url : serverHttp+"/user/findUserIntegral",
                data : {
                    memToken:that.memToken
                },
                success : function(data) {
                    if (data.code == 200) {
                        that.userJf = data.data;
                    }
                },
                error : function(data) {
                }
            });
        },
        getUserInfo (){
            var that = this;
            $.ajax({
                type : 'POST',
                url : "/user/getUserInfoWeb",
                success:function (res) {
                    if (res.code == 200) {
                        that.userName = res.data.memNickname;
                        that.oldUserName = res.data.memNickname;
                        that.headerImg = res.data.memHeadimg;
                        that.phone = res.data.memMobile;
                        that.oldPhone = res.data.memMobile;
                    }
                }
            })
        },
        change () {
            var that = this;
            $("#fileInput").click();
        },
        gotoUserCenter () {
            window.location.href = serverHttp + "/user/personalCenter?from=H5&memToken="+this.memToken;
        },
        updateUserInfo (){
            if (!checkWebLogin()) {
                // $('.navlogorebox').trigger('click')
                return false;
            }
            var getName = this.userName;
            if (this.oldUserName == this.userName){
                getName = '';
            }

            if (this.phone == "") {
                layer.msg("请填写手机号码");
                return;
            }
            var getPhone = this.phone;
            if (this.oldPhone == this.phone) {
                getPhone = '';
            }
            if (this.oldPhone != this.phone && this.code == "") {
                layer.msg('请输入验证码');
                return;
            }
            if (!isMobile(this.phone)) {
                layer.msg("请填写正确的手机号码");
                return;
            }
            var that = this;
            $.ajax({
                type : 'POST',
                url : "/user/updateUserInfoWeb",
                data : {
                    userName:getName,
                    phone:getPhone,
                    headerImg:that.headerImg,
                    valCode:that.code
                },
                success:function (res) {
                    if (res.code == 200) {
                        layer.msg("设置成功");
                        setTimeout(function (){
                            if (that.oldPhone != that.phone) {
                                $.ajax({
                                    type: 'GET',
                                    url: "/login/logOutWeb",
                                    success: function (res) {
                                        localStorage.removeItem('isVip')
                                        $('.navlogorebox').trigger('click')
                                    }
                                })
                            }else{
                                window.location.href = "/user/personalCenter?from=web";
                            }
                        },2000);
                    }else{
                        alert(res.message);
                    }
                }
            })
        },
        updatePass (){
            if (!checkWebLogin()) {
                // $('.navlogorebox').trigger('click')
                return false;
            }
            var passwd = $("#passwd").val();
            var passwdRe = $("#passwdRe").val();
            if(passwd == ""){
                layer.msg('请输入密码', {
                    offset: 't',
                    anim: 6
                });
                return;
            } else{
                if(!isPass(passwd)){
                    layer.msg('密码格式有误，请输入8位包含数字、字母和符号的密码', {
                        offset: 't',
                        anim: 6
                    });
                    return;
                }
            }
            if(passwdRe == ""){
                layer.msg('请输入确认密码', {
                    offset: 't',
                    anim: 6
                });
                return;
            }
            if(passwdRe !== passwd){
                layer.msg('两次输入的密码不一致，请重新输入', {
                    offset: 't',
                    anim: 6
                });
                return;
            }
            var that = this;
            $.ajax({
                type : 'POST',
                url : "/login/updatePwd",
                data : {
                    passwd:md5(passwd),
                    from:'web',
                },
                success:function (res) {
                    if (res.code == 200) {
                        layer.msg("设置成功,请重新登录");
                        setTimeout(function (){
                            $('.navlogorebox').trigger('click')
                        },2000);
                    }else{
                        alert(res.message);
                    }
                }
            })
        },
        chooseImg (){
            var that = this;
            //上传图片https://files.imevos.com/upload/headimgs/4d1d21aebda441c9924daed3fefee8e3.jpg
            var formData = new FormData();
            formData.append('file', document.getElementById('fileInput').files[0]);
            formData.append('type',2);
            $.ajax({
                url: "/user/uploadHearderImgWeb",
                type: 'post',
                data: formData,
                dataType: "json",
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                success:function(data){
                    if (data.code == 200) {
                        that.headerImg = data.data;
                    } else if (data.code == 10003) {
                        layer.msg("未查询到用户");
                    } else if (data.code == 10010) {
                        layer.msg("图片上传失败");
                    }
                },
                error:function (returndata){
                }
            });
        }
    }
})
