def user_login(): # data = request.get_json # account = data['account'] # password = data['password'] # 1、request.get_json 会自动传入ClientForm print(request.get_json()) form = ClientForm() print(form.account) print(form.password) # 2、对ClientForm对实例进行校验 if form.validate(): account = form.account.data password = form.password.data try: user = User.is_password_right(account=account, password=password) if user: token = creat_token(user.id) return NoException(data=token) else: raise ParameterException(msg="查无此人", error_code=602) except Exception: raise ParameterException(msg="登录失败", error_code=602) else: # 若form不满足校验规则,返回报错600,后续可以细化 raise ParameterException()
def create_client(): data = request.json form = ClientForm(data=data) if form.validate(): promise = { ClientTypeEnum.USER_EMAIL: __register_user_by_email, }
def create_client(): data = request.json form = ClientForm(data=data) if form.validate(): __register_user_by_email() return 'success' else: return 'Failed'
def create_client(): data = request.json form = ClientForm(data=data) if form.validate(): promise = {ClientTypeEnum.USER_EMAIL: __register_user_by_email} promise[form.type.data]() return 'success'
def register(): data = request.json form = ClientForm(data=data) if form.validate(): pormise = {ClientTypeEnum.EMAIL: __register_user_by_email} pormise[form.type.data]() else: raise BizException return 'success'
def create_client(): form = ClientForm() form.validate_for_api() promise = { ClientTypeEnum.USER_EMAIL:__register_user_by_email } promise[form.type.data]() # 我们可以预知 return Success()
def create_client(): data = request.json # 获取json参数 form = ClientForm(data=data) # 在form中进行校验 form.validate_for_api() # 抛出验证错误 promise = { ClientTypeEnum.USER_EMAIL: __register_user_by_email, ClientTypeEnum.USER_MOBILE: __register_user_by_mobile, } promise[form.type.data]() # 执行邮箱注册逻辑 return Success()
async def create_client(request): ''' http://127.0.0.1:5000/v1/client/register {"account":"*****@*****.**","secret":"123456","type":100,"nickname":"sanic"} ''' form = ClientForm(data=request.json) if ClientForm(data=request.json).validate(): promise = {100: __register_user_by_email} await promise[form.type.data](request) return Success(request) return ParameterException(request)
def create_client(): # 客户端使用 json 方式提交数据 data = request.json form = ClientForm(data=data) if form.validate(): # 使用字典处理不同的注册方式 promise = { ClientTypeEnum.USER_EMAIL: __register_user_by_email, } promise[form.type.data]() return 'success'
def create_client(): # request.json的数据隐藏在了ClientForm的基类中直接调用,简化调用 form = ClientForm() form.validate_for_api() promise = { ClientTypeEnum.USER_EMAIL: __reister_user_by_email, } # 调用__reister_user_by_email方法,通过枚举类型拿到 promise[form.type.data]() # return一个类本质是返回的一个HTTPException 实际上是一个html返回 return Success()
def user_login(): data = request.json form = ClientForm(data=data) if form.validate(): identity = User.verify(form.account.data, form.secret.data) user = User.query.get(identity['uid']) user_info = { 'uid': identity['uid'], 'auth': identity['scope'], 'nickname': user.nickname } return jsonify(user_info), 201
def create_client(): data = request.json form = ClientForm(data=data) if form.validate(): promise = { ClientTypeEnum.USER_EMAIL: __register_user_by_email, } promise[form.type.data]() # 注册 登录 # 参数 校验 接收参数 # WTForms pass
def get_token(): data = request.json form = ClientForm(data=data) if form.validate(): identity=User.verify(form.account.data,form.secret.data) expiration = current_app.config['TOKEN_EXPIRATION'] token = generate_auth_token(identity['uid'], identity['scope'], expiration) t = { 'token': token.decode('ascii') } return jsonify(t), 201
def create_client(): # 获取客户端信息 data = request.json form = ClientForm(data=data) if form.validate(): # 处理不同客户端注册的方案 # 替代switchcase-{Enum_name:handle_func} promise = {ClientTypeEnum.USER_EMAIL: __register_user_by_email} # request.args.to_dict() # 表单:网页; json:移动端 # 注册 登录 # 参数 校验 接收参数 # WTForms 验证表单 pass
def get_token(): """ :return: 返回加密后的Token和一个HTTP状态码 """ # api的get_token就相当于web的login # 接收用户的参数 form = ClientForm().validate_for_api() promise = { ClientTypeEnum.USER_EMAIL: User.verify } identity = promise[ClientTypeEnum(form.type.data)]( form.account.data, form.secret.data ) # 生成Token token = generate_auth_token(identity['uid'], form.type.data, identity['scope'], expiration=current_app.config['TOKEN_EXPIRATION']) # 将Token字节码转换为ascii码 t = { 'token': token.decode('ascii'), 'uid': identity['uid'] } return jsonify(t), 201
def create_client(): """ 用户注册 :return: {username} --- tags: - 用户模块 parameters: - name: username in: body type: string required: true example: simple - name: password in: body type: string required: true example: 123456 - name: type in: body type: int required: true example: 100 responses: 200: description: 返回信息 examples: success : {"error_code": 0,"msg": "ok","request": "POST /v1/client/register"} """ form = ClientForm().validate_for_api() promise = { ClientTypeEnum.USER_NAME: __register_user_by_username, } promise[form.type.data]() return Success(message="用户注册成功!")
def create_client(): # 校验参数 # 表单用WTForms来校验 # 主要这里我们使用表单form的形式提交数据,除此之外还有json # 我们再postman工具中可以看到,有很多提交的类型 # form-data就是表单、raw+JSON(application/json)是json的类型 # 网页中多为表单、微信小程序中多为json # 从客户端接受参数有两种类型 # request.json # request.args.to_dict() data = request.json # 注意这里data=data的用法(这是针对request.json类型) # 我这里特意查了下这种用法,这种叫做关键字参数用法 # 函数定义的时候,用**来表示这种特定的参数,如 def fun(a,b,**others): form = ClientForm().validate_for_api() # 注意这里promise只是个字典而已 promise = { ClientTypeEnum.USER_EMAIL: __register_user_by_email } promise[form.type.data]() #這裡為什麼可以return這個呢,因為它本質上是一個HTTPException #經驗:我們可以接受定義時候的複雜,但是不能接受調用的時候的複雜 return Success()
def create_client(): """ :return: 客户端 注册 """ # 校验客户端的数据, 使用app.validators.forms.py # data = request.json # 接受数据, 必须是data=data # data = request.args.to_dict() # form = ClientForm(data=data) # 使用自定义的校验ClientForm校验数据 # if form.validate(): # promise = { # ClientTypeEnum.USER_EMAIL: __register_user_by_email # } # # print(form.account.data) # # print(form.secret.data) # # print(form.type.data) # 100 # promise[ClientTypeEnum(form.type.data)]() # 调用相应的方法 # else: # print(form.errors) # raise ClientTypeError() form = ClientForm().validate_for_api() promise = {ClientTypeEnum.USER_EMAIL: __register_user_by_email} promise[ClientTypeEnum(form.type.data)]() return Success()
def get_token(): form = ClientForm().validate_for_api() promise = { ClientTypeEnum.USER_EMAIL: User.verify } #验证身份通过,也成功拿到用户的id identity = promise[ClientTypeEnum(form.type.data)]( form.account.data, form.secret.data ) #下一步就是生成令牌 #令牌是需要加密的,会涉及到加密的算法,flask自带了一个 itsdangerous 库,方便我们去生成令牌 #expiration = current_app.config['TOKEN_EXPIRATION'], #多了逗号坑人..返回就是元组了.. expiration = current_app.config['TOKEN_EXPIRATION'] token = generate_auth_token( identity['uid'], form.type.data, identity['scope'], expiration ) t = { "token":token.decode('ascii') #这不是普通的字符串,还需要decode下 } return jsonify(t),201
def get_token(): form = ClientForm().validate_for_api() promise = { ClientTypeEnum.USER_EMAIL: Voter.verify, } identity = promise[ClientTypeEnum(100)](form.account.data, form.secret.data) # Token expiration = current_app.config['TOKEN_EXPIRATION'] token = generate_auth_token( identity['uid'], # form.type.data, identity['scope'], expiration) if identity['scope'] == 'UserScope': scope = 1 elif identity['scope'] == 'AdminScope': scope = 2 elif identity['scope'] == 'CmsScope': scope = 3 t = { 'code': 200, 'token': token.decode('ascii'), 'scope': scope, 'msg': 'ok' } return jsonify(t), 200
def create_client(): form = ClientForm().validate_parameter_for_api() promise = {ClientTypeNum.USER_EMAIL: __register_user_by_email} promise[form.type.data]() return Success()
def register_one_account(): form = ClientForm().validate_for_api() if User.query.filter_by(username=form.username.data).first(): return ParameterException(msg='User already exists') else: User.register_by_username(form.username.data, form.secret.data) return Success(msg='Register Success')
def create_client(): # request.args.to_dict() form = ClientForm().validate_for_api() promise = {ClientTypeEnum.USER_EMAIL: __register_user_by_email} promise[form.type.data]() # AOP在全局的出口发现未知异常 return Success()
def create_client(): form = ClientForm().validate_for_api() code = form.code.data token = __register(code) t = { 'token': token.decode('ascii') } return jsonify(t), 201
def create_client(): # 如果初始化Form时传递的是json类型的数据,一定要用关键字参数data= form = ClientForm().validate_for_api() promise = { ClientTypeEnum.USER_EMAIL: __register_user_by_email, } promise[form.type.data]() return Success(code=201)
def create_client(): form = ClientForm().validate_for_api() promise = { ClientTypeEnum.USER_EMAIL: __register_user_by_email, } promise[form.type.data]() # define complexly,use easily return Success()
def create_client(): form = ClientForm().validate_for_api() promise = { ClientTypeEnum.USER_EMAIL: __register_user_by_email } promise[form.type.data]() # 这里的目的不是返回 Success 的实例,而是需要它构造 json 返回给前端 return Success()
def create_client(): form = ClientForm().validate_for_api() promise = { ClientTypeEnum.USER_EMAIL: __register_user_by_email, ClientTypeEnum.USER_MINA: __register_user_by_mina } promise[form.type.data]() return Success()
def client_register(): # self.errors type验证 form = ClientForm().validate_for_api() # 方便多种类型用户注册 promise = {ClientTypeEnum.USER_MOBILE: _register_user_by_mobile} promise[form.type.data]() return Success(msg='注册成功')
def create_client(): """ 用户客户端注册 登录 参数 校验 接收参数 WTForms 参数校验 验证表单 表单和JSON对象提交过来的参数的区别: 表单-网页 json-移动端 客户端提交数据的方式有很多种 :return: """ # request.json # request.args.to_dict() data = request.json form = ClientForm(data=data) # 注意,json形式需要data=data if form.validate(): promise = {ClientTypeEnum.USER_EMAIL: __register_user_by_email} promise[form.type.data]() return 'success'