Exemple #1
0
def transfer():
    """
    用户转账功能函数
    :return:
    """
    target = input('请输入收款方姓名:').strip()
    source_data = user_data[0]
    target_data = db_handler.load_db(target)
    source_credit = source_data['credit']  # 原账户额度
    target_credit = target_data['credit']  # 对方账户额度
    amount = input('请输入要转账的金额:').strip()
    if amount.isdigit():
        amount = int(amount)
        if amount <= source_credit:
            amount = int(amount)
            target_data['credit'] = target_credit + amount
            db_handler.save_db(source_data['id'], source_data)
            db_handler.save_db(target, target_data)
            # 流水记录
            cost_record('transfer to %s' % target, amount, source_data)
            # 日志记录
            logger.logger.info('{0} transfer to {1} ${2}'.format(
                source_data['id'], target, amount))
            print('转账成功!')
        else:
            print('\033[31;1m 您的额度不够!无法完成转账。\033[0m')
    else:
        print('\033[31;1m Inval values! \033[0m')
Exemple #2
0
def cost_record(*args, **kwargs):
    """
    用户记录流水函数, 涉及到记录流水的,数据都会留到此函数最后才更改用户数据 save_db
    :param args:
    :param kwargs:
    :return:
    """
    record = '{0} <{1}>  $:{2}'.format(time.ctime(), args[0], args[1])
    data = args[2]
    data['cost_record'].append(record)  # 把流水记录加到用户数据文件json里面
    db_handler.save_db(data['id'], data)
def block_user():
    """
    manager冻结账户
    :return:
    """
    username = input('username >>:').strip()
    user_data = db_handler.load_db(username)
    if user_data['status'] == 0:  # 判断block之前用户是否已经被block
        user_data['status'] = 1
        db_handler.save_db(user_data['id'], user_data)  # 写入用户json文件
        # 记录数据库
        logger.ad_logger.info('account %s is blockd!' % username)
        print('账户%s 冻结成功!' % username)
def unfreeze():
    """解冻账户"""
    account = input("\033[32;1m Please input account:\033[0m")
    all_account_list = db_handler.find_all_account()
    if account not in all_account_list:
        utils.print_error('No account [%s]' % account)
    else:
        account_data = db_handler.load_account_data(account)['data']
        account_data['status'] = 0  # 不判断,设置状态为正常
        db_handler.save_db(account_data)
        # 日志
        access_logger = logger('access')
        access_logger.warning('unfreeze %s successful!' % account)
        print('\033[32;1maccount [%s] unfreeze successfull\033[0m' % account)
def freeze():
    """冻结账户(不允许登录)"""
    account = input("\033[32;1m Please input freeze account:\033[0m")
    all_account_list = db_handler.find_all_account()
    if account not in all_account_list:
        utils.print_error("%s is not in account list!" % account)
    else:
        account_data = db_handler.load_account_data(account)['data']
        account_data['status'] = 1  # 这个地方待定(locked)
        db_handler.save_db(account_data)
        # 日志
        access_logger = logger('access')
        access_logger.warning('freeze %s successful!' % account)
        print('\033[32;1maccount [%s] freeze successfull\033[0m' % account)
def reset_passwd():
    """重置密码"""
    account = input(
        "\033[32;1m Please input the account which need to reset passwd:\033[0m"
    )
    all_account_list = db_handler.find_all_account()
    if account not in all_account_list:
        utils.print_error('[%s] has not been registered yet!')
    else:
        account_data = db_handler.load_account_data(account)['data']
        # {'status': 0, 'data': {'id': '1001', 'password': '******', 'credit': 15000, 'balance': 15000, 'enroll_date': '2018-03-10', 'expire_date': '2020-12-04', 'pay_day': 22, 'status': 0}}
        account_data['password'] = account_sample.acc_dic['password']
        db_handler.save_db(account_data)
        # 日志
        access_logger = logger('access')
        access_logger.warning('%s reset password successful!' % account)
        print('\033[32;1maccount [%s] reset password successfull\033[0m' %
              account)
def credit_modify():
    """
    manager修改用户额度
    :return:
    """
    username = input('username >>:').strip()
    user_data = db_handler.load_db(username)
    old_credit = user_data['credit']
    new_credit = input('目前用户额度为 %s , 新额度:' % old_credit)
    if new_credit.isdigit():
        new_credit = int(new_credit)
        user_data['credit'] = new_credit
        db_handler.save_db(user_data['id'], user_data)  # 写入用户json文件
        # 记录数据库
        logger.ad_logger.info('%s credit modify : %s.' % (username, new_credit))
        print('修改成功,目前用户{0}额度为{1}'.format(user_data['id'], user_data['credit']))
    else:
        print('Invalid value!')
def make_transaction(logger, user_obj, tran_type, amount, **kwargs):
    """
    处理所有的交易
    :param account_data: 用户账户数据
    :param tran_type: 交易类型
    :param amount: 交易数量
    :param kwargs: mainly for logging usage
    :return: 
    """
    amount = float(amount)
    if tran_type in settings.TRANSACTION_TYPE:

        interest = amount * settings.TRANSACTION_TYPE[tran_type][
            'interest']  # 利息interest:amount * 0.05
        old_balance = user_obj['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]''' %
                    (user_obj['data']['credit'],
                     (amount + interest), old_balance))
                return {'status': 1, 'error': '交易失败,余额不足'}

        user_obj['data']['balance'] = new_balance  # 把新余额存到用户内存账户数据里
        save_db(user_obj['data'])  # 数据要同时更新到硬盘账户文件中

        logger.info(
            "account:%s   action:%s   amount:%s  interest:%s   balance:%s" %
            (user_obj['data']['id'], tran_type, amount, interest, new_balance))
        return {'status': 0, 'msg': '交易操作成功'}
    else:
        print("\033[31;1mTransaction type [%s] is not exist!\033[0m" %
              tran_type)
        return {
            'status': 1,
            'error': '交易失败,Transaction type [%s] is not exist!' % tran_type
        }