12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- 'use strict';
- const _ = require('lodash');
- const assert = require('assert');
- const Service = require('egg').Service;
- const columns = require('../public/query');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const moment = require('moment');
- //
- class UsualService extends Service {
- constructor(ctx) {
- super(ctx, 'usual');
- this.http = this.ctx.service.util.httpUtil;
- this.fetchService = this.ctx.service.fetch;
- this.createService = this.ctx.service.create;
- this.updateService = this.ctx.service.update;
- this.wxService = this.ctx.service.wx;
- this.prefix = '/db';
- }
- async reserve({ id }) {
- assert(id, '缺少预约安排信息');
- const data = await this.http.$post(`${this.prefix}/reserve`, { query: { id } });
- if (data && data.code) {
- throw new BusinessError(ErrorCode.SERVICE_FAULT, data.message);
- }
- let result = true;
- if (data <= 0) result = false;
- return { data: result };
- }
- /**
- * 发送消息
- * @param {Object} body 参数
- * @property {Array} user_ids 用户列表
- * @property {String} content 消息内容
- */
- async sendMessage({ user_ids, content }) {
- if (!this.ctx.user) throw new BusinessError(ErrorCode.SERVICE_FAULT, '未获取发送人信息,无法发送信息');
- // 找到对应的用户,且用户存在openid的数据
- const templateId = 'vQi6KbytlFvyT6rLjeAwih5Ji6qMY2nBGXeMvNXrUkw';
- const time = moment().format('YYYY-MM-DD HH:mm:ss');
- const { wxServer } = this.app.config;
- if (!wxServer) throw new BusinessError(ErrorCode.SERVICE_FAULT, '未设置已读地址');
- const data = { templateId, data: { keyword2: { value: content }, keyword3: { value: time } } };
- const warningMessage = { send_id: this.ctx.user.id, send_name: this.ctx.user.name };
- for (const id of user_ids) {
- const user = await this.fetchService.index({ table: 'security_guard_base' }, { id });
- if (!user) continue;
- const { gopenid: openid, unionid, id: receive_id, name: receive_name } = user.data;
- // 发送消息
- const msgData = {
- ...warningMessage,
- send_time: time,
- receive_id,
- receive_name,
- content,
- };
- const msg = await this.createService.index({ table: 'warning_message' }, msgData);
- if (!msg.data) throw new BusinessError(ErrorCode.SERVICE_FAULT, '创建消息失败');
- const sendObject = { ...data, openid, unionid, url: `${wxServer}/api/usual/readMessage/${msg.data.id}` };
- await this.wxService.sendTemplate(sendObject);
- }
- }
- async readMessage({ id }) {
- await this.updateService.index({ table: 'warning_message' }, { id }, { is_read: '1' });
- }
- }
- module.exports = UsualService;
|