AddAccount.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const API = require("../../lib/API.js")
  2. const db = require("../../plugin/DataBase/db.js")
  3. const axios = require("axios")
  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 res = await axios.get(endpoint, {
  29. proxy: false
  30. })
  31. if (!res || !res.data || !res.data[0])
  32. return res.json({
  33. ...BaseStdResponse.ERR,
  34. msg: '获取电费信息失败!请稍后再试'
  35. })
  36. balance = res.data[0][1]
  37. koufei_date = res.data[0][2]
  38. } catch (error) {
  39. this.logger.error(`获取电费信息失败!${error.stack}`)
  40. return res.json({
  41. ...BaseStdResponse.ERR,
  42. msg: '获取电费信息失败!请稍后再试'
  43. })
  44. }
  45. sql = 'INSERT INTO power_task (create_user, create_time, update_time, area, building, room, email, lowest, notes, balance, koufei_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
  46. r = await db.query(sql, [uuid, time, time, area, building, room, email, lowest, notes ?? '', balance, koufei_date])
  47. } else {
  48. sql = 'UPDATE power_task SET area = ?, building = ?, room = ?, email = ?, lowest = ?, notes = ? WHERE id = ?'
  49. r = await db.query(sql, [area, building, room, email, lowest, notes ?? '', id])
  50. }
  51. try {
  52. if (r && r.affectedRows > 0) {
  53. res.json({
  54. ...BaseStdResponse.OK
  55. })
  56. } else {
  57. return res.json({ ...BaseStdResponse.ERR, msg: '添加电费提醒任务失败!数据库错误' })
  58. }
  59. } catch (err) {
  60. this.logger.error(`添加电费提醒任务失败!${err.stack}`)
  61. res.json({
  62. ...BaseStdResponse.ERR,
  63. msg: "添加电费提醒任务失败!",
  64. })
  65. }
  66. }
  67. }
  68. module.exports.AddAccount = AddAccount