
如何通过PHP和Vue生成员工考勤的加班申请流程
随着工作节奏的加快和职场压力的增大,加班成为了很多员工的常态。而规范和管理员工加班申请流程,既可以提高工作效率,又可以保护员工的权益。本文介绍了如何使用PHP和Vue来生成员工考勤的加班申请流程。
步骤一:建立数据库
首先,我们需要建立一个数据库来存储员工的考勤信息和加班申请记录。可以使用MySQL或其他数据库管理系统来创建一个名为"attendance"的数据库,并在该数据库中创建两个表:employees和overtime_requests。
员工表employees的结构如下:
立即学习“PHP免费学习笔记(深入)”;
CREATE TABLE employees (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
department VARCHAR(50),
position VARCHAR(50)
);加班申请表overtime_requests的结构如下:
CREATE TABLE overtime_requests (
id INT PRIMARY KEY AUTO_INCREMENT,
employee_id INT,
overtime_date DATE,
overtime_hours INT,
reason VARCHAR(100),
status VARCHAR(20)
);步骤二:后端开发
接下来,我们使用PHP来处理后端逻辑。创建一个名为"overtime.php"的文件,用于处理加班申请相关的操作。下面是一个示例代码:
<?php
// 连接数据库
$connection = new mysqli("localhost", "username", "password", "attendance");
// 获取员工列表
function getEmployees() {
global $connection;
$query = "SELECT * FROM employees";
$result = $connection->query($query);
$employees = [];
while ($row = $result->fetch_assoc()) {
$employees[] = $row;
}
return $employees;
}
// 提交加班申请
function submitOvertimeRequest($employeeId, $overtimeDate, $overtimeHours, $reason) {
global $connection;
$query = "INSERT INTO overtime_requests (employee_id, overtime_date, overtime_hours, reason, status) VALUES ('$employeeId', '$overtimeDate', '$overtimeHours', '$reason', 'pending')";
$result = $connection->query($query);
return $result;
}
// 获取加班申请列表
function getOvertimeRequests() {
global $connection;
$query = "SELECT * FROM overtime_requests";
$result = $connection->query($query);
$overtimeRequests = [];
while ($row = $result->fetch_assoc()) {
$overtimeRequests[] = $row;
}
return $overtimeRequests;
}
// 更新加班申请状态
function updateOvertimeRequestStatus($requestId, $status) {
global $connection;
$query = "UPDATE overtime_requests SET status = '$status' WHERE id = '$requestId'";
$result = $connection->query($query);
return $result;
}
?>步骤三:前端开发
现在,我们使用Vue来处理前端交互和展示。创建一个名为"overtime.vue"的文件,用于处理加班申请的前端逻辑。下面是一个示例代码:
<template>
<div>
<h2>加班申请</h2>
<form @submit="submitRequest">
<label for="employee">员工:</label>
<select v-model="selectedEmployee" id="employee" required>
<option v-for="employee in employees" :value="employee.id">{{ employee.name }}</option>
</select>
<br>
<label for="date">加班日期:</label>
<input v-model="selectedDate" type="date" id="date" required>
<br>
<label for="hours">加班小时数:</label>
<input v-model="hours" type="number" id="hours" required>
<br>
<label for="reason">加班原因:</label>
<textarea v-model="reason" id="reason" required></textarea>
<br>
<button type="submit">提交申请</button>
</form>
<h2>加班申请列表</h2>
<table>
<thead>
<tr>
<th>员工</th>
<th>加班日期</th>
<th>加班小时数</th>
<th>加班原因</th>
<th>状态</th>
</tr>
</thead>
<tbody>
<tr v-for="request in requests" :key="request.id">
<td>{{ request.employee_id }}</td>
<td>{{ request.overtime_date }}</td>
<td>{{ request.overtime_hours }}</td>
<td>{{ request.reason }}</td>
<td>{{ request.status }}</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
import axios from 'axios';
export default {
data() {
return {
employees: [],
selectedEmployee: '',
selectedDate: '',
hours: 0,
reason: '',
requests: []
};
},
mounted() {
this.getEmployees();
this.getRequests();
},
methods: {
getEmployees() {
axios.get('overtime.php?action=getEmployees')
.then(response => {
this.employees = response.data;
})
.catch(error => {
console.error(error);
});
},
submitRequest() {
const data = {
employeeId: this.selectedEmployee,
overtimeDate: this.selectedDate,
overtimeHours: this.hours,
reason: this.reason
};
axios.post('overtime.php?action=submitRequest', data)
.then(response => {
this.getRequests();
this.clearForm();
})
.catch(error => {
console.error(error);
});
},
getRequests() {
axios.get('overtime.php?action=getRequests')
.then(response => {
this.requests = response.data;
})
.catch(error => {
console.error(error);
});
},
clearForm() {
this.selectedEmployee = '';
this.selectedDate = '';
this.hours = 0;
this.reason = '';
}
}
};
</script>步骤四:添加路由和界面
最后,我们需要在项目中添加路由和界面来展示加班申请流程。可以使用Vue Router来实现页面的跳转和显示。
在main.js文件中添加以下代码:
import Vue from 'vue';
import VueRouter from 'vue-router';
import Overtime from './components/Overtime.vue';
Vue.use(VueRouter);
const routes = [
{
path: '/',
name: 'overtime',
component: Overtime
}
];
const router = new VueRouter({
routes
});
new Vue({
router,
render: h => h(App)
}).$mount('#app');现在,你可以在项目中使用以下代码来展示加班申请流程界面:
<template>
<div id="app">
<router-view></router-view>
</div>
</template>至此,我们通过PHP和Vue生成了一个简单的员工考勤加班申请流程。通过以上代码示例,你可以学习到如何使用PHP处理后端逻辑并与数据库进行交互,同时使用Vue处理前端交互和展示申请列表。在实际项目中,你可以进一步完善该流程,添加更多功能和验证机制来满足实际需求。
以上就是如何通过PHP和Vue生成员工考勤的加班申请流程的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号