Exemplo n.º 1
0
def login():
    form = LoginForm().validate_for_api()
    user = manager.user_model.verify(form.username.data, form.password.data)
    # 此处不能用装饰器记录日志
    Log.create_log(
        message=f'{user.username}登陆成功获取了令牌',
        user_id=user.id, user_name=user.username,
        status_code=200, method='post', path='/cms/user/login',
        authority='无', commit=True
    )
    access_token, refresh_token = get_tokens(user)
    return json_res(access_token=access_token, refresh_token=refresh_token)
Exemplo n.º 2
0
def login_mini():
    form = LoginMiniForm().validate_for_api()
    appid = current_app.config.get('APP_ID')
    secret = current_app.config.get('APP_SECRET')
    code = form.code.data
    url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&js_code=' + code + \
          '&grant_type=authorization_code'
    res = requests.get(url)
    if 'openid' not in res.json().keys():
        return Failed('小程序用户异常')
    openid = res.json()['openid']
    user = manager.user_model.query.filter_by(openid=openid).first_or_404()
    # 此处不能用装饰器记录日志
    Log.create_log(
        message=f'{user.username}小程序登陆成功获取了令牌',
        user_id=user.id, user_name=user.username,
        status_code=200, method='post', path='/cms/user/login/mini',
        authority='无', commit=True
    )
    access_token, refresh_token = get_tokens(user)
    return json_res(access_token=access_token, refresh_token=refresh_token)