dock.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. // console.log(body);
  42. const dock = await this.model.findOne({ _id: ObjectId(id) });
  43. if (!dock) {
  44. throw new BusinessError('没有查询到该对接会');
  45. }
  46. if (body._id) {
  47. const goods = dock.apply.map(i => i.goodsList).flat().find(function(item) {
  48. return item.id === body._id;
  49. });
  50. if (body.dockStatus) {
  51. goods.dockStatus = body.dockStatus;
  52. }
  53. } else {
  54. dock.apply.goodsList.push({
  55. ...body,
  56. });
  57. }
  58. const res = await dock.save();
  59. const info = _.last(res.apply.goodsList);
  60. return info;
  61. }
  62. async apply({ id }, body) {
  63. const dock = await this.model.findOne({ _id: ObjectId(id) });
  64. if (!dock) {
  65. throw new BusinessError('没有查询到该对接会');
  66. }
  67. if (body._id) {
  68. // 修改
  69. const apply = dock.apply.find(item => item._id === body._id);
  70. if (body.user_name) {
  71. apply.user_name = body.user_name;
  72. }
  73. if (body.goodsList) {
  74. apply.goodsList = body.goodsList;
  75. }
  76. if (body.contact_tel) {
  77. apply.contact_tel = body.contact_tel;
  78. }
  79. if (body.apply_time) {
  80. apply.apply_time = body.apply_time;
  81. }
  82. if (body.role) {
  83. apply.role = body.role;
  84. }
  85. if (body.status) {
  86. apply.status = body.status;
  87. }
  88. } else {
  89. // 保存
  90. dock.apply.push({
  91. ...body,
  92. apply_time: moment().format('YYYY-MM-DD HH:mm:ss'),
  93. });
  94. }
  95. const res = await dock.save();
  96. const info = _.last(res.apply);
  97. return info;
  98. }
  99. async check({ id, dock_id }, { status }) {
  100. assert(status, '请审核是否允许参加对接会');
  101. const dock = await this.model.findOne({ _id: ObjectId(dock_id) });
  102. if (!dock) {
  103. throw new BusinessError('没有查询到该对接会');
  104. }
  105. const user = dock.apply.id(id);
  106. if (!user) {
  107. throw new BusinessError('没有查询到用户的申请');
  108. }
  109. user.status = status;
  110. const res = dock.save();
  111. return res;
  112. }
  113. // async dockCheck({ id }, { is_allowed, reason = '' }) {
  114. // const dock = await this.model.findOne({ _id: ObjectId(id) });
  115. // if (!dock) {
  116. // throw new BusinessError('没有查询到该对接会');
  117. // }
  118. // assert(is_allowed, '请选择审核结果');
  119. // dock.is_allowed = is_allowed;
  120. // dock.reason = reason;
  121. // const res = await dock.save();
  122. // return res;
  123. // }
  124. // 根据申请人id查询所有申请的对接会
  125. async myapply({ user_id, status, skip, limit }) {
  126. const total = await this.model.count({ status, 'apply.user_id': user_id });
  127. const data = await this.model.find({ status, 'apply.user_id': user_id }).skip(Number(skip)).limit(Number(limit));
  128. const result = { total, data };
  129. return result;
  130. }
  131. async updatevipuser({ id }, info) {
  132. const dock = await this.model.findById(id);
  133. if (!dock) {
  134. throw new BusinessError('没有查询到该对接会');
  135. }
  136. const vipuser = await dock.vipuser.id(info.vipid);
  137. if (!vipuser) {
  138. throw new BusinessError('没有查询到vip用户');
  139. }
  140. if (info.vipname) {
  141. vipuser.vipname = info.vipname;
  142. }
  143. if (info.vipphone) {
  144. vipuser.vipphone = info.vipphone;
  145. }
  146. if (info.role) {
  147. vipuser.role = info.role;
  148. }
  149. if (info.content) {
  150. vipuser.content = info.content;
  151. }
  152. return await dock.save();
  153. }
  154. async createvipuser({ id }, info) {
  155. const dock = await this.model.findById(id);
  156. if (!dock) {
  157. throw new BusinessError('没有查询到该对接会');
  158. }
  159. const vipuser = {};
  160. vipuser.uid = info.uid;
  161. vipuser.vipname = info.vipname;
  162. vipuser.vipphone = info.vipphone;
  163. vipuser.role = info.role;
  164. vipuser.company = info.company;
  165. vipuser.email = info.email;
  166. vipuser.content = info.content;
  167. dock.vipuser.push(vipuser);
  168. return await dock.save();
  169. }
  170. async dockfetch({ id }) {
  171. const dock = await this.model.findById(id);
  172. const data = JSON.parse(JSON.stringify(dock));
  173. if (dock) {
  174. const applydata = [];
  175. for (const elm of dock.apply) {
  176. const url = 'http://localhost:9004/api/market/transaction?userid=' + elm.user_id;
  177. const res = await this.ctx.curl(url, {
  178. method: 'get',
  179. headers: {
  180. 'content-type': 'application/json',
  181. },
  182. dataType: 'json',
  183. });
  184. const newdata = { ...JSON.parse(JSON.stringify(elm)), transdata: res.data.data };
  185. applydata.push(newdata);
  186. }
  187. data.apply = applydata;
  188. }
  189. return data;
  190. }
  191. async getdock({ id }) {
  192. const res = await this.model.find({ vipuser: { $elemMatch: { uid: id } } });
  193. return res;
  194. }
  195. async getdockByopenid({ openid }) {
  196. const res = await this.model.findOne({ openid });
  197. return res;
  198. }
  199. async getapply({ user_id }) {
  200. const res = await this.model.find({ 'apply.user_id': { $regex: user_id } });
  201. return _.flatten(res);
  202. }
  203. async getgoodslist({ name }) {
  204. assert(name, '请输入产品名称');
  205. const res = await this.model.find({ 'apply.goodsList.name': { $regex: name } });
  206. const data = [];
  207. for (const elm of res) {
  208. const applys = elm.apply;
  209. for (const el of applys) {
  210. const goodlists = el.goodsList;
  211. const goodlist = _.filter(goodlists, function(o) {
  212. const oo = _.includes(o.name, name);
  213. console.log(oo);
  214. return oo;
  215. });
  216. data.push(goodlist);
  217. }
  218. }
  219. return _.flatten(data);
  220. }
  221. }
  222. module.exports = ChatService;