Mcp.js 6.8 KB

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