コード例 #1
0
 def __init__(self, *args, **kwargs):
     HttpClient.__init__(self, *args, **kwargs)
     self.default_headers = {
         'Connection': 'keep-alive',
         'Content-Type': 'application/json'
     }
     self.version = "v2"
コード例 #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)