router.js 7.4 KB

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