dock.js 7.3 KB

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