router.js 7.5 KB

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