返回

前端工程师终极指南:使用Vue.js构建个人所得税计算器,兼容移动端

前端

前言

个人所得税是每个人都需要缴纳的一项重要税收,它关系到每个人的经济利益。随着我国经济的不断发展,个人的收入水平也在不断提高,这就导致了个人所得税的计算也变得越来越复杂。为了方便纳税人准确计算自己的所得税,前端工程师们可以使用Vue.js构建一个个人所得税计算器,它不仅可以准确计算个人的所得税,还兼容移动端,让用户随时随地都能轻松计算自己的税款。

使用Vue.js构建个人所得税计算器

1. 环境搭建

在开始构建个人所得税计算器之前,需要先搭建好开发环境。首先,需要安装Node.js和npm。Node.js是一个运行时环境,它可以运行JavaScript代码。npm是一个包管理工具,它可以帮助我们管理项目中使用的依赖包。

2. 项目初始化

安装好Node.js和npm之后,就可以开始初始化项目了。首先,创建一个新的项目目录,然后在该目录下运行以下命令:

npm init -y

这将创建一个名为package.json的文件,该文件包含了项目的基本信息。

3. 安装依赖包

接下来,我们需要安装项目中所需的依赖包。在package.json文件中,添加以下依赖包:

"vue": "^3.2.31",
"axios": "^0.27.2",
"vee-validate": "^4.5.0",

然后,运行以下命令安装这些依赖包:

npm install

4. 创建组件

安装好依赖包之后,就可以开始创建组件了。首先,创建一个名为App.vue的文件,该文件将作为项目的根组件。在App.vue文件中,添加以下代码:

<template>
  <div id="app">
    <h1>个人所得税计算器</h1>
    <form @submit.prevent="calculateTax">
      <div class="form-group">
        <label for="salary">工资收入</label>
        <input type="number" id="salary" v-model="salary">
      </div>
      <div class="form-group">
        <label for="bonus">奖金收入</label>
        <input type="number" id="bonus" v-model="bonus">
      </div>
      <div class="form-group">
        <label for="otherIncome">其他收入</label>
        <input type="number" id="otherIncome" v-model="otherIncome">
      </div>
      <div class="form-group">
        <label for="deductions">五险一金</label>
        <input type="number" id="deductions" v-model="deductions">
      </div>
      <button type="submit">计算</button>
    </form>
    <div v-if="tax">
      <h2>所得税:{{ tax }}</h2>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import { defineComponent, ref } from "vue";

export default defineComponent({
  setup() {
    const salary = ref(0);
    const bonus = ref(0);
    const otherIncome = ref(0);
    const deductions = ref(0);
    const tax = ref(0);

    const calculateTax = async () => {
      const response = await axios.post("/api/calculate-tax", {
        salary: salary.value,
        bonus: bonus.value,
        otherIncome: otherIncome.value,
        deductions: deductions.value,
      });
      tax.value = response.data.tax;
    };

    return {
      salary,
      bonus,
      otherIncome,
      deductions,
      tax,
      calculateTax,
    };
  },
});
</script>

<style>
#app {
  font-family: sans-serif;
}

.form-group {
  margin-bottom: 1rem;
}

label {
  display: block;
  margin-bottom: 0.5rem;
}

input[type="number"] {
  width: 100%;
  padding: 0.375rem 0.75rem;
  font-size: 1rem;
  line-height: 1.5;
  color: #495057;
  background-color: #fff;
  background-clip: padding-box;
  border: 1px solid #ced4da;
  border-radius: 0.25rem;
  transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}

input[type="number"]:focus {
  color: #495057;
  background-color: #fff;
  border-color: #80bdff;
  outline: 0;
  box-shadow: 0 0 0 0.2rem rgb(0 123 255 / 25%);
}

button {
  margin-top: 1rem;
  padding: 0.375rem 0.75rem;
  font-size: 1rem;
  line-height: 1.5;
  color: #fff;
  background-color: #0d6efd;
  border: 1px solid #0d6efd;
  border-radius: 0.25rem;
  transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}

button:hover {
  color: #fff;
  background-color: #0b5ed7;
  border-color: #0b5ed7;
}

button:focus {
  color: #fff;
  background-color: #0b5ed7;
  border-color: #0b5ed7;
  outline: 0;
  box-shadow: 0 0 0 0.2rem rgb(13 110 253 / 25%);
}

h2 {
  margin-top: 1rem;
}
</style>

在App.vue文件中,我们定义了根组件App,它包含了一个表单和一个结果展示区域。在表单中,用户可以输入自己的工资收入、奖金收入、其他收入和五险一金。在结果展示区域中,我们将展示计算出的所得税。

5. 创建API

接下来,我们需要创建一个API来处理所得税的计算。在项目根目录下,创建一个名为api的文件夹,然后在api文件夹中创建一个名为calculate-tax.js的文件。在calculate-tax.js文件中,添加以下代码:

const express = require("express");
const bodyParser = require("body-parser");

const app = express();

app.use(bodyParser.json());

app.post("/calculate-tax", async (req, res) => {
  const { salary, bonus, otherIncome, deductions } = req.body;

  // 计算应纳税所得额
  const taxableIncome = salary + bonus + otherIncome - deductions;

  // 计算所得税
  let tax = 0;
  if (taxableIncome <= 5000) {
    tax = 0;
  } else if (taxableIncome > 5000 && taxableIncome <= 8000) {
    tax = (taxableIncome - 5000) * 0.03;
  } else if (taxableIncome > 8000 && taxableIncome <= 17000) {
    tax = (taxableIncome - 8000) * 0.1 - 210;
  } else if (taxableIncome > 17000 && taxableIncome <= 30000) {
    tax = (taxableIncome - 17000) * 0.2 - 1410;
  } else if (taxableIncome > 30000 && taxableIncome <= 40000) {
    tax = (taxableIncome - 30000) * 0.25 - 2660;
  } else if (taxableIncome > 40000