def make_transaction(log_obj, account_data, tran_type, amount, **kwargs): amount = float(amount) if tran_type in setting.TRANSACTION_TYPE: interest = amount * setting.TRANSACTION_TYPE[tran_type][ "interest"] #计算利息 old_balance = account_data["balance"] if setting.TRANSACTION_TYPE[tran_type]["action"] == "plus": new_balance = old_balance + amount + interest elif setting.TRANSACTION_TYPE[tran_type]["action"] == "minus": new_balance = old_balance - amount - interest if kwargs.get("re_account"): #转账用的语句。获取转账对方的账号 re_account_data = accounts.load_current_balance( kwargs.get("re_account")) re_account_balance = re_account_data["balance"] + amount re_account_data["balance"] = re_account_balance print("对方账户信息:%s" % re_account_data) accounts.dump_account(re_account_data) elif new_balance < 0: print( "\033[31;1m[%s]账户的信用余额不足以支付此次交易[-%s],你当前的余额是[%s]\033[0m" % (account_data['id'], amount + interest, old_balance)) return None account_data["balance"] = new_balance accounts.dump_account(account_data) #保存数据 log_obj.info("account:%s action:%s amount:%s interest:%s " % (account_data['id'], tran_type, amount, interest)) return account_data #返回最新数据 else: print("\033[31;1m交易类型【%s】 is not exist!!!\033[0m" % tran_type)
def sign_up(user_data): exist_flag = True while exist_flag is True: user = input("\033[32;1m注册用户ID>>>\033[0m:").strip() password = input("\033[32;1m用户密码>>>\033[0m:").strip() exist_flag = acc_check(user) if exist_flag: print("用户ID已经存在,请注册其他用户ID") exist_flag = True continue else: pay_day = 22 today = datetime.datetime.now().strftime('%Y-%m-%d') after_5_years = int(datetime.datetime.now().strftime('%Y')) + 10 after_5_years_today = datetime.datetime.now().replace(year=after_5_years) expire_day = (after_5_years_today + datetime.timedelta(-1)).strftime('%Y-%m-%d') account_data = {"enroll_date": today, "balance": 0, "password": password, "id": user, "status": 0, "expire_date": expire_day, "credit": 30000, "pay_day": pay_day} accounts.dump_account(account_data) user_data['is_authenticated'] = True user_data['user'] = user user_data['account_data'] = account_data print("\033[33;1m用户信息注册成功\033[0m".center(50, "-")) time.sleep(2) print("\n\033[32;1m欢迎来到购物商城,祝购物愉快~\033[0m\n") return True
def make_transaction(log_obj, acount_data, tran_type, amount, **others): amount = float(amount) if tran_type in settings.TRANSACTION_TYPE: interest = amount * settings.TRANSACTION_TYPE[tran_type]['interset'] old_balace = acount_data['balence'] if settings.TRANSACTION_TYPE[tran_type]['action'] == 'plus': new_balance = old_balace + amount + interest elif settings.TRANSACTION_TYPE[tran_type]['action'] == 'minus': new_balance = old_balace - amount - interest if new_balance < 0: print( '\033[31;1mYour credit [%s] is not enough for this transaction' % (acount_data['creait'])) return acount_data['balence'] = new_balance accounts.dump_account(acount_data) log_obj.info('account:%s\taction:%s\tamount:%s\tinterest:%s' % (acount_data['id'], tran_type, amount, interest)) return acount_data else: print('\033[31;1mTransaction type [%s] is not exist!\033[0m' % tran_type)
def charge_money(money, account_data): # input("\033[41;1m金额充值功能未实现,按任意键结束~\033[0m") # exit() exit_flag = False while exit_flag is not True: user_charge = input( "请确定是否充值金额\033[32;1m[y=yes]|[b=back]\033[0m>>>:").strip() if user_charge == "y": print("正在连接ATM信用卡中心,请稍后~") time.sleep(1.5) flag = False while not flag: charge_number = input( "请输入您的充值金额\033[32;1m[b=back]\033[0m>>>:").strip() if charge_number.isdigit(): charge_number = int(charge_number) old_balance = account_data['balance'] old_credit = account_data["credit"] new_balance = old_balance + charge_number new_credit = old_credit - charge_number account_data['balance'] = new_balance account_data["credit"] = new_credit accounts.dump_account(account_data) money = new_balance print("\033[32;1m充值成功,账号余额为%s元~\033[0m" % money) elif charge_number == "b": flag = True else: print("\033[31;1m输入错误,请输入数字\033[0m") continue elif user_charge == "b": exit_flag = True else: print("\033[31;1m输入错误,请输入数字\033[0m") return money
def make_transaction(log_obj, account_data, tran_type, amount, **others): ''' deal all the user transactions :param account_data: user account data :param tran_type: transaction type :param amount: transaction amount :param others: mainly for logging usage :return: ''' amount = float(amount) if tran_type in settings.TRANSACTION_TYPE: interest = amount * settings.TRANSACTION_TYPE[tran_type]['interest'] old_balance = account_data['balance'] if settings.TRANSACTION_TYPE[tran_type]['action'] == 'plus': new_balance = old_balance + amount + interest elif settings.TRANSACTION_TYPE[tran_type]['action'] == 'minus': new_balance = old_balance - amount - interest #check credit if new_balance < -account_data['credit']: print( '''\033[31;1mYour credit [%s] is not enough for this transaction [-%s], your current balance is [%s]''' % (account_data['credit'], (amount + interest), old_balance)) return account_data['balance'] = new_balance accounts.dump_account(account_data) #save the new balance back to file log_obj.info("account:%s action:%s amount:%s interest:%s" % (account_data['id'], tran_type, amount, interest)) return account_data else: print("\033[31;1mTransaction type [%s] is not exist!\033[0m" % tran_type)
def make_transaction(account_data, transaction_type, amount, log_obj, **kargs): amount = float(amount) if transaction_type in setting.TRANSACTION_TYPE: interest = amount * setting.TRANSACTION_TYPE[transaction_type][ 'interest'] old_balance = account_data['balance'] print(interest, old_balance) if setting.TRANSACTION_TYPE[transaction_type]['action'] == 'plus': new_balance = old_balance + amount + interest log_obj.info( 'Your account repay is %s,your account new balance is %s' % (amount, new_balance)) elif setting.TRANSACTION_TYPE[transaction_type]['action'] == 'minus': new_balance = old_balance - amount - interest log_obj.info( 'Your account withdraw %s,your account new balance is %s' % (amount, new_balance)) if new_balance < 0: print( 'Your Credit %s is not enough for transaction %s and Now your current balance is %s ' % (account_data['credit'], amount + interest, old_balance)) return account_data['balance'] = new_balance accounts.dump_account(account_data) return account_data else: print('Transaction is not exist')
def create_account(acc_data): flag = True while flag: id = input('Account_name:').strip() if len(id) == 0: print('Account name is empty.') continue password = input('Password:'******'Password cannot be empty.') continue pay_day = input('Pay day:').strip() if not pay_day.isdigit(): print('Pay day is invalid.') continue credit = input('Credit:').strip() if not credit.isdigit(): print('Credit is invalid.') continue acc_data['id'] = id acc_data['password'] = password acc_data['pay_day'] = int(pay_day) expire_date=datetime.now()+timedelta(days=1825) acc_data['expire_date'] = expire_date.strftime("%Y-%m-%d") acc_data['enroll_date'] = time.strftime("%Y-%m-%d",time.localtime()) acc_data['balance'] = int(credit) acc_data['credit'] = int(credit) accounts.dump_account(acc_data) management_logger.info('Account [%s] is created successfully.'%acc_data['id']) flag = False
def add_user(): """ 新建用户 :return: """ back_flag = True while back_flag: account_id = input("请输入卡id:") if len(account_id) > 0 and account_id.isdigit(): account_id = int(account_id) if not accounts.is_account(account_id): username = input("请输入用户名:") if len(username) > 0 and username.isalnum(): account_data = account_info(account_id, username) # 返回创建的用户信息 accounts.dump_account(account_data) back_flag = False else: print("用户名只能是数字和字母") else: print("卡号已存在") elif account_id == "b" or account_id == "back": # 返回功能 back_flag = False else: print("请合法输入卡号或者返回键b")
def sign_up(): """ :return: """ pay_day = 22 exist_flag = True while exist_flag is True: account = input("\033[32;1m请输入注册ID>>>\033[0m:").strip() password = input("\033[32;1m请输入注册密码>>>\033[0m:").strip() exist_flag = acc_check(account) if exist_flag: print("\033[31;1m%s用户ID也存在,请注册其他ID~\033[0m" % account) exist_flag = True continue else: today = datetime.datetime.now().strftime('%Y-%m-%d') after_5_years = int(datetime.datetime.now().strftime('%Y')) + 10 after_5_years_today = datetime.datetime.now().replace(year = after_5_years) expire_day = (after_5_years_today + datetime.timedelta(-1)).strftime('%Y-%m-%d') account_data = {"enroll_date": today, "balance": 20000, "password": password, "id": account, "credit": 20000, "status": 0, "expire_date": expire_day,"pay_day": pay_day} accounts.dump_account(account_data) print("用户添加成功,信息详情如下:") print (account_data) return True
def save(acc_data): """ 存钱 :param acc_data: :return: """ account_data = accounts.load_current_balance(acc_data['account_id']) current_balance = ''' --------- 信用卡信息 -------- Credit : %s Balance: %s ''' % (account_data['credit'], account_data['balance']) print(current_balance) back_flag = False while not back_flag: save_amount = input("请输入存款金额[b=back]>>>:").strip() if save_amount == 'b': back_flag = True elif len(save_amount) > 0 and save_amount.isdigit(): save_amount = int(save_amount) old_balance = account_data['balance'] new_balance = old_balance + save_amount # new_balance = transaction.make_transaction(trans_logger, account_data, 'save', save_amount) account_data['balance'] = new_balance accounts.dump_account(account_data) time.sleep(0.1) print("充值成功,账号余额为\033[32;1m%s\033[0m,重新登录后生效" % new_balance) return True # if new_balance: # # account_data['balance'] = new_balance # # accounts.dump_account(account_data) # # print('''\033[42;1mNew Balance:%s\033[0m''' % (new_balance['balance'])) # back_flag = True else: print('输入错误~')
def account_modify(): ''' 修改账户信息 :return: ''' retry_count = 0 items = ['enroll_date','password','id','credit','status','balance','expire_date','pay_day'] while True: account = input("输入要修改的账户ID>>>:") acc_data = check_account(account) # 判断用户可用性 db_path = db_handler.db_handler(settings.DATABASE) # settings.DATABASE = conn_parms account_file = "%s/%s.json" % (db_path, account) if acc_data: # Not None # 用户存在显示用户信息 display_account_info(acc_data) # print(type(acc_data)) while True: input_item = input("请选择您要修改的项目>>>:") input_value = input("请输入您要修改的值>>>:") # 修改账户信息 acc_data[input_item] = input_value account_data = acc_data print("\033[31;1m您已将账户ID为[%s] 中项目 [%s] 值修改为 [%s] !\033[0m" % (account,input_item,input_value)) # 修改数据写回用户ID数据文件 accounts.dump_account(account_data) return True else: print("您要修改的用户 [%s] 不存在!" % account) return True
def make_transaction(log_obj, account_data, tran_type, amount, **others): ''' deal all the user transactions :param account_data: user account data :param tran_type: transaction type :param amount: transaction amount :param others: mainly for logging usage :return: ''' amount = float(amount) if tran_type in settings.TRANSACTION_TYPE: #如果交易类型符合原始交易类型 interest = amount * settings.TRANSACTION_TYPE[tran_type][ 'interest'] #手续费=金额*利率 old_balance = account_data['balance'] #原始余额 if settings.TRANSACTION_TYPE[tran_type]['action'] == 'plus': #如果存钱 new_balance = old_balance + amount + interest #新余额=原始余额+存入的钱+手续费 elif settings.TRANSACTION_TYPE[tran_type]['action'] == 'minus': #如果取钱 new_balance = old_balance - amount - interest #新余额=原始余额-取出的钱-手续费 #check credit if new_balance < 0: #如果余额小于0,打印余额不足 print( '''\033[31;1mYour credit [%s] is not enough for this transaction [-%s], your current balance is [%s]''' % (account_data['credit'], (amount + interest), old_balance)) return account_data['balance'] = new_balance accounts.dump_account( account_data) #save the new balance back to file,最新数据存到账户文件里 log_obj.info( "account:%s action:%s amount:%s interest:%s" % # 记录交易日志 (account_data['id'], tran_type, amount, interest)) return account_data #返回最新余额 else: #如果不符合原始交易类型则提示交易类型不存在 print("\033[31;1mTransaction type [%s] is not exist!\033[0m" % tran_type)
def make_transaction(log_obj,account_data,trans_type,amount,**otherss): ''' 处理交易事务 :param log_obj: 日志对象 :param account_data: 账户信息 :param tran_type: 交易的类型 :param amount: 利息 :param otherss: :return: ''' amount = float(amount) if trans_type in settings.TRANSACTION_TYPE: interest = amount * settings.TRANSACTION_TYPE[trans_type]["interest"] old_balance = account_data["balance"] if settings.TRANSACTION_TYPE[trans_type]["action"] == "plus": new_balance = old_balance + amount + interest elif settings.TRANSACTION_TYPE[trans_type]["action"] == 'minus': new_balance = old_balance - amount - interest if new_balance < 0: print('''You credit [%s] is not enough for this transaction [-%s], your current balance is [%s]''' %(account_data['credit'],(amount + interest), old_balance)) return account_data['balance'] = new_balance accounts.dump_account(account_data) # save the new balance back to file log_obj.info("account:%s action:%s amount:%s interest:%s" % (account_data['id'], trans_type, amount, interest)) return account_data else: print("\033[31;1mTransaction type [%s] is not exist!\033[0m" % trans_type)
def make_transaction(log_obj,account_data,tran_type,amount,**others): ''' deal all the user transactions :param account_data: user account data :param tran_type: transaction type :param amount: transaction amount :param others: mainly for logging usage :return: ''' amount = float(amount) if tran_type in settings.TRANSACTION_TYPE: interest = amount * settings.TRANSACTION_TYPE[tran_type]['interest'] old_balance = account_data['balance'] if settings.TRANSACTION_TYPE[tran_type]['action'] == 'plus': new_balance = old_balance + amount + interest elif settings.TRANSACTION_TYPE[tran_type]['action'] == 'minus': new_balance = old_balance - amount - interest #check credit if new_balance <0: print('''\033[31;1mYour credit [%s] is not enough for this transaction [-%s], your current balance is [%s]''' %(account_data['credit'],(amount + interest), old_balance )) return account_data['balance'] = new_balance accounts.dump_account(account_data) #save the new balance back to file log_obj.info("account:%s action:%s amount:%s interest:%s" % (account_data['id'], tran_type, amount,interest) ) return account_data else: print("\033[31;1mTransaction type [%s] is not exist!\033[0m" % tran_type)
def add_account(): acc_dict = {} flag = True while flag: print('欢迎进入新用户注册界面'.center(50, '-')) acc_dict['name'] = input('请输入持卡人姓名: ') acc_dict['password'] = input('请输入密码: ') acc_dict['id'] = random.randint(1, 6), acc_dict['enroll_date'] = time.asctime() two_year_datetime = datetime.datetime.now() + datetime.timedelta( days=730) acc_dict['expire_date'] = time.ctime( time.mktime(two_year_datetime.timetuple())) acc_dict['status'] = 0 db_path = db_handler.db_handler(setting.DATABASE) if os.path.exists('%s.json' % os.path.join(db_path, acc_dict['id'])): print('用户名重复,请重新注册') continue else: accounts.dump_account(acc_dict) print('您注册的用户信息如下'.center(50, '-')) print(''' 持卡人姓名: %s 卡号: %d 注册时间: %s 过期时间: %s 用户状态: %d ''') % (acc_dict['name'], acc_dict['id'], acc_dict['enroll_date'], acc_dict['expire_date'], acc_dict['status']) return
def make_transaction(log_obj, account_data, tran_type, amount, **others): amount = float(amount) if tran_type in settings.TRANSACTION_TYPE: transaction = settings.TRANSACTION_TYPE[tran_type] interest = amount * transaction['interest'] old_balance = account_data['balance'] action = transaction['action'] if action == 'plus': new_balance = old_balance + amount + interest elif action == 'minus': new_balance = old_balance - amount - interest # check credit if new_balance < 0: print( '''\033[31;1m Your credit [%s] is not enough for this transaction [-%s], your current balance is [%s]''' % (account_data['credit'], (amount + interest), old_balance)) return account_data['balance'] = new_balance accounts.dump_account( account_data) # save the new balance back to file log_obj.info("accounts:%s action:%s amount:%s interest:%s" % (account_data['id'], tran_type, amount, interest)) return account_data else: print("\033[31;1mTransaction type [%s] is not exist!\033[0m" % tran_type)
def make_transaction(log_obj, acc_data, tran_type, amount, **others): if acc_data['status'] > 0: print("\033[31;1mAccount [%s] is frozen.\033[0m" % acc_data['id']) return amount = float(amount) if tran_type in settings.TRANSACTION_TYPE: interest = amount * settings.TRANSACTION_TYPE[tran_type]['interest'] old_balance = acc_data['balance'] if settings.TRANSACTION_TYPE[tran_type]['action'] == 'plus': new_balance = old_balance + amount + interest elif settings.TRANSACTION_TYPE[tran_type]['action'] == 'minus': new_balance = old_balance - amount - interest if new_balance < 0: print( '''\033[31;1mYour credit [%s] is not enough for this transaction [-%s], your current balance is [%s]''' % (acc_data['credit'], (amount + interest), old_balance)) return acc_data['balance'] = new_balance accounts.dump_account(acc_data) log_obj.info( "account:%s action:%s amount:%s interest:%s current_balance:%s" % (acc_data['id'], tran_type, amount, interest, acc_data['balance'])) return acc_data else: print("\033[31;1mTransaction type [%s] is not exist!\033[0m" % tran_type)
def make_transaction(account_data, tran_type, amount, *args, **others): ''' 处理用户账户交易的所有操作的函数。 deal all the user transactions 传递给make_transaction3个参数: :param account_data: user account db,用户的json数据库信息 :param tran_type: transaction type,交易操作类型 :param amount: transaction amount,交易操作的金额 :param others: mainly for logging usage,其它扩展用的参数组。 创建的变量: amount,把传入的交易的金额,变成浮点数并存入变量amount。 interest,本地交易操作的利息。 old_balance,账户余额。 new_balance,交易后的金额数。 执行的操作: 首先判断交易类型,是否为全局配置文件中的定义的交易类型的中的一种,分别有还款,提款,转账,消费。 如果交易类型存在,判断交易操作的方法, 如果操作的方法为plus,则把交易金额添加入账户余额中。 如果操作的方法为minus,则从交易金额中,减去交易金额以及利息。 如果减完后的金额为负数,就返回提示信息:”你当前的额度不足完成本次提款操作,你的余额为***“,退出此函数,并返回一个空值。 如果交易后的金额正常,就更新用户的json数据库中的余额信息。 然后通过accounts.dump_account(account_data)函数把更新后的json信息写入到json文件中。 并返回更新后的用户账户json信息。 如果交易类型不存在,就返回错误提示。 返回值: 如果正常运行,就返回交易后的用户账户json信息。 否则,返回一个空值。 ''' amount = round(float(amount), 2) if tran_type in settings.TRANSACTION_TYPE: interest = round( amount * settings.TRANSACTION_TYPE[tran_type]['interest'], 2) old_balance = account_data['balance'] if settings.TRANSACTION_TYPE[tran_type]['action'] == 'plus': new_balance = round(old_balance + amount + interest, 2) elif settings.TRANSACTION_TYPE[tran_type]['action'] == 'minus': new_balance = round(old_balance - amount - interest, 2) #check credit if new_balance < 0: print( '''\033[31;1mYour credit [%s] is not enough for this transaction [-%s], your current balance is [%s]''' % (account_data['credit'], (amount + interest), old_balance)) return None account_data['balance'] = new_balance accounts.dump_account(account_data) #save the new balance back to file # log_obj.info("account:%s action:%s amount:%s interest:%s" % # (account_data['id'], tran_type, amount,interest) ) return account_data else: print("\033[31;1mTransaction type [%s] is not exist!\033[0m" % tran_type)
def make_transaction(log_obj, account_data, trans_type, amount, **other): ''' deal all the user transaction :param log_obj :param account_data :param trans_type :param repay_amount :param other :return : None ''' amount = float(amount) if trans_type in setting.TRANSACTION_TYPE: interest = amount * setting.TRANSACTION_TYPE[trans_type]['interest'] old_balance = account_data['balance'] if setting.TRANSACTION_TYPE[trans_type]['action'] == 'plus': new_balance = old_balance + amount + interest elif setting.TRANSACTION_TYPE[trans_type]['action'] == 'minus': new_balance = old_balance - amount - interest if new_balance < 0: print( '\33[31;1mYour Credit [%s] is not enough for this transaction\033[0m' % account_data['id']) return account_data['balance'] = new_balance accounts.dump_account(account_data) log_obj.info('account: %s,action: %s,amount: %s,interest: %s' % (account_data['id'], trans_type, amount, interest))
def unlock_or_yes(account, flag): data = accounts.load_current_balance(account) if data["status"] == 1: data["status"] = flag accounts.dump_account(data) return flag else: print("该账户处于正常状态。")
def lock_or_not(account, flag): data = accounts.load_current_balance(account) if data["status"] == 1: print("该账户已经锁定!") else: data["status"] = flag accounts.dump_account(data) return flag
def mod_passwd(id): acc_data=accounts.load_account(id) password=input('Please input the new password:'******'Password cannot be empty.') else: acc_data['password'] = password accounts.dump_account(acc_data) management_logger.info('Account [%s] password is changed successfully'%acc_data['id'])
def mod_credit(id): acc_data = accounts.load_account(id) credit = input('Please input the new credit:') if credit.isdigit(): acc_data['credit']=credit accounts.dump_account(acc_data) management_logger.info('Account [%s] credit is changed successfully' % acc_data['id']) else: print("Invalid credit number.")
def modify(): """ 修改用户信息 :return: """ # 可以修改的项目 items = ["password", "credit", "status", "expire_date", "pay_day"] acc_data = False continue_flag = False while acc_data is False: account = input("\033[32;1maccount id:\033[0m").strip() account_data = acc_check(account) if account_data is False: # 用户不存在 print( "Account [\033[31;1m%s\033[0m] is not exist,try another account." % account) continue else: while continue_flag is not True: # 判断输入,要求为json格式 print('''You can choose the items like this: { "password": "******", "credit": 15000, "status": 0, "expire_date": "2021-01-01", "pay_day": 22 }''') modify_items = input( "Input modify items(\033[32;1mjson\033[0m):").strip() try: modify_items_dict = json.loads(modify_items) except Exception as e: print("\033[31;1mInput not a json data type!\033[0m") continue error_flag = False # 初始化错误标记 for index in modify_items_dict: if index in items: account_data[index] = modify_items_dict[index] # 修改 else: print( "Your input item [\033[31;1m%s\033[0m] is error!" % index) error_flag = True # 输入有错误 continue if error_flag: # 输入有错误,要求重新输入 continue accounts.dump_account(account_data) # 更新到数据库 print("\033[32;1mAccount infomation updated!\033[0m") continue_flag = True acc_data = True return True
def active_account(): mod_acc = input("请输入您要解除冻结的账户名:").strip() mod_acc_data = accounts.load_account(mod_acc) if mod_acc_data: mod_acc_data['status'] = 1 accounts.dump_account(mod_acc_data) logger.logger('operation', mod_acc, "账户 %s 被管理员解除冻结" % mod_acc) print("账户 %s 已成功解除冻结,现为普通用户!" % mod_acc) return else: print("账户 %s 不存在!" % mod_acc)
def freeze_account(): mod_acc = input("请输入您要冻结的账户名:").strip() mod_acc_data = accounts.load_account(mod_acc) if mod_acc_data: mod_acc_data['status'] = 2 accounts.dump_account(mod_acc_data) logger.logger('operation', mod_acc, "账户 %s 被管理员冻结" % mod_acc) print("账户 %s 已成功冻结!" % mod_acc) return else: print("账户 %s 不存在!" % mod_acc)
def mod_pay_day(id): acc_data = accounts.load_account(id) pay_day = input('Please input the new pay day:') if pay_day.isdigit(): if int(pay_day) > 0 and int(pay_day) < 31: acc_data['pay_day']=pay_day accounts.dump_account(acc_data) management_logger.info('Account [%s] pay day is changed successfully' % acc_data['id']) else: print('It must be a day number(1-30)') else: print('Invalid day number')
def change_user(): back_flag = True while back_flag: account_id = input("请输入卡id:") if len(account_id) > 0 and account_id.isdigit(): account_data = accounts.load_account(account_id) if account_data: menu = u''' ------ Change Menu -------- \033[32;1m 1. 账户名称 2. 账户密码 3. 账户额度 4. 账户状态 5. 账户角色 6. 返回 \033[0m''' menu_dict = { "1": "username", "2": "password", "3": "credit_limit", "4": "status", "5": "role", "6": "b", } exit_flag = True while exit_flag: print(menu) user_choice = input(">>:").strip() if user_choice in menu_dict: if user_choice == "6": # 退出修改 exit_flag = False else: # 目前修改未做任何限制 user_info = input("请输入修改后的信息:").strip() account_data[menu_dict[user_choice]] = user_info print(account_data) accounts.dump_account(account_data) # 写入修改后的数据 else: print("你选择的菜单有误,请重新选择") # username = input("请输入用户名:") # if len(username) > 0 and username.isalpha(): # account_data = account_info(account_id, username) # 返回创建的用户信息 # accounts.dump_account(account_data) # back_flag = False # else: # print("用户名只能是数字和字母") else: print("卡号不存在") elif account_id == "b" or account_id == "back": # 返回功能 back_flag = False else: print("请合法输入卡号或者返回键b")
def modify(): """ 修改用户信息的函数 :return: True """ "" items = ["password", "credit", "status", "expire_date", "pay_day"] # 可以修改项目的列表 acc_data = False continue_flag = False while acc_data is False: account = input("\033[32;1m用户名: \033[0m").strip() account_data = acc_check(account) if account_data is False: # 输入用户不存在 print("用户[\033[31;1m%s\033[0m]不存在,请输入其他用户名" % account) continue else: # 用户存在返回的是用户信息 while continue_flag is not True: print("可以修改的项目有: %s" % items) print( """修改格式例如: {"password": "******", "credit": 15000, "status": 0, "expire_date": "2021-01-01", "pay_day": 22}""" ) modify_items = input("\033[32;1m输入修改的选项: \033[0m").strip() try: modify_items_dict = json.loads( modify_items) # 使用json将字符串类型的字典转化成真正的字典 except Exception as e: print("\033[31;1m输入的内容是json不能支持的数据类型,请重新输入\033[0m") continue error_flag = False # 初始化错误标记 for index in modify_items_dict: # 取出所输入字典的key if index in items: # 所输入字典的key在可以修改项目的列表中 account_data[index] = modify_items_dict[index] # 修改 else: print("你输入的项目[\033[31;1m%s\033[0m]为不能修改的项目" % index) error_flag = True # 输入有错误 break if error_flag: # 输入有错误,要求重新输入 continue accounts.dump_account(account_data) # 更新到数据库 print("\033[32;1m用户信息更新成功!\033[0m") continue_flag = True acc_data = True return True
def acc_login(user_data, log_obj): ''' account login func :user_data: user info data , only saves in memory :return: ''' retry_count = 0 while user_data[ 'is_authenticated'] is not True and retry_count < 3: #判断user_data['is_authenticated']是否已经通过验证,并判断用户尝试次数 account = input("\033[32;1maccount:\033[0m").strip() '''缺少判断账户是否已经锁定''' account_data = accounts.load_current_balance(account) if account_data is False: print("\033[31;1m用户%s不存在\033[0m" % account) continue elif account_data['status'] >= 3: print("\033[31;1m用户%s已经锁定,请联系管理员!\033[0m" % account) return times = account_data['status'] while retry_count < 3: password = input("\033[32;1mpassword:\033[0m").strip() auth = acc_auth2(account, password) #调用acc_auth2验证用户输入的用户名和密码,返回用户数据data if auth: #not None means passed the authentication #如果auth获取到了字典 user_data['is_authenticated'] = True #将user_data改成已经完成验证 user_data[ 'account_id'] = account #将account_data存入user_data。省去查询时候调用 account_data['status'] = 0 #登录成功后,重置status accounts.dump_account(account_data) return auth #返回account_data 用户信息 times += 1 #用户尝试次数 '''缺少锁定账户''' account_data['status'] = times accounts.dump_account(account_data) if times == 3: log_obj.error("account [%s] too many login attempts" % account) return retry_count += 1 if retry_count == 3: log_obj.error("account [%s] too many login attempts" % account) # logger记录登录失败信息 else: #登录三次失败以后。 log_obj.error("account [%s] too many login attempts, is locked " % account) #logger记录登录用户已经锁定信息 exit()
def lock_auth(account): """ 锁定用户 :param account: :return: """ account_data = accounts.load_account(account) if account_data: if account_data['status'] == 2: # 已经被注销的用户无需锁定 return None else: account_data['status'] = 1 # 锁定该用户,1为锁定状态 # print(account_data) accounts.dump_account(account_data) # 将更新的数据写入文件 return account_data else: pass