Mcp.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. const db = require('../../plugin/DataBase/db')
  2. const path = require('path')
  3. const Logger = require('../Logger')
  4. const mq = require('../../plugin/mq')
  5. const EmailTemplate = require('../../plugin/Email/emailTemplate')
  6. class Mcp {
  7. constructor() {
  8. this.logger = new Logger(path.join(__dirname, '../logs/MCP.log'), 'INFO')
  9. this.messageQueue = 'runforge_message_queue'
  10. this.auto_day = [
  11. { label: '周一', value: 1 },
  12. { label: '周二', value: 2 },
  13. { label: '周三', value: 3 },
  14. { label: '周四', value: 4 },
  15. { label: '周五', value: 5 },
  16. { label: '周六', value: 6 },
  17. { label: '周日', value: 0 }
  18. ]
  19. this.autoTimeLabel = (record) => {
  20. if (record.auto_time === -1) {
  21. if (record.today_auto_time)
  22. return `随机-今日${record.today_auto_time}时`
  23. return '随机-待分配'
  24. }
  25. const match = auto_time.find(item => item.value === record.auto_time)
  26. return match ? match.label : '-'
  27. }
  28. this.emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
  29. this.banEmailList = ['icloud.com']
  30. }
  31. async bind_account({ sender, bind_code }) {
  32. try {
  33. if ([sender, bind_code].some(value => value === '' || value === null || value === undefined))
  34. return '缺少参数'
  35. this.logger.info(`MCP接收绑定账号请求:${sender},绑定码:${bind_code}`)
  36. let selectSql = 'SELECT bot_account FROM lepao_extra WHERE bot_account = ?'
  37. let selectRows = await db.query(selectSql, [sender])
  38. if (!selectRows) return '系统出错,请稍后再试'
  39. if (selectRows.length !== 0) return '您已绑定其他账号,请先解绑'
  40. let umo = `QQ2749761853:FriendMessage:${sender}`
  41. let sql = `
  42. SELECT
  43. f.student_num, a.name, f.bot_account
  44. FROM
  45. lepao_extra f
  46. LEFT JOIN
  47. lepao_account a
  48. ON
  49. f.student_num = a.student_num
  50. WHERE
  51. f.bind_code = ?
  52. `
  53. const rows = await db.query(sql, [bind_code])
  54. if (!rows || rows.length === 0) return '系统出错,请稍后再试'
  55. if (rows[0].bot_account !== null) {
  56. if (rows[0].bot_account !== sender) return '该账号已被他人绑定,请联系客服解绑'
  57. return '该账号您已绑定'
  58. }
  59. let insertSql = `
  60. UPDATE lepao_extra SET bot_account = ?, bot_umo = ? WHERE bind_code = ?
  61. `
  62. let insertRows = await db.query(insertSql, [sender, umo, bind_code])
  63. if (!insertRows || insertRows.affectedRows !== 1)
  64. return '系统出错,请稍后再试'
  65. return `绑定成功,姓名:${rows[0].name ?? '未更新,请使用乐跑登录器更新账号信息'},学号:${rows[0].student_num}`
  66. } catch (error) {
  67. this.logger.error(`MCP绑定账号出错:${error.message}`)
  68. return '系统出错,请稍后再试'
  69. }
  70. }
  71. async get_account_info({ sender }) {
  72. try {
  73. if ([sender].some(value => value === '' || value === null || value === undefined))
  74. return '缺少参数'
  75. this.logger.info(`MCP接收获取账号信息请求:${sender}`)
  76. let sql = `
  77. SELECT
  78. l.name,
  79. l.student_num,
  80. l.state,
  81. l.area,
  82. l.auto_time,
  83. l.total_num,
  84. l.term_num,
  85. l.academy_name,
  86. l.sex,
  87. l.grade_id,
  88. l.email,
  89. l.auto_run,
  90. l.today_auto_time,
  91. l.target_count,
  92. l.auto_day,
  93. l.notice_type
  94. FROM
  95. lepao_account l
  96. LEFT JOIN
  97. lepao_extra f
  98. ON
  99. l.student_num = f.student_num
  100. WHERE
  101. f.bot_account = ?
  102. `
  103. const rows = await db.query(sql, [sender])
  104. if (!rows) return '系统出错,请稍后再试'
  105. if (rows.length == 0) return '您尚未绑定乐跑账号,请先绑定'
  106. let data = rows[0]
  107. let returnMsg = `
  108. 姓名:${data.name ?? '未更新,请使用乐跑登录器更新账号信息'};学号:${data.student_num};账号状态:${data.state === 1 ? '正常' : '需使用乐跑登录器更新账号信息'};乐跑跑区:${data.area ?? "随机分配"};自动乐跑状态:${data.auto_run === 1 ? '开启' : '关闭'}
  109. `
  110. if (data.auto_run === 1) {
  111. returnMsg += `自动乐跑时间:${this.autoTimeLabel(data)};`
  112. returnMsg += `自动乐跑星期:${data.auto_day.slice().sort((a, b) => {
  113. if (a === 0) return 1; if (b === 0) return -1; return a - b;
  114. }).map(day => this.auto_day.find(item => item.value === day)?.label).join(',').replace(/周/g, '')};`
  115. }
  116. if (data.sex) returnMsg += `性别:${data.sex === 1 ? '男' : '女'};`
  117. if (data.email) returnMsg += `邮箱:${data.email};`
  118. if (data.grade) returnMsg += `邮箱:${data.grade};`
  119. if (data.academy_name) returnMsg += `学院:${data.academy_name};`
  120. if (data.grade_id) returnMsg += `年级:${data.grade_id}级;`
  121. if (data.target_count) returnMsg += `目标乐跑次数:${data.target_count};`
  122. if (data.total_num) returnMsg += `累计乐跑次数:${data.total_num};`
  123. if (data.notice_type) returnMsg += `通知方式:${data.notice_type};`
  124. return returnMsg
  125. } catch (error) {
  126. this.logger.error(`MCP查询账号信息出错:${error.message}`)
  127. return '系统出错,请稍后再试'
  128. }
  129. }
  130. async unbind_account({ sender }) {
  131. try {
  132. if ([sender].some(value => value === '' || value === null || value === undefined))
  133. return '缺少参数'
  134. this.logger.info(`MCP接收解绑账号请求:${sender}`)
  135. let insertSql = `
  136. UPDATE lepao_extra SET bot_account = NULL, bot_umo = NULL WHERE bot_account = ?
  137. `
  138. let insertRows = await db.query(insertSql, [sender])
  139. if (!insertRows || insertRows.affectedRows !== 1)
  140. return '系统出错,请稍后再试'
  141. return `解绑成功`
  142. } catch (error) {
  143. this.logger.error(`MCP解绑账号出错:${error.message}`)
  144. return '系统出错,请稍后再试'
  145. }
  146. }
  147. async change_email({ sender, email }) {
  148. try {
  149. if ([sender, email].some(value => value === '' || value === null || value === undefined))
  150. return '缺少参数'
  151. this.logger.info(`MCP接收更换邮箱请求:${sender}`)
  152. if (!this.emailRegex.test(email))
  153. return '请检查邮箱格式是否正确'
  154. const emailDomain = email.split('@')[1].toLowerCase()
  155. if (this.banEmailList.includes(emailDomain))
  156. return `暂不支持使用 ${emailDomain} 域名的邮箱,请更换其他邮箱后重试`
  157. let insertSql = `
  158. UPDATE
  159. lepao_account la
  160. JOIN
  161. lepao_extra le
  162. ON
  163. la.student_num = le.student_num
  164. SET
  165. la.email = ?
  166. WHERE
  167. le.bot_account = ?
  168. `
  169. let insertRows = await db.query(insertSql, [email, sender])
  170. if (!insertRows || insertRows.affectedRows !== 1)
  171. return '系统出错,请稍后再试'
  172. return `更换成功`
  173. } catch (error) {
  174. this.logger.error(`MCP更换邮箱出错:${error.message}`)
  175. return '系统出错,请稍后再试'
  176. }
  177. }
  178. async set_notification({ sender, mode }) {
  179. try {
  180. if ([sender, mode].some(value => value === '' || value === null || value === undefined))
  181. return '缺少参数'
  182. this.logger.info(`MCP接收设置通知请求:${sender},mode:${mode}`)
  183. if (mode !== 'email' && mode !== 'bot' && mode !== 'none') return '通知type不合法,仅支持 email, bot, none三种模式'
  184. let insertSql = `
  185. UPDATE
  186. lepao_account la
  187. JOIN
  188. lepao_extra le
  189. ON
  190. la.student_num = le.student_num
  191. SET
  192. la.notice_type = ?
  193. WHERE
  194. le.bot_account = ?
  195. `
  196. let insertRows = await db.query(insertSql, [mode, sender])
  197. if (!insertRows || insertRows.affectedRows !== 1)
  198. return '系统出错,请稍后再试'
  199. return `操作成功`
  200. } catch (error) {
  201. this.logger.error(`MCP更换通知方式出错:${error.message}`)
  202. return '系统出错,请稍后再试'
  203. }
  204. }
  205. async create_work_order({ sender, email, title, content }) {
  206. try {
  207. if ([sender, email, title, content].some(value => value === '' || value === null || value === undefined))
  208. return '缺少参数'
  209. this.logger.info(`MCP接收工单创建请求:${sender},Email:${email}`)
  210. if (!this.emailRegex.test(email))
  211. return '请检查邮箱格式是否正确'
  212. const emailDomain = email.split('@')[1].toLowerCase()
  213. if (this.banEmailList.includes(emailDomain))
  214. return `暂不支持使用 ${emailDomain} 域名的邮箱,请更换其他邮箱后重试`
  215. let selectSql = `
  216. SELECT
  217. l.create_user
  218. FROM
  219. lepao_account l
  220. LEFT JOIN
  221. lepao_extra e
  222. ON
  223. l.student_num = e.student_num
  224. WHERE
  225. e.bot_account = ?
  226. `
  227. let selectRows = await db.query(selectSql, [sender])
  228. if (!selectRows || selectRows.length === 0) return '您还未绑定乐跑账号哦~请绑定后再试'
  229. const time = new Date().getTime()
  230. const uuid = selectRows[0].create_user
  231. let msg = []
  232. let message = {
  233. time,
  234. content,
  235. files: [],
  236. uuid,
  237. type: 'user'
  238. }
  239. msg.push(message)
  240. const systemMsg = {
  241. time,
  242. content: `该问题由用户${sender}通过小妍助理自动生成并提交~我们会尽快处理,请耐心等待`,
  243. uuid: 'e4fe0277-0b1a-41a1-b25f-8b6e4cec3281',
  244. type: 'system'
  245. }
  246. msg.push(systemMsg)
  247. let sql = 'INSERT INTO work_order (title, email, msg, create_user, create_time, update_time) VALUES (?, ?, ?, ?, ?, ?)'
  248. let r = await db.query(sql, [title, email, msg, uuid, time, time])
  249. if (!r || r.affectedRows !== 1)
  250. return '系统出错,请稍后再试'
  251. let kefuSql = `
  252. SELECT email FROM users
  253. WHERE JSON_CONTAINS(permission, '"admin"') OR JSON_CONTAINS(permission, '"service"')
  254. `
  255. let kefuRows = await db.query(kefuSql);
  256. let emails = [...new Set(kefuRows.map(row => row.email))]
  257. for (const email of emails) {
  258. if (!email) break
  259. EmailTemplate.orderNewReply(email, { id: r.insertId || id, content, files: [] })
  260. }
  261. return `提交成功`
  262. } catch (error) {
  263. this.logger.error(`MCP工单创建出错:${error.message}`)
  264. return '系统出错,请稍后再试'
  265. }
  266. }
  267. }
  268. const MCP = new Mcp()
  269. module.exports.MCP = MCP