Mcp.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. const db = require('../../plugin/DataBase/db')
  2. const Logger = require('../Logger')
  3. class Mcp {
  4. constructor() {
  5. this.logger = new Logger(path.join(__dirname, '../logs/MCP.log'), 'INFO')
  6. this.auto_day = [
  7. { label: '周一', value: 1 },
  8. { label: '周二', value: 2 },
  9. { label: '周三', value: 3 },
  10. { label: '周四', value: 4 },
  11. { label: '周五', value: 5 },
  12. { label: '周六', value: 6 },
  13. { label: '周日', value: 0 }
  14. ]
  15. this.autoTimeLabel = (record) => {
  16. if (record.auto_time === -1) {
  17. if (record.today_auto_time)
  18. return `随机-今日${record.today_auto_time}时`
  19. return '随机-待分配'
  20. }
  21. const match = auto_time.find(item => item.value === record.auto_time)
  22. return match ? match.label : '-'
  23. }
  24. }
  25. async bind_account({ sender, bind_code }) {
  26. try {
  27. this.logger.info(`MCP接收绑定账号请求:${sender},绑定码:${bind_code}`)
  28. let sql = `
  29. SELECT
  30. f.student_num, f.name, f.bot_account, a.name
  31. FROM
  32. lepao_extra f
  33. LEFT JOIN
  34. lepao_account a
  35. ON
  36. f.student_num = a.student_num
  37. WHERE
  38. f.bind_code = ?
  39. `
  40. const rows = await db.query(sql, [bind_code])
  41. if (!rows) return '系统出错,请稍后再试'
  42. if (rows.length !== 0) {
  43. if (rows[0].bot_account !== sender) return '该账号已被他人绑定,请联系客服解绑'
  44. return '该账号您已绑定'
  45. }
  46. let insertSql = `
  47. UPDATE lepao_extra SET bot_account = ? WHERE bind_code = ?
  48. `
  49. let insertRows = await db.query(insertSql, [sender, bind_code])
  50. if (!insertRows || insertRows.affectedRows !== 1)
  51. return '系统出错,请稍后再试'
  52. return `绑定成功,姓名:${rows[0].name ?? '未更新,请使用乐跑登录器更新账号信息'},学号:${rows[0].student_num}`
  53. } catch (error) {
  54. this.logger.error(`MCP绑定账号出错:${e}`)
  55. return '系统出错,请稍后再试'
  56. }
  57. }
  58. async get_account_info({ sender }) {
  59. try {
  60. this.logger.info(`MCP接收获取账号信息请求:${sender}`)
  61. let sql = `
  62. SELECT
  63. l.name,
  64. l.student_num,
  65. l.state,
  66. l.area,
  67. l.auto_time,
  68. l.total_num,
  69. l.term_num,
  70. l.academy_name,
  71. l.sex,
  72. l.grade_id,
  73. l.email,
  74. l.auto_run,
  75. l.today_auto_time,
  76. l.target_count,
  77. l.auto_day,
  78. l.notice_type
  79. FROM
  80. lepao_account l
  81. LEFT JOIN
  82. lepao_extra f
  83. ON
  84. l.student_num = f.student_num
  85. WHERE
  86. f.bot_account = ?
  87. `
  88. const rows = await db.query(sql, [sender])
  89. if (!rows) return '系统出错,请稍后再试'
  90. if (rows.length == 0) return '您尚未绑定乐跑账号,请先绑定'
  91. let data = rows[0]
  92. let returnMsg = `
  93. 姓名:${data.name ?? '未更新,请使用乐跑登录器更新账号信息'}
  94. 学号:${data.student_num}
  95. 账号状态:${data.state === 1 ? '正常' : '需使用乐跑登录器更新账号信息'}
  96. 乐跑跑区:${data.area ?? "随机分配"}
  97. 自动乐跑状态:${data.state === 1 ? '开启' : '关闭'}
  98. `
  99. if (data.auto_run === 1) {
  100. returnMsg += `自动乐跑时间:${this.autoTimeLabel(data)}`
  101. returnMsg += `自动乐跑星期:${data.auto_day.slice().sort((a, b) => {
  102. if (a === 0) return 1; if (b === 0) return -1; return a - b;
  103. }).map(day => this.auto_day.find(item => item.value === day)?.label).join(',').replace(/周/g, '')}`
  104. }
  105. if (data.sex) returnMsg += `性别:${data.sex === 1 ? '男' : '女'}`
  106. if (data.email) returnMsg += `邮箱:${data.email}`
  107. if (data.grade) returnMsg += `邮箱:${data.grade}`
  108. if (data.academy_name) returnMsg += `学院:${data.academy_name}`
  109. if (data.grade_id) returnMsg += `年级:${data.grade_id}级`
  110. if (data.target_count) returnMsg += `目标乐跑次数:${data.target_count}`
  111. if (data.total_num) returnMsg += `累计乐跑次数:${data.total_num}`
  112. return returnMsg
  113. } catch (error) {
  114. this.logger.error(`MCP查询账号信息出错:${e}`)
  115. return '系统出错,请稍后再试'
  116. }
  117. }
  118. async unbind_account({ sender }) {
  119. try {
  120. this.logger.info(`MCP接收解绑账号请求:${sender}`)
  121. let insertSql = `
  122. UPDATE lepao_extra SET bot_account = NULL, bot_nmo = NULL WHERE bot_account = ?
  123. `
  124. let insertRows = await db.query(insertSql, [sender])
  125. if (!insertRows || insertRows.affectedRows !== 1)
  126. return '系统出错,请稍后再试'
  127. return `解绑成功`
  128. } catch (error) {
  129. this.logger.error(`MCP解绑账号出错:${e}`)
  130. return '系统出错,请稍后再试'
  131. }
  132. }
  133. async set_notification({ sender, type }) {
  134. try {
  135. this.logger.info(`MCP接收设置通知请求:${sender},type:${type}`)
  136. if(type !== 'email' || type !== 'bot' || type !== 'none') return '通知type不合法,仅支持 email, bot, none三种模式'
  137. let insertSql = `
  138. UPDATE
  139. lepao_account la
  140. JOIN
  141. lepao_extra le
  142. ON
  143. la.student_num = le.student_num
  144. SET
  145. la.notice_type = ?
  146. WHERE
  147. le.bot_account = ?
  148. `
  149. let insertRows = await db.query(insertSql, [type, sender])
  150. if (!insertRows || insertRows.affectedRows !== 1)
  151. return '系统出错,请稍后再试'
  152. return `操作成功`
  153. } catch (error) {
  154. this.logger.error(`MCP更换通知方式出错:${e}`)
  155. return '系统出错,请稍后再试'
  156. }
  157. }
  158. }
  159. const MCP = new Mcp()
  160. module.exports.MCP = MCP