1. 程式人生 > >裝飾器應用之登入

裝飾器應用之登入

# filename = "E:\\pythonStudy\\jingdong.txt"
# with open(filename,'a') as f:
    # f.writelines("thirteen:1234\n")
    # print("寫入成功!")

# filename = "E:\\pythonStudy\\weixin.txt"
# with open(filename,'a') as f:
    # f.writelines("thirteen2:1234\n")
    # print("寫入成功!")

login_stats = False
choice=""

content='''*******************************請 選 擇 :***********************************
        1:home 
        2:finance
        3:book
******************************************************************************'''

def chose_auth_type(auth_type="jingdong"):
    def login(function_name):
        def inner():
            global login_stats
            if login_stats is False:
                if input_information(auth_type):
                    print("登入成功!ovo")
                    login_stats = True
                    function_name()
                else:
                    print("登入失敗!")
                    print()
            else:
                function_name()
        return inner
    return login

@chose_auth_type("jingdong")
def home():
    print("Welcome to home page!")

@chose_auth_type("weixin")
def finance():#金融
    print("Welcome to finance page!")

@chose_auth_type()
def book():
    print("Welcome to book page!")

def input_information(auth_type):
    username=input("username:").strip()
    passwords=input("passwords:").strip()
    if auth_type == "jingdong":
        print("******************京東賬號登入中......*************************************")
        return auth_login(username,passwords,auth_type)
    elif auth_type == "weixin":
        print("******************微信賬號登入中......************************************")
        return auth_login(username,passwords,auth_type)

    
def auth_login(username,passwords,auth_type):
#get_information()返回來的值賦給user和password
    rest=get_information(auth_type)
    if username != "q" or passwords != "q":
        if username in rest.keys():
            if rest[username] == passwords:
                return True
            else:
                print()
                print('''*********************使用者名稱或密碼錯誤!請重試!*****************************''')
                input_information(auth_type)
        else:
            print()
            print('''*********************使用者名稱或密碼錯誤!請重試!*****************************''')
            input_information(auth_type)
    else:
        return False

def get_information(auth_type):#根據auth_type引數,從不同的檔案中讀入使用者名稱和密碼
    rest={}
    filename = "E:\\pythonStudy\\"+auth_type+".txt"
    with open(filename) as f:
        for line in f:
            (key, value) = line.strip().split(':')
    #將讀取到的一行資訊通過“:”分割成為一個列表,之後對應賦值給key,value兩個變數
            rest[key] = value
        return rest

while(True):
    print(content)
    choice=input(">>>:")
    if choice == "q":
        print("歡迎再來!=*v*=")
        break
    elif choice == "1":
        home()
    elif choice == "2":
        finance()
    elif choice == "3":
        book()
    else :
        print("輸入錯誤!")
        continue