30天搭建消防安全培训小程序

server/2025/3/25 21:58:53/

一、功能需求

搭建一款消防安全培训答题小程序,大体上实现功能如下:

1.重要消防相关信息发布提醒;

2.培训课程库播放,文档的,加视频的;

3.题库、考试单选、多选、判断三类题 ;

4.考试成绩查询、输出表单 ;

5.单次培训:限时内完成,签到(手签名),限时内完成考试;

二、项目结构

使用微信开发者工具创建一个新的小程序项目,项目结构大致如下:

pages
├── index           // 首页,显示重要信息提醒
├── course          // 培训课程库页面
├── exam            // 考试页面
├── result          // 考试成绩查询页面
├── signin          // 单次培训签到页面

三、关键代码

实现一个基本的消防安全培训答题小程序,包含重要信息发布、培训课程库、题库考试、成绩查询和单次培训签到等功能。以下是一个消防安全培训答题小程序的实现思路及部分代码:

app.json

{"pages": ["pages/index/index","pages/course/course","pages/exam/exam","pages/result/result","pages/signin/signin"],"window": {"backgroundTextStyle": "light","navigationBarBackgroundColor": "#fff","navigationBarTitleText": "消防安全培训答题小程序","navigationBarTextStyle": "black"}
}

pages/index/index.wxml

<view class="container"><view class="notice" wx:for="{{notices}}" wx:key="*this">{{item}}</view><button bindtap="goToCourse">进入培训课程库</button><button bindtap="goToSignin">参加单次培训</button>
</view>

pages/index/index.js

Page({data: {notices: ["近期将开展消防安全培训,请及时参加!"]},goToCourse() {wx.navigateTo({url: '/pages/course/course'});},goToSignin() {wx.navigateTo({url: '/pages/signin/signin'});}
});

pages/course/course.wxml

<view class="container"><view class="doc-item" wx:for="{{documents}}" wx:key="*this"><text>{{item.title}}</text><button bindtap="openDoc" data-url="{{item.url}}">查看文档</button></view><video src="{{videoUrl}}" controls></video>
</view>

pages/course/course.js

Page({data: {documents: [{ title: "消防知识手册", url: "https://example.com/fire_manual.pdf" },{ title: "消防应急预案", url: "https://example.com/fire_plan.pdf" }],videoUrl: "https://example.com/fire_video.mp4"},openDoc(e) {const url = e.currentTarget.dataset.url;wx.downloadFile({url: url,success: function (res) {const filePath = res.tempFilePath;wx.openDocument({filePath: filePath,success: function (res) {console.log('打开文档成功');}});}});}
});

pages/exam/exam.wxml

<view class="container"><view wx:for="{{questions}}" wx:key="index"><view>{{item.question}}</view><view wx:if="{{item.type === 'single'}}" wx:for="{{item.options}}" wx:key="*this"><radio-group bindchange="onSingleAnswerChange" data-index="{{index}}"><radio value="{{item}}">{{item}}</radio></radio-group></view><view wx:if="{{item.type === 'multiple'}}" wx:for="{{item.options}}" wx:key="*this"><checkbox-group bindchange="onMultipleAnswerChange" data-index="{{index}}"><checkbox value="{{item}}">{{item}}</checkbox></checkbox-group></view><view wx:if="{{item.type === 'judge'}}"><radio-group bindchange="onJudgeAnswerChange" data-index="{{index}}"><radio value="true">正确</radio><radio value="false">错误</radio></radio-group></view></view><button bindtap="submitExam">提交考试</button>
</view>

pages/exam/exam.js

Page({data: {questions: [{question: "以下哪种灭火器适用于扑灭电器火灾?",type: "single",options: ["泡沫灭火器", "二氧化碳灭火器", "水基型灭火器"],answer: "二氧化碳灭火器"},{question: "消防设施包括以下哪些?",type: "multiple",options: ["灭火器", "消火栓", "应急照明"],answer: ["灭火器", "消火栓", "应急照明"]},{question: "火灾发生时,应尽快乘坐电梯逃生。",type: "judge",answer: "false"}],userAnswers: []},onSingleAnswerChange(e) {const index = e.currentTarget.dataset.index;const answer = e.detail.value;const userAnswers = this.data.userAnswers;userAnswers[index] = answer;this.setData({userAnswers});},onMultipleAnswerChange(e) {const index = e.currentTarget.dataset.index;const answer = e.detail.value;const userAnswers = this.data.userAnswers;userAnswers[index] = answer;this.setData({userAnswers});},onJudgeAnswerChange(e) {const index = e.currentTarget.dataset.index;const answer = e.detail.value;const userAnswers = this.data.userAnswers;userAnswers[index] = answer;this.setData({userAnswers});},submitExam() {const { questions, userAnswers } = this.data;let score = 0;for (let i = 0; i < questions.length; i++) {const question = questions[i];const userAnswer = userAnswers[i];if (Array.isArray(question.answer)) {if (userAnswer.sort().toString() === question.answer.sort().toString()) {score++;}} else {if (userAnswer === question.answer) {score++;}}}wx.setStorageSync('examScore', score);wx.navigateTo({url: '/pages/result/result'});}
});

