用户投票实现
新建用户投票详情表t_vote_detail
create table `t_vote_detail` (`id` int (11),`vote_id` int (11),`vote_item_id` int (11),`vote_date` datetime ,`openid` varchar (600)
);
/*
SQLyog Ultimate v11.33 (64 bit)
MySQL - 5.7.18-log : Database - db_vote3
*********************************************************************
*//*!40101 SET NAMES utf8 */;/*!40101 SET SQL_MODE=''*/;/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`db_vote3` /*!40100 DEFAULT CHARACTER SET utf8 */;USE `db_vote3`;/*Table structure for table `t_vote` */DROP TABLE IF EXISTS `t_vote`;CREATE TABLE `t_vote` (`id` int(11) NOT NULL AUTO_INCREMENT,`title` varchar(600) DEFAULT NULL,`explanation` varchar(3000) DEFAULT NULL,`cover_image` varchar(600) DEFAULT NULL,`vote_end_time` datetime DEFAULT NULL,`openid` varchar(600) DEFAULT NULL,`type` int(11) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;/*Data for the table `t_vote` */insert into `t_vote`(`id`,`title`,`explanation`,`cover_image`,`vote_end_time`,`openid`,`type`) values (1,'2','2','20230516092542000000157.jpg','2023-05-17 09:25:33','o30ur5JpAsAUyGBkR0uW4IxvahR8',1),(2,'1','2','20230517045026000000236.jpg','2023-05-20 16:50:22','o30ur5JpAsAUyGBkR0uW4IxvahR8',2),(3,'1cccce测试','2','20230518041341000000412.jpg','2023-05-19 16:13:38','o30ur5JpAsAUyGBkR0uW4IxvahR8',1);/*Table structure for table `t_vote_detail` */DROP TABLE IF EXISTS `t_vote_detail`;CREATE TABLE `t_vote_detail` (`id` int(11) NOT NULL AUTO_INCREMENT,`vote_id` int(11) DEFAULT NULL,`vote_item_id` int(11) DEFAULT NULL,`vote_date` datetime DEFAULT NULL,`openid` varchar(600) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;/*Data for the table `t_vote_detail` */insert into `t_vote_detail`(`id`,`vote_id`,`vote_item_id`,`vote_date`,`openid`) values (2,2,3,'2023-05-20 12:09:39','o30ur5JpAsAUyGBkR0uW4IxvahR8');/*Table structure for table `t_vote_item` */DROP TABLE IF EXISTS `t_vote_item`;CREATE TABLE `t_vote_item` (`id` int(11) NOT NULL AUTO_INCREMENT,`vote_id` int(11) DEFAULT NULL,`name` varchar(600) DEFAULT NULL,`image` varchar(600) DEFAULT NULL,`number` int(11) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;/*Data for the table `t_vote_item` */insert into `t_vote_item`(`id`,`vote_id`,`name`,`image`,`number`) values (1,1,'1',NULL,0),(2,1,'2',NULL,0),(3,2,'1','20230517045034000000735.jpg',1),(4,2,'2','20230517045037000000283.jpg',0),(5,2,'长城','20230517045042000000183.jpg',0),(6,3,'2',NULL,0),(7,3,'3',NULL,0);/*Table structure for table `t_wxuserinfo` */DROP TABLE IF EXISTS `t_wxuserinfo`;CREATE TABLE `t_wxuserinfo` (`id` int(11) NOT NULL AUTO_INCREMENT,`openid` varchar(90) DEFAULT NULL,`nick_name` varchar(150) DEFAULT NULL,`avatar_url` varchar(600) DEFAULT NULL,`register_date` datetime DEFAULT NULL,`last_login_date` datetime DEFAULT NULL,`status` char(3) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;/*Data for the table `t_wxuserinfo` */insert into `t_wxuserinfo`(`id`,`openid`,`nick_name`,`avatar_url`,`register_date`,`last_login_date`,`status`) values (7,'o30ur5PiPOr52bBsetXcIV93NL-U','小锋四号@java1234','20230410102248000000487.jpg','2023-04-10 10:21:30','2023-05-04 21:20:38','0'),(9,'o30ur5JpAsAUyGBkR0uW4IxvahR0','微信用户','default.png','2023-04-30 07:42:19','2023-04-30 07:42:19','1'),(10,'1',NULL,NULL,NULL,NULL,'1'),(11,'2',NULL,NULL,NULL,NULL,'1'),(12,'3',NULL,NULL,NULL,NULL,'1'),(13,'4',NULL,NULL,NULL,NULL,'1'),(14,'5',NULL,NULL,NULL,NULL,'1'),(15,'6',NULL,NULL,NULL,NULL,'1'),(16,'7',NULL,NULL,NULL,NULL,'1'),(17,'8',NULL,NULL,NULL,NULL,'1'),(19,'o30ur5JpAsAUyGBkR0uW4IxvahR8','小锋111@java1234','20230514112056000000757.jpeg','2023-05-11 08:44:11','2023-05-20 12:08:14','0');/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
新建VoteDetail实体:
package com.java1234.entity;import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;import java.util.Date;/**投票详情* @author java1234_小锋 (公众号:java1234)* @site www.java1234.vip* @company 南通小锋网络科技有限公司*/
@TableName("t_vote_detail")
@Data
public class VoteDetail {private Integer id; // 编号private Integer voteId; // 投票IDprivate Integer voteItemId; // 投票选项IDprivate String openid; // 投票人openid@TableField(select=false,exist = false)private WxUserInfo wxUserInfo;@JsonSerialize(using=CustomDateTimeSerializer.class)private Date voteDate; // 投票时间}
新建VoteDetailMapper
package com.java1234.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.java1234.entity.VoteDetail;/*** 投票详情Mapper接口* @author java1234_小锋* @site www.java1234.com* @company 南通小锋网络科技有限公司* @create 2022-02-23 22:00*/
public interface VoteDetailMapper extends BaseMapper<VoteDetail>{}
新建IVoteDetailService
package com.java1234.service;import com.baomidou.mybatisplus.extension.service.IService;
import com.java1234.entity.VoteDetail;/*** 投票详情Service接口* @author java1234_小锋* @site www.java1234.com* @company 南通小锋网络科技有限公司* @create 2022-02-23 22:01*/
public interface IVoteDetailService extends IService<VoteDetail> {
}
新建IVoteDetailServiceImpl
package com.java1234.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java1234.entity.VoteDetail;
import com.java1234.mapper.VoteDetailMapper;
import com.java1234.service.IVoteDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** 投票详情Service实现类* @author java1234_小锋* @site www.java1234.com* @company 南通小锋网络科技有限公司* @create 2022-02-23 22:02*/
@Service("voteDetailService")
public class IVoteDetailServiceImpl extends ServiceImpl<VoteDetailMapper, VoteDetail> implements IVoteDetailService {@Autowiredprivate VoteDetailMapper voteDetailMapper;
}
新建VoteDetailController
package com.java1234.controller;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.java1234.entity.R;
import com.java1234.entity.VoteDetail;
import com.java1234.entity.VoteItem;
import com.java1234.service.IVoteDetailService;
import com.java1234.service.IVoteItemService;
import com.java1234.util.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;import java.util.Date;
import java.util.HashMap;
import java.util.Map;/*** 投票详情Controller控制器* @author java1234_小锋 (公众号:java1234)* @site www.java1234.vip* @company 南通小锋网络科技有限公司*/
@RestController
@RequestMapping("/voteDetail")
public class VoteDetailController {@Autowiredprivate IVoteDetailService voteDetailService;@Autowiredprivate IVoteItemService voteItemService;/*** 添加投票* @param voteDetail* @param token* @return*/@RequestMapping("/add")@Transactionalpublic R add(@RequestBody VoteDetail voteDetail, @RequestHeader String token){System.out.println("token="+token);Claims claims = JwtUtils.validateJWT(token).getClaims();System.out.println("openid="+claims.getId());String openid=claims.getId();int count = voteDetailService.count(new QueryWrapper<VoteDetail>().eq("openid", openid).eq("vote_id", voteDetail.getVoteId()));Map<String,Object> resultMap=new HashMap<>();if(count>0){resultMap.put("info","您已经投票过,不能重复投票!");}else {resultMap.put("info", "投票成功!");voteDetail.setOpenid(openid);voteDetail.setVoteDate(new Date());// 对指定投票项的number+1操作VoteItem voteItem=new VoteItem();voteItem.setId(voteDetail.getVoteItemId());voteItemService.update(new UpdateWrapper<VoteItem>().setSql("number=number+1").eq("id",voteDetail.getVoteItemId()));voteDetailService.save(voteDetail);}return R.ok(resultMap);}}
前端投票提交:
submitVote:async function(e){let form={voteItemId:this.sItem,voteId:this.vote.id}const result=await requestUtil({url:"/voteDetail/add",data:form,method:"post"});if(result.code==0){uni.showToast({icon:"success",title:result.info})}}