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.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
@ -16,6 +17,7 @@ import java.util.List;
import java.util.stream.Collectors;
@Service
@Transactional(rollbackFor = Exception.class)
public class UserService {
@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: {
//
watchEnter(e) {
let keyNum = e.which; //
//keycody=13
if (keyNum === 13) {
//
this.submit('form');
}
},
submit(form) {
this.$refs[form].validate((valid) => {
if (valid) {

View File

@ -5,64 +5,99 @@
<el-row type="flex" justify="space-between" align="middle">
<span class="title">工作空间</span>
<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/>
</span>
</el-row>
</div>
<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="owner" label="创建者"/>
<el-table-column prop="enable" label="启用"/>
<el-table-column width="50">
<el-table-column prop="description" label="描述"/>
<el-table-column width="100">
<template slot-scope="scope">
<el-button @click="edit(scope.row)" type="primary" icon="el-icon-edit" size="mini" circle
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>
</el-table-column>
</el-table>
</el-card>
<ms-create-box :tips="btnTips" :exec="create"/>
<el-dialog title="创建工作空间" :visible.sync="createVisible" width="30%">
<el-form :model="form" label-position="left" label-width="100px" size="small">
<el-form-item label="名称">
<el-form :model="form" :rules="rules" ref="form" label-position="left" label-width="100px" size="small">
<el-form-item label="名称" prop="name">
<el-input v-model="form.name" autocomplete="off"/>
</el-form-item>
<el-form-item label="启用">
<el-switch v-model="form.enable"/>
<el-form-item label="描述">
<el-input type="textarea" v-model="form.description"></el-input>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button type="primary" @click="createVisible = false" size="medium">创建</el-button>
</span>
<el-button type="primary" @click="submit('form')" size="medium">创建</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
import MsCreateBox from "./CreateBox";
import {Message} from "element-ui";
export default {
name: "MsWorkspace",
components: {MsCreateBox},
mounted() {
this.list();
},
methods: {
create() {
this.createVisible = true;
this.$get("/test/user", function (response) {
window.console.log(response);
this.form = {};
},
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) {
this.createVisible = true;
window.console.log(row);
this.loading = true;
let self = this;
let getUser1 = this.$get("/test/user");
let getUser2 = this.$get("/test/sleep");
this.$all([getUser1, getUser2], function (r1, r2) {
window.console.log(r1.data.data, r2.data.data);
self.loading = false;
this.form = row;
// let self = this;
// let getUser1 = this.$get("/test/user");
// let getUser2 = this.$get("/test/sleep");
// 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() {
@ -71,16 +106,17 @@
createVisible: false,
btnTips: "添加工作空间",
condition: "",
items: [{
id: '123456',
name: 'Default workspace',
owner: 'mk',
enable: "是"
}],
items: [],
form: {
name: "",
enable: false
}
// name: "",
// description: ""
},
rules: {
name: [
{required: true, message: '请输入工作空间名称', trigger: 'blur'},
{min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur'}
]
},
}
}
}