This commit is contained in:
shiziyuan9527 2020-02-11 17:36:36 +08:00
commit 27098f14e2
6 changed files with 203 additions and 28 deletions

View File

@ -0,0 +1,30 @@
package io.metersphere.controller;
import io.metersphere.base.domain.Workspace;
import io.metersphere.service.WorkspaceService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@RequestMapping("workspace")
@RestController
public class WorkspaceController {
@Resource
private WorkspaceService workspaceService;
@PostMapping("save")
public Workspace saveWorkspace(@RequestBody Workspace workspace) {
return workspaceService.saveWorkspace(workspace);
}
@GetMapping("delete/{workspaceId}")
public void saveWorkspace(@PathVariable String workspaceId) {
workspaceService.deleteWorkspace(workspaceId);
}
@PostMapping("list")
public List<Workspace> getWorkspaceList() {
return workspaceService.getWorkspaceList();
}
}

View File

@ -0,0 +1,42 @@
package io.metersphere.controller.handler;
import com.alibaba.fastjson.JSON;
import io.metersphere.controller.ResultHolder;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
/**
* 统一处理返回结果集
*/
@RestControllerAdvice(value = {"io.metersphere.controller"})
public class ResultResponseBodyAdvice implements ResponseBodyAdvice<Object> {
@Override
public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> converterType) {
return MappingJackson2HttpMessageConverter.class.isAssignableFrom(converterType) || StringHttpMessageConverter.class.isAssignableFrom(converterType);
}
@Override
public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> converterType, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
// 处理空值
if (o == null && StringHttpMessageConverter.class.isAssignableFrom(converterType)) {
return null;
}
if (!(o instanceof ResultHolder)) {
if (o instanceof String) {
return JSON.toJSONString(ResultHolder.success(o));
}
return ResultHolder.success(o);
}
return o;
}
}

View File

