Пример #1
0
 def post(self, request, *args, **kwargs):
     """
     用户登录接口
     ---
     parameters:
         - name: nick_name
           description: 用户名
           required: true
           type: string
           paramType: form
         - name: password
           description: 密码
           required: true
           type: string
           paramType: form
     """
     nick_name = request.POST.get("nick_name", None)
     password = request.POST.get("password", None)
     try:
         if not nick_name:
             code = 400
             result = general_message(code, "username is missing", "请填写用户名")
             return Response(result, status=code)
         elif not password:
             code = 400
             result = general_message(code, "password is missing", "请填写密码")
             return Response(result, status=code)
         user, msg, code = user_svc.is_exist(nick_name, password)
         if not user:
             code = 400
             result = general_message(code, "authorization fail ", msg)
             return Response(result, status=code)
         serializer = self.get_serializer(data=request.data)
         if serializer.is_valid():
             user = serializer.object.get('user') or request.user
             token = serializer.object.get('token')
             response_data = jwt_response_payload_handler(
                 token, user, request)
             result = general_message(200,
                                      "login success",
                                      "登录成功",
                                      bean=response_data)
             response = Response(result)
             if api_settings.JWT_AUTH_COOKIE:
                 expiration = (datetime.datetime.now() +
                               datetime.timedelta(days=30))
                 response.set_cookie(api_settings.JWT_AUTH_COOKIE,
                                     token,
                                     expires=expiration)
             jwt_manager = JwtManager()
             jwt_manager.set(response_data["token"], user.user_id)
             return response
         result = general_message(
             400, "login failed",
             "{}".format(list(dict(serializer.errors).values())[0][0]))
         return Response(result, status=status.HTTP_400_BAD_REQUEST)
     except Exception as e:
         logging.exception(e)
         result = error_message()
         return Response(result, status=500)
Пример #2
0
 def post(self, request):
     """
     用户登录接口
     ---
     parameters:
         - name: nick_name
           description: 用户名
           required: true
           type: string
           paramType: form
         - name: password
           description: 密码
           required: true
           type: string
           paramType: form
     """
     user_name = request.POST.get("nick_name", None)
     raw_passwd = request.POST.get("password", None)
     try:
         if not user_name or not raw_passwd:
             code = 405
             result = general_message(code, "username is missing", "请填写用户名")
             return Response(result, status=code)
         elif not raw_passwd:
             code = 405
             result = general_message(code, "password is missing", "请填写密码")
             return Response(result, status=code)
         user, msg, code = user_svc.is_exist(user_name, raw_passwd)
         if not user:
             code = 400
             result = general_message(code, "authorization fail ", msg)
             return Response(result, status=code)
         else:
             u = authenticate(username=user_name, password=raw_passwd)
             http_client = HttpClient()
             url = "http://" + request.get_host() + '/console/api-token-auth'
             default_headers = {'Connection': 'keep-alive', 'Content-Type': 'application/json'}
             data = {"nick_name": user_name, "password": raw_passwd}
             res, body = http_client._post(url, default_headers, json.dumps(data))
             if res.get("status", 400) != 200:
                 code = 400
                 result = general_message(code, "login failed", "登录失败")
                 return Response(result, status=code)
             logger.debug("res {0} body {1}".format(res, body))
             token = body.get("token", "")
             data = {'token': token}
             login(request, u)
             code = 200
             result = general_message(code, "login success", "登录成功", bean=data)
     except Exception as e:
         logger.exception(e)
         code = 500
         result = error_message(e.message)
     return Response(result, status=code)