Exemplo n.º 1
0
 def auth(self, *args):
     '''验证用户身份和密码'''
     cmd_dic = args[0]
     remote_name = cmd_dic['username']
     remote_pass = cmd_dic['password']
     response_data = {}
     self.account_data = db_handler.file_db_handle(
         "select * from accounts where account=%s" % remote_name)
     if self.account_data:
         if self.account_data['password'] == remote_pass:
             exp_time_stamp = time.mktime(
                 time.strptime(self.account_data['expire_date'],
                               "%Y-%m-%d"))
             if time.time() > exp_time_stamp:
                 print("\033[31;1m账户 [%s] 已经过期,请联系管理员更新有效期!\033[0m" %
                       self.account_data['id'])
             else:  #通过了认证,返回用户的账户信息数据。
                 response_data = {
                     'res_type': 0,
                     'res_data': self.account_data
                 }
                 self.homedir = self.rootdir + '/' + self.account_data[
                     'home_dir']
         else:
             response_data = {'res_type': 1, 'res_data': "密码不正确!"}
             print("\033[31;1m密码不正确!\033[0m")
     else:
         response_data = {'res_type': 2, 'res_data': "用户ID不存在!"}
         print("\033[31;1m用户ID不存在!\033[0m")
     self.request.send(json.dumps(response_data).encode("utf-8"))
     print("send", json.dumps(response_data).encode("utf-8"))
Exemplo n.º 2
0
def acc_auth(account, password):
    '''
    判断输入的用户密码是否与数据库中的密码匹配的函数。


    输入参数:
        account: credit account number,用户输入的信用卡的账户
        password: credit card password,用户输入的信用卡的密码
    创建的变量:
        db_api, 把file_execute模块的内存地址赋值给db_api,用意是使用一套统一的‘语法’,覆盖后端各种数据库查询命令的差异。
        db,db_api,也就是file_execute处理完后,正常会返回一个数据库json文件,
        exp_time_stamp,账号的过期期限,此处转换为数值型,然后于当前时间,如果当前时间大于有效期时间,说明已经过期。
    特殊的方法:


    执行操作:
        接收到了用户输入的信用卡的账号密码。
        然后调用db_api,此处传给api是一个语句‘select * from accounts where account=1234’ 以及用户输入的账户。
        db_api仅仅是file_execute函数的别名。
        此处传入的语句,用途有两个:
            使处理函数知道这个是select,查询操作。
            把用户输入的账户id,传给处理函数,比如:用户输入的账号id:1234。
        然后db_api开始处理,主要是操作数据库json文件。
            如果账号存在,就返回账号的数据库json文件,并赋值给data变量。
            如果账号不存在,直接退出整个程序,不用返回。
        接下来,把数据库中的密码data['password']和用户输入的密码比较是否一致,如果一致,
        就判断账号是否已经过期。
            如果过期,就提示用户。
            如果没有,就说明了用户以及通过了认证,并且返回json数据。
    返回值:
        if passed the authentication , retun the account object, otherwise ,return None
        如果密码匹配并且没有过期,就返回data,即用户数据库json文件。
        如果密码错误或者过期,就打印错误提示。

    '''
    # db_api = db_handler.db_handler()
    # db = db_api("select * from accounts where account=%s" % account)
    data = db_handler.file_db_handle(
        "select * from accounts where account=%s" % account)

    #print(db)
    if data:
        if data['password'] == password:

            exp_time_stamp = time.mktime(
                time.strptime(data['expire_date'], "%Y-%m-%d"))
            if time.time() > exp_time_stamp:
                print(
                    "\033[31;1mAccount [%s] has expired,please contact the back to get a new card!\033[0m"
                    % account)
            else:  # passed the authentication
                return data
        else:
            print("\033[31;1mAccount ID or password is incorrect!\033[0m")
    else:
        print("\033[31;1mAccount ID or password is incorrect!\033[0m")
Exemplo n.º 3
0
def load_atm_balance(account_id):
    '''
    用于查询账户余额信息。
    return account balance and other basic info
    传入参数:
        account_id:用户的卡号
    创建参数:
        db:为该用户卡号的json数据库信息。
    处理动作:
        把file_execute函数重命名为db_api。
        把查询操作语句传递给db_api("select * from accounts where account=%s" % account_id),
        db_api会返回该用户卡号的json数据库信息。
    返回值:
        db:为该用户卡号的json数据库信息。
    '''
    data = db_handler.file_db_handle(
        "select * from accounts where account=%s in ATM" % account_id)
    return data
Exemplo n.º 4
0
def dump_atm_account(account_data):
    '''
    交易操作完成后,把更新后的用户账户json信息写入数据库。
    after updated transaction or account db , dump it back to file db
    传入参数:
        account_data:更新后的用户账户json信息
    创建参数:
        db_api,file_execute函数的重命名。
    执行的操作:
        运行db_api,也就是file_execute函数,并传入参数,数据库操作语句以及更新后的用户账户json信息。


    返回值:

    '''
    data = db_handler.file_db_handle(
        "update from accounts where account=%s in ATM" % account_data['id'],
        account_data=account_data)

    return True
Exemplo n.º 5
0
def buy_products(user_data):
    '''
    打印商品列表,提示用户选择商品,把商品加入购物车中。
    '''
    data = db_handler.file_db_handle("select * from product")
    while True:
        print("####以下为商品列表#####")
        print("项目\t商品名称\t\t单价")
        for index, item in enumerate(data):
            print("{}   {}\t{}".format(index, item[0], item[1]))
        user_choice = input("which product do you want?\n(press 'q' to quit)")
        if user_choice.isdigit():
            user_choice = int(user_choice)
            if user_choice < len(data) and user_choice >= 0:
                user_data['shopping_cart'].append(data[user_choice])
                print("{} has added to your cart...".format(data[user_choice]))
        elif user_choice == 'q':
            print('Back to Main Menu!')
            break
        else:
            print("Invalid input")