要求:郵箱驗證注冊邏輯接口實現
a. 驗證用戶名存在
b. 獲取郵箱驗證碼
1. 獲取郵箱驗證碼接口 a.發送郵件 b.郵箱和驗證碼保存到內存中
2. 5分鍾之內 不能重復發送
{1111@qq.com:{ ctime:第一次發送的時間戳,code:1233}}
3. 5分鍾之內 發送次數不能超過三次
{1111@qq.com:{ ctime:第一次發送的時間戳,code:1233,count:1}}
⚠️注意: 發送驗證碼后,要把驗證碼保存在內存中,是為了注冊的時候能再次使用。
const express=require('express')
const router= express.Router()
const User=require('../db/model/userModel')
const Mail=require('../utils/mail')
let codes={} //通過內存保存驗證碼
/**
* @api {post} /user/reg 用戶注冊
* @apiName 用戶注冊
* @apiGroup User
*
* @apiParam {String} us 用戶名.
* @apiParam {String} ps 用戶密碼.
* @apiParam {String} code 驗證碼.
* @apiSuccess {String} firstname Firstname of the User.
* @apiSuccess {String} lastname Lastname of the User.
*/
router.post('/reg',(req,res)=>{
// 獲取數據
let {us,ps,code}=req.body
// if(!us || !ps) {return res.send({err:-1,msg:'參數錯誤'})}
if(us&&ps&&code){
//判斷驗證碼是否ok
console.log(codes[us])
console.log(code)
console.log(codes)
if(codes[us]!=code){return res.send({err:-4,msg : '驗證碼錯誤'})}
User.find({us})
.then((data)=>{
if(data.length===0){
// 用戶名不存在 可以注冊
return User.insertMany({us:us,ps:ps})
}else{
res.send({err:-3,msg:'用戶名已存在'})
}
})
.then(()=>{
res.send({err:0,msg:'注冊ok'})
})
.catch((err)=>{
res.send({err:-2,msg:'注冊err'})
})
}else{
return res.send({err:-1,msg:'參數錯誤'})
}
console.log(us,ps)
// 數據處理
// 返回數據
})
/**
* @api {post} /user/login 用戶登錄
* @apiName login
* @apiGroup User
*
* @apiParam {String} us 用戶名.
* @apiParam {String} ps 用戶密碼.
* @apiSuccess {String} firstname Firstname of the User.
* @apiSuccess {String} lastname Lastname of the User.
*/
router.post('/login',(req,res)=>{
let {us,ps}=req.body
if(!us||!ps ){ return res.send({err:-1,msg : '參數錯誤'})}
// {us:us,ps:ps} === {us,ps}
User.find({us,ps})
.then((data)=>{
if(data.length>0){
res.send({err:0,msg : '登錄ok'})
}else{
res.send({err:-2,msg : '用戶名或密碼不正確'})
}
console.log(data)
})
.catch((err)=>{
return res.send({err:-1,msg : '內部錯誤'})
})
})
/**
* @api {post} /user/getMailCode 發送郵箱驗證碼
* @apiName 發送郵箱驗證碼
* @apiGroup User
*
* @apiParam {String} mail 郵箱.
* @apiSuccess {String} firstname Firstname of the User.
* @apiSuccess {String} lastname Lastname of the User.
*/
// 發送郵件驗證碼
router.post('/getMailCode',(req,res)=>{
console.log(req.body)
let {mail}=req.body //這里也要判斷參數是否存在,不存在的話拋出錯誤
let code=parseInt(Math.random()*10000)// 產生隨機碼
console.log(codes)
Mail.send(mail,code)
.then(()=>{
codes[mail]=code
//將郵箱和郵箱匹配的驗證碼保存到緩存中】
res.send({err:0,msg:'驗證碼發送ok'})
})
.catch((err)=>{
res.send({err:-1,msg:'驗證碼發送no ok'})
})
})
module.exports=router
補充擴展:手機驗證碼注冊,不同之處就是 封裝的模塊(SendMail)不也一樣,有不同的運營商,調用運營商提供的接口就可以。
sendMail模塊👇

apiDoc 插件:通過注釋自動生成api文檔 :https://www.npmjs.com/package/apidoc
三步驟:安裝,配置,按照規范寫注釋。
安裝: npm install -g apidoc
運行: apidoc -i src/ -o doc/
/** * @api {get} /user/:id Request User information * @apiName GetUser * @apiGroup User * * @apiParam {Number} id User's unique ID. * * @apiSuccess {String} firstname Firstname of the User. * @apiSuccess {String} lastname Lastname of the User. */
apidoc.json文件:寫公共信息
{ "name": "這是個接口文檔", "version": "0.1.0", "description": "use注冊的接口文檔(注冊,登錄,發送驗證碼)", "title": "你好呀", "url" : "http://127.0.0.1:3000" }
具體示例可以看上邊接口。
