dock.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 {
  38. ...JSON.parse(JSON.stringify(res)),
  39. password: res.password.secret,
  40. room_id: res.room_id,
  41. };
  42. }
  43. // 产品
  44. async goods({ id }, body) {
  45. // console.log(body);
  46. const dock = await this.model.findOne({ _id: ObjectId(id) });
  47. if (!dock) {
  48. throw new BusinessError('没有查询到该对接会');
  49. }
  50. if (body._id) {
  51. let { apply } = dock;
  52. apply = apply.map(a => {
  53. a.goodsList = a.goodsList.map(g => {
  54. if (g.id === body._id) {
  55. g.dockStatus = body.dockStatus;
  56. }
  57. return g;
  58. });
  59. // goodsList中有一个是审核通过的,就把这个人通过 dockStatus === 1
  60. const r = a.goodsList.find(f => f.dockStatus === '1');
  61. if (r) a.status = '1';
  62. return a;
  63. });
  64. dock.apply = apply;
  65. }
  66. const res = await dock.save();
  67. const info = _.last(res.apply.goodsList);
  68. return info;
  69. }
  70. async apply({ id }, body) {
  71. const dock = await this.model.findOne({ _id: ObjectId(id) });
  72. if (!dock) {
  73. throw new BusinessError('没有查询到该对接会');
  74. }
  75. if (body._id) {
  76. // 修改
  77. const apply = dock.apply.find(item => item._id === body._id);
  78. if (body.user_name) {
  79. apply.user_name = body.user_name;
  80. }
  81. if (body.goodsList) {
  82. apply.goodsList = body.goodsList;
  83. }
  84. if (body.contact_tel) {
  85. apply.contact_tel = body.contact_tel;
  86. }
  87. if (body.apply_time) {
  88. apply.apply_time = body.apply_time;
  89. }
  90. if (body.role) {
  91. apply.role = body.role;
  92. }
  93. if (body.status) {
  94. apply.status = body.status;
  95. }
  96. } else {
  97. // 如果是新添,查找之前该userid是否申请了,如果申请了,就把商品列表合并,如果没申请,就保存
  98. const { apply } = dock;
  99. const ri = apply.findIndex(f => f.user_id === body.user_id);
  100. if (ri < 0) {
  101. // 保存
  102. dock.apply.push({
  103. ...body,
  104. apply_time: moment().format('YYYY-MM-DD HH:mm:ss'),
  105. });
  106. } else {
  107. // 之前申请过,合并goodsList
  108. apply[ri].goodsList = [ ...apply[ri].goodsList, ...body.goodsList ];
  109. dock.apply = apply;
  110. console.log(apply);
  111. }
  112. }
  113. const res = await dock.save();
  114. const info = _.last(res.apply);
  115. return info;
  116. }
  117. async check({ id, dock_id }, { status }) {
  118. assert(status, '请审核是否允许参加对接会');
  119. const dock = await this.model.findOne({ _id: ObjectId(dock_id) });
  120. if (!dock) {
  121. throw new BusinessError('没有查询到该对接会');
  122. }
  123. const user = dock.apply.id(id);
  124. if (!user) {
  125. throw new BusinessError('没有查询到用户的申请');
  126. }
  127. user.status = status;
  128. const res = dock.save();
  129. return res;
  130. }
  131. // async dockCheck({ id }, { is_allowed, reason = '' }) {
  132. // const dock = await this.model.findOne({ _id: ObjectId(id) });
  133. // if (!dock) {
  134. // throw new BusinessError('没有查询到该对接会');
  135. // }
  136. // assert(is_allowed, '请选择审核结果');
  137. // dock.is_allowed = is_allowed;
  138. // dock.reason = reason;
  139. // const res = await dock.save();
  140. // return res;
  141. // }
  142. // 根据申请人id查询所有申请的对接会
  143. async myapply({ user_id, status, skip, limit }) {
  144. const total = await this.model.count({ status, 'apply.user_id': user_id });
  145. const data = await this.model
  146. .find({ status, 'apply.user_id': user_id })
  147. .skip(Number(skip))
  148. .limit(Number(limit));
  149. const result = { total, data };
  150. return result;
  151. }
  152. async updatevipuser({ id }, info) {
  153. const dock = await this.model.findById(id);
  154. if (!dock) {
  155. throw new BusinessError('没有查询到该对接会');
  156. }
  157. const vipuser = await dock.vipuser.id(info.vipid);
  158. if (!vipuser) {
  159. throw new BusinessError('没有查询到vip用户');
  160. }
  161. if (info.vipname) {
  162. vipuser.vipname = info.vipname;
  163. }
  164. if (info.vipphone) {
  165. vipuser.vipphone = info.vipphone;
  166. }
  167. if (info.role) {
  168. vipuser.role = info.role;
  169. }
  170. if (info.content) {
  171. vipuser.content = info.content;
  172. }
  173. return await dock.save();
  174. }
  175. async createvipuser({ id }, info) {
  176. const dock = await this.model.findById(id);
  177. if (!dock) {
  178. throw new BusinessError('没有查询到该对接会');
  179. }
  180. const vipuser = {};
  181. vipuser.uid = info.uid;
  182. vipuser.vipname = info.vipname;
  183. vipuser.vipphone = info.vipphone;
  184. vipuser.role = info.role;
  185. vipuser.company = info.company;
  186. vipuser.email = info.email;
  187. vipuser.content = info.content;
  188. dock.vipuser.push(vipuser);
  189. return await dock.save();
  190. }
  191. async dockfetch({ id }) {
  192. const dock = await this.model.findById(id);
  193. const data = JSON.parse(JSON.stringify(dock));
  194. if (dock) {
  195. const applydata = [];
  196. for (const elm of dock.apply) {
  197. const url =
  198. 'http://localhost:9004/api/market/transaction?userid=' + elm.user_id;
  199. const res = await this.ctx.curl(url, {
  200. method: 'get',
  201. headers: {
  202. 'content-type': 'application/json',
  203. },
  204. dataType: 'json',
  205. });
  206. const newdata = {
  207. ...JSON.parse(JSON.stringify(elm)),
  208. transdata: res.data.data,
  209. };
  210. applydata.push(newdata);
  211. }
  212. data.apply = applydata;
  213. }
  214. return data;
  215. }
  216. async getdock({ id }) {
  217. const res = await this.model.find({ vipuser: { $elemMatch: { uid: id } } });
  218. return res;
  219. }
  220. async getdockByopenid({ openid }) {
  221. const res = await this.model.findOne({ openid });
  222. return res;
  223. }
  224. async getapply({ user_id }) {
  225. const res = await this.model.find({ 'apply.user_id': { $regex: user_id } });
  226. return _.flatten(res);
  227. }
  228. async getgoodslist({ name }) {
  229. assert(name, '请输入产品名称');
  230. const res = await this.model.find({
  231. 'apply.goodsList.name': { $regex: name },
  232. });
  233. const data = [];
  234. for (const elm of res) {
  235. const applys = elm.apply;
  236. for (const el of applys) {
  237. const goodlists = el.goodsList;
  238. const goodlist = _.filter(goodlists, function(o) {
  239. const oo = _.includes(o.name, name);
  240. console.log(oo);
  241. return oo;
  242. });
  243. data.push(goodlist);
  244. }
  245. }
  246. return _.flatten(data);
  247. }
  248. async dockVipDelete(data) {
  249. const { id } = data;
  250. assert(id, '缺少vip用户的uid');
  251. console.log(id);
  252. const res = await this.model.update(
  253. { vipuser: { $elemMatch: { uid: id } } },
  254. { $pull: { vipuser: { uid: id } } }
  255. );
  256. return res;
  257. }
  258. async update({ id }, data) {
  259. const arr = await this.model.findById(id);
  260. const bool = _.isEqual(data.videodata, arr.videodata);
  261. if (!bool) {
  262. // TODO MQ
  263. const { mq } = this.ctx;
  264. if (mq) {
  265. const exchange = 'dock_video';
  266. const parm = {
  267. durable: true,
  268. };
  269. await mq.fanout(exchange, id, JSON.stringify(data.videodata), parm);
  270. }
  271. }
  272. const res = await this.model.update({ _id: ObjectId(id) }, data);
  273. return res;
  274. }
  275. }
  276. module.exports = ChatService;