mock平台

notice.js 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. const yapi = require('../yapi.js');
  2. function arrUnique(arr1, arr2) {
  3. let arr = arr1.concat(arr2);
  4. let res = arr.filter(function(item, index, arr) {
  5. return arr.indexOf(item) === index;
  6. });
  7. return res;
  8. }
  9. const noticeObj = {
  10. mail: {
  11. title: '邮件',
  12. hander: (emails, title, content)=>{
  13. yapi.commons.sendMail({
  14. to: emails,
  15. contents: content,
  16. subject: title
  17. });
  18. }
  19. }
  20. }
  21. yapi.emitHook('addNotice', noticeObj)
  22. yapi.commons.sendNotice = async function(projectId, data) {
  23. const projectModel = require('../models/project.js');
  24. const userModel = require('../models/user.js');
  25. const followModel = require('../models/follow.js');
  26. const followInst = yapi.getInst(followModel);
  27. const userInst = yapi.getInst(userModel);
  28. const projectInst = yapi.getInst(projectModel);
  29. const list = await followInst.listByProjectId(projectId);
  30. const starUsers = list.map(item => item.uid);
  31. const projectList = await projectInst.get(projectId);
  32. const projectMenbers = projectList.members
  33. .filter(item => item.email_notice)
  34. .map(item => item.uid);
  35. const users = arrUnique(projectMenbers, starUsers);
  36. const usersInfo = await userInst.findByUids(users);
  37. const emails = usersInfo.map(item => item.email).join(',');
  38. try {
  39. Object.keys(noticeObj).forEach(key=>{
  40. let noticeItem = noticeObj[key];
  41. try{
  42. noticeItem.hander(emails, data.title, data.content)
  43. }catch(err){
  44. yapi.commons.log('发送' + (noticeItem.title || key) + '失败' + err.message, 'error')
  45. }
  46. })
  47. // yapi.commons.sendMail({
  48. // to: emails,
  49. // contents: data.content,
  50. // subject: data.title
  51. // });
  52. } catch (e) {
  53. yapi.commons.log('发送失败:' + e, 'error');
  54. }
  55. };