feat: 跳转新连接,复制内容

This commit is contained in:
waliang.wang 2017-07-31 19:43:08 +08:00
parent 9d31a3bd32
commit 6215d66df5
7 changed files with 89 additions and 19 deletions

View File

@ -8,7 +8,8 @@ import {
GET_INTERFACE_RES_PARAMS, GET_INTERFACE_RES_PARAMS,
PUSH_INTERFACE_NAME, PUSH_INTERFACE_NAME,
PUSH_INTERFACE_METHOD, PUSH_INTERFACE_METHOD,
FETCH_INTERFACE_PROJECT FETCH_INTERFACE_PROJECT,
ADD_INTERFACE_CLIPBOARD
} from '../constants/action-types.js' } from '../constants/action-types.js'
import axios from 'axios' import axios from 'axios'
@ -80,4 +81,11 @@ export function fetchInterfaceProject(id) {
type: FETCH_INTERFACE_PROJECT, type: FETCH_INTERFACE_PROJECT,
payload: axios.get('/project/get', { params: {id}}) payload: axios.get('/project/get', { params: {id}})
} }
} }
export function addInterfaceClipboard (func) {
return {
type: ADD_INTERFACE_CLIPBOARD,
payload: func
}
}

View File

@ -17,6 +17,7 @@ export const GET_INTERFACE_REQ_PARAMS = 'GET_INTERFACE_REQ_PARAMS'
export const GET_INTERFACE_RES_PARAMS = 'GET_INTERFACE_RES_PARAMS' export const GET_INTERFACE_RES_PARAMS = 'GET_INTERFACE_RES_PARAMS'
export const PUSH_INTERFACE_METHOD = 'PUSH_INTERFACE_METHOD' export const PUSH_INTERFACE_METHOD = 'PUSH_INTERFACE_METHOD'
export const FETCH_INTERFACE_PROJECT = 'FETCH_INTERFACE_PROJECT' export const FETCH_INTERFACE_PROJECT = 'FETCH_INTERFACE_PROJECT'
export const ADD_INTERFACE_CLIPBOARD = 'ADD_INTERFACE_CLIPBOARD'
// group // group
export const FETCH_GROUP_LIST = 'FETCH_GROUP_LIST' export const FETCH_GROUP_LIST = 'FETCH_GROUP_LIST'

View File

