微信小程序投票管理系统:打造智能、便捷的投票体验

前言

随着社交网络的兴起和移动互联网的普及,人们对于参与和表达意见的需求越来越强烈。在这个背景下,微信小程序投票管理系统应运而生。它为用户提供了一个智能、便捷的投票平台,使用户可以轻松创建和参与各种类型的投票活动。本文将详细介绍微信小程序投票管理系统的设计与功能,并探讨其在实际应用中的优势和耐人寻味之处。

投票管理的实现

实现流程

1.用户进入后显示投票页面

2.用户可以选择自己想投票的选项并进行投票

3.投票完成后显示总的投票结果以及投票数量

4.限制每个用户每天投票只能进行一次

用例图演示

用户在系统内可以登陆,选择投票对象、进行投票、查看投票等等。

 数据表

 

 总体设计

 投票管理系统后端

mapper

package com.ctb.minoa.mapper;import com.ctb.minoa.model.Voteinfo;public interface VoteinfoMapper {int deleteByPrimaryKey(int id);int insert(Voteinfo record);int insertSelective(Voteinfo record);Voteinfo selectByPrimaryKey(int id);int updateByPrimaryKeySelective(Voteinfo record);int updateByPrimaryKey(Voteinfo record);
}

controller

/*** @Autho biao**/
@RestController
@RequestMapping("/wx/vote")
public class VoteinfoController {@Autowiredprivate VoteinfoMapper voteinfoMapper;@RequestMapping("/index")public Object index(Voteinfo voteinfo) {Voteinfo voteinfo1 = voteinfoMapper.selectByPrimaryKey(3);Map<Object, Object> data = new HashMap<Object, Object>();data.put("voteinfoList",voteinfo1);return ResponseUtil.ok(data);}
}

工具类getopenid---获取用户的openid

package util;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;/*** biao*拼接用户信息对官方进行用户的openid进行查询**/
public class getopenid {public static String getOpenid(String url) {String result = "";BufferedReader in = null;try {String urlNameString = url;URL realUrl = new URL(urlNameString);// 打开和URL之间的连接URLConnection connection = realUrl.openConnection();// 设置通用的请求属性connection.setRequestProperty("accept", "*/*");connection.setRequestProperty("connection", "Keep-Alive");connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");// 建立实际的连接connection.connect();// 获取所有响应头字段Map<String, List<String>> map = connection.getHeaderFields();// 遍历所有的响应头字段// 定义 BufferedReader输入流来读取URL的响应in = new BufferedReader(new InputStreamReader(connection.getInputStream()));String line;while ((line = in.readLine()) != null) {result += line;}} catch (Exception e) {System.out.println("发送GET请求出现异常!" + e);e.printStackTrace();}// 使用finally块来关闭输入流finally {try {if (in != null) {in.close();}} catch (Exception e2) {e2.printStackTrace();}}return result;}public static String jointStr(String code) {String result = "https://api.weixin.qq.com/sns/jscode2session?appid=wxf55dc8be7c3885ab&secret=05e3ac3badee5c088ff97b5f3cac3974&js_code=";String rigth = "&grant_type=authorization_code";return result + code + rigth;}public static String outopenid(String code){String json = getOpenid(jointStr(code));JSONObject jsonObj = JSON.parseObject(json);System.out.println("openid:"+jsonObj.getString("openid"));return jsonObj.getString("openid");}}

解释:

  1. 该类包含三个静态方法:getOpenid()jointStr()outopenid()
  2. getOpenid(String url)方法:

    • 参数:一个字符串类型的URL。
    • 返回值:一个字符串类型的结果。
    • 功能:通过发送GET请求到指定的URL,并从响应中解析出openid。
      • 首先,创建一个URL对象,表示要连接的URL。
      • 然后,使用openConnection()方法打开与URL之间的连接。
      • 设置通用的请求属性,如"accept"、"connection"和"user-agent"。
      • 建立实际的连接。
      • 获取所有响应头字段。
      • 定义一个BufferedReader输入流来读取URL的响应。
      • 逐行读取响应内容,并将其拼接到结果字符串中。
      • 关闭输入流。
      • 返回结果字符串。
  3. jointStr(String code)方法:

