teacher.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { CrudService } = require('naf-framework-mongoose/lib/service');
  5. const { BusinessError, ErrorCode } = require('naf-core').Error;
  6. const stringRandom = require('string-random');
  7. class TeacherService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'teacher');
  10. this.model = this.ctx.model.Teacher;
  11. this.umodel = this.ctx.model.User;
  12. }
  13. // 根据状态删除教师信息
  14. async deleteByStatus({ status }) {
  15. await this.model.deleteMany({ status });
  16. return 'deleted';
  17. }
  18. // 查询详情
  19. async fetchTeacher({ id }) {
  20. // 将文件拼到查询到的数据后
  21. return await this.model.findById(id, '+file');
  22. }
  23. async status(data) {
  24. const { teachersid, status, remark } = data;
  25. for (const teacherid of teachersid) {
  26. const teacher = await this.model.findById(teacherid);
  27. teacher.status = status;
  28. await teacher.save();
  29. let detail = '';
  30. if (status === '1') {
  31. const passwd = stringRandom();
  32. detail = '您的账号身份已确认,密码为:' + passwd + '请尽快登录账号上传课件资料附件';
  33. // 状态更新后创建教师用户
  34. const newdata = { name: teacher.name, mobile: teacher.phone, type: '3', uid: teacher.id };
  35. newdata.passwd = passwd;
  36. await this.umodel.create(newdata);
  37. } else if (status === '4') {
  38. detail = '您已通过审核被正式录入教师库';
  39. }
  40. const moment = require('moment');
  41. let date = new Date();
  42. date = moment(date).format('YYYY-MM-DD HH:mm:ss');
  43. await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, teacher.openid, '您有一个新的通知', detail, date, remark);
  44. }
  45. }
  46. }
  47. module.exports = TeacherService;