Example #1
0
 def authenticate(email, password):
     """
     用户登录,登录成功返回token,写将登录时间写入数据库;登录失败返回失败原因
     :param password:
     :return: json
     """
     userInfo = Users().getOne(
         {Users.email == email}, 'id desc',
         ('email', 'password', 'id', 'nick_name', 'head_img'))
     if (userInfo is None):
         return Utils.formatError(Code.BAD_REQUEST, '找不到用户')
     else:
         if (Users.check_password(userInfo['password'], password)):
             updated_at = int(time.time())
             Users().edit({'updated_at': updated_at},
                          {Users.email == email})
             token = UsersAuthJWT.encode_auth_token(userInfo['id'],
                                                    updated_at)
             userInfo.pop('password')  #删除密码
             return Utils.formatBody(
                 {
                     'token': token.decode(),
                     'user': userInfo
                 }, '登陆成功')
         else:
             return Utils.formatError(Code.BAD_REQUEST, '密码不正确')
 def authenticate(email, password):
     """
     用户登录,登录成功返回token,写将登录时间写入数据库;登录失败返回失败原因
     :param password:
     :return: json
     """
     filters = {
         Users.email == email
     }
     userInfo = Users().getOne(filters)
     userInfoPas = Users().getOne(filters, order = 'id desc', field = ('password',))
     if(userInfo is None):
         return BaseController().error('找不到用户')
     else:
         if (Users.check_password(userInfoPas['password'], password)):
             updated_at = int(time.time())
             Users.update(email, updated_at)
             token = UsersAuthJWT.encode_auth_token(userInfo['id'], updated_at)
             return BaseController().successData({'token': token.decode(), 'user': userInfo}, '登陆成功')
         else:
             return BaseController().error('密码不正确')