dock.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. const jwt = require('jsonwebtoken');
  9. class ChatService extends CrudService {
  10. constructor(ctx) {
  11. super(ctx, 'dock');
  12. this.model = this.ctx.model.Dock;
  13. }
  14. // 创建登录Token
  15. async createJwtPwd(password) {
  16. const { secret } = this.config.jwt;
  17. const token = await jwt.sign(password, secret);
  18. return token;
  19. }
  20. async create(body) {
  21. // roomid与密码每次加一
  22. const findroom = await this.model.find().sort({ room_id: -1 });
  23. if (findroom.length > 0) {
  24. const room = (parseInt(findroom[0].room_id) + 1) + '';
  25. // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
  26. const pas = await this.createJwtPwd(room);
  27. body.room_id = room;
  28. body.password = { secret: pas };
  29. } else {
  30. // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
  31. const room = '1001';
  32. const pas = await this.createJwtPwd(room);
  33. body.room_id = room;
  34. body.password = { secret: pas };
  35. }
  36. const res = await this.model.create(body);
  37. return { ...JSON.parse(JSON.stringify(res)), password: res.password.secret, room_id: res.room_id };
  38. }
  39. // 产品
  40. async goods({ id }, body) {
  41. const apply = await this.model.findOne({ _id: ObjectId(id) });
  42. if (!apply) {
  43. throw new BusinessError('没有查询到该申请用户');
  44. }
  45. apply.goods.push({
  46. ...body,
  47. // apply_time: moment().format('YYYY-MM-DD HH:mm:ss'),
  48. });
  49. const res = await apply.save();
  50. const info = _.last(res.goods);
  51. return info;
  52. }
  53. async apply({ id }, body) {
  54. const dock = await this.model.findOne({ _id: ObjectId(id) });
  55. if (!dock) {
  56. throw new BusinessError('没有查询到该对接会');
  57. }
  58. dock.apply.push({
  59. ...body,
  60. apply_time: moment().format('YYYY-MM-DD HH:mm:ss'),
  61. });
  62. const res = await dock.save();
  63. const info = _.last(res.apply);
  64. return info;
  65. }
  66. async check({ id, dock_id }, { status }) {
  67. assert(status, '请审核是否允许参加对接会');
  68. const dock = await this.model.findOne({ _id: ObjectId(dock_id) });
  69. if (!dock) {
  70. throw new BusinessError('没有查询到该对接会');
  71. }
  72. const user = dock.apply.id(id);
  73. if (!user) {
  74. throw new BusinessError('没有查询到用户的申请');
  75. }
  76. user.status = status;
  77. const res = dock.save();
  78. return res;
  79. }
  80. // async dockCheck({ id }, { is_allowed, reason = '' }) {
  81. // const dock = await this.model.findOne({ _id: ObjectId(id) });
  82. // if (!dock) {
  83. // throw new BusinessError('没有查询到该对接会');
  84. // }
  85. // assert(is_allowed, '请选择审核结果');
  86. // dock.is_allowed = is_allowed;
  87. // dock.reason = reason;
  88. // const res = await dock.save();
  89. // return res;
  90. // }
  91. // 根据申请人id查询所有申请的对接会
  92. async myapply({ user_id, status, skip, limit }) {
  93. const total = await this.model.count({ status, 'apply.user_id': user_id });
  94. const data = await this.model.find({ status, 'apply.user_id': user_id }).skip(Number(skip)).limit(Number(limit));
  95. const result = { total, data };
  96. return result;
  97. }
  98. async updatevipuser({ id }, info) {
  99. const dock = await this.model.findById(id);
  100. if (!dock) {
  101. throw new BusinessError('没有查询到该对接会');
  102. }
  103. const vipuser = await dock.vipuser.id(info.vipid);
  104. if (!vipuser) {
  105. throw new BusinessError('没有查询到vip用户');
  106. }
  107. if (info.vipname) {
  108. vipuser.vipname = info.vipname;
  109. }
  110. if (info.vipphone) {
  111. vipuser.vipphone = info.vipphone;
  112. }
  113. if (info.role) {
  114. vipuser.role = info.role;
  115. }
  116. if (info.company) {
  117. vipuser.company = info.company;
  118. }
  119. if (info.email) {
  120. vipuser.email = info.email;
  121. }
  122. if (info.content) {
  123. vipuser.content = info.content;
  124. }
  125. return await dock.save();
  126. }
  127. async createvipuser({ id }, info) {
  128. const dock = await this.model.findById(id);
  129. if (!dock) {
  130. throw new BusinessError('没有查询到该对接会');
  131. }
  132. const vipuser = {};
  133. vipuser.vipname = info.vipname;
  134. vipuser.vipphone = info.vipphone;
  135. vipuser.role = info.role;
  136. vipuser.company = info.company;
  137. vipuser.email = info.email;
  138. vipuser.content = info.content;
  139. dock.vipuser.push(vipuser);
  140. return await dock.save();
  141. }
  142. }
  143. module.exports = ChatService;