pages/result/result.wxml

<view class="container"><view>您的考试成绩是:{{score}} 分</view><button bindtap="exportResult">导出成绩表单</button>
</view>

pages/result/result.js

Page({data: {score: 0},onLoad() {const score = wx.getStorageSync('examScore');this.setData({score});},exportResult() {// 这里可以实现导出表单的逻辑,如生成 PDF 等wx.showToast({title: '导出功能待实现',icon: 'none'});}
});

pages/signin/signin.wxml 

<view class="container"><canvas id="signCanvas" style="width: 300px; height: 200px;"></canvas><button bindtap="saveSignature">保存签名</button><button bindtap="startExam">开始考试</button>
</view>

pages/signin/signin.js

Page({data: {signature: ''},onLoad() {const ctx = wx.createCanvasContext('signCanvas');this.setData({ctx});},saveSignature() {const ctx = this.data.ctx;ctx.draw(false, () => {wx.canvasToTempFilePath({canvasId: 'signCanvas',success: (res) => {const signature = res.tempFilePath;this.setData({signature});wx.showToast({title: '签名保存成功',icon: 'success'});}});});},startExam() {if (this.data.signature) {wx.navigateTo({url: '/pages/exam/exam'});} else {wx.showToast({title: '请先完成签名',icon: 'none'});}}
});


http://www.ppmy.cn/server/176972.html

相关文章

双指针算法-day15(分组循环)

1.股票平滑下跌阶段的数目 题目 解析 注意点&#xff1a;答案要加上数组长度&#xff0c;单个数字也满足要求&#xff1b; 代码 class Solution { public:long long getDescentPeriods(vector<int>& prices) {// 时间复杂度&#xff1a;O(n)// 空间复杂度&#x…

【leetcode hot 100 17】电话号码的字母组合

分析&#xff1a;当设计关键字“所有组合”时&#xff0c;要考虑深度优先遍历、广度优先遍历&#xff08;层次遍历&#xff09;&#xff0c;其中&#xff1a; 深度优先搜索&#xff1a; 自顶向下的递归实现深搜定义子问题在当前递归层结合子问题结果解决原问题 广度优先搜索 利…

【网络安全】从浏览器到服务端讲JavaScript安全

未经许可,不得转载。 原作者余弦,本文对原文进行二次创作。 文章目录 基本隐私收集XSS探针JSON 劫持内网浅渗透内网IP获取内网IP端口获取内网主机存活获取路由Web控制台操作内网脆弱Web应用控制突破浏览器边界XSS Virus攻击MongoDB数组绕过注入攻击MongoDB 查询条件绕过(Byp…

在C语言基础上学Java【Java】【一】

众所周知&#xff0c;Java是C风格的语言&#xff0c;对于学过C语言的人学Java可以快速适应。 废话不多说&#xff0c;直接边看代码边学。 数据类型&#xff0c;输入和输出 import java.util.Scanner;//为了使用Scanner public class a1 {//a1是类名&#xff0c;就是文件名&am…

Kubeasz工具快速部署K8Sv1.27版本集群(二进制方式)

文章目录 一、基本信息二、服务器初始化操作三、使用Kubeasz部署K8S集群四、验证集群 一、基本信息 1、部署需要满足前提条件&#xff1a; 注意1&#xff1a;确保各节点时区设置一致、时间同步&#xff1b;注意2&#xff1a;确保在干净的系统上开始安装&#xff1b;注意3&…

Linux 终端操作核心组合键

一、终端操作核心组合键 组合键功能描述使用场景示例Ctrl C强制终止当前正在运行的前台进程停止卡死的命令&#xff08;如 ping&#xff09;Ctrl D1. 发送 EOF&#xff08;文件结束符&#xff09; 2. 退出当前终端会话退出 bash 或 python 交互环境Ctrl Z暂停当前进程并放入…

Gitlab服务器数据迁移及版本升级

公司目前使用的GITLAB服务器&#xff0c;docker方式部署&#xff0c;GITLAB版本为13.11.0&#xff0c;由于版本太老存在安全漏洞&#xff0c;原服务器还部署了其他应用&#xff0c;不方便做升级操作&#xff0c;解决思路是将数据迁移新版本的gitlab服务器。 由于gitlab数据备份…

Word 小黑第40套

对应大猫43 主题 -浏览主题 -选择W样式标准文件就行 1级段落和2级段落&#xff08;用项目符号不影响原本段落文字符号 颜色修改为自动&#xff09; 整段变红的 不是把光标定位到红色字体那里 要选择几个红色字体 再创建样式 插入的空白页一定要是下一页&#xff0c;不能插空白…