Пример #1
0
    def post(self):

        args = parse_base.parse_args()
        password = args.get('password')
        action = args.get('action').lower()

        if action == USER_ACTION_REGISTER:

            args_register = parse_register.parse_args()
            username = args_register.get('username')
            phone = args_register.get('phone')

            cinema_user = CinemaUser()
            cinema_user.username = username
            cinema_user.password = password
            cinema_user.phone = phone

            if not cinema_user.save():
                abort(400, msg='创建失败')

            data = {
                'status': HTTP_CREATE_OK,
                'msg': '用户创建成功',
                'data': cinema_user,
            }

            return marshal(data, single_cinema_user_fields)

        elif action == USER_ACTION_LOGIN:

            args_login = parse_login.parse_args()
            username = args_login.get('username')
            phone = args_login.get('phone')

            user = get_cinema_user(username) or get_cinema_user(phone)

            if not user:
                abort(400, msg='用户不存在')

            if not user.check_password(password):
                abort(401, msg='密码错误')

            if user.is_delete:
                abort(401, msg='用户不存在')

            token = generate_cinema_user_token()

            cache.set(token, user.id, timeout=60 * 60 * 24 * 7)

            data = {
                'msg': '登录成功',
                'status': HTTP_OK,
                'token': token,
            }

            return data

        else:
            abort(400, msg='请提供正确的参数')
Пример #2
0
    def post(self):

        args = parse_base.parse_args()

        password = args.get("password")
        action = args.get("action").lower()

        if action == USER_ACTION_REGISTER:
            args_register = parse_register.parse_args()
            phone = args_register.get("phone")
            username = args_register.get("username")

            cinema_user = CinemaUser()

            cinema_user.username = username
            cinema_user.password = password
            cinema_user.phone = phone

            if not cinema_user.save():
                abort(400, msg="create fail")

            data = {
                "status": HTTP_CREATE_OK,
                "msg": "用户创建成功",
                "data": cinema_user
            }

            return marshal(data, single_cinema_user_fields)
        elif action == USER_ACTION_LOGIN:

            args_login = parse_login.parse_args()

            username = args_login.get("username")
            phone = args_login.get("phone")

            user = get_cinema_user(username) or get_cinema_user(phone)

            if not user:
                abort(400, msg="用户不存在")

            if not user.check_password(password):
                abort(401, msg="密码错误")

            if user.is_delete:
                abort(401, msg="用户不存在")

            token = generate_cinema_user_token()

            cache.set(token, user.id, timeout=60 * 60 * 24 * 7)

            data = {"msg": "login success", "status": HTTP_OK, "token": token}

            return data

        else:
            abort(400, msg="其提供正确的参数")