一个投票系统:
前几天在贴吧有人带价求做一个投票APP,1200不多,但是忙活了将近9天晚自习。在这里总结一下。PS:由于没认真写,很多不规范。
总结:
邮件认证:
上次说到那个加密模块解密模块,思路是利用mongodb的ttl索引,认证成功将原来的date更改为1(非时间字段)结束索引从而在24小时内保留用户。
var Nodemailer=require('nodemailer'); var jade=require('jade'); /** * 验证连接生成 * @ re {param} email -收件人地址 * @ secreatKey {param} -加密标记 * @name @ {param}-用户名 * @ callback (error,info)-错误信息,response */ function mailer(re,secretKey,name){ this.to=re, this.secretKey=secretKey, this.name=name } module.exports=mailer; mailer.prototype.sendProveMail = function (callback) { var That = this; var date = new Date(); var JadeByHtml = { secret: this.secretKey, name: this.name, time: date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + "-" + date.getHours() + ":" + (date.getMinutes() < 10 ? '0' + date.getMinutes():+date.getMinutes()) } var pro = new Promise(function (resolve, reject) { //教给渲染引擎来做 jade.renderFile('views/mailer/prove.jade', JadeByHtml, function (error, html) { resolve(html) } ) }); pro.then(function (html) { var transporter = Nodemailer.createTransport({ host: 'smtp.163.com', secureConnection: true, port: 25, auth: { user: '1318175619@163.com', pass: 'asd123456' } }); var mailOptions = { secureConnection: true, from: "DDH <13181726619@163.com>", // sender address to: That.to, // list of receivers subject: 'DDH邮件账户认证 ✔', // Subject line // text: '邮件激活认证✔', // plaintext body html: html// html body }; transporter.sendMail(mailOptions, function (error, info) { console.log(error, info); if (error) { callback(error, null); } else { callback(null, info.response); } }) }) }
Schema定义:
var mongoose = require('mongoose'); var Schema = mongoose.Schema; var testSchema = new Schema({ id: { type: String }, userid:{type:String}, title:{type:String}, info:{type:String}, date:{type: Date,default: Date.now}, //投票选项[{value}] content:[], //已投票人员 finish:[], view:{type:Number,default:0} }, { versionKey: false }); //关联voting表 mongoose.model('Voting', testSchema, 'voting');
插入1次用户投票
Voting.update({"id":tableid,"content.valueName":option},{$inc:{"content.$.count":1}},{$push:{finish:user}}fun()=>{})
不定选项的后台录入:
选项可能是2个,可能是3个,可能是4个,上限9个。这么说的话:前端按照规则命名input1..input2..
exports.posttable=function(req,res){ var arr=[]; for(i=1;i<=9;i++){ if(req.body["input"+i]!=undefined){ arr.push(req.body["input"+i]); }else{ break; } } var rearr=[]; for(var i=0;i<arr.length;i++) { var json = {}; json['valueName']=arr[i]; json['count']=0; rearr.push(json) } var table={ id:Date.now(), userid:req.session.user, title:req.body.title, info:req.body.info, content:rearr }; //入库存档 pvoting.posttable(table,function(error,doc){ //option渲染 res.render('postvote',{option:doc.centent,user:req.session.user}) }) };
剩下的无非就是页面渲染逻辑(PS:前端有人给写),数据库的更改,然后开了delete的路由,查看投票和管理投票的路由和数据操作。
效果:
PS:感谢我的前端队友,虽然我还要自己写渲染逻辑。