    • 参数:一个字符串类型的code。
    • 返回值:一个字符串类型的拼接后的URL。
    • 功能:根据给定的code和appid和密钥拼接成一个完整的URL,用于向微信服务器发送请求以获取用户的openid。
      • 定义一个字符串变量result,存储拼接后的URL前半部分。
      • 定义一个字符串变量rigth,存储拼接后的URL后半部分。
      • 返回拼接后的完整URL。
  4. outopenid(String code)方法:

    • 参数:一个字符串类型的code。
    • 返回值:一个字符串类型的openid。
    • 功能:调用getOpenid()方法获取响应中的JSON字符串,然后将其解析为JSON对象,并从中提取出openid。
      • 调用getOpenid()方法,传入拼接后的URL,获取响应中的JSON字符串。
      • 使用JSON.parseObject()方法将JSON字符串转换为JSON对象。
      • 从JSON对象中获取名为"openid"的值,并打印出来。
      • 返回提取出的openid。

获取小程序传递的参数

String openid =request.getParameter("openid");Writer out = response.getWriter();String sqlSel = "SELECT * FROM openid WHERE openid = '"+ openid +"'";String sqlIns = "INSERT INTO openid VALUES ('"+ openid +"')";System.out.println(sqlIns);sqlUtils sqlutils = new sqlUtils();if (!openid.equals("")) {int count = sqlutils.selectOpenid(sqlSel);System.out.println(count);if (count == 0) {sqlutils.DMLsql(sqlIns);System.out.println(sqlIns);out.write("false");System.out.println("没投票");} else {out.write("true");//数据库中已有数据System.out.println("1投票");}}

获取参数并修改投票数量

int votevalue = Integer.parseInt(request.getParameter("votevalue"));String s="SELECT * FROM voteinfo where id =" + votevalue;sqlUtils utils=new sqlUtils();List<vote1> selectsql = utils.selectsql11(s);int a = 0;for(vote1 tl:selectsql){a=tl.getValue();}++a;System.out.println(a);String s1="update voteinfo set value =" + a + " where id =" + votevalue;int a1 = utils.DMLsql(s1);utils.Exceptionsql();

投票管理系统前端

投票页面wxml


<image src="{{indeximage}}" class="indexImage_top"></image>
<view class="text-top">请选择你喜欢的角色</view>
<radio-group bindchange="radioChang" data-id="{{item.id}}"><view class="index_class" wx:for="{{voteinfo}}" wx:for-item="item" wx:key="index"><view class="voteInfo_class"><radio value="{{item.id}}"><view>{{item.id}}号选手:{{item.name}}</view><image src="{{item.imagesrc}}" class="voteImage"></image></radio></view></view><button class="btn" type="primary" bindtap="sureVote">确定投票</button>
</radio-group>
<view class="text-bottom">
<view>每人只能投票一次,投票后可以查看每个选项的投票次数</view></view>

wxss

/*首页图*/
.indexImage_top {width: 100%;height: 300rpx;
}.text-top{display: flex;align-items: center;justify-content: center;/*对齐方式*/height: 100rpx;font-weight: 600;font-size: large;
}
.text-bottom{display: flex;flex-direction: column;/*以列的方式进行排列*/align-items: center;justify-content: center;height: 40rpx;font-size:smaller;margin-top:40px;/*margin外边距 上右下左 padding内边距*/margin-bottom: 60rpx;
}
/*投票view的边距*/
.voteInfo_class{padding: 20px 10px 20px 10px;
}
/*投票的图片大小*/
.voteImage {width: 250rpx;height: 250rpx;
}
/*首页排版*/
.index_class {display: flex;flex-direction: row;flex-wrap: wrap;justify-content:space-around;display:inline-block
}.btn{margin-top: 30rpx;width: 300rpx;
}

js

Page({data: {indeximage:"/pages/images/index.png",voteinfo:[],radioValue: '', //当前用户选中的投票值userinfo: {}, //当前用户的微信信息openid: '', //当前用户的openidislogin: false, //判断用户是否已经登陆isopenid: false, //判断用户是否已经投票a: 1 ,//判断是否对数据库查询用户是否投票},onLoad: function () {let islogin = wx.getStorageSync('islogin') || false //判断是否登陆let userinfo = wx.getStorageSync('userinfo') //取登陆后的信息//console.log(islogin)if (islogin) {this.setData({islogin: true,userinfo: userinfo,})}//判断本获取投票名字和图片this.getvoteinfo()//微信登录获取openidthis.wxlogin()},//首页确定投票事件sureVote: function () {this.setData({a: 1})//let openid = wx.getStorageSync('openid')console.log(this.data.openid)//微信登录获取个人信息if (!this.data.islogin) {//登陆及获取用户信息this.wxgeiuserinfo()//console.log(this.data.islogin)//console.log(this.data.userinfo)} else {//console.log(this.data.islogin)//console.log(this.data.userinfo)if (this.radioValue == undefined) {wx.showToast({title: '请选择你的投票对象',icon: 'none'})} else {this.data.isopenid = wx.getStorageSync('isopenid') //判断用户是否投票console.log(this.data.isopenid)if (this.data.isopenid) { //如果投过票  就直接跳转wx.setStorageSync('votevalue', this.radioValue)wx.redirectTo({url: '/pages/result/result'})} else {wx.request({url: 'http://localhost:8080/vote/voteupdate',data: {votevalue: this.radioValue},method: 'get',header: {'content-type': 'application/json'},})wx.setStorageSync('votevalue', this.radioValue)wx.redirectTo({url: '/pages/result/result'})}}}},//单选框组选中事件radioChang: function (e) {console.log("选择的值为" + e.detail.value)this.radioValue = e.detail.valueconsole.log(this.radioValue)if (this.data.a == 1) {//判断该用户是否投票this.isopenid()this.setData({a: 0})}},//获取openidwxlogin: function () {console.log("我是获取openid")var that = thiswx.login({success(res) {if (res.code) {//console.log(res.data)//发起请求wx.request({url: 'http://localhost:8080/vote/openid',data: {code: res.code},header: {'content-type': 'application/json'},success: function (res) {console.log(res);let openid = res.datathat.setData({openid: openid})//console.log(that.data.openid);wx.setStorageSync('openid', that.data.openid)},fail: function (res) {console.log("失败");}})} else {console.log('登录失败!' + res.errMsg)}}})},//登陆及获取用户信息wxgeiuserinfo: function () {console.log("我是登陆及获取用户信息")let that = this;wx.getUserProfile({desc: '获取个人信息以便于您的投票',success(res) {let userinfo = res.userInfothat.setData({islogin: true,userinfo: userinfo})wx.setStorageSync('islogin', true)wx.setStorageSync('userinfo', userinfo)console.log(that.islogin)},fail() {wx.showToast({title: '请求信息失败',icon: 'error'})}})},//判断该用户是否投票isopenid: function () {console.log("我是判断该用户是否投票")let that = thiswx.request({url: 'http://localhost:8080/vote/isopenid',data: {openid: this.data.openid},method: 'get',header: {'content-type': 'application/json'},success(res) {console.log(res.data)let isopenid = res.datawx.setStorageSync('isopenid', isopenid)},fail() {wx.showToast({title: '网络连接失败!',icon: 'error'})console.log("失败");},})},//获取投票信息getvoteinfo: function () {console.log("我是获取投票图片")var that = thiswx.request({url: 'http://localhost:8080/vote/voteinfo',success: function (res) {console.log(res);that.setData({voteinfo: res.data})console.log(that.data.voteinfo);wx.setStorageSync('voteinfo', res.data)wx.setStorageSync('isinfo', true)},fail: function (res) {wx.showToast({title: '网络连接失败!',icon: 'error'})console.log("失败");}})},onRefresh(){this.getvoteinfo()},//分享onShareAppMessage: function (res) {var that = this;//console.log(JSON.stringify(that.data.array)) return {title: "快来和我一起投票吧",path: '/pages/index/index',imageUrl: "/pages/images/index.png"}},
//下拉刷新
onPullDownRefresh: function () {//调用刷新时将执行的方法this.onRefresh();
}
})

json

下拉刷新,当用户在页面顶部向下滑动时,可以触发下拉刷新操作,从而更新页面内容,在上述js中并需定义方法

{"enablePullDownRefresh": true
}

投票结果wxml

<view class="center"><image class="image_user" src="{{userImage}}"></image><view class="text_user">{{userName}},你好</view><view wx:if="{{isopenid}}"class="text_tip">你今天已经参加过本投票</view><view wx:if="{{!isopenid}}" class="view_text">你的投票结果为:{{votevalue}}号选手:{{voteinfo[votevalue-1].name}}</view><view class="view_text2">截止到{{date}}的投票结果为:</view><view wx:for="{{voteinfo}}" wx:for-item="item" wx:key="index"><view class="view_text1">{{item.id}}号选手:{{item.name}},票数为:{{item.value}}票</view><view class="index_class"><progress class="progress_box" percent="{{item.value/num*100}}" active stroke-width="20"border-radius="50"/></view></view>
</view>

wxss

.view_text2{font-weight: 300;font-size:small;margin-bottom:30px;
}
.view_text{height: 100rpx;font-weight: 600;font-size: large;
}
/*首页排版*/
.index_class {display: flex;
}/*进度条排版*/
.progress_box{width: 500rpx;margin-bottom:50px;
}.center{display: flex;flex-direction: column;align-items: center;justify-content: center;
}.image_user{height: 100px;width: 100px;border-radius: 50px;margin-top:30px;margin-bottom:10px;
}
.text_user{font-weight: 600;font-size: large;margin-bottom:10px;
}
.text_tip{font-weight: 600;font-size: large;color: red;margin-bottom:10px;
}

js

Page({data: {date: new Date().toLocaleString(),votevalue: '',voteinfo: [],userName: '',userImage: '',isopenid: true,num:0},onLoad: function (options) {//获取投票信息this.getvoteinfo()let userinfo = wx.getStorageSync('userinfo') //取用户的头像 名字this.setData({userName: userinfo.nickName,userImage: userinfo.avatarUrl})let isopenid = wx.getStorageSync('isopenid') //判断用户是否已经进行过投票this.setData({isopenid: isopenid})console.log(this.data.isopenid)var that = thisvar votevalue = wx.getStorageSync('votevalue') //取用户的投票的对象this.setData({votevalue: votevalue})console.log(votevalue)},onShareAppMessage: function (res) {var that = this;//console.log(JSON.stringify(that.data.array)) return {title: "快来和我一起投票吧",path: '/pages/index/index',imageUrl: "/pages/images/index.png"}},getvoteinfo: function () {console.log("我是获取投票图片")var that = thiswx.request({url: 'http://localhost:8080/vote/voteinfo',/*method: 'get',header: {'content-type': 'application/json'},*/success: function (res) {//console.log(res);that.setData({voteinfo: res.data})var num = 0var i = 0console.log(that.data.voteinfo);for(i = 0;i<that.data.voteinfo.length;i++){num = num + that.data.voteinfo[i].value}that.setData({num: num})console.log(that.data.num);},fail: function (res) {wx.showToast({title: '网络连接失败!',icon: 'error'})console.log("失败");}})}})

效果演示

当我们登录不同账号时都将有唯一标识,进行判断是否可以参与投票,这里也是注册了两个账号测试

一开始需要进行登录之后通过openid拿到个人用户信息去判断该用户是否参与投票

登录之后可以直接投票并每天只能参与一次投票

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/118344.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

嚼一嚼Halcon中的3D手眼标定

文章目录 一、问题概述1、何为手眼标定&#xff1f;2、手眼标定的2种形式1&#xff09;眼在手上&#xff08;eye in hand&#xff09;&#xff1a;即相机固定在机械臂末端2&#xff09;眼在手外&#xff08;eye to hand&#xff09;&#xff1a;即相机固定在机械臂以外的地方 3…

微信小程序:点击按钮出现右侧弹窗

效果 代码 wxml <!-- 弹窗信息 --> <view class"popup-container" wx:if"{{showPopup}}"><view class"popup-content"><!-- 弹窗内容 --><text>这是一个右侧弹窗</text></view> </view> <…

Linux:实用操作

Linux&#xff1a;实用操作 1. 各类小技巧1.1 controlc(ctrl c) 强制停止1.2 可以通过快捷键&#xff1a;control d(ctrl d)&#xff0c;退出账户的登录1.3 历史命令搜索1.4 光标移动快捷键 2. 软件安装2.1 介绍2.2 yum命令(需要root权限)在这里插入图片描述 3. systemctl4.…

Java CC 解析 SQL 语法示例

示例&#xff1a;SimpleSelectParser 解析 select 11; 输出 2&#xff1b; 0&#xff09;总结 编写 JavaCC 模板&#xff0c;*.jj 文件。 编译生成代码文件。 移动代码文件到对应的包下。 调用生成的代码文件。 1&#xff09;JavaCC 模板 main/javacc/SimpleSelectParse…

【疯狂Java】数组

1、一维数组 (1)初始化 ①静态初始化&#xff1a;只指定元素&#xff0c;不指定长度 new 类型[] {元素1,元素2,...} int[] intArr; intArr new int[] {5,6,7,8}; ②动态初始化&#xff1a;只指定长度&#xff0c;不指定元素 new 类型[数组长度] int[] princes new in…

Transformer模型 | iTransformer时序预测

Transformer 模型在自然语言处理和计算机视觉领域取得了巨大的成功,并成为了基础模型。然而,最近一些研究开始质疑基于Transformer的时间序列预测模型的有效性。这些模型通常将同一时间戳的多个变量嵌入到不可区分的通道中,并在这些时间标记上应用注意力机制来捕捉时间依赖关…

二、PHP基础学习[变量]

部分内容引用自&#xff1a;https://blog.csdn.net/lady_killer9/article/details/108978062 一、PHP基础学习 1.语法与注释 示例&#xff1a; <?php // PHP 代码/* 这是 PHP 多行 注释 */ ?>2.输出 示例&#xff1a;echo 123; 3.变量 规矩&#xff1a; 变量以 …

【Overload游戏引擎细节分析】Lambert材质Shader分析

一、经典光照模型&#xff1a;Phong模型 现实世界的光照是极其复杂的&#xff0c;而且会受到诸多因素的影响&#xff0c;这是以目前我们所拥有的处理能力无法模拟的。经典光照模型冯氏光照模型(Phong Lighting Model)通过单独计算光源成分得到综合光照效果&#xff0c;然后添加…

城市正视图(Urban Elevations, ACM/ICPC World Finals 1992, UVa221)rust解法

如图5-4所示&#xff0c;有n&#xff08;n≤100&#xff09;个建筑物。左侧是俯视图&#xff08;左上角为建筑物编号&#xff0c;右下角为高度&#xff09;&#xff0c;右侧是从南向北看的正视图。 输入每个建筑物左下角坐标&#xff08;即x、y坐标的最小值&#xff09;、宽度…

sysfs接口与用户空间库libgpiod

sysfs接口是Linux内核提供的一种让用户空间程序访问和控制内核对象&#xff08;包括设备、驱动等&#xff09;信息的机制&#xff0c;它基于虚拟文件系统&#xff0c;将内核对象信息以文件系统的形式表现出来&#xff0c;用户或程序员可以通过读写这些文件来获取或修改内核对象…

React之diff原理

一、是什么 跟Vue一致&#xff0c;React通过引入Virtual DOM的概念&#xff0c;极大地避免无效的Dom操作&#xff0c;使我们的页面的构建效率提到了极大的提升 而diff算法就是更高效地通过对比新旧Virtual DOM来找出真正的Dom变化之处 传统diff算法通过循环递归对节点进行依…

嵌入式中的MCU、ARM、DSP、FPGA

目录 “角色扮演” MCU ARM 特点 DSP 特点 FPGA 特点 应用 “角色扮演” MCU&#xff08;Microcontroller Unit&#xff09;、ARM&#xff08;Advanced RISC Machine&#xff09;、DSP&#xff08;Digital Signal Processor&#xff09;和FPGA&#xff08;Field-Progr…

Python解读市场趋势:LSTM 和 GRU 在预测 Google 股价方面的探索

我将向您展示如何使用 LSTM 和 GRU 预测股票价格。 导入库 import pandas as pd import numpy as np import seaborn as sns import matplotlib.pyplot as plt import matplotlib.dates as mdates import plotly.express as pxfrom keras.preprocessing.sequence import Timese…

记低版本okhttp超时会导致死锁

一、问题起源 在处理一次生产环境cpu拉满问题时&#xff0c;把日志拉下来看发现很多http请求调用出错&#xff0c;项目使用的是okhttp 3.8.1版本。 二、问题描述 问题出在okhttp3.Dispatcher.finished(Dispatcher.java:201)代码如下&#xff1a; void finished(AsyncCall c…

PHP 危险函数2-代码执行语句

代码执行语句 eval() 不是函数&#xff0c;不能被动态调用&#xff0c;并且需要以 ;结束 直接输出&#xff0c;不执行 <?php$code"phpinfo();";echo $code;?>eval() 语句执行 <?php$code"phpinfo();";eval($code); // eval 不是函数&am…

vue3中常见的组合式API

一、toRef 和 toRefs 应用&#xff1a;要将响应式对象中的某个属性单独提供给外部使用时。 语法&#xff1a; toRef语法&#xff1a;const name toRef(person, name) toRefs语法&#xff1a;toRefs(person)&#xff0c;如果在return中直接写&#xff0c;需要用...(展开运算符…

【TGRS 2023】RingMo: A Remote Sensing Foundation ModelWith Masked Image Modeling

RingMo: A Remote Sensing Foundation Model With Masked Image Modeling, TGRS 2023 论文&#xff1a;https://ieeexplore.ieee.org/stamp/stamp.jsp?tp&arnumber9844015 代码&#xff1a;https://github.com/comeony/RingMo MindSpore/RingMo-Framework (gitee.com) …

软件测试之【单元测试、系统测试、集成测试】

一、单元测试的概念 单元测试&#xff08;Unit Testing&#xff09;是对软件基本组成单元进行的测试&#xff0c;如函数&#xff08;function或procedure&#xff09;或一个类的方法&#xff08;method&#xff09;。当然这里的基本单元不仅仅指的是一个函数或者方法&#xff…

Python:实现日历功能

背景 日常生活中&#xff0c;每天都要用到日历&#xff0c;日历成为我们生活中的必需品&#xff0c;那么如何制作日历呢&#xff0c;其实方法有很多&#xff0c;可以直接在excel中制作&#xff0c;也可以手画等等。 学习过编程的朋友&#xff0c;能否想到用Python编写一…

【Pytorch】Pytorch学习笔记02 - 单变量时间序列 LSTM

目录 说明简单神经网络LSTM原理Pytorch LSTM生成数据初始化前向传播方法训练模型自动化模型构建 总结参考文献 说明 这篇文章主要介绍如何使用PyTorch的API构建一个单变量时间序列 LSTM。文章首先介绍了LSTM&#xff0c;解释了它们在时间序列数据中的简单性和有效性。然后&…