dock.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.vipid);
  95. if (!vipuser) {
  96. throw new BusinessError('没有查询到vip用户');
  97. }
  98. if (info.vipname) {
  99. vipuser.vipname = info.vipname;
  100. }
  101. if (info.vipphone) {
  102. vipuser.vipphone = info.vipphone;
  103. }
  104. if (info.role) {
  105. vipuser.role = info.role;
  106. }
  107. if (info.company) {
  108. vipuser.company = info.company;
  109. }
  110. if (info.email) {
  111. vipuser.email = info.email;
  112. }
  113. if (info.content) {
  114. vipuser.content = info.content;
  115. }
  116. return await dock.save();
  117. }
  118. async createvipuser({ id }, info) {
  119. const dock = await this.model.findById(id);
  120. if (!dock) {
  121. throw new BusinessError('没有查询到该对接会');
  122. }
  123. const vipuser = {};
  124. vipuser.uid = info.uid;
  125. vipuser.vipname = info.vipname;
  126. vipuser.vipphone = info.vipphone;
  127. vipuser.role = info.role;
  128. vipuser.company = info.company;
  129. vipuser.email = info.email;
  130. vipuser.content = info.content;
  131. dock.vipuser.push(vipuser);
  132. return await dock.save();
  133. }
  134. async dockfetch({ id }) {
  135. const dock = await this.model.findById(id);
  136. const data = JSON.parse(JSON.stringify(dock));
  137. if (dock) {
  138. const applydata = [];
  139. for (const elm of dock.apply) {
  140. const url = 'http://localhost:9004/api/market/transaction?userid=' + elm.user_id;
  141. const res = await this.ctx.curl(url, {
  142. method: 'get',
  143. headers: {
  144. 'content-type': 'application/json',
  145. },
  146. dataType: 'json',
  147. });
  148. const newdata = { ...JSON.parse(JSON.stringify(elm)), transdata: res.data.data };
  149. applydata.push(newdata);
  150. }
  151. data.apply = applydata;
  152. }
  153. return data;
  154. }
  155. }
  156. module.exports = ChatService;