@ -9,6 +9,7 @@ import io.metersphere.dto.UserDTO;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -16,6 +17,7 @@ import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Service @Service
@Transactional(rollbackFor = Exception.class)
public class UserService { public class UserService {
@Resource @Resource

View File

@ -0,0 +1,48 @@
package io.metersphere.service;
import io.metersphere.base.domain.Workspace;
import io.metersphere.base.mapper.WorkspaceMapper;
import io.metersphere.commons.MSException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;
@Service
@Transactional(rollbackFor = Exception.class)
public class WorkspaceService {
@Resource
private WorkspaceMapper workspaceMapper;
public Workspace saveWorkspace(Workspace workspace) {
if (StringUtils.isBlank(workspace.getName())) {
MSException.throwException("Workspace name cannot be null.");
}
// TODO 组织ID 暂无
if (StringUtils.isBlank(workspace.getOrganizationId())) {
workspace.setOrganizationId("root");
}
long currentTime = System.currentTimeMillis();
if (StringUtils.isBlank(workspace.getId())) {
workspace.setId(UUID.randomUUID().toString()); // 设置ID
workspace.setCreateTime(currentTime);
workspace.setUpdateTime(currentTime); // 首次 update time
workspaceMapper.insertSelective(workspace);
} else {
workspace.setUpdateTime(currentTime);
workspaceMapper.updateByPrimaryKeySelective(workspace);
}
return workspace;
}
public List<Workspace> getWorkspaceList() {
return workspaceMapper.selectByExample(null);
}
public void deleteWorkspace(String workspaceId) {
workspaceMapper.deleteByPrimaryKey(workspaceId);
}
}

View File

@ -81,7 +81,24 @@
} }
}); });
}, },
created: function () {
// ,,
document.addEventListener("keydown", this.watchEnter);
},
destroyed() {
//
document.removeEventListener("keydown", this.watchEnter);
},
methods: { methods: {
//
watchEnter(e) {
let keyNum = e.which; //
//keycody=13
if (keyNum === 13) {
//
this.submit('form');
}
},
submit(form) { submit(form) {
this.$refs[form].validate((valid) => { this.$refs[form].validate((valid) => {
if (valid) { if (valid) {

View File

@ -5,36 +5,39 @@
<el-row type="flex" justify="space-between" align="middle"> <el-row type="flex" justify="space-between" align="middle">
<span class="title">工作空间</span> <span class="title">工作空间</span>
<span class="search"> <span class="search">
<el-input type="text" size="small" placeholder="根据ID名称搜索" prefix-icon="el-icon-search" <el-input type="text" size="small" placeholder="根据名称搜索" prefix-icon="el-icon-search"
maxlength="60" v-model="condition" clearable/> maxlength="60" v-model="condition" clearable/>
</span> </span>
</el-row> </el-row>
</div> </div>
<el-table :data="items" style="width: 100%"> <el-table :data="items" style="width: 100%">
<el-table-column prop="id" label="ID" width="180"/>
<el-table-column prop="name" label="名称"/> <el-table-column prop="name" label="名称"/>
<el-table-column prop="owner" label="创建者"/> <el-table-column prop="description" label="描述"/>
<el-table-column prop="enable" label="启用"/> <el-table-column width="100">
<el-table-column width="50">
<template slot-scope="scope"> <template slot-scope="scope">
<el-button @click="edit(scope.row)" type="primary" icon="el-icon-edit" size="mini" circle <el-button @click="edit(scope.row)" type="primary" icon="el-icon-edit" size="mini" circle
class="edit"/> class="edit"/>
<el-popconfirm title="这个工作空间确定要删除吗?" @onConfirm="del(scope.row)">
<el-button slot="reference" type="primary" icon="el-icon-delete" size="mini"
circle
class="edit"/>
</el-popconfirm>
</template> </template>
</el-table-column> </el-table-column>
</el-table> </el-table>
</el-card> </el-card>
<ms-create-box :tips="btnTips" :exec="create"/> <ms-create-box :tips="btnTips" :exec="create"/>
<el-dialog title="创建工作空间" :visible.sync="createVisible" width="30%"> <el-dialog title="创建工作空间" :visible.sync="createVisible" width="30%">
<el-form :model="form" label-position="left" label-width="100px" size="small"> <el-form :model="form" :rules="rules" ref="form" label-position="left" label-width="100px" size="small">
<el-form-item label="名称"> <el-form-item label="名称" prop="name">
<el-input v-model="form.name" autocomplete="off"/> <el-input v-model="form.name" autocomplete="off"/>
</el-form-item> </el-form-item>
<el-form-item label="启用"> <el-form-item label="描述">
<el-switch v-model="form.enable"/> <el-input type="textarea" v-model="form.description"></el-input>
</el-form-item> </el-form-item>
</el-form> </el-form>
<span slot="footer" class="dialog-footer"> <span slot="footer" class="dialog-footer">
<el-button type="primary" @click="createVisible = false" size="medium">创建</el-button> <el-button type="primary" @click="submit('form')" size="medium">创建</el-button>
</span> </span>
</el-dialog> </el-dialog>
</div> </div>
@ -42,27 +45,59 @@
<script> <script>
import MsCreateBox from "./CreateBox"; import MsCreateBox from "./CreateBox";
import {Message} from "element-ui";
export default { export default {
name: "MsWorkspace", name: "MsWorkspace",
components: {MsCreateBox}, components: {MsCreateBox},
mounted() {
this.list();
},
methods: { methods: {
create() { create() {
this.createVisible = true; this.createVisible = true;
this.$get("/test/user", function (response) { this.form = {};
window.console.log(response); },
submit(formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
this.loading = true;
this.$post("/workspace/save", this.form, () => {
this.createVisible = false;
this.loading = false;
this.list();
Message.success('保存成功');
})
} else {
return false;
}
}); });
}, },
edit(row) { edit(row) {
this.createVisible = true;
window.console.log(row); window.console.log(row);
this.loading = true; this.loading = true;
let self = this; this.form = row;
let getUser1 = this.$get("/test/user");
let getUser2 = this.$get("/test/sleep"); // let self = this;
this.$all([getUser1, getUser2], function (r1, r2) { // let getUser1 = this.$get("/test/user");
window.console.log(r1.data.data, r2.data.data); // let getUser2 = this.$get("/test/sleep");
self.loading = false; // this.$all([getUser1, getUser2], function (r1, r2) {
// window.console.log(r1.data.data, r2.data.data);
// self.loading = false;
// });
},
del(row) {
this.$get('/workspace/delete/' + row.id, () => {
Message.success('删除成功');
this.list();
}); });
window.console.log(row);
},
list() {
this.$post('/workspace/list', {}, response => {
this.items = response.data;
})
} }
}, },
data() { data() {
@ -71,16 +106,17 @@
createVisible: false, createVisible: false,
btnTips: "添加工作空间", btnTips: "添加工作空间",
condition: "", condition: "",
items: [{ items: [],
id: '123456',
name: 'Default workspace',
owner: 'mk',
enable: "是"
}],
form: { form: {
name: "", // name: "",
enable: false // description: ""
} },
rules: {
name: [
{required: true, message: '请输入工作空间名称', trigger: 'blur'},
{min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur'}
]
},
} }
} }
} }