如何通過PHP和Vue生成員工考勤的加班申請(qǐng)流程
隨著工作節(jié)奏的加快和職場(chǎng)壓力的增大,加班成為了很多員工的常態(tài)。而規(guī)范和管理員工加班申請(qǐng)流程,既可以提高工作效率,又可以保護(hù)員工的權(quán)益。本文介紹了如何使用PHP和Vue來生成員工考勤的加班申請(qǐng)流程。
步驟一:建立數(shù)據(jù)庫(kù)
首先,我們需要建立一個(gè)數(shù)據(jù)庫(kù)來存儲(chǔ)員工的考勤信息和加班申請(qǐng)記錄。可以使用MySQL或其他數(shù)據(jù)庫(kù)管理系統(tǒng)來創(chuàng)建一個(gè)名為”attendance”的數(shù)據(jù)庫(kù),并在該數(shù)據(jù)庫(kù)中創(chuàng)建兩個(gè)表:employees和overtime_requests。
員工表employees的結(jié)構(gòu)如下:
CREATE TABLE employees (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
department VARCHAR(50),
position VARCHAR(50)
);
登錄后復(fù)制
加班申請(qǐng)表overtime_requests的結(jié)構(gòu)如下:
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)
);
登錄后復(fù)制
步驟二:后端開發(fā)
接下來,我們使用PHP來處理后端邏輯。創(chuàng)建一個(gè)名為”overtime.php”的文件,用于處理加班申請(qǐng)相關(guān)的操作。下面是一個(gè)示例代碼:
<?php
// 連接數(shù)據(jù)庫(kù)
$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;
}
// 提交加班申請(qǐng)
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;
}
// 獲取加班申請(qǐng)列表
function getOvertimeRequests() {
global $connection;
$query = "SELECT * FROM overtime_requests";
$result = $connection->query($query);
$overtimeRequests = [];
while ($row = $result->fetch_assoc()) {
$overtimeRequests[] = $row;
}
return $overtimeRequests;
}
// 更新加班申請(qǐng)狀態(tài)
function updateOvertimeRequestStatus($requestId, $status) {
global $connection;
$query = "UPDATE overtime_requests SET status = '$status' WHERE id = '$requestId'";
$result = $connection->query($query);
return $result;
}
?>
登錄后復(fù)制
步驟三:前端開發(fā)
現(xiàn)在,我們使用Vue來處理前端交互和展示。創(chuàng)建一個(gè)名為”overtime.vue”的文件,用于處理加班申請(qǐng)的前端邏輯。下面是一個(gè)示例代碼:
<template>
<div>
<h2>加班申請(qǐng)</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">加班小時(shí)數(shù):</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">提交申請(qǐng)</button>
</form>
<h2>加班申請(qǐng)列表</h2>
<table>
<thead>
<tr>
<th>員工</th>
<th>加班日期</th>
<th>加班小時(shí)數(shù)</th>
<th>加班原因</th>
<th>狀態(tài)</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>
登錄后復(fù)制
步驟四:添加路由和界面
最后,我們需要在項(xiàng)目中添加路由和界面來展示加班申請(qǐng)流程。可以使用Vue Router來實(shí)現(xiàn)頁(yè)面的跳轉(zhuǎn)和顯示。
在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');
登錄后復(fù)制
現(xiàn)在,你可以在項(xiàng)目中使用以下代碼來展示加班申請(qǐng)流程界面:
<template>
<div id="app">
<router-view></router-view>
</div>
</template>
登錄后復(fù)制
至此,我們通過PHP和Vue生成了一個(gè)簡(jiǎn)單的員工考勤加班申請(qǐng)流程。通過以上代碼示例,你可以學(xué)習(xí)到如何使用PHP處理后端邏輯并與數(shù)據(jù)庫(kù)進(jìn)行交互,同時(shí)使用Vue處理前端交互和展示申請(qǐng)列表。在實(shí)際項(xiàng)目中,你可以進(jìn)一步完善該流程,添加更多功能和驗(yàn)證機(jī)制來滿足實(shí)際需求。
以上就是如何通過PHP和Vue生成員工考勤的加班申請(qǐng)流程的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!






