dock.js 7.7 KB

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