js+html实现银行流水生成器,逻辑部分代码分享哈~~~

150 阅读2分钟

下载地址:www.pan38.com/yun/share.p… 提取密码:1133

我们直接看框架

然后我们就是这个框架给人家定制的,花了2000多,我自己虽然有技术但是这种涉及到数据计算的还是开发不来,太复杂了,生成表格没问题,下面直接分享源码哈~~~

class BankStatementGenerator {
  constructor() {
    this.transactions = [];
    this.balance = 0;
    this.accountNumber = this._generateAccountNumber();
    this.accountName = this._generateRandomName();
    this.currencies = ['CNY', 'USD', 'EUR', 'JPY'];
    this.transactionTypes = ['存款', '取款', '转账收入', '转账支出', '利息', '手续费'];
    this.counterparties = ['支付宝', '微信支付', '工资收入', '京东商城', '美团外卖', '滴滴出行'];
  }

  // 生成随机账户号
  _generateAccountNumber() {
    return '62' + Array.from({length: 17}, () => Math.floor(Math.random() * 10)).join('');
  }

  // 生成随机姓名
  _generateRandomName() {
    const surnames = ['张', '王', '李', '赵', '刘', '陈', '杨', '黄'];
    const givenNames = ['伟', '芳', '娜', '秀英', '敏', '静', '丽', '强'];
    return surnames[Math.floor(Math.random() * surnames.length)] + 
           givenNames[Math.floor(Math.random() * givenNames.length)];
  }

  // 生成随机日期
  _generateRandomDate(startDate, endDate) {
    const start = new Date(startDate).getTime();
    const end = new Date(endDate).getTime();
    return new Date(start + Math.random() * (end - start));
  }

  // 生成单笔交易记录
  generateTransaction() {
    const type = this.transactionTypes[Math.floor(Math.random() * this.transactionTypes.length)];
    const amount = parseFloat((Math.random() * 10000).toFixed(2));
    const currency = this.currencies[Math.floor(Math.random() * this.currencies.length)];
    const date = this._generateRandomDate('2024-01-01', '2025-07-11');
    const counterparty = this.counterparties[Math.floor(Math.random() * this.counterparties.length)];
    const remark = `${type} - ${counterparty}`;
    
    // 计算余额
    if (type === '存款' || type === '转账收入' || type === '利息') {
      this.balance += amount;
    } else {
      this.balance -= amount;
    }

    const transaction = {
      date: date.toISOString().split('T')[0],
      type,
      amount,
      currency,
      balance: parseFloat(this.balance.toFixed(2)),
      counterparty,
      remark
    };

    this.transactions.push(transaction);
    return transaction;
  }

  // 批量生成交易记录
  generateTransactions(count) {
    this.transactions = [];
    this.balance = 10000; // 初始余额
    for (let i = 0; i < count; i++) {
      this.generateTransaction();
    }
    return this.transactions;
  }

  // 按月份筛选交易记录
  filterByMonth(year, month) {
    return this.transactions.filter(t => {
      const [y, m] = t.date.split('-');
      return y === year.toString() && m === month.toString().padStart(2, '0');
    });
  }

  // 按交易类型筛选
  filterByType(type) {
    return this.transactions.filter(t => t.type === type);
  }

  // 导出为CSV格式
  exportToCSV() {
    const headers = ['日期', '交易类型', '金额', '币种', '余额', '交易对手', '备注'];
    const rows = this.transactions.map(t => [
      t.date,
      t.type,
      t.amount,
      t.currency,
      t.balance,
      t.counterparty,
      t.remark
    ]);
    
    let csvContent = headers.join(',') + '\n';
    rows.forEach(row => {
      csvContent += row.join(',') + '\n';
    });
    
    return csvContent;
  }

  // 获取统计信息
  getStatistics() {
    const stats = {
      totalTransactions: this.transactions.length,
      totalIncome: 0,
      totalExpense: 0,
      startBalance: this.transactions.length > 0 ? this.transactions[0].balance - this.transactions[0].amount : 0,
      endBalance: this.transactions.length > 0 ? this.transactions[this.transactions.length - 1].balance : 0,
      byType: {},
      byCurrency: {}
    };

    this.transactions.forEach(t => {
      if (t.type === '存款' || t.type === '转账收入' || t.type === '利息') {
        stats.totalIncome += t.amount;
      } else {
        stats.totalExpense += t.amount;
      }

      // 按类型统计
      if (!stats.byType[t.type]) {
        stats.byType[t.type] = { count: 0, amount: 0 };
      }
      stats.byType[t.type].count++;
      stats.byType[t.type].amount += t.amount;

      // 按币种统计
      if (!stats.byCurrency[t.currency]) {
        stats.byCurrency[t.currency] = { count: 0, amount: 0 };
      }
      stats.byCurrency[t.currency].count++;
      stats.byCurrency[t.currency].amount += t.amount;
    });

    return stats;
  }
}

// 使用示例
const generator = new BankStatementGenerator();
generator.generateTransactions(100); // 生成100条交易记录
console.log(generator.transactions); // 查看所有交易记录
console.log(generator.getStatistics()); // 查看统计信息
console.log(generator.exportToCSV()); // 获取CSV格式数据