dock.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. const moment = require('moment');
  8. class ChatService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'dock');
  11. this.model = this.ctx.model.Dock;
  12. }
  13. async create(body) {
  14. const res = await this.model.create(body);
  15. if (res) {
  16. const url = this.ctx.app.config.axios.auth.baseUrl;
  17. const newdata = {
  18. name: body.adminuser,
  19. phone: body.phone,
  20. passwd: body.passwd,
  21. uid: res.id,
  22. role: '4',
  23. pid: body.pid,
  24. code: body.code,
  25. };
  26. const user = await this.ctx.curl(url, {
  27. method: 'post',
  28. headers: {
  29. 'content-type': 'application/json',
  30. },
  31. dataType: 'json',
  32. data: JSON.stringify(newdata),
  33. });
  34. if (user.data.errcode === 0) {
  35. const result = await this.model.findById(res.id);
  36. result.u_id = user.data.data.id;
  37. result.save();
  38. }
  39. }
  40. return res;
  41. }
  42. async apply({ id }, body) {
  43. const dock = await this.model.findOne({ _id: ObjectId(id) });
  44. if (!dock) {
  45. throw new BusinessError('没有查询到该对接会');
  46. }
  47. dock.apply.push({
  48. ...body,
  49. apply_time: moment().format('YYYY-MM-DD HH:mm:ss'),
  50. });
  51. const res = await dock.save();
  52. const info = _.last(res.apply);
  53. return info;
  54. }
  55. async check({ id, dock_id }, { status }) {
  56. assert(status, '请审核是否允许参加对接会');
  57. const dock = await this.model.findOne({ _id: ObjectId(dock_id) });
  58. if (!dock) {
  59. throw new BusinessError('没有查询到该对接会');
  60. }
  61. const user = dock.apply.id(id);
  62. if (!user) {
  63. throw new BusinessError('没有查询到用户的申请');
  64. }
  65. user.status = status;
  66. const res = dock.save();
  67. return res;
  68. }
  69. async dockCheck({ id }, { is_allowed, reason = '' }) {
  70. const dock = await this.model.findOne({ _id: ObjectId(id) });
  71. if (!dock) {
  72. throw new BusinessError('没有查询到该对接会');
  73. }
  74. assert(is_allowed, '请选择审核结果');
  75. dock.is_allowed = is_allowed;
  76. dock.reason = reason;
  77. const res = await dock.save();
  78. return res;
  79. }
  80. // 根据申请人id查询所有申请的对接会
  81. async myapply({ user_id, status, skip, limit }) {
  82. const total = await this.model.count({ status, 'apply.user_id': user_id });
  83. const data = await this.model.find({ status, 'apply.user_id': user_id }).skip(Number(skip)).limit(Number(limit));
  84. const result = { total, data };
  85. return result;
  86. }
  87. }
  88. module.exports = ChatService;