Ejemplo n.º 1
0
    def post(uid=None):
        if not check_utils.check_param_format(request.path,
                                              [r"^/v1/bms/user/$"]):
            return status_code.URL_ERROR
        data = request.json
        if not (data and isinstance(data, dict)):
            return status_code.JSON_PARAMS_ERROR
        for k, v in USER_REQUIRED_FIELD.items():
            if not data.get(k):
                return {
                    "code": status_code.REQUIRED_PARAM_CODE,
                    "msg_cn": status_code.REQUIRED_PARAM_MSG_CN.format(v),
                    "msg_en": status_code.REQUIRED_PARAM_MSG_EN.format(k)
                }
        # 正则验证

        try:
            user_service = UserService()
            user = user_service.get_user_by_account(
                account=data.get("account"))
            if user:
                return status_code.USER_ACCOUNT_EXIST
            user_service.create_user(account=data.get("account"),
                                     password=data.get("password"),
                                     phone=data.get("phone"),
                                     username=data.get("username"),
                                     gender=data.get("gender"),
                                     email=data.get("email"),
                                     birth=data.get("birth"),
                                     is_active=data.get("is_active"))
            return status_code.SUCCESS
        except Exception as ex:
            print(ex)
            logger.error("创建用户失败,{}".format(ex))
            return status_code.FAIL
Ejemplo n.º 2
0
    def post():
        data = request.json
        if not (data and isinstance(data, dict)):
            return status_code.JSON_PARAMS_ERROR
        for k, v in USER_REQUIRED_FIELD.items():
            if not data.get(k):
                return {
                    "code": status_code.REQUIRED_PARAM_CODE,
                    "msg_cn": status_code.REQUIRED_PARAM_MSG_CN.format(v),
                    "msg_en": status_code.REQUIRED_PARAM_MSG_EN.format(k)
                }
        # 正则验证

        account = data.get("account")
        password = data.get("password")
        phone = data.get("phone")
        try:
            user_service = UserService()
            user = user_service.get_user_by_account(account=account)
            if user:
                return status_code.USER_ACCOUNT_EXIST
            user_service.create_user(account=account,
                                     password=password,
                                     phone=phone)
            return status_code.SUCCESS
        except Exception as ex:
            print(ex)
            logger.error("用户注册失败,{}".format(ex))
            return status_code.FAIL
Ejemplo n.º 3
0
 def post():
     data = request.json
     session.clear()
     if not (data and isinstance(data, dict)):
         return status_code.JSON_PARAMS_ERROR
     account = data.get("account")
     password = data.get("password")
     if not account:
         return {
             "code": status_code.REQUIRED_PARAM_CODE,
             "msg_cn": status_code.REQUIRED_PARAM_MSG_CN.format("帐号"),
             "msg_en": status_code.REQUIRED_PARAM_MSG_EN.format("account")
         }
     if not password:
         return {
             "code": status_code.REQUIRED_PARAM_CODE,
             "msg_cn": status_code.REQUIRED_PARAM_MSG_CN.format("密码"),
             "msg_en": status_code.REQUIRED_PARAM_MSG_EN.format("password")
         }
     try:
         user_service = UserService()
         user = user_service.get_user_by_account(account=account)
         if not user:
             return status_code.USER_NOT_EXIST
         if not user.get("is_active"):
             return status_code.USER_IS_NOT_ACTIVE
         if not user_service.check_user_password(user_id=user.get("id"),
                                                 password=password):
             return status_code.PASSWORD_ERROR
         token = user_service.change_user_token(user_id=user.get("id"))
         session["user_id"] = user.get("id")
         session["token"] = token
         result = status_code.SUCCESS
         result["token"] = token
         return result
     except Exception as ex:
         print(ex)
         logger.error("用户登录失败,{}".format(ex))
         return status_code.FAIL
Ejemplo n.º 4
0
    def post():
        """
        登录
        :return:
        """

        client_ip = request.remote_addr
        data = request.json
        # 判断是否有数据
        if data is None:
            # 写运行日志

            # 写操作日志
            LogService.write_log(client_ip=client_ip, action_cn="用户登录", action_en="user login",
                                 result_cn="失败", result_en="FAIL", reason="请求参数错误")
            result = Message(status_code=STATUS_CODE["ERROR"], msg_cn="请求参数错误",
                             msg_en="request parameter error.").to_dict()
            return jsonify(result)
        # 判断账户、密码的数据格式
        if not data.get("account").strip():
            # 写运行日志

            # 写操作日志
            LogService.write_log(client_ip=client_ip, action_cn="用户登录", action_en="user login",
                                 result_cn="失败", result_en="FAIL", reason="登录账号为空")
            result = Message(status_code=STATUS_CODE["ERROR"], msg_cn="登录账号为空",
                             msg_en="login account is empty.").to_dict()
            return jsonify(result)
        if not data.get("password").strip():
            # 写运行日志

            # 写操作日志
            LogService.write_log(client_ip=client_ip, action_cn="用户登录", action_en="user login",
                                 result_cn="失败", result_en="FAIL", reason="登录密码为空")
            result = Message(status_code=STATUS_CODE["ERROR"], msg_cn="登录密码为空",
                             msg_en="login password is empty.").to_dict()
            return jsonify(result)
        # UserService对象
        user_service = UserService()
        # 获取用户
        user = user_service.get_user_by_account(data.get("account").strip())
        # 判断是否存在
        if not user:
            # 写运行日志

            # 写操作日志
            LogService.write_log(client_ip=client_ip, action_cn="用户登录", action_en="user login",
                                 result_cn="失败", result_en="FAIL", reason="账户不存在")
            result = Message(status_code=STATUS_CODE["ERROR"], msg_cn="账户不存在",
                             msg_en="user account is not existed.").to_dict()
            return jsonify(result)
        # 判断密码是否正确
        if not user.check_password(data.get("password").strip()):
            # 写运行日志

            # 写操作日志
            LogService.write_log(client_ip=client_ip, action_cn="用户登录", action_en="user login",
                                 result_cn="失败", result_en="FAIL", reason="登录密码错误")
            result = Message(status_code=STATUS_CODE["ERROR"], msg_cn="登录密码错误",
                             msg_en="login password error.").to_dict()
            return jsonify(result)
        # 判断是否激活
        if not user.is_active:
            # 写运行日志

            # 写操作日志
            LogService.write_log(client_ip=client_ip, action_cn="用户登录", action_en="user login",
                                 result_cn="失败", result_en="FAIL", reason="用户未激活")
            result = Message(status_code=STATUS_CODE["ERROR"], msg_cn="用户未激活, 请联系管理员",
                             msg_en="user is not active, please contact administrator.").to_dict()
            return jsonify(result)
        # 生成新的token
        user_token = uuid.uuid1().hex
        # 更新token
        user_service.update_user_token(user, user_token)
        # 将用户信息保存在session中
        session["user_token"] = user_token
        session["user_id"] = user.id
        # 写运行日志

        # 写操作日志
        LogService.write_log(client_ip=client_ip, action_cn="用户登录", action_en="user login",
                             result_cn="成功", result_en="SUCCESS", reason="登录成功", user_id=user.id)
        result = Message(status_code=STATUS_CODE["SUCCESS"], msg_cn="登录成功",
                         msg_en="user login successful.").to_dict()
        return jsonify(result)