dock.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 } = 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. role: '3',
  22. pid: body.pid,
  23. code: body.code,
  24. room_id: '123',
  25. password: '123',
  26. };
  27. const user = await this.ctx.curl(url, {
  28. method: 'post',
  29. headers: {
  30. 'content-type': 'application/json',
  31. },
  32. dataType: 'json',
  33. data: JSON.stringify(newdata),
  34. });
  35. if (user.data.errcode === 0) {
  36. const result = await this.model.findById(res.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. // 产品
  45. async goods({ id }, body) {
  46. const apply = await this.model.findOne({ _id: ObjectId(id) });
  47. if (!apply) {
  48. throw new BusinessError('没有查询到该申请用户');
  49. }
  50. apply.goods.push({
  51. ...body,
  52. // apply_time: moment().format('YYYY-MM-DD HH:mm:ss'),
  53. });
  54. const res = await apply.save();
  55. const info = _.last(res.goods);
  56. return info;
  57. }
  58. async apply({ id }, body) {
  59. const dock = await this.model.findOne({ _id: ObjectId(id) });
  60. if (!dock) {
  61. throw new BusinessError('没有查询到该对接会');
  62. }
  63. dock.apply.push({
  64. ...body,
  65. apply_time: moment().format('YYYY-MM-DD HH:mm:ss'),
  66. });
  67. const res = await dock.save();
  68. const info = _.last(res.apply);
  69. return info;
  70. }
  71. async check({ id, dock_id }, { status }) {
  72. assert(status, '请审核是否允许参加对接会');
  73. const dock = await this.model.findOne({ _id: ObjectId(dock_id) });
  74. if (!dock) {
  75. throw new BusinessError('没有查询到该对接会');
  76. }
  77. const user = dock.apply.id(id);
  78. if (!user) {
  79. throw new BusinessError('没有查询到用户的申请');
  80. }
  81. user.status = status;
  82. const res = dock.save();
  83. return res;
  84. }
  85. // async dockCheck({ id }, { is_allowed, reason = '' }) {
  86. // const dock = await this.model.findOne({ _id: ObjectId(id) });
  87. // if (!dock) {
  88. // throw new BusinessError('没有查询到该对接会');
  89. // }
  90. // assert(is_allowed, '请选择审核结果');
  91. // dock.is_allowed = is_allowed;
  92. // dock.reason = reason;
  93. // const res = await dock.save();
  94. // return res;
  95. // }
  96. // 根据申请人id查询所有申请的对接会
  97. async myapply({ user_id, status, skip, limit }) {
  98. const total = await this.model.count({ status, 'apply.user_id': user_id });
  99. const data = await this.model.find({ status, 'apply.user_id': user_id }).skip(Number(skip)).limit(Number(limit));
  100. const result = { total, data };
  101. return result;
  102. }
  103. async updatevipuser({ id }, info) {
  104. const dock = await this.model.findById(id);
  105. if (!dock) {
  106. throw new BusinessError('没有查询到该对接会');
  107. }
  108. const vipuser = await dock.vipuser.id(info.vipid);
  109. if (!vipuser) {
  110. throw new BusinessError('没有查询到vip用户');
  111. }
  112. if (info.vipname) {
  113. vipuser.vipname = info.vipname;
  114. }
  115. if (info.vipphone) {
  116. vipuser.vipphone = info.vipphone;
  117. }
  118. if (info.role) {
  119. vipuser.role = info.role;
  120. }
  121. if (info.company) {
  122. vipuser.company = info.company;
  123. }
  124. if (info.email) {
  125. vipuser.email = info.email;
  126. }
  127. if (info.content) {
  128. vipuser.content = info.content;
  129. }
  130. return await dock.save();
  131. }
  132. async createvipuser({ id }, info) {
  133. const dock = await this.model.findById(id);
  134. if (!dock) {
  135. throw new BusinessError('没有查询到该对接会');
  136. }
  137. const vipuser = {};
  138. vipuser.vipname = info.vipname;
  139. vipuser.vipphone = info.vipphone;
  140. vipuser.role = info.role;
  141. vipuser.company = info.company;
  142. vipuser.email = info.email;
  143. vipuser.content = info.content;
  144. dock.vipuser.push(vipuser);
  145. return await dock.save();
  146. }
  147. }
  148. module.exports = ChatService;