router.js 8.3 KB

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