flask09-Api分离

  • 注册
API
class Register(Resource):
    @marshal_with(users_fields)
    def post(self):
        parse = parser.parse_args()

        user = User()
        user.username = parse.get('username')
        user.password = generate_password_hash(parse.get('password'))
        user.email = parse.get('email')
        user.phone = parse.get('phone')
        user.token = str(uuid.uuid5(uuid.uuid4(),'register'))

        response = {
            'status':406,
            'msg':'注册失败',
            'date':str(time.ctime()),
        }

        testuser = User.query.filter(User.username == user.username).filter(User.email==user.email).first()

        if testuser: # 用户已存在
            response['error'] = '用户已注册'

            return response
        else: # 用户不存在
            # 判断邮箱
            testuser =User.query.filter(User.email == user.email).first()
            if testuser:
                response['error'] = '邮箱已被注册'
                return response

            # 用户名

            testuser = User.query.filter(User.username==user.username).first()
            if testuser:
                response['error'] = '用户名已被注册'
                return response

            db.session.add(user)
            db.session.commit()


            response['status'] = 200
            response['msg'] = '注册成功'
            response['data'] = user
            return response
Ajax请求
 $('#register').click(function () {
        $.post('/register/', {
            'username': $('#username').val(),
            'password': $('#password').val(),
            'phone': $('#phone').val(),
            'email': $('#email').val()
        }, function (response) {
            console.log(response)
            if (response['status'] == 200) {

                $.cookie('token', response['data']['token'])

                console.log($.cookie('token'))
                window.open('/static/html/login.html', '_self')
            }
        })
    })
  • 登录
API
class Login(Resource):
    @marshal_with(users_fields)
    def post(self):
        parse = parser.parse_args()

        username = parse.get('username')
        password = parse.get('password')

        response = {
            'status': 406,
            'msg': '登录失败',
            'date': str(time.ctime()),
        }

        user = User.query.filter(User.username == username).first()
        if user:
            if check_password_hash(user.password,password):
                
                user.token = str(uuid.uuid5(uuid.uuid4(),'login'))
                db.session.add(user)
                db.session.commit()

                response['msg'] = '登录成功'
                response['status'] = 200
                response['data'] = user
                return response
            else:
                response['error'] = '密码错误'
                return response

        else:
            response['error'] = '用户不存在'
            return response
Ajax请求
$('#login').click(function () {
        $.post('/login/', {
            'username': $('#username').val(),
            'password': $('#password').val(),
        }, function (response) {
            console.log(response)
            if (response['status'] == 200) {
                $.cookie('token', response['data']['token'])
                window.open('/static/html/index.html','_self')

            }
        })
    })
  • 首页
API
class Index(Resource):
    @marshal_with(users_fields)
    def get(self):
        parse = parser.parse_args()
        token = parse.get('token')

        response ={
            'status':406,
            'msg':'未获取到token',
            'date':str(time.ctime())
        }

        if token:
            user = User.query.filter(User.token==token).first()

            response['status']=200
            response['data']=user

            return response
        else:
            return response
Ajax请求
$.get('/index/',{
        'token':$.cookie('token')
    },function (response) {
        if (response['status']==200){

            var username = response['data']['username']
            console.log(username)

            $.cookie('token',response['data']['token'])


            $('').attr('href','#').html(username).appendTo($('.username'))

        }else {
            $('

未登录

').appendTo($('.username')) } })

你可能感兴趣的:(flask09-Api分离)