Exemple #1
0
def Login(TheUsername, ThePassword):
	'''
	描述:管理员登录的数据库函数
	参数:用户名,密码
	返回:成功{result:success}
		 失败:{result:fail,reason:xxx,code:xxx}
		 成功还有session
	'''
	Success = True
	Reason = ""
	TheSession = ""
	Code = 0
	Return = {}
	print(TheUsername, ThePassword)
	if Success:
		try:
			TheAdmin = Admin.objects.get(Username = TheUsername)
		except:
			Success = False
			Reason = "帐号不存在!"
			Code = Constants.ERROR_CODE_NOT_FOUND
	print(Success)
	if Success:
		try:
			if check_password(ThePassword, TheAdmin.Password) != True:
				Success = False
				Reason = "密码不正确!"  
				Code = Constants.ERROR_CODE_NOT_FOUND
		except:
			Success = False
			Reason = "帐号不存在!"
			Code = Constants.ERROR_CODE_NOT_FOUND
	if Success:
		try:
			TheSession = GlobalFunctions.GenerateSessionID()
			TheAdmin.Session = TheSession
			TheAdmin.save()
		except:
			Success = False
			Reason = "登录失败!"
			Code = Constants.ERROR_CODE_UNKNOWN
	if Success:
		Return["result"] = "success"
		Return["session"] = TheSession
	else:
		Return["result"] = "fail"
		Return["reason"] = Reason
		Return["code"] = Code
	return Return
Exemple #2
0
def LoginUser(request):
    '''
    描述:处理登录请求
    参数:request
    成功返回:
    { 
    "result": "success",
    "session": "123456abcdef",
    "openId": "asfsfs"
    }
    失败返回:
    {"errid": 101, "errmsg": "身份信息不存在"}
    '''
    Success = True
    Return = {}
    Info = {}
    TheParam = {}
    ErrorId = Constants.UNDEFINED_NUMBER
    Reason = ""
    TheCode = ""
    TheSession = ""
    TheOpenID = ""
    TheSessionKey = ""
    if Success:
        try:
            TheCode = request.GET.get("code")
        except:
            Success = False
            ErrorId = Constants.ERROR_CODE_INVALID_PARAMETER
            Reason = "请求参数不合法!"
    if Success:
        try:
            TheParam = {}
            TheParam = GlobalFunctions.GetAppIDWechat()
            TheParam["js_code"] = TheCode
            TheParam["grant_type"] = "authorization_code"
        except:
            Success = False
            ErrorId = Constants.ERROR_CODE_INVALID_PARAMETER
            Reason = "请求参数不合法!"
    #换取openid 和 key
    #print(TheParam)
    if Success:
        try:
            TheRequest = requests.get(
                "https://api.weixin.qq.com/sns/jscode2session",
                params=TheParam,
                timeout=(5, 10),
                allow_redirects=True)
        except:
            Success = False
            ErrorId = Constants.ERROR_CODE_NETWORK_ERROR
            Reason = "网络繁忙,访问超时!"

    if Success:
        try:
            if TheRequest.status_code < 200 or TheRequest.status_code >= 400:
                Success = False
                ErrorId = Constants.ERROR_CODE_NETWORK_ERROR
                Reason = "网络繁忙,访问微信失败!!"
            TheJson = TheRequest.json()
            #print(TheJson)
            if "errcode" in TheJson:
                if TheJson["errcode"] == -1:
                    Success = False
                    ErrorId = Constants.ERROR_CODE_NETWORK_ERROR
                    Reason = "网络繁忙,访问微信失败!"
                elif TheJson["errcode"] == 40029:
                    Success = False
                    ErrorId = Constants.ERROR_CODE_INVALID_PARAMETER
                    Reason = "Code无效!"
                elif TheJson["errcode"] == 45011:
                    Success = False
                    ErrorId = Constants.ERROR_CODE_INVALID_CHANGE
                    Reason = "访问过于频繁,每个用户每分钟最多访问100次!"
                elif TheJson["errcode"] != 0:
                    Success = False
                    ErrorId = Constants.ERROR_CODE_NETWORK_ERROR
                    Reason = TheJson["errmsg"]
        except:
            Success = False
            ErrorId = Constants.ERROR_CODE_NETWORK_ERROR
            Reason = "网络繁忙,访问微信失败!"

    if Success:
        try:
            TheOpenID = TheJson["openid"]
            TheSessionKey = TheJson["session_key"]
        except:
            Success = False
            ErrorId = Constants.ERROR_CODE_NETWORK_ERROR
            Reason = "网络繁忙,访问微信失败!"

    if Success:
        try:
            TheSession = GlobalFunctions.GenerateSessionID()
        except:
            Success = False
            ErrorId = Constants.ERROR_CODE_UNKNOWN
            Reason = "生成session失败!"

    if Success:
        try:
            Return = UserManager.AddUserID(TheOpenID, TheSessionKey,
                                           TheSession)
        except:
            Success = False
            ErrorId = Constants.ERROR_CODE_UNKNOWN
            Reason = "访问数据库失败!"
    if Success == False:
        Return["errid"] = ErrorId
        Return["errmsg"] = Reason

    Response = JsonResponse(Return)
    if Success == True:
        Response.status_code = 200
    else:
        Response.status_code = 400
    return Response