Home >Web Front-end >uni-app >How uniapp application implements work log and task management

How uniapp application implements work log and task management

PHPz
PHPzOriginal
2023-10-20 18:37:591032browse

How uniapp application implements work log and task management

Uniapp (cross-platform application development framework) is an option for developing mobile applications. The work log and task management functions in Uniapp can be implemented using Vue.js and the backend API.

Overall idea: First, you need to design the database table structure, including user table, log table and task table. Then, build a back-end API to implement user authentication and log and task addition, deletion, modification and query functions. Then, interact with the back-end API through the Uniapp front-end page to implement specific functions.

The following is a specific code example of how to implement work log and task management functions.

  1. Design database table structure:

User tableuser

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
);

Log tablelog

CREATE TABLE `log` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `userId` int(11) NOT NULL,
  `content` text NOT NULL,
  `createTime` datetime NOT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON DELETE CASCADE
);

Task listtask

CREATE TABLE `task` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `userId` int(11) NOT NULL,
  `title` varchar(255) NOT NULL,
  `deadline` date NOT NULL,
  `status` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON DELETE CASCADE
);
  1. Build the back-end API:

After using Node.js and Express framework End API to realize user authentication and log and task addition, deletion, modification and query functions. The following is an API example for adding logs:

const express = require('express');
const bodyParser = require('body-parser');
const mysql = require('mysql');
const app = express();

app.use(bodyParser.json());

// 创建数据库连接
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'your_database_name'
});

// 添加日志
app.post('/api/log', (req, res) => {
  const { userId, content } = req.body;
  const createTime = new Date().toISOString().slice(0, 19).replace('T', ' ');
  const query = 'INSERT INTO log (userId, content, createTime) VALUES (?, ?, ?)';
  connection.query(query, [userId, content, createTime], (error, results) => {
    if (error) {
      console.log('Error:', error);
      res.status(500).json({ error: 'Failed to add log' });
    } else {
      res.json({ id: results.insertId });
    }
  });
});

// 其他API的实现类似,包括修改、删除和查询日志、任务等功能。

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});
  1. Uniapp front-end page implementation:

In the Uniapp page, use Vue.js to implement front-end interaction logic. The following is an example of adding logs and tasks:

<template>
  <div>
    <input v-model="logContent" type="text" placeholder="输入日志内容">
    <button @click="addLog">添加日志</button>
    <input v-model="taskTitle" type="text" placeholder="输入任务标题">
    <input v-model="taskDeadline" type="date" placeholder="选择任务截止日期">
    <button @click="addTask">添加任务</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      logContent: '',
      taskTitle: '',
      taskDeadline: ''
    };
  },
  methods: {
    addLog() {
      const userId = 1; // 当前登录的用户ID
      // 调用后端API添加日志
      uni.request({
        method: 'POST',
        url: 'http://localhost:3000/api/log',
        data: { userId: userId, content: this.logContent },
        success: (res) => {
          // 添加日志成功后的处理
          console.log('Add log success:', res);
        },
        fail: (err) => {
          console.error('Add log failed:', err);
        }
      });
    },
    addTask() {
      const userId = 1; // 当前登录的用户ID
      // 调用后端API添加任务
      uni.request({
        method: 'POST',
        url: 'http://localhost:3000/api/task',
        data: { userId: userId, title: this.taskTitle, deadline: this.taskDeadline },
        success: (res) => {
          // 添加任务成功后的处理
          console.log('Add task success:', res);
        },
        fail: (err) => {
          console.error('Add task failed:', err);
        }
      });
    }
  }
};
</script>

With the above code example, you can implement work log and task management functions in the Uniapp application. When the user enters content on the front-end page and clicks the button, the back-end API will be called to add data. You can further improve other functions according to business needs, such as deleting and modifying logs, tasks, etc.

Hope this article is helpful to you!

The above is the detailed content of How uniapp application implements work log and task management. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn