AddAccount.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const API = require("../../lib/API.js")
  2. const db = require("../../plugin/DataBase/db.js")
  3. const { axiosWithQgOutbound } = require("../../lib/Lepao/qgOutboundAxios")
  4. const { BaseStdResponse } = require("../../BaseStdResponse.js")
  5. const AccessControl = require("../../lib/AccessControl.js")
  6. class AddAccount extends API {
  7. constructor() {
  8. super()
  9. this.setPath('/Power/Account')
  10. this.setMethod('POST')
  11. }
  12. async onRequest(req, res) {
  13. let { uuid, session, area, building, room, email, lowest, notes, id } = req.body
  14. if ([uuid, session, area, building, lowest, room].some(value => value === '' || value === null || value === undefined))
  15. return res.json({
  16. ...BaseStdResponse.MISSING_PARAMETER
  17. })
  18. if (!await AccessControl.checkSession(uuid, session))
  19. return res.status(401).json({
  20. ...BaseStdResponse.ACCESS_DENIED
  21. })
  22. const time = new Date().getTime()
  23. let sql, r
  24. if (!id) {
  25. let balance, koufei_date
  26. try {
  27. const endpoint = `https://hqpay.ctbu.edu.cn/weixin/ashx/frmuser.ashx?test=lastlist&pid=${room}&dyid=${building}`
  28. const apiRes = await axiosWithQgOutbound({
  29. method: 'get',
  30. url: endpoint,
  31. timeout: 15000,
  32. logger: this.logger,
  33. scene: 'PowerAccount'
  34. })
  35. if (!apiRes || !apiRes.data || !apiRes.data[0])
  36. return res.json({
  37. ...BaseStdResponse.ERR,
  38. msg: '获取电费信息失败!请稍后再试'
  39. })
  40. balance = apiRes.data[0][1]
  41. koufei_date = apiRes.data[0][2]
  42. } catch (error) {
  43. this.logger.error(`获取电费信息失败!${error.stack}`)
  44. return res.json({
  45. ...BaseStdResponse.ERR,
  46. msg: '获取电费信息失败!请稍后再试'
  47. })
  48. }
  49. sql = 'INSERT INTO power_task (create_user, create_time, update_time, area, building, room, email, lowest, notes, balance, koufei_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
  50. r = await db.query(sql, [uuid, time, time, area, building, room, email, lowest, notes ?? '', balance, koufei_date])
  51. } else {
  52. sql = 'UPDATE power_task SET area = ?, building = ?, room = ?, email = ?, lowest = ?, notes = ?, is_notice = 0 WHERE id = ?'
  53. r = await db.query(sql, [area, building, room, email, lowest, notes ?? '', id])
  54. }
  55. try {
  56. if (r && r.affectedRows > 0) {
  57. res.json({
  58. ...BaseStdResponse.OK
  59. })
  60. } else {
  61. return res.json({ ...BaseStdResponse.ERR, msg: '添加电费提醒任务失败!数据库错误' })
  62. }
  63. } catch (err) {
  64. this.logger.error(`添加电费提醒任务失败!${err.stack}`)
  65. res.json({
  66. ...BaseStdResponse.ERR,
  67. msg: "添加电费提醒任务失败!",
  68. })
  69. }
  70. }
  71. }
  72. module.exports.AddAccount = AddAccount