Mcp.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. const db = require('../../plugin/DataBase/db')
  2. const path = require('path')
  3. const Logger = require('../Logger')
  4. const mq = require('../../plugin/mq')
  5. class Mcp {
  6. constructor() {
  7. this.logger = new Logger(path.join(__dirname, '../logs/MCP.log'), 'INFO')
  8. this.messageQueue = 'runforge_message_queue'
  9. this.auto_day = [
  10. { label: '周一', value: 1 },
  11. { label: '周二', value: 2 },
  12. { label: '周三', value: 3 },
  13. { label: '周四', value: 4 },
  14. { label: '周五', value: 5 },
  15. { label: '周六', value: 6 },
  16. { label: '周日', value: 0 }
  17. ]
  18. this.autoTimeLabel = (record) => {
  19. if (record.auto_time === -1) {
  20. if (record.today_auto_time)
  21. return `随机-今日${record.today_auto_time}时`
  22. return '随机-待分配'
  23. }
  24. const match = auto_time.find(item => item.value === record.auto_time)
  25. return match ? match.label : '-'
  26. }
  27. this.emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
  28. this.banEmailList = ['icloud.com']
  29. }
  30. async bind_account({ sender, bind_code }) {
  31. try {
  32. this.logger.info(`MCP接收绑定账号请求:${sender},绑定码:${bind_code}`)
  33. let sql = `
  34. SELECT
  35. f.student_num, a.name, f.bot_account
  36. FROM
  37. lepao_extra f
  38. LEFT JOIN
  39. lepao_account a
  40. ON
  41. f.student_num = a.student_num
  42. WHERE
  43. f.bind_code = ?
  44. `
  45. const rows = await db.query(sql, [bind_code])
  46. if (!rows || rows.length === 0) return '系统出错,请稍后再试'
  47. if (rows[0].bot_account) {
  48. if (rows[0].bot_account !== sender) return '该账号已被他人绑定,请联系客服解绑'
  49. return '该账号您已绑定'
  50. }
  51. let insertSql = `
  52. UPDATE lepao_extra SET bot_account = ? WHERE bind_code = ?
  53. `
  54. let insertRows = await db.query(insertSql, [sender, bind_code])
  55. if (!insertRows || insertRows.affectedRows !== 1)
  56. return '系统出错,请稍后再试'
  57. return `绑定成功,姓名:${rows[0].name ?? '未更新,请使用乐跑登录器更新账号信息'},学号:${rows[0].student_num}`
  58. } catch (error) {
  59. this.logger.error(`MCP绑定账号出错:${e}`)
  60. return '系统出错,请稍后再试'
  61. }
  62. }
  63. async get_account_info({ sender }) {
  64. try {
  65. this.logger.info(`MCP接收获取账号信息请求:${sender}`)
  66. let sql = `
  67. SELECT
  68. l.name,
  69. l.student_num,
  70. l.state,
  71. l.area,
  72. l.auto_time,
  73. l.total_num,
  74. l.term_num,
  75. l.academy_name,
  76. l.sex,
  77. l.grade_id,
  78. l.email,
  79. l.auto_run,
  80. l.today_auto_time,
  81. l.target_count,
  82. l.auto_day,
  83. l.notice_type
  84. FROM
  85. lepao_account l
  86. LEFT JOIN
  87. lepao_extra f
  88. ON
  89. l.student_num = f.student_num
  90. WHERE
  91. f.bot_account = ?
  92. `
  93. const rows = await db.query(sql, [sender])
  94. if (!rows) return '系统出错,请稍后再试'
  95. if (rows.length == 0) return '您尚未绑定乐跑账号,请先绑定'
  96. let data = rows[0]
  97. let returnMsg = `
  98. 姓名:${data.name ?? '未更新,请使用乐跑登录器更新账号信息'};学号:${data.student_num};账号状态:${data.state === 1 ? '正常' : '需使用乐跑登录器更新账号信息'};乐跑跑区:${data.area ?? "随机分配"};自动乐跑状态:${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_umo = 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 change_email({ sender, email }) {
  135. try {
  136. this.logger.info(`MCP接收更换邮箱请求:${sender}`)
  137. if (!this.emailRegex.test(email))
  138. return '请检查邮箱格式是否正确'
  139. const emailDomain = email.split('@')[1].toLowerCase()
  140. if (this.banEmailList.includes(emailDomain))
  141. return `暂不支持使用 ${emailDomain} 域名的邮箱,请更换其他邮箱后重试`
  142. let insertSql = `
  143. UPDATE
  144. lepao_account la
  145. JOIN
  146. lepao_extra le
  147. ON
  148. la.student_num = le.student_num
  149. SET
  150. la.email = ?
  151. WHERE
  152. le.bot_account = ?
  153. `
  154. let insertRows = await db.query(insertSql, [email, sender])
  155. if (!insertRows || insertRows.affectedRows !== 1)
  156. return '系统出错,请稍后再试'
  157. return `更换成功`
  158. } catch (error) {
  159. this.logger.error(`MCP更换邮箱出错:${e}`)
  160. return '系统出错,请稍后再试'
  161. }
  162. }
  163. async set_notification({ sender, mode }) {
  164. try {
  165. this.logger.info(`MCP接收设置通知请求:${sender},mode:${mode}`)
  166. if (mode !== 'email' && mode !== 'bot' && mode !== 'none') return '通知type不合法,仅支持 email, bot, none三种模式'
  167. let insertSql = `
  168. UPDATE
  169. lepao_account la
  170. JOIN
  171. lepao_extra le
  172. ON
  173. la.student_num = le.student_num
  174. SET
  175. la.notice_type = ?
  176. WHERE
  177. le.bot_account = ?
  178. `
  179. let insertRows = await db.query(insertSql, [mode, sender])
  180. if (!insertRows || insertRows.affectedRows !== 1)
  181. return '系统出错,请稍后再试'
  182. return `操作成功`
  183. } catch (error) {
  184. this.logger.error(`MCP更换通知方式出错:${e}`)
  185. return '系统出错,请稍后再试'
  186. }
  187. }
  188. }
  189. const MCP = new Mcp()
  190. module.exports.MCP = MCP