{"size": 3539, "ext": "js", "max_stars_repo_path": "fe/src/controllers/user.js", "max_stars_repo_name": "kafeidouzi/maoer", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "fe/src/controllers/user.js", "max_issues_repo_name": "kafeidouzi/maoer", "max_issues_repo_issues_event_min_datetime": "2021-03-09T16:26:11.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T17:13:01.000Z", "max_forks_repo_path": "fe/src/controllers/user.js", "max_forks_repo_name": "kafeidouzi/maoer", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 12, "max_forks_count": null, "avg_line_length": 31.5982142857, "max_line_length": 96, "alphanum_fraction": 0.3814636903}
import userView from '../views/user.art'


let _url = ''
let _type=''
export default {
    async render(){
        //封装该函数，不让下边代码放入ajax回调内执行，优雅
        //利用async  await特性
        let result =await this.isSignin()
        //userView是loader返回的函数
        //该函数可以用于路由模板的渲染
        //res.render(userView(req,res),{data})
        //又可以用于直接返回字符串 userView(data),
        //可以直接传参
        //console.log(result.ret)
        let html = userView({
            isSignin:result.ret,
            username:result.data.username
        })
        $('.user-menu').html(html)
        this.bindEventToBtn()
    },
    isSignin(){
        return $.ajax({
            url:'./api/users/isSignin',
            headers:{
                'x-access-token':localStorage.getItem('x-access-token')
            },
            dataType:'json',
            success(result){
                return result
            }
        })
    },

    bindEventToBtn(){
        $('.hidden-xs').on('click',function(){

            _type = $(this).attr('id')
            _url = _type === 'btn-signin'? '/api/users/signin' :'api/users/signup'
            if(_url === 'api/users/signup'){
                $('.navbar-nav> .user-menu>.dropdown-menu>li.user-header').css('height','278px')
            }else{
                $('.navbar-nav> .user-menu>.dropdown-menu>li.user-header').css('height','163px')
            }
            $('input').val('')
        })
        $('#btn-submit').on('click',()=>{
           // let data = $('#inputEmail3').val()
            let data = $('#user-form').serialize()
           // console.log(data)
            $.ajax({
                url :_url,
                type:'POST',
                data,
                success(result,textStatus,jqXHR){
                    //console.log(result)
                    if(_type === 'btn-signin'){
                        if(result.ret){
                            let html = userView({
                                isSignin:true,
                                username : result.data.username
                            })
                            $('.user-menu').html(html)
                            _type=''
                            let token = jqXHR.getResponseHeader('x-access-token')
                            localStorage.setItem('x-access-token',token)
                        }else{
                            alert(result.data.msg)
                        }
                    }else if(_type === 'btn-signup'){
                        if(result.ret){
                            alert('亲，注册成功了哟')
                        }else{
                            console.log(result)
                            alert(result.data.msg)
                        }
                    }
                    else{
                        location.reload()
                    }
                }
            })
        })
        $('.dropdown').on('click','#btn-signout',()=>{
            localStorage.removeItem('x-access-token')
            location.reload()
            // $.ajax({
            //     url :'/api/users/signout',
            //     success(result){
               
                    
            //     }
            // })
        })
        $('.code').on('click',()=>{
            let data = $('#user-form').serialize()
            $.ajax({
                url:'/api/users/yanzheng',
                type:'post',
                data,
                success(result){
                    alert(result.data.msg)
                }

            })
        })
        $('')
    }

}