router.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. const express = require('express')
  2. const { sendMessageToAPI, setApiKey, setApiUrl, setapp_code ,setmodel} = require('./wechat/ChatGPT')
  3. const sqlite3 = require('sqlite3')
  4. const jsonwebtoken = require('jsonwebtoken')
  5. const path = require('path')
  6. const secretKey = 'co666'
  7. const {
  8. wxlogin,
  9. Status,
  10. User,
  11. setWx,
  12. stopWx,
  13. loadConfigValues
  14. } = require('./wechat/main')
  15. //sqlite数据库路径
  16. let sqliteDbPath = "./db/data.db"
  17. //打开数据库
  18. var db = new sqlite3.Database(sqliteDbPath)
  19. const router = express.Router()
  20. // 定义中间件.unless指定哪些接口不需要进行token身份认证
  21. const { expressjwt: jwt } = require("express-jwt")
  22. const checkTokenMiddleware = jwt({ secret: secretKey, algorithms: ["HS256"] }).unless({
  23. path: [/^\/userlogin/, /^\/register/],
  24. })
  25. // 验证token
  26. const errorcheckToken = (err, req, res, next) => {
  27. if (err.name === 'UnauthorizedError') {
  28. return res.send({ status: 401, msg: '请先登录' })
  29. }
  30. res.send({ status: 500, msg: '未知错误' })
  31. }
  32. // 封装验证Token和错误处理的函数
  33. const checkToken = (req, res, next) => {
  34. checkTokenMiddleware(req, res, (err) => {
  35. if (err) {
  36. errorcheckToken(err, req, res, next)
  37. } else {
  38. next()
  39. }
  40. })
  41. }
  42. router.use(checkToken)
  43. //用户登录
  44. router.post('/userlogin', (req, res) => {
  45. var username = req.body.username
  46. var password = req.body.password
  47. // 匹配密码
  48. db.all('select * from user where username=?', username, function (err, row) {
  49. if (err) res.send({ status: 500, msg: "数据库查询失败" })
  50. else {
  51. if (row == "") {
  52. res.send({ status: 500, msg: "此用户不存在" })
  53. } else {
  54. if (row[0].password != password) {
  55. res.send({ status: 500, msg: "密码错误" })
  56. } else {
  57. // 如果用户名存在且密码匹配,则登录成功。
  58. const tokenStr = jsonwebtoken.sign({ username: username }, secretKey, { expiresIn: '24h' })
  59. res.send({ status: 200, msg: "登录成功", token: "Bearer " + tokenStr })
  60. }
  61. }
  62. }
  63. })
  64. })
  65. //更改账户信息
  66. function findusername(req, res, next) {
  67. // 从请求头中获取 Token
  68. const token = req.headers['authorization']
  69. jsonwebtoken.verify(token.split(' ')[1], secretKey, (err, decoded) => {
  70. if (err) {
  71. return res.send({ status: 401, msg: 'Token无效' })
  72. } else {
  73. req.username = decoded.username
  74. next()
  75. }
  76. })
  77. }
  78. router.post('/getusername', findusername,(req,res) => {
  79. const username = req.username
  80. res.send({status: 200,msg:username})
  81. })
  82. router.post('/changeaccount', findusername, (req, res) => {
  83. const username = req.username
  84. const oldpassword = req.body.oldpassword
  85. const newusername = req.body.newusername
  86. const newpassword = req.body.newpassword
  87. // 查询用户是否存在以及旧密码是否正确
  88. db.get('SELECT * FROM user WHERE username=? AND password=?', [username, oldpassword], (err, row) => {
  89. if (err) {
  90. res.send({ status: 500, msg: "数据库查询失败" })
  91. } else {
  92. if (!row) {
  93. res.send({ status: 500, msg: "用户名或密码错误" })
  94. } else {
  95. if (newusername.length<5){
  96. res.send({ status: 500, msg: "用户名不能小于5位" })
  97. } else {
  98. if (newpassword.length<6){
  99. res.send({ status: 500, msg: "密码不能小于6位" })
  100. } else {
  101. // 更新用户名和密码
  102. db.run('UPDATE user SET username=?, password=? WHERE username=?', [newusername, newpassword, username], (err) => {
  103. if (err) {
  104. res.send({ status: 500, msg: "更新账户信息失败" })
  105. } else {
  106. res.send({ status: 200, msg: "账户信息更新成功" })
  107. }
  108. })
  109. }
  110. }
  111. }
  112. }
  113. })
  114. })
  115. //获取二维码 启动bot
  116. router.get('/getqrcode',async(req,res) => {
  117. wxlogin()
  118. .then(qrcodeUrl =>{
  119. res.send({ qrcode: qrcodeUrl })
  120. })
  121. })
  122. // 发送头像图片文件
  123. router.get('/getavatar',async(req,res) => {
  124. try {
  125. const avatarFilePath = path.join(__dirname,'./wechat/avatar/avatar.jpg')
  126. res.sendFile(avatarFilePath)
  127. } catch(error) {
  128. res.send({status:500,msg:'获取头像失败!' + error.message})
  129. }
  130. })
  131. router.get('/getwxname', async (req, res) => {
  132. res.send({ wxname: User.name })
  133. })
  134. //获取二维码状态
  135. router.get('/getstatus',async(req,res) => {
  136. res.send({status:Status.status})
  137. })
  138. // 停止机器人
  139. router.get('/stop', async (req, res) => {
  140. try {
  141. stopWx()
  142. res.send({ Status: 200, msg: '停止机器人成功' })
  143. } catch (error) {
  144. res.send({ Status: 500, msg: '停止机器人失败' + error })
  145. }
  146. })
  147. //获取api设置
  148. router.post('/getapiconfig', async (req, res) => {
  149. db.all('SELECT * FROM apiconfig', [], (err, rows) => {
  150. if (err) {
  151. res.send({ status: 500, msg: '查询失败!' })
  152. return
  153. }
  154. res.send({ status: 200, msg: rows })
  155. })
  156. })
  157. //设置api接口相关配置
  158. router.post('/apiconfig',async(req,res) => {
  159. const { apiKey,apiUrl,app_code,model } = req.body
  160. try {
  161. setApiKey(apiKey)
  162. setApiUrl(apiUrl)
  163. setapp_code(app_code)
  164. setmodel(model)
  165. res.send({status: 200,msg: '设置成功!'})
  166. } catch (error) {
  167. res.send({status: 500, msg: '设置失败!'})
  168. }
  169. })
  170. //获取机器人设置
  171. router.post('/getwxconfig', async (req, res) => {
  172. db.all('SELECT * FROM wxconfig', [], (err, rows) => {
  173. if (err) {
  174. res.send({ status: 500, msg: '查询失败!' })
  175. return
  176. }
  177. res.send({ status: 200, msg: rows })
  178. })
  179. })
  180. //设置微信机器人
  181. router.post('/wxconfig', async (req, res) => {
  182. const { autoReplySingle, suffix, prefix, atReply, keyWords, blackName, whiteRoom } = req.body
  183. try {
  184. setWx('autoReplySingle', autoReplySingle)
  185. setWx('suffix', suffix)
  186. setWx('prefix', prefix)
  187. setWx('whiteRoom', whiteRoom)
  188. setWx('atReply', atReply)
  189. setWx('keyWords', keyWords)
  190. setWx('blackName', blackName)
  191. loadConfigValues()
  192. res.send({ status: 200, msg: '设置成功!' })
  193. } catch (error) {
  194. res.send({ status: 500, msg: '设置失败!' })
  195. }
  196. })
  197. //获取消息发送记录
  198. router.post('/messagehistory',async (req,res)=>{
  199. db.all('SELECT * FROM message', [], (err, rows) => {
  200. if (err) {
  201. res.send({ status: 500, msg: '查询失败!' })
  202. return
  203. }
  204. res.send({ status: 200, msg: rows })
  205. })
  206. })
  207. //清空消息发送记录
  208. router.post('/clearmessage',async(req,res) => {
  209. db.run('DELETE FROM message', (err) => {
  210. if (err) {
  211. res.send({ status: 500, msg: '删除失败!' })
  212. } else {
  213. res.send({ status: 200, msg: '删除成功!' })
  214. }
  215. })
  216. })
  217. module.exports = router