diff --git a/model/znl.js b/model/znl.js index 2a5b78d36bbdeb7152cb5a7fd92ba46d515f31a5..0272a54c8c3756441f2f8e4eb6e4f67fd357abc9 100644 --- a/model/znl.js +++ b/model/znl.js @@ -86,12 +86,21 @@ const UserInfoSchema = new Schema({ status: { type: String }, permissions: [{ type: Schema.Types.ObjectId, ref: 'Permission' }] }); -const Permission = mongoose.model('db_permission', PermissionSchema,'db_permission') -const Role = mongoose.model('db_role', RoleSchema,'db_role') -const UserInfo = mongoose.model('db_user', UserInfoSchema,'db_user') +//验证码存储 +const VerificationCodeSchema = new Schema({ + phone: { type: String, required: true }, + code: { type: Number, required: true }, + time: { type: Date, required: true }, + status: { type: Boolean, default: false } +}); +const Permission = mongoose.model('db_permission', PermissionSchema, 'db_permission') +const Role = mongoose.model('db_role', RoleSchema, 'db_role') +const UserInfo = mongoose.model('db_user', UserInfoSchema, 'db_user') +const VerificationCode = mongoose.model('db_verification', VerificationCodeSchema, 'db_verification') module.exports = { Permission, Role, - UserInfo + UserInfo, + VerificationCode }; diff --git a/package-lock.json b/package-lock.json index c4a5ab03f5ec57a5952ef8537a936609c3403049..1f2c0de0953606d8ee9856cbf04219b77f8f3833 100644 --- a/package-lock.json +++ b/package-lock.json @@ -108,6 +108,29 @@ "acorn": "^2.1.0" } }, + "agent-base": { + "version": "6.0.2", + "resolved": "https://registry.npmmirror.com/agent-base/-/agent-base-6.0.2.tgz", + "integrity": "sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ==", + "requires": { + "debug": "4" + }, + "dependencies": { + "debug": { + "version": "4.3.5", + "resolved": "https://registry.npmmirror.com/debug/-/debug-4.3.5.tgz", + "integrity": "sha512-pt0bNEmneDIvdL1Xsd9oDQ/wrQRkXDT4AUWlNZNPKvW5x/jyO9VFXkJUP07vQ2upmw5PlaITaPKc31jK13V+jg==", + "requires": { + "ms": "2.1.2" + } + }, + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmmirror.com/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + } + } + }, "align-text": { "version": "0.1.4", "resolved": "https://registry.npmmirror.com/align-text/-/align-text-0.1.4.tgz", @@ -138,6 +161,11 @@ "resolved": "https://registry.npmmirror.com/asap/-/asap-1.0.0.tgz", "integrity": "sha512-Ej9qjcXY+8Tuy1cNqiwNMwFRXOy9UwgTeMA8LxreodygIPV48lx8PU1ecFxb5ZeU1DpMKxiq6vGLTxcitWZPbA==" }, + "asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmmirror.com/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, "base64id": { "version": "2.0.0", "resolved": "https://registry.npmmirror.com/base64id/-/base64id-2.0.0.tgz", @@ -151,6 +179,11 @@ "safe-buffer": "5.1.2" } }, + "bignumber.js": { + "version": "9.1.2", + "resolved": "https://registry.npmmirror.com/bignumber.js/-/bignumber.js-9.1.2.tgz", + "integrity": "sha512-2/mKyZH9K85bzOEfhXDBFZTGd1CTs+5IHpeFQo9luiBG7hghdC851Pj2WAhb6E3R6b9tZj/XKhbg4fum+Kepug==" + }, "body-parser": { "version": "1.18.3", "resolved": "https://registry.npmmirror.com/body-parser/-/body-parser-1.18.3.tgz", @@ -355,6 +388,11 @@ "resolved": "https://registry.npmmirror.com/decamelize/-/decamelize-1.2.0.tgz", "integrity": "sha512-z2S+W9X73hAUUki+N+9Za2lBlun89zigOyGrsax+KUQ6wKW4ZoWpEYBkGhQjwAjjDCkWxhY0VKEhk8wzY7F5cA==" }, + "delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmmirror.com/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==" + }, "depd": { "version": "1.1.2", "resolved": "https://registry.npmmirror.com/depd/-/depd-1.1.2.tgz", @@ -488,6 +526,16 @@ "unpipe": "~1.0.0" } }, + "form-data": { + "version": "3.0.1", + "resolved": "https://registry.npmmirror.com/form-data/-/form-data-3.0.1.tgz", + "integrity": "sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==", + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + } + }, "forwarded": { "version": "0.2.0", "resolved": "https://registry.npmmirror.com/forwarded/-/forwarded-0.2.0.tgz", @@ -524,6 +572,30 @@ "statuses": ">= 1.4.0 < 2" } }, + "https-proxy-agent": { + "version": "5.0.1", + "resolved": "https://registry.npmmirror.com/https-proxy-agent/-/https-proxy-agent-5.0.1.tgz", + "integrity": "sha512-dFcAjpTQFgoLMzC2VwU+C/CbS7uRL0lWmxDITmqm7C+7F0Odmj6s9l6alZc6AELXhrnggM2CeWSXHGOdX2YtwA==", + "requires": { + "agent-base": "6", + "debug": "4" + }, + "dependencies": { + "debug": { + "version": "4.3.5", + "resolved": "https://registry.npmmirror.com/debug/-/debug-4.3.5.tgz", + "integrity": "sha512-pt0bNEmneDIvdL1Xsd9oDQ/wrQRkXDT4AUWlNZNPKvW5x/jyO9VFXkJUP07vQ2upmw5PlaITaPKc31jK13V+jg==", + "requires": { + "ms": "2.1.2" + } + }, + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmmirror.com/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + } + } + }, "iconv-lite": { "version": "0.4.23", "resolved": "https://registry.npmmirror.com/iconv-lite/-/iconv-lite-0.4.23.tgz", @@ -574,6 +646,14 @@ "with": "~4.0.0" } }, + "json-bigint": { + "version": "1.0.0", + "resolved": "https://registry.npmmirror.com/json-bigint/-/json-bigint-1.0.0.tgz", + "integrity": "sha512-SiPv/8VpZuWbvLSMtTDU8hEfrZWg/mH/nV/b4o0CYbSxu1UIQPLdwKOCIyLQX+VIPO5vrLX3i8qtqFyhdPSUSQ==", + "requires": { + "bignumber.js": "^9.0.0" + } + }, "jsonwebtoken": { "version": "9.0.2", "resolved": "https://registry.npmmirror.com/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz", @@ -839,6 +919,35 @@ "resolved": "https://registry.npmmirror.com/negotiator/-/negotiator-0.6.3.tgz", "integrity": "sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==" }, + "node-fetch": { + "version": "2.7.0", + "resolved": "https://registry.npmmirror.com/node-fetch/-/node-fetch-2.7.0.tgz", + "integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==", + "requires": { + "whatwg-url": "^5.0.0" + }, + "dependencies": { + "tr46": { + "version": "0.0.3", + "resolved": "https://registry.npmmirror.com/tr46/-/tr46-0.0.3.tgz", + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" + }, + "webidl-conversions": { + "version": "3.0.1", + "resolved": "https://registry.npmmirror.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz", + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" + }, + "whatwg-url": { + "version": "5.0.0", + "resolved": "https://registry.npmmirror.com/whatwg-url/-/whatwg-url-5.0.0.tgz", + "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", + "requires": { + "tr46": "~0.0.3", + "webidl-conversions": "^3.0.0" + } + } + } + }, "object-assign": { "version": "4.1.1", "resolved": "https://registry.npmmirror.com/object-assign/-/object-assign-4.1.1.tgz", @@ -1194,6 +1303,11 @@ } } }, + "tslib": { + "version": "1.13.0", + "resolved": "https://registry.npmmirror.com/tslib/-/tslib-1.13.0.tgz", + "integrity": "sha512-i/6DQjL8Xf3be4K/E6Wgpekn5Qasl1usyw++dAA35Ue5orEn65VIxOA+YvNNl9HV3qv70T7CNwjODHZrLwvd1Q==" + }, "type-is": { "version": "1.6.18", "resolved": "https://registry.npmmirror.com/type-is/-/type-is-1.6.18.tgz", @@ -1259,6 +1373,11 @@ "resolved": "https://registry.npmmirror.com/utils-merge/-/utils-merge-1.0.1.tgz", "integrity": "sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA==" }, + "uuid": { + "version": "9.0.1", + "resolved": "https://registry.npmmirror.com/uuid/-/uuid-9.0.1.tgz", + "integrity": "sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==" + }, "vary": { "version": "1.1.2", "resolved": "https://registry.npmmirror.com/vary/-/vary-1.1.2.tgz", diff --git a/package.json b/package.json index 2ef06c2d6143a06fd988b4fe1ca85435f8defff0..c3e3a5e3e95336ea8742577d8093a2f92e3d02fc 100644 --- a/package.json +++ b/package.json @@ -16,9 +16,10 @@ "jsonwebtoken": "^9.0.2", "mongoose": "^8.4.0", "morgan": "~1.9.1", + "socket.io": "^4.7.5", + "tencentcloud-sdk-nodejs": "^4.0.870", "multer": "^1.4.5-lts.1", "path": "^0.12.7", - "redis": "^4.6.14", - "socket.io": "^4.7.5" + "redis": "^4.6.14" } } diff --git a/routes/znl.js b/routes/znl.js index aed88cbfdee003c71952f8d7296ca96c317a367c..2646327b10402691580263627bc655ba62b3a96d 100644 --- a/routes/znl.js +++ b/routes/znl.js @@ -1,7 +1,8 @@ const express = require('express'); const router = express.Router(); -const { UserModel, NavModel } = require('../model/znl'); +const { UserModel, NavModel, VerificationCode } = require('../model/znl'); const jwt = require('jsonwebtoken'); +const tencentcloud = require('tencentcloud-sdk-nodejs') const SECRET = 'ice'; // 生成Token的函数 @@ -62,26 +63,6 @@ router.post('/refresh', (req, res) => { } }); -// 注册接口 -router.post('/register', async (req, res) => { - let { username, password } = req.body; - const userInfo = await UserModel.find({ username }); - if (userInfo.length) { - res.send({ - status: 0, - message: '用户名已存在' - }); - } else { - const user = new UserModel({ username, password }); - await user.save(); - res.send({ - status: 0, - message: '注册成功', - data: user - }); - } -}); - // 获取导航数据接口 router.get('/getNav', async (req, res) => { let data = await NavModel.find().lean(); @@ -107,25 +88,139 @@ router.get('/getNav', async (req, res) => { }); }); -router.get('/getAll',async (req,res)=>{ +router.get('/getAll', async (req, res) => { let userId = req.body await UserInfo.findOne({ _id: userId }) - .populate('role') - .populate({ - path: 'permissions', - model: 'Permission', - populate: { - path: 'children', - model: 'Permission' - } + .populate('role') + .populate({ + path: 'permissions', + model: 'Permission', + populate: { + path: 'children', + model: 'Permission' + } + }) + .exec((err, user) => { + if (err) { + console.error(err); + return; + } + // `user`现在包含了完整的信息,包括其角色和所有权限 + console.log(user); + }); +}) +//注册接口 +router.post('/register', async (req, res) => { + let { phone, password, code } = req.body; + const userInfo = await UserModel.find({ username }); + if (userInfo.length) { + return res.send({ + status: 0, + message: '用户名已存在' + }); + } else { + const { time } = await VerificationCode.find({ phone }); + if (code == captchaTemp && phoneTemp == phone && Date.now() < time) { + await UserModel.create({ username: phone, password }); + return res.send({ + status: 0, + message: '注册成功', + }); + } else { + return res.send({ + status: 0, + message: '验证码错误' + }); + } + } +}) +//重置密码接口 +router.post('/resetPassword', async (req, res) => { + let { phone, password } = req.body; + const userInfo = await UserModel.find({ username: phone }); + if (userInfo.length) { + res.send({ + status: 0, + message: '用户名不存在' + }); + } else { + await UserModel.updateOne(username, { password: password }); + res.send({ + status: 0, + message: '重置成功', + }); + } +}) + +let captchaTemp = '' +let phoneTemp = '' + +//获取腾讯短信验证码 +const smsClient = tencentcloud.sms.v20210111.Client +const client = new smsClient({ + credential: { + /* 必填:腾讯云账户密钥对secretId,secretKey。 */ + secretId: 'AKIDLoKoBCRgyMj4a2U8naSI0Y6fVN6wrqrr', + secretKey: 'BRMEvfdZOExjfYXarLtV910uxALfwAUu' + }, + region: 'ap-beijing', + profile: { + signMethod: 'HmacSHA256', + httpProfile: { + reqMethod: 'POST', + reqTimeout: 30, + endpoint: 'sms.tencentcloudapi.com' + }, + }, +}) + +const params = { + SmsSdkAppId: '1400864231', + SignName: 'Come二水公众号', + TemplateId: '2023700', + TemplateParamSet: ['1'], + PhoneNumberSet: ['要发送的手机号'], +}; +//保存发出的验证码 +async function saveSvg(phone, code) { + const expirationTimeInSeconds = 300; // 假设验证码有效期为5分钟 + const expirationTime = Date.now() + expirationTimeInSeconds * 1000; + // console.log(1); + + const verificode = await VerificationCode.create({ + phone: phone, + code: code, + time: expirationTime, // 设置过期时间为当前时间加上有效期(以毫秒为单位) }) - .exec((err, user) => { - if (err) { - console.error(err); - return; - } - // `user`现在包含了完整的信息,包括其角色和所有权限 - console.log(user); - }); - }) + return verificode; +} +router.post('/getSvg', async (req, res) => { + const { phone } = req.body; + captchaTemp = '' + for (let i = 0; i < 6; i++) { + captchaTemp += String(Math.ceil(Math.random() * 9)) + } + params.TemplateParamSet[0] = captchaTemp + params.PhoneNumberSet = [phone] + console.log(params.PhoneNumberSet); + // 设置定时器,在一分钟后清除验证码变量 + setTimeout(() => { + captchaTemp = '' + }, 30000) + //发送请求 + // console.log('params',params); + client.SendSms(params, function (err, response) { + // 请求异常返回,打印异常信息 + if (err) { + return res.json({ success: false, message: "验证码发送失败", error: err }); + } + // 请求正常返回,打印response对象 + // console.log(response) + saveSvg(phoneTemp, captchaTemp) + .then((newCode) => console.log('验证码储存成功:')) + .catch((err) => console.error('验证码储存失败:', err)); + 返回前端验证码 + res.json({ status: 0, message: "验证码发送成功", data: captchaTemp }); + }) +}) module.exports = router;