dock.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.uid = user.data.data.id;
  37. result.adminuser = body.adminuser;
  38. result.phone = body.phone;
  39. result.save();
  40. }
  41. }
  42. return { ...JSON.parse(JSON.stringify(res)), adminuser: body.adminuser, phone: body.phone };
  43. }
  44. async apply({ id }, body) {
  45. const dock = await this.model.findOne({ _id: ObjectId(id) });
  46. if (!dock) {
  47. throw new BusinessError('没有查询到该对接会');
  48. }
  49. dock.apply.push({
  50. ...body,
  51. apply_time: moment().format('YYYY-MM-DD HH:mm:ss'),
  52. });
  53. const res = await dock.save();
  54. const info = _.last(res.apply);
  55. return info;
  56. }
  57. async check({ id, dock_id }, { status }) {
  58. assert(status, '请审核是否允许参加对接会');
  59. const dock = await this.model.findOne({ _id: ObjectId(dock_id) });
  60. if (!dock) {
  61. throw new BusinessError('没有查询到该对接会');
  62. }
  63. const user = dock.apply.id(id);
  64. if (!user) {
  65. throw new BusinessError('没有查询到用户的申请');
  66. }
  67. user.status = status;
  68. const res = dock.save();
  69. return res;
  70. }
  71. async dockCheck({ id }, { is_allowed, reason = '' }) {
  72. const dock = await this.model.findOne({ _id: ObjectId(id) });
  73. if (!dock) {
  74. throw new BusinessError('没有查询到该对接会');
  75. }
  76. assert(is_allowed, '请选择审核结果');
  77. dock.is_allowed = is_allowed;
  78. dock.reason = reason;
  79. const res = await dock.save();
  80. return res;
  81. }
  82. // 根据申请人id查询所有申请的对接会
  83. async myapply({ user_id, status, skip, limit }) {
  84. const total = await this.model.count({ status, 'apply.user_id': user_id });
  85. const data = await this.model.find({ status, 'apply.user_id': user_id }).skip(Number(skip)).limit(Number(limit));
  86. const result = { total, data };
  87. return result;
  88. }
  89. async updatevipuser({ id }, info) {
  90. const dock = await this.model.findById(id);
  91. if (!dock) {
  92. throw new BusinessError('没有查询到该对接会');
  93. }
  94. const vipuser = await dock.vipuser.id(info.id);
  95. vipuser.vipname = info.vipname;
  96. vipuser.vipphone = info.vipphone;
  97. vipuser.role = info.role;
  98. vipuser.company = info.company;
  99. vipuser.email = info.email;
  100. vipuser.content = info.content;
  101. return await dock.save();
  102. }
  103. }
  104. module.exports = ChatService;