1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- 'use strict';
- const assert = require('assert');
- const _ = require('lodash');
- const { ObjectId } = require('mongoose').Types;
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const moment = require('moment');
- class ChatService extends CrudService {
- constructor(ctx) {
- super(ctx, 'dock');
- this.model = this.ctx.model.Dock;
- }
- async create(body) {
- const res = await this.model.create(body);
- if (res) {
- const url = this.ctx.app.config.axios.auth.baseUrl;
- const newdata = {
- name: body.adminuser,
- phone: body.phone,
- passwd: body.passwd,
- uid: res.id,
- role: '4',
- pid: body.pid,
- code: body.code,
- };
- const user = await this.ctx.curl(url, {
- method: 'post',
- headers: {
- 'content-type': 'application/json',
- },
- dataType: 'json',
- data: JSON.stringify(newdata),
- });
- if (user.data.errcode === 0) {
- const result = await this.model.findById(res.id);
- result.u_id = user.data.data.id;
- result.save();
- }
- }
- return res;
- }
- async apply({ id }, body) {
- const dock = await this.model.findOne({ _id: ObjectId(id) });
- if (!dock) {
- throw new BusinessError('没有查询到该对接会');
- }
- dock.apply.push({
- ...body,
- apply_time: moment().format('YYYY-MM-DD HH:mm:ss'),
- });
- const res = await dock.save();
- const info = _.last(res.apply);
- return info;
- }
- async check({ id, dock_id }, { status }) {
- assert(status, '请审核是否允许参加对接会');
- const dock = await this.model.findOne({ _id: ObjectId(dock_id) });
- if (!dock) {
- throw new BusinessError('没有查询到该对接会');
- }
- const user = dock.apply.id(id);
- if (!user) {
- throw new BusinessError('没有查询到用户的申请');
- }
- user.status = status;
- const res = dock.save();
- return res;
- }
- async dockCheck({ id }, { is_allowed, reason = '' }) {
- const dock = await this.model.findOne({ _id: ObjectId(id) });
- if (!dock) {
- throw new BusinessError('没有查询到该对接会');
- }
- assert(is_allowed, '请选择审核结果');
- dock.is_allowed = is_allowed;
- dock.reason = reason;
- const res = await dock.save();
- return res;
- }
- // 根据申请人id查询所有申请的对接会
- async myapply({ user_id, status, skip, limit }) {
- const total = await this.model.count({ status, 'apply.user_id': user_id });
- const data = await this.model.find({ status, 'apply.user_id': user_id }).skip(Number(skip)).limit(Number(limit));
- const result = { total, data };
- return result;
- }
- }
- module.exports = ChatService;
|