fix: 加入邮件通知功能

This commit is contained in:
gaoxiaolin.gao 2018-06-29 17:35:29 +08:00
parent 834994b048
commit 8577935975
3 changed files with 113 additions and 10 deletions

View File

@ -1,5 +1,5 @@
import React, { Component } from 'react';
import { Button, message } from 'antd';
import { Button, message, Checkbox } from 'antd';
import { connect } from 'react-redux'
import axios from 'axios';
import PropTypes from 'prop-types';
@ -28,7 +28,8 @@ class WikiPage extends Component {
isEditor: false,
isUpload: true,
desc: '',
markdown: ''
markdown: '',
notice: props.projectMsg.switch_notice
};
}
@ -83,7 +84,8 @@ class WikiPage extends Component {
let option = {
project_id: currProjectId,
desc,
markdown
markdown,
email_notice: this.state.notice
};
let result = await axios.post('/api/plugin/wiki_desc/up', option);
if (result.data.errcode === 0) {
@ -93,13 +95,21 @@ class WikiPage extends Component {
message.error(`更新失败: ${result.data.errmsg}`);
}
};
// 取消编辑
onCancel = () => {
this.setState({ isEditor: false });
};
// 邮件通知
onEmailNotice = (e) => {
this.setState({
notice: e.target.checked
})
}
render() {
const { isEditor, username, editorTime } = this.state;
const { isEditor, username, editorTime, notice } = this.state;
const editorEable = this.props.projectMsg.role === 'admin' || this.props.projectMsg.role === 'owner' || this.props.projectMsg.role === 'dev'
return (
<div className="g-row">
@ -114,7 +124,8 @@ class WikiPage extends Component {
<Button icon="upload" type="primary" className="upload-btn" onClick={this.onUpload}>
更新
</Button>
<Button onClick={this.onCancel}>取消</Button>
<Button onClick={this.onCancel} className="upload-btn">取消</Button>
<Checkbox checked={notice} onChange={this.onEmailNotice}>通知相关人员</Checkbox>
</div>
)}
</div>

View File

@ -1,14 +1,19 @@
const baseController = require('controllers/base.js');
const wikiModel = require('./wikiModel.js');
const projectModel = require('models/project.js');
const jsondiffpatch = require('jsondiffpatch');
const formattersHtml = jsondiffpatch.formatters.html;
const yapi = require('yapi.js');
const util = require('./util.js');
const fs = require('fs-extra')
const path = require('path');
class wikiController extends baseController {
constructor(ctx) {
super(ctx);
this.Model = yapi.getInst(wikiModel);
this.projectModel = yapi.getInst(projectModel);
}
@ -61,6 +66,9 @@ class wikiController extends baseController {
}
}
let notice = params.email_notice;
delete params.email_notice;
// 如果当前数据库里面没有数据
let result = await this.Model.get(params.project_id)
if(!result) {
@ -80,14 +88,57 @@ class wikiController extends baseController {
up_time: yapi.commons.time()
});
let upRes = await this.Model.up(result._id, data);
ctx.body = yapi.commons.resReturn(upRes)
if(notice) {
let logData = {
project_id: params.project_id,
current: params.desc,
old: result ? result.toObject().desc : ''
}
let diffView = util.showDiffMsg(jsondiffpatch, formattersHtml, logData);
let annotatedCss = fs.readFileSync(path.resolve(yapi.WEBROOT, 'node_modules/jsondiffpatch/public/formatters-styles/annotated.css'), 'utf8');
let htmlCss = fs.readFileSync(path.resolve(yapi.WEBROOT, 'node_modules/jsondiffpatch/public/formatters-styles/html.css'), 'utf8');
let project = await this.projectModel.getBaseInfo(params.project_id);
let wikiUrl = `http://${ctx.request.host}/project/${params.project_id}/wiki`
yapi.commons.sendNotice(params.project_id, {
title: `${this.getUsername()} 更新了wiki说明`,
content: `<html>
<head>
<style>
${annotatedCss}
${htmlCss}
</style>
</head>
<body>
<div><h3>${this.getUsername()}更新了wiki</h3>
<p>修改用户: ${this.getUsername()}</p>
<p>修改项目: <a href="${wikiUrl}">${project.name}</a></p>
<p>详细改动日志: ${this.diffHTML(diffView)}</p></div>
</body>
</html>`
})
}
// let upRes = await this.Model.get(result._id)
return (ctx.body = yapi.commons.resReturn(upRes));
return 1;
} catch (err) {
ctx.body = yapi.commons.resReturn(null, 400, err.message);
}
}
diffHTML(html) {
if (html.length === 0) {
return `<span style="color: #555">没有改动该操作未改动wiki数据</span>`
}
return html.map(item => {
return (`<div>
<h4 class="title">${item.title}</h4>
<div>${item.content}</div>
</div>`)
})
}
}

View File

@ -1,3 +1,6 @@
/**
* 格式化
* @param val {Object or String or Number} 日期对象 或是可new Date的对象或时间戳
@ -20,4 +23,42 @@ exports.formatDate = val => {
}
// 时间
const convert2Decimal = num => (num > 9 ? num : `0${num}`)
const convert2Decimal = num => (num > 9 ? num : `0${num}`)
// const json5_parse = require('../client/common.js').json5_parse;
exports.showDiffMsg = (jsondiffpatch, formattersHtml, curDiffData) => {
const diffText = (left, right) => {
left = left || '';
right = right || '';
if (left == right) {
return null;
}
var delta = jsondiffpatch.diff(left, right);
return formattersHtml.format(delta, left)
}
let diffView = [];
if (curDiffData && typeof curDiffData === 'object' && curDiffData.current) {
const { current, old } = curDiffData;
if (current != old) {
diffView.push({
title: 'wiki更新',
content: diffText(old, current)
})
}
}
return diffView = diffView.filter(item => item.content)
}