mirror of
https://github.com/YMFE/yapi.git
synced 2025-01-18 13:04:46 +08:00
feat: perfect websocket
This commit is contained in:
parent
adbe2f7c6b
commit
99ef07342c
@ -1,11 +1,12 @@
|
||||
import React,{Component} from 'react'
|
||||
import React, { Component } from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
import { connect } from 'react-redux';
|
||||
import InterfaceEditForm from './InterfaceEditForm.js'
|
||||
import { updateInterfaceData } from '../../../../reducer/modules/interface.js';
|
||||
import axios from 'axios'
|
||||
import {message} from 'antd'
|
||||
import { message } from 'antd'
|
||||
import './Edit.scss'
|
||||
import { withRouter, Link } from 'react-router-dom';
|
||||
|
||||
@connect(
|
||||
state => {
|
||||
@ -13,66 +14,93 @@ import './Edit.scss'
|
||||
curdata: state.inter.curdata,
|
||||
currProject: state.project.currProject
|
||||
}
|
||||
},{
|
||||
}, {
|
||||
updateInterfaceData
|
||||
}
|
||||
)
|
||||
|
||||
class InterfaceEdit extends Component{
|
||||
class InterfaceEdit extends Component {
|
||||
static propTypes = {
|
||||
curdata: PropTypes.object,
|
||||
currProject:PropTypes.object,
|
||||
updateInterfaceData: PropTypes.func
|
||||
currProject: PropTypes.object,
|
||||
updateInterfaceData: PropTypes.func,
|
||||
match: PropTypes.object
|
||||
}
|
||||
|
||||
constructor(props){
|
||||
constructor(props) {
|
||||
super(props)
|
||||
const {curdata, currProject} = this.props;
|
||||
const { curdata, currProject } = this.props;
|
||||
this.state = {
|
||||
mockUrl: location.protocol + '//' + location.hostname + (location.port !== "" ? ":" + location.port : "") + `/mock/${currProject._id}${currProject.basepath}${curdata.path}`
|
||||
mockUrl: location.protocol + '//' + location.hostname + (location.port !== "" ? ":" + location.port : "") + `/mock/${currProject._id}${currProject.basepath}${curdata.path}`,
|
||||
curdata: {},
|
||||
status: 0
|
||||
}
|
||||
}
|
||||
|
||||
onSubmit =async (params)=>{
|
||||
onSubmit = async (params) => {
|
||||
params.id = params._id = this.props.curdata._id;
|
||||
let result =await axios.post('/api/interface/up', params);
|
||||
if(result.data.errcode === 0){
|
||||
let result = await axios.post('/api/interface/up', params);
|
||||
if (result.data.errcode === 0) {
|
||||
this.props.updateInterfaceData(params);
|
||||
message.success('保存成功');
|
||||
}else{
|
||||
} else {
|
||||
message.success(result.data.errmsg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
componentWillMount(){
|
||||
let s = new WebSocket('ws://yapi.local.qunar.com:3000/api/interface/solve_conflict?id=1');
|
||||
s.onopen = (e)=>{
|
||||
console.log('open',e)
|
||||
s.send('abc')
|
||||
//s.close()
|
||||
s.send('aaaaa')
|
||||
componentWillUnmount(){
|
||||
console.log('unmount')
|
||||
try{
|
||||
if(this.state.status === 1){
|
||||
this.WebSocket.close()
|
||||
}
|
||||
}catch(e){
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
componentWillMount() {
|
||||
let s = new WebSocket('ws://yapi.local.qunar.com:3000/api/interface/solve_conflict?id=' + this.props.match.params.actionId);
|
||||
s.onopen = () => {
|
||||
this.WebSocket = s;
|
||||
}
|
||||
|
||||
s.onclose = (e)=>{
|
||||
console.log('close',e)
|
||||
}
|
||||
s.onmessage = (e) => {
|
||||
let result = JSON.parse(e.data);
|
||||
if (result.errno === 0) {
|
||||
this.setState({
|
||||
curdata: result.data,
|
||||
status: 1
|
||||
})
|
||||
} else {
|
||||
this.setState({
|
||||
curdata: result.data,
|
||||
status: 2
|
||||
})
|
||||
}
|
||||
|
||||
s.onmessage = (e)=>{
|
||||
console.log('message',e)
|
||||
}
|
||||
|
||||
s.onerror = (e)=>{
|
||||
console.log('error',e)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
render(){
|
||||
render() {
|
||||
console.log(this.state.status)
|
||||
return <div className="interface-edit">
|
||||
<InterfaceEditForm mockUrl={this.state.mockUrl} basepath={this.props.currProject.basepath} onSubmit={this.onSubmit} curdata={this.props.curdata} />
|
||||
{this.state.status === 1 ?
|
||||
<InterfaceEditForm mockUrl={this.state.mockUrl} basepath={this.props.currProject.basepath} onSubmit={this.onSubmit} curdata={this.state.curdata} />
|
||||
:
|
||||
null}
|
||||
{
|
||||
this.state.status === 2 ?
|
||||
<div style={{textAlign: 'center', fontSize: '14px', paddingTop: '10px'}}>
|
||||
<Link to={'/user/profile/' + this.state.curdata.uid}><b>{this.state.curdata.username}</b></Link>
|
||||
<span>正在编辑该接口,请稍后再试...</span>
|
||||
</div>
|
||||
:
|
||||
null}
|
||||
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
export default InterfaceEdit;
|
||||
export default withRouter(InterfaceEdit);
|
@ -1,7 +1,7 @@
|
||||
import interfaceModel from '../models/interface.js';
|
||||
import baseController from './base.js';
|
||||
import yapi from '../yapi.js';
|
||||
|
||||
import userModel from '../models/user.js';
|
||||
|
||||
class interfaceController extends baseController {
|
||||
constructor(ctx) {
|
||||
@ -306,18 +306,31 @@ class interfaceController extends baseController {
|
||||
}
|
||||
|
||||
async solveConflict(ctx) {
|
||||
let id = parseInt(ctx.query.id, 10);
|
||||
if(!id) return ctx.websocket.send("id 参数有误");
|
||||
|
||||
ctx.websocket.send('Hello World');
|
||||
ctx.websocket.on('message', function (message) {
|
||||
// do something with the message from client
|
||||
console.log(message);
|
||||
});
|
||||
|
||||
ctx.websocket.on('close', function(){
|
||||
console.log('websocket: close')
|
||||
})
|
||||
try {
|
||||
let id = parseInt(ctx.query.id, 10), result, userInst, userinfo, data;
|
||||
if (!id) return ctx.websocket.send("id 参数有误");
|
||||
result = await this.Model.get(id), userinfo;
|
||||
if(result.edit_uid !== 0 && result.edit_uid !== this.getUid()){
|
||||
userInst = yapi.getInst(userModel);
|
||||
userinfo = await userInst.findById(result.edit_uid);
|
||||
data = {
|
||||
errno: result.edit_uid,
|
||||
data: {uid: result.edit_uid, username: userinfo.username}
|
||||
}
|
||||
}else{
|
||||
this.Model.upEditUid(id, this.getUid() ).then()
|
||||
data = {
|
||||
errno: 0,
|
||||
data: result
|
||||
}
|
||||
}
|
||||
ctx.websocket.send(JSON.stringify(data));
|
||||
ctx.websocket.on('close', ()=> {
|
||||
this.Model.upEditUid(id, 0).then()
|
||||
})
|
||||
} catch (err) {
|
||||
yapi.commons.log(err, 'error')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,7 +54,7 @@ class interfaceModel extends baseModel {
|
||||
enum: ['json', 'text', 'xml']
|
||||
},
|
||||
res_body: String
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
save(data) {
|
||||
@ -123,7 +123,15 @@ class interfaceModel extends baseModel {
|
||||
data.up_time = yapi.commons.time();
|
||||
return this.model.update({
|
||||
_id: id
|
||||
}, data, { runValidators: true });
|
||||
}, data, {runValidators: true });
|
||||
}
|
||||
|
||||
upEditUid(id, uid){
|
||||
return this.model.update({
|
||||
_id: id
|
||||
},
|
||||
{edit_uid: uid},
|
||||
{runValidators: true });
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@ class interfaceCase extends baseModel {
|
||||
method: { type: String },
|
||||
req_params: [{
|
||||
name: String, value: String
|
||||
}],
|
||||
}],
|
||||
req_query: [{
|
||||
name: String, value: String
|
||||
}],
|
||||
|
@ -1,14 +1,13 @@
|
||||
import koaRouter from 'koa-router';
|
||||
const route = require('koa-route');
|
||||
import interfaceController from './controllers/interface.js';
|
||||
|
||||
const router = koaRouter();
|
||||
|
||||
function websocket(app) {
|
||||
console.log('load websocket...')
|
||||
app.ws.use(function (ctx, next) {
|
||||
return next(ctx);
|
||||
});
|
||||
app.ws.use(route.all('/api/interface/solve_conflict', async function (ctx) {
|
||||
router.get('/api/interface/solve_conflict', async function (ctx) {
|
||||
let inst = new interfaceController(ctx);
|
||||
await inst.init(ctx);
|
||||
if (inst.$auth === true) {
|
||||
@ -16,8 +15,10 @@ function websocket(app) {
|
||||
} else {
|
||||
ctx.ws.send('请登录...');
|
||||
}
|
||||
}));
|
||||
})
|
||||
|
||||
app.ws.use(router.routes())
|
||||
app.ws.use(router.allowedMethods());
|
||||
}
|
||||
|
||||
module.exports = websocket
|
@ -1,5 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
var _stringify = require('babel-runtime/core-js/json/stringify');
|
||||
|
||||
var _stringify2 = _interopRequireDefault(_stringify);
|
||||
|
||||
var _regenerator = require('babel-runtime/regenerator');
|
||||
|
||||
var _regenerator2 = _interopRequireDefault(_regenerator);
|
||||
@ -40,6 +44,10 @@ var _yapi = require('../yapi.js');
|
||||
|
||||
var _yapi2 = _interopRequireDefault(_yapi);
|
||||
|
||||
var _user = require('../models/user.js');
|
||||
|
||||
var _user2 = _interopRequireDefault(_user);
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
var interfaceController = function (_baseController) {
|
||||
@ -593,38 +601,77 @@ var interfaceController = function (_baseController) {
|
||||
key: 'solveConflict',
|
||||
value: function () {
|
||||
var _ref6 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee6(ctx) {
|
||||
var id;
|
||||
var _this2 = this;
|
||||
|
||||
var id, result, userInst, userinfo, data;
|
||||
return _regenerator2.default.wrap(function _callee6$(_context6) {
|
||||
while (1) {
|
||||
switch (_context6.prev = _context6.next) {
|
||||
case 0:
|
||||
id = parseInt(ctx.query.id, 10);
|
||||
_context6.prev = 0;
|
||||
id = parseInt(ctx.query.id, 10), result = void 0, userInst = void 0, userinfo = void 0, data = void 0;
|
||||
|
||||
if (id) {
|
||||
_context6.next = 3;
|
||||
_context6.next = 4;
|
||||
break;
|
||||
}
|
||||
|
||||
return _context6.abrupt('return', ctx.websocket.send("id 参数有误"));
|
||||
|
||||
case 3:
|
||||
|
||||
ctx.websocket.send('Hello World');
|
||||
ctx.websocket.on('message', function (message) {
|
||||
// do something with the message from client
|
||||
console.log(message);
|
||||
});
|
||||
|
||||
ctx.websocket.on('close', function () {
|
||||
console.log('websocket: close');
|
||||
});
|
||||
case 4:
|
||||
_context6.next = 6;
|
||||
return this.Model.get(id);
|
||||
|
||||
case 6:
|
||||
result = _context6.sent;
|
||||
userinfo;
|
||||
|
||||
if (!(result.edit_uid !== 0 && result.edit_uid !== this.getUid())) {
|
||||
_context6.next = 16;
|
||||
break;
|
||||
}
|
||||
|
||||
userInst = _yapi2.default.getInst(_user2.default);
|
||||
_context6.next = 12;
|
||||
return userInst.findById(result.edit_uid);
|
||||
|
||||
case 12:
|
||||
userinfo = _context6.sent;
|
||||
|
||||
data = {
|
||||
errno: result.edit_uid,
|
||||
data: { uid: result.edit_uid, username: userinfo.username }
|
||||
};
|
||||
_context6.next = 18;
|
||||
break;
|
||||
|
||||
case 16:
|
||||
this.Model.upEditUid(id, this.getUid()).then();
|
||||
data = {
|
||||
errno: 0,
|
||||
data: result
|
||||
};
|
||||
|
||||
case 18:
|
||||
ctx.websocket.send((0, _stringify2.default)(data));
|
||||
ctx.websocket.on('close', function () {
|
||||
_this2.Model.upEditUid(id, 0).then();
|
||||
});
|
||||
_context6.next = 25;
|
||||
break;
|
||||
|
||||
case 22:
|
||||
_context6.prev = 22;
|
||||
_context6.t0 = _context6['catch'](0);
|
||||
|
||||
_yapi2.default.commons.log(_context6.t0, 'error');
|
||||
|
||||
case 25:
|
||||
case 'end':
|
||||
return _context6.stop();
|
||||
}
|
||||
}
|
||||
}, _callee6, this);
|
||||
}, _callee6, this, [[0, 22]]);
|
||||
}));
|
||||
|
||||
function solveConflict(_x6) {
|
||||
|
@ -170,6 +170,13 @@ var interfaceModel = function (_baseModel) {
|
||||
_id: id
|
||||
}, data, { runValidators: true });
|
||||
}
|
||||
}, {
|
||||
key: 'upEditUid',
|
||||
value: function upEditUid(id, uid) {
|
||||
return this.model.update({
|
||||
_id: id
|
||||
}, { edit_uid: uid }, { runValidators: true });
|
||||
}
|
||||
}]);
|
||||
return interfaceModel;
|
||||
}(_base2.default);
|
||||
|
@ -18,15 +18,14 @@ var _interface2 = _interopRequireDefault(_interface);
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
var route = require('koa-route');
|
||||
|
||||
var router = (0, _koaRouter2.default)();
|
||||
|
||||
function websocket(app) {
|
||||
console.log('load websocket...');
|
||||
app.ws.use(function (ctx, next) {
|
||||
return next(ctx);
|
||||
});
|
||||
app.ws.use(route.all('/api/interface/solve_conflict', function () {
|
||||
router.get('/api/interface/solve_conflict', function () {
|
||||
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx) {
|
||||
var inst;
|
||||
return _regenerator2.default.wrap(function _callee$(_context) {
|
||||
@ -64,7 +63,10 @@ function websocket(app) {
|
||||
return function (_x) {
|
||||
return _ref.apply(this, arguments);
|
||||
};
|
||||
}()));
|
||||
}());
|
||||
|
||||
app.ws.use(router.routes());
|
||||
app.ws.use(router.allowedMethods());
|
||||
}
|
||||
|
||||
module.exports = websocket;
|
Loading…
Reference in New Issue
Block a user