dock.js 4.4 KB

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