IT-例子 2024-03-07 17:03 采纳率: 0%
浏览 15

微信小程序+Nodejs+没有SQL数据库

微信小程序原生开发一个开学登记数据,数据添加到MySQL数据库,后端使用Nodejs开发;以及没有SQL数据库表创建,然后数据库表需要有姓名、性别、学号、班级、籍贯、身份证号等数据,然后小程序页面需要有一个登录页面;使用学号登录密码登录,学生表中需要有学号、密码即可;小程序页面登录进去就有自己的学号显示在文本框上并且不可修改,性别使用选择器:男、女;剩下的就需要学生自己手动输入了;

  • 写回答

5条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-03-07 17:03
    关注

    引自免费微信小程序:皆我百晓生

    当然,我会为您提供详细的解决方案。

    首先,在后端(Node.js)部分,由于您提到没有SQL数据库,我们可以考虑使用MongoDB这样的NoSQL数据库来替代。以下是如何安装MongoDB并创建一个简单的数据模型的步骤:

    1. 安装MongoDB:通过官方文档或包管理器(如npm或yarn)在服务器上安装MongoDB及其Node.js驱动程序mongoose。
    npm install mongoose
    
    1. 创建学生数据模型(Student.js):
    const mongoose = require('mongoose');
    const Schema = mongoose.Schema;
    
    const StudentSchema = new Schema({
      studentId: { type: String, required: true, unique: true },
      password: { type: String, required: true },
      name: { type: String, required: true },
      gender: { type: String, enum: ['男', '女'], required: true },
      class: { type: String, required: true },
      hometown: { type: String, required: true },
      idNumber: { type: String, required: true },
    });
    
    module.exports = mongoose.model('Student', StudentSchema);
    
    1. 创建后端API接口(server.js)以处理微信小程序发送的请求:
    const express = require('express');
    const bodyParser = require('body-parser');
    const mongoose = require('mongoose');
    const Student = require('./models/Student');
    
    mongoose.connect('mongodb://localhost/mydatabase', {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    });
    
    const app = express();
    app.use(bodyParser.json());
    
    // 登录API
    app.post('/login', async (req, res) => {
      const { studentId, password } = req.body;
      try {
        const student = await Student.findOne({ studentId, password });
        if (student) {
          res.status(200).json({ success: true, studentId });
        } else {
          res.status(401).json({ error: 'Invalid credentials' });
        }
      } catch (error) {
        res.status(500).json({ error: 'Internal Server Error' });
      }
    });
    
    // 注册API(添加新学生信息)
    app.post('/register', async (req, res) => {
      const { studentId, password, name, gender, class: clazz, hometown, idNumber } = req.body;
      try {
        const newStudent = new Student({ studentId, password, name, gender, class: clazz, hometown, idNumber });
        await newStudent.save();
        res.status(201).json({ success: true, studentId });
      } catch (error) {
        res.status(500).json({ error: 'Failed to register the student' });
      }
    });
    
    const PORT = process.env.PORT || 3000;
    app.listen(PORT, () => console.log(`Server is running on port ${PORT}`));
    

    对于前端微信小程序部分:

    1. 创建登录页面(pages/login/login.wxml):
    <view class="container">
      <input placeholder="请输入学号" type="text" bindinput="onInputStudentId" />
      <input placeholder="请输入密码" type="password" bindinput="onInputPassword" />
      <button bindtap="handleLogin">登录</button>
    </view>
    
    1. 对应的逻辑层(pages/login/login.js):
    Page({
      data: {
        studentId: '',
        password: '',
      },
      onInputStudentId(e) {
        this.setData({ studentId: e.detail.value });
      },
      onInputPassword(e) {
        this.setData({ password: e.detail.value });
      },
      async handleLogin() {
        const { studentId, password } = this.data;
        try {
          const response = await wx.request({
            url: 'https://your-server-url.com/login',
            method: 'POST',
            data: {
              studentId,
              password,
            },
          });
          if (response.statusCode === 200 && response.data.success) {
            wx.setStorageSync('studentId', response.data.studentId);
            wx.navigateTo({ url: '../profile/profile' });
          } else {
            wx.showToast({ title: '登录失败,请检查学号和密码', icon: 'none' });
          }
        } catch (err) {
          wx.showToast({ title: '网络错误,请稍后再试', icon: 'none' });
        }
      },
    });
    
    1. 在个人资料页面(pages/profile/profile.wxml)显示已登录的学号:
    <view class="container">
      <text>您的学号是:</text>
      <text>{{ studentId }}</text>
      <!-- 其他输入项... -->
    </view>
    
    1. 对应的逻辑层(pages/profile/profile.js),加载页面时获取缓存中的学号:
    Page({
      data: {
        studentId: '',
      },
      onLoad() {
        const cachedStudentId = wx.getStorageSync('studentId');
        if (cachedStudentId) {
          this.setData({ studentId: cachedStudentId });
        }
      },
      // 其他方法...
    });
    

    请注意替换上述代码中的https://your-server-url.com/为您的实际服务器地址。同时,为了保证安全性,务必对密码进行加密存储,并且在客户端和服务端实现必要的验证机制。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 3月7日

悬赏问题

  • ¥20 python爬虫遇到空文本取不出来
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络
  • ¥66 关于川崎机器人调速问题
  • ¥15 winFrom界面无法打开
  • ¥30 crossover21 ARM64版本安装软件问题
  • ¥15 mymetaobjecthandler没有进入