@ -38,7 +38,8 @@ const success = () => {
url: state.addInterface.url, url: state.addInterface.url,
seqGroup: state.addInterface.seqGroup, seqGroup: state.addInterface.seqGroup,
interfaceName: state.addInterface.interfaceName, interfaceName: state.addInterface.interfaceName,
server_ip: state.login.server_ip server_ip: state.login.server_ip,
clipboard: state.addInterface.clipboard
} }
}, },
{ {
@ -67,7 +68,8 @@ class AddInterface extends Component {
getReqParams: PropTypes.func, getReqParams: PropTypes.func,
getResParams: PropTypes.func, getResParams: PropTypes.func,
pushInputValue: PropTypes.func, pushInputValue: PropTypes.func,
pushInterfaceName: PropTypes.func pushInterfaceName: PropTypes.func,
clipboard: PropTypes.func
} }
constructor (props) { constructor (props) {
@ -110,6 +112,7 @@ class AddInterface extends Component {
const reg = /add-interface\/edit\/(\d+)/g const reg = /add-interface\/edit\/(\d+)/g
const regTwo = /add-interface\/(\d+)/g const regTwo = /add-interface\/(\d+)/g
const url = location.href const url = location.href
if ( url.match(reg) ) { if ( url.match(reg) ) {
return RegExp.$1 return RegExp.$1
} else { } else {
@ -182,6 +185,19 @@ class AddInterface extends Component {
}) })
} }
initInterfaceDataTwo (interfaceId) {
const params = { id: interfaceId }
axios.get('/interface/get', {params: params})
.then(result => {
result = result.data.data
this.getMockURL(result.project_id, result)
})
.catch(e => {
console.log(e)
})
}
setLoading (boolean) { setLoading (boolean) {
this.setState({ this.setState({
isLoading: boolean ? 'is-loading' : '' isLoading: boolean ? 'is-loading' : ''
@ -214,14 +230,23 @@ class AddInterface extends Component {
}) })
} }
@autobind
jumpEditUrl (_id) {
const origin = location.origin
const pathname = location.pathname
location.href = `${origin}${pathname}#/add-interface/edit/${_id}`
this.initInterfaceDataTwo(_id)
setTimeout(() => {
this.props.clipboard()
}, 1000)
}
@autobind @autobind
saveForms () { saveForms () {
let postURL = undefined let postURL = undefined
const { interfaceName, url, seqGroup, reqParams, resParams, method } = this.props const { interfaceName, url, seqGroup, reqParams, resParams, method } = this.props
const ifTrue = this.verificationURL() const ifTrue = this.verificationURL()
const interfaceId = this.getInterfaceId() const interfaceId = this.getInterfaceId()
const origin = location.origin
const pathname = location.pathname
const params = { const params = {
title: interfaceName, title: interfaceName,
path: url, path: url,
@ -244,14 +269,21 @@ class AddInterface extends Component {
this.setLoading(true) this.setLoading(true)
axios.post(postURL, params) axios.post(postURL, params)
.then(() => { .then(data => {
const id = data.data.data._id
const _id = id || interfaceId
this.setLoading() this.setLoading()
success() success()
this.changeState(true) this.changeState(true)
// 初始化 mock // 初始化 mock
this.mockData() this.mockData()
if (id) {
this.setState({showMock: 'show-mock'})
}
location.href = `${origin}${pathname}#/project/${interfaceId}` this.jumpEditUrl(_id)
}) })
.catch(e => { .catch(e => {
console.log(e) console.log(e)
@ -263,12 +295,13 @@ class AddInterface extends Component {
const { server_ip } = this.props const { server_ip } = this.props
const { isLoading, isSave, mockJson='', mockURL, tagName, showMock } = this.state const { isLoading, isSave, mockJson='', mockURL, tagName, showMock } = this.state
let Pane = '' let Pane = ''
let mockGroup = ''
if (showMock) { if (showMock) {
Pane = <TabPane tab="请求接口" key="3"><InterfaceTest /></TabPane> Pane = <TabPane tab="请求接口" key="3"><InterfaceTest /></TabPane>
mockGroup = <MockUrl mockURL={mockURL} serverIp={server_ip} projectData={this.state.projectData} showMock={showMock}/>
} }
return ( return (
<section className="add-interface-box"> <section className="add-interface-box">
<div className="content"> <div className="content">
<Tabs type="card"> <Tabs type="card">
<TabPane tab={tagName} key="1"> <TabPane tab={tagName} key="1">
@ -276,7 +309,7 @@ class AddInterface extends Component {
<ReqMethod /> <ReqMethod />
<ReqHeader /> <ReqHeader />
<ReqParams data={this.props} /> <ReqParams data={this.props} />
<MockUrl mockURL={mockURL} serverIp={server_ip} projectData={this.state.projectData} showMock={showMock}/> {mockGroup}
<h3 className="req-title">返回部分</h3> <h3 className="req-title">返回部分</h3>
<ResParams /> <ResParams />
<Result isSave={isSave} mockJson={mockJson} /> <Result isSave={isSave} mockJson={mockJson} />
@ -284,7 +317,7 @@ class AddInterface extends Component {
{Pane} {Pane}
</Tabs> </Tabs>
</div> </div>
<Affix offsetBottom={0} className="save-button" onChange={affixed => console.log(affixed)}> <Affix offsetBottom={0} className="save-button">
<Button type="primary" onClick={this.saveForms}>保存</Button> <Button type="primary" onClick={this.saveForms}>保存</Button>
</Affix> </Affix>
<div className={`loading ${isLoading}`}></div> <div className={`loading ${isLoading}`}></div>

View File

@ -244,6 +244,11 @@
} }
#mock-clipboard { #mock-clipboard {
margin: 3px 0 0 0; margin: 3px 0 0 0;
background: #108ee9;
border-radius: 5px;
padding: 5px 20px;
border: none;
color: #FFF;
} }
} }

View File

@ -1,20 +1,34 @@
import '../AddInterface.scss' import '../AddInterface.scss'
import React, { Component } from 'react' import React, { Component } from 'react'
import { Button, message } from 'antd' import { message } from 'antd'
import Clipboard from 'clipboard' import Clipboard from 'clipboard'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import {
addInterfaceClipboard
} from '../../../actions/addInterface.js'
const success = () => { const success = () => {
message.success('复制成功!') message.success('复制成功!')
} }
@connect(
() => {
return {}
},
{
addInterfaceClipboard
}
)
class MockUrl extends Component { class MockUrl extends Component {
static propTypes = { static propTypes = {
mockURL: PropTypes.string, mockURL: PropTypes.string,
serverIp: PropTypes.string, serverIp: PropTypes.string,
mockData: PropTypes.string, mockData: PropTypes.string,
showMock: PropTypes.string, showMock: PropTypes.string,
projectData: PropTypes.object projectData: PropTypes.object,
addInterfaceClipboard: PropTypes.func
} }
constructor(props) { constructor(props) {
@ -22,13 +36,15 @@ class MockUrl extends Component {
} }
componentDidMount () { componentDidMount () {
this.props.addInterfaceClipboard(this.clipboard)
setTimeout(this.clipboard, 500) setTimeout(this.clipboard, 500)
} }
clipboard () { clipboard () {
document.querySelector('#clipboard-button').innerHTML = '<button id="mock-clipboard">复制</button>'
const btn = document.querySelector('#mock-clipboard') const btn = document.querySelector('#mock-clipboard')
const txt = document.querySelector('#mock-p').innerHTML const txt = document.querySelector('#mock-p').innerHTML
new Clipboard(btn, { new Clipboard(btn, {
text: () => txt, text: () => txt,
target () { target () {
@ -39,11 +55,12 @@ class MockUrl extends Component {
render () { render () {
const { serverIp, showMock } = this.props const { serverIp, showMock } = this.props
return ( return (
<section className={`mock-url-box ${showMock}`}> <section className={`mock-url-box ${showMock}`}>
<span className="title">mock地址 : </span> <span className="title">mock地址 : </span>
<p id="mock-p">{this.props.mockURL}</p> <p id="mock-p">{this.props.mockURL}</p>
<Button type="primary" id="mock-clipboard">复制</Button> <span id="clipboard-button"></span>
<div className="host"><label>请配置host:</label> {this.props.projectData.prd_host} { serverIp }</div> <div className="host"><label>请配置host:</label> {this.props.projectData.prd_host} { serverIp }</div>
</section> </section>
) )

View File

@ -66,9 +66,8 @@ class ReqMethod extends Component {
<td> <td>
<span className="h3">请求方式 : </span> <span className="h3">请求方式 : </span>
<Select value={method} style={{ width: 180 }} onChange={this.handleChange} size="large"> <Select value={method} style={{ width: 180 }} onChange={this.handleChange} size="large">
<Option value="">选择请求方式</Option>
<Option value="POST">POST</Option>
<Option value="GET">GET</Option> <Option value="GET">GET</Option>
<Option value="POST">POST</Option>
<Option value="PUT">PUT</Option> <Option value="PUT">PUT</Option>
<Option value="DELETE">DELETE</Option> <Option value="DELETE">DELETE</Option>
</Select> </Select>

View File

@ -8,7 +8,8 @@ import {
GET_INTERFACE_RES_PARAMS, GET_INTERFACE_RES_PARAMS,
PUSH_INTERFACE_NAME, PUSH_INTERFACE_NAME,
PUSH_INTERFACE_METHOD, PUSH_INTERFACE_METHOD,
FETCH_INTERFACE_PROJECT FETCH_INTERFACE_PROJECT,
ADD_INTERFACE_CLIPBOARD
} from '../../constants/action-types.js' } from '../../constants/action-types.js'
const initialState = { const initialState = {
@ -25,7 +26,8 @@ const initialState = {
], ],
reqParams: '', reqParams: '',
resParams: '', resParams: '',
project: {} project: {},
clipboard: () => {}
} }
export default (state = initialState, action) => { export default (state = initialState, action) => {
@ -80,6 +82,11 @@ export default (state = initialState, action) => {
...state, ...state,
project: action.payload.data.data project: action.payload.data.data
} }
case ADD_INTERFACE_CLIPBOARD:
return {
...state,
clipboard: action.payload
}
default: default:
return state return state
} }