Vue.config.devtools = true;
var v = new Vue({
    el:".container",
    data:{
        userName:"",
        headerImg:"",
        phone:"",
        isLogin: 0,
        memToken: '',
        memHeadImg: '',
        userJf:-1,
        integralType: "",
        integralCount: "",
    },
    created:function (){
        var that = this;
        if(!checkLogin()){
            // goLogin();
            return false;
        }
        if($.cookie("memToken")){
            that.memToken = $.cookie("memToken");
            that.isLogin=1;
        }
        that.getUserInfo();
        that.findUserIntegral();
    },
    mounted:function (){
        setTimeout(() => {
            $(".loading-item").fadeOut(200);
        },500)
    },
    methods:{
        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 : serverHttp+"/user/getUserInfo",
                data : {
                    memToken:that.memToken
                },
                success:function (res) {
                    console.log(res);
                    if (res.code == 200) {
                        that.userName = res.data.memNickname;
                        that.headerImg = res.data.memHeadimg;
                        that.phone = res.data.memMobile;
                    }
                }
            })
        },
        change () {
            var that = this;
            $("#fileInput").click();
        },
        gotoUserCenter () {
            window.location.href = serverHttp + "/user/personalCenter?from=H5&memToken="+this.memToken;
        },
        updateUserInfo (){
            if(!checkLogin()){
                // goLogin();
                return false;
            }
            var that = this;
            $.ajax({
                type : 'POST',
                url : serverHttp+"/user/updateUserInfo",
                data : {
                    memToken:that.memToken,
                    userName:that.userName,
                    phone:that.phone,
                    headerImg:that.headerImg,
                },
                success:function (res) {
                    if (res.code == 200) {
                        alert("设置成功");
                        setTimeout(function (){
                            window.location.href = serverHttp + "/user/personalCenter?from=H5";
                        },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('memToken',this.memToken);
            formData.append('type',2);
            $.ajax({
                url: serverHttp+"/user/uploadHearderImg",
                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) {
                        alert("为查询到用户");
                    } else if (data.code == 10010) {
                        alert("图片上传失败");
                    }
                },
                error:function (returndata){
                }
            });
        },
        isPass(pass){
            var reg = /^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,16}$/;
            var isJudge = reg.test(pass);
            return isJudge;
        },
        updatePass (){
            if(!checkLogin()){
                // goLogin();
                return false;
            }
            var passwd = $("#passwd").val();
            var passwdRe = $("#passwdRe").val();
            if(passwd == ""){
                alert('请输入密码');
                return;
            } else{
                if(!this.isPass(passwd)){
                    alert('密码格式有误，请输入8位包含数字、字母和符号的密码');
                    return;
                }
            }
            if(passwdRe == ""){
                alert('请输入确认密码');
                return;
            }
            if(passwdRe !== passwd){
                alert('两次输入的密码不一致，请重新输入');
                return;
            }
            var that = this;
            $.ajax({
                type : 'POST',
                url : "/login/updatePwd",
                data : {
                    memCard:$.cookie("memCard"),
                    passwd:md5(passwd),
                    from:'H5',
                },
                success:function (res) {
                    if (res.code == 200) {
                        alert("设置成功");
                        setTimeout(function (){
                            window.location.href = serverHttp + "/user/personalCenter?from=H5";
                        },2000);
                    }
                }
            })
        },
    }
})
