首頁  >  文章  >  後端開發  >  如何透過PHP和Vue產生員工考勤的加班申請流程

如何透過PHP和Vue產生員工考勤的加班申請流程

PHPz
PHPz原創
2023-09-24 09:41:02875瀏覽

如何透過PHP和Vue產生員工考勤的加班申請流程

如何透過PHP和Vue產生員工考勤的加班申請流程

#隨著工作節奏的加快和職場壓力的增大,加班成為了許多員工的常態。而規範和管理員工加班申請流程,既可以提高工作效率,又可以保護員工的權益。本文介紹如何使用PHP和Vue來產生員工考勤的加班申請流程。

步驟一:建立資料庫
首先,我們需要建立一個資料庫來儲存員工的考勤資訊和加班申請記錄。可以使用MySQL或其他資料庫管理系統來建立一個名為"attendance"的資料庫,並在該資料庫中建立兩個表:employees和overtime_requests。

員工表employees的架構如下:

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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn