U opt namespace list

This commit is contained in:
zhenorzz 2022-01-28 10:57:07 +08:00
parent 378a48bb01
commit 58b8c4a7db
10 changed files with 90 additions and 161 deletions

View File

@ -14,6 +14,7 @@ func (n Namespace) Routes() []core.Route {
return []core.Route{
core.NewRoute("/namespace/getList", http.MethodGet, n.GetList),
core.NewRoute("/namespace/getTotal", http.MethodGet, n.GetTotal),
core.NewRoute("/namespace/getOption", http.MethodGet, n.GetOption),
core.NewRoute("/namespace/getBindUserList", http.MethodGet, n.GetBindUserList),
core.NewRoute("/namespace/getUserOption", http.MethodGet, n.GetUserOption),
core.NewRoute("/namespace/add", http.MethodPost, n.Add).Roles(core.RoleAdmin),
@ -52,6 +53,18 @@ func (Namespace) GetTotal(gp *core.Goploy) core.Response {
}
}
func (Namespace) GetOption(gp *core.Goploy) core.Response {
namespaceUsers, err := model.NamespaceUser{UserID: gp.UserInfo.ID}.GetUserNamespaceList()
if err != nil {
return response.JSON{Code: response.Error, Message: err.Error()}
}
return response.JSON{
Data: struct {
NamespaceUsers model.NamespaceUsers `json:"list"`
}{NamespaceUsers: namespaceUsers},
}
}
func (Namespace) GetUserOption(gp *core.Goploy) core.Response {
namespaceUsers, err := model.NamespaceUser{NamespaceID: gp.Namespace.ID}.GetAllUserByNamespaceID()
if err != nil {

View File

@ -23,7 +23,29 @@ type NamespaceUser struct {
// NamespaceUsers -
type NamespaceUsers []NamespaceUser
// GetBindUserListByNamespaceID -
func (nu NamespaceUser) GetUserNamespaceList() (NamespaceUsers, error) {
rows, err := sq.
Select("namespace_id, namespace.name, namespace_user.role").
From(namespaceUserTable).
Join(namespaceTable + " ON namespace_user.namespace_id = namespace.id").
Where(sq.Eq{"user_id": nu.UserID}).
RunWith(DB).
Query()
if err != nil {
return nil, err
}
namespaceUsers := NamespaceUsers{}
for rows.Next() {
var namespaceUser NamespaceUser
if err := rows.Scan(&namespaceUser.NamespaceID, &namespaceUser.NamespaceName, &namespaceUser.Role); err != nil {
return nil, err
}
namespaceUsers = append(namespaceUsers, namespaceUser)
}
return namespaceUsers, nil
}
func (nu NamespaceUser) GetBindUserListByNamespaceID() (NamespaceUsers, error) {
rows, err := sq.
Select("namespace_user.id, namespace_id, user_id, user.name, namespace_user.role, namespace_user.insert_time, namespace_user.update_time").
@ -47,7 +69,6 @@ func (nu NamespaceUser) GetBindUserListByNamespaceID() (NamespaceUsers, error) {
return namespaceUsers, nil
}
// GetAllUserByNamespaceID -
func (nu NamespaceUser) GetAllUserByNamespaceID() (NamespaceUsers, error) {
rows, err := sq.
Select("user_id, user.name, namespace_user.role").
@ -71,7 +92,6 @@ func (nu NamespaceUser) GetAllUserByNamespaceID() (NamespaceUsers, error) {
return namespaceUsers, nil
}
// GetAllGteManagerByNamespaceID -
func (nu NamespaceUser) GetAllGteManagerByNamespaceID() (NamespaceUsers, error) {
rows, err := sq.
Select("user_id, role").
@ -97,7 +117,6 @@ func (nu NamespaceUser) GetAllGteManagerByNamespaceID() (NamespaceUsers, error)
return namespaceUsers, nil
}
// AddMany -
func (nu NamespaceUsers) AddMany() error {
if len(nu) == 0 {
return nil
@ -113,7 +132,6 @@ func (nu NamespaceUsers) AddMany() error {
return err
}
// AddAdminByNamespaceID -
func (nu NamespaceUser) AddAdminByNamespaceID() error {
builder := sq.
@ -127,7 +145,6 @@ func (nu NamespaceUser) AddAdminByNamespaceID() error {
return err
}
// AddAdminByUserID -
func (nu NamespaceUser) AddAdminByUserID() error {
builder := sq.
Replace(namespaceUserTable).
@ -139,7 +156,6 @@ func (nu NamespaceUser) AddAdminByUserID() error {
return err
}
// DeleteRow -
func (nu NamespaceUser) DeleteRow() error {
_, err := sq.
Delete(namespaceUserTable).
@ -149,7 +165,6 @@ func (nu NamespaceUser) DeleteRow() error {
return err
}
// DeleteByUserID -
func (nu NamespaceUser) DeleteByUserID() error {
_, err := sq.
Delete(namespaceUserTable).

View File

@ -3,7 +3,7 @@ import { ElMessageBox, ElMessage } from 'element-plus'
import {
NamespaceKey,
getNamespaceId,
removeNamespaceId,
removeNamespace,
} from '@/utils/namespace'
import { logout } from '@/utils/auth'
@ -53,7 +53,7 @@ service.interceptors.response.use(
})
return Promise.reject(res.message)
} else if (10002 === res.code) {
removeNamespaceId()
removeNamespace()
return Promise.reject(res.message)
} else {
ElMessage({

View File

@ -56,6 +56,15 @@ export class NamespaceUserOption extends Request {
}
}
export class NamespaceOption extends Request {
readonly url = '/namespace/getOption'
readonly method = 'get'
public datagram!: {
list: NamespaceUserData['datagram'][]
}
}
export class NamespaceUserList extends Request {
readonly url = '/namespace/getBindUserList'
readonly method = 'get'

View File

@ -11,7 +11,6 @@ export interface HttpResponse<T> {
export interface Pagination {
page: number
rows: number
// total: number
}
export interface Total {

View File

@ -11,19 +11,20 @@
trigger="click"
size="medium"
placement="bottom-start"
@visible-change="handleNamespaceVisible"
@command="handleNamespaceChange"
>
<span class="el-dropdown-link">
{{ namespace.name }}<i class="el-icon-arrow-down el-icon--right" />
</span>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-menu v-loading="namespaceListLoading">
<el-dropdown-item
v-for="item in namespaceList"
:key="item.id"
:key="item.namespaceId"
:command="item"
>
{{ item.name }}
{{ item.namespaceName }}
</el-dropdown-item>
</el-dropdown-menu>
</template>
@ -121,12 +122,8 @@ import logo from '@/assets/images/logo.png'
import { mapState } from 'vuex'
import Breadcrumb from '@/components/Breadcrumb/index.vue'
import Hamburger from '@/components/Hamburger/index.vue'
import {
getNamespace,
getNamespaceList,
setNamespaceId,
removeNamespaceId,
} from '@/utils/namespace'
import { NamespaceOption } from '@/api/namespace'
import { getNamespace, setNamespace, removeNamespace } from '@/utils/namespace'
import { ElLoading } from 'element-plus'
import { defineComponent } from 'vue'
@ -140,26 +137,40 @@ export default defineComponent({
logo: logo,
starCount: 0,
forkCount: 0,
namespaceListLoading: false,
namespace: getNamespace(),
namespaceList: getNamespaceList(),
namespaceList: [],
}
},
computed: {
...mapState(['app', 'user']),
},
created() {
// fetch('https://api.github.com/repos/zhenorzz/goploy').then(response => response.json()).then(data => {
// this.starCount = data.stargazers_count
// this.forkCount = data.forks_count
// })
document.title = `Goploy-${this.namespace.name}`
},
methods: {
toggleSideBar() {
this.$store.dispatch('app/toggleSideBar')
},
handleNamespaceVisible(visible) {
if (visible === true) {
this.namespaceListLoading = true
new NamespaceOption()
.request()
.then((response) => {
this.namespaceList = response.data.list
})
.finally(() => {
this.namespaceListLoading = false
})
}
},
handleNamespaceChange(namespace) {
setNamespaceId(namespace.id.toString())
setNamespace({
id: namespace.namespaceId,
name: namespace.namespaceName,
role: namespace.role,
})
ElLoading.service({ fullscreen: true })
location.reload()
},
@ -171,7 +182,7 @@ export default defineComponent({
async logout() {
await this.$store.dispatch('user/logout')
await this.$store.dispatch('tagsView/delAllViews')
removeNamespaceId()
removeNamespace()
this.$router.push(`/login`)
},
},
@ -221,104 +232,6 @@ export default defineComponent({
font-size: 18px;
}
}
.github {
display: inline-block;
line-height: 50px;
.github-btn {
display: inline-block;
font: 700 11px/14px 'Helvetica Neue', Helvetica, Arial, sans-serif;
height: 20px;
overflow: hidden;
margin-right: 3px;
position: relative;
top: 5px;
.gh-btn,
.gh-count {
padding: 2px 5px 2px 4px;
color: #333;
text-decoration: none;
text-shadow: 0 1px 0 #fff;
white-space: nowrap;
cursor: pointer;
border-radius: 3px;
}
.gh-btn,
.gh-count,
.gh-ico {
float: left;
}
.gh-btn {
background-color: #eee;
background-image: linear-gradient(to bottom, #fcfcfc 0, #eee 100%);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#fcfcfc', endColorstr='#eeeeee', GradientType=0);
background-repeat: no-repeat;
border: 1px solid #d5d5d5;
position: relative;
&:focus,
&:hover {
text-decoration: none;
background-color: #ddd;
background-image: linear-gradient(to bottom, #eee 0, #ddd 100%);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#eeeeee', endColorstr='#dddddd', GradientType=0);
border-color: #ccc;
}
&:active {
background-image: none;
background-color: #dcdcdc;
border-color: #b5b5b5;
box-shadow: inset 0 2px 4px rgba(0,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AwLjUgMC40IDEuMSAxLjMgMC45YzcuNS0yLjYgMTMtOS43IDEzLTE4LjFDNTEgMjEuOSA0Mi41IDEzLjQgMzIgMTMuNHoiLz48L3N2Zz4=);
background-size: 100% 100%;
background-repeat: no-repeat;
}
.gh-count {
position: relative;
display: none;
margin-left: 4px;
background-color: #fafafa;
border: 1px solid #d4d4d4;
z-index: 1;
display: block;
&:focus,
&:hover {
color: #4183c4;
}
&:after,
&:before {
content: '';
position: absolute;
display: inline-block;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
}
&:before {
top: 50%;
left: -2px;
margin-top: -3px;
border-width: 2px 2px 2px 0;
border-right-color: #fafafa;
}
&:after {
top: 50%;
left: -3px;
z-index: -1;
margin-top: -4px;
border-width: 3px 3px 3px 0;
border-right-color: #d4d4d4;
}
}
}
}
.user-menu {
float: right;
height: 100%;

View File

@ -3,11 +3,7 @@ import { UserState } from './types'
import { RootState } from '../../types'
import { Login, Info } from '@/api/user'
import { setLogin, logout } from '@/utils/auth'
import {
getNamespaceId,
setNamespaceId,
setNamespaceList,
} from '@/utils/namespace'
import { getNamespaceId, setNamespace } from '@/utils/namespace'
import { resetRouter } from '@/router'
const state: UserState = {
@ -43,9 +39,8 @@ const actions: ActionTree<UserState, RootState> = {
const { data } = response
if (!getNamespaceId()) {
const namespace = data.namespaceList[data.namespaceList.length - 1]
setNamespaceId(namespace.id.toString())
setNamespace(namespace)
}
setNamespaceList(data.namespaceList)
setLogin('ok')
resolve(response)

View File

@ -1,5 +1,4 @@
export const NamespaceKey = 'G-N-ID'
const NamespaceListKey = 'goploy_namespace_list'
export interface Namespace {
id: number
@ -42,37 +41,22 @@ export function getRole() {
}
export function getNamespace(): Namespace {
const namespaceId = getNamespaceId()
const namespaceList = getNamespaceList()
if (namespaceId && namespaceList) {
return namespaceList.find(
(_) => _.id.toString() === namespaceId
) as Namespace
}
return { id: 0, name: '', role: '' }
}
export function getNamespaceId(): string | undefined {
const namespaceId =
const n =
sessionStorage.getItem(NamespaceKey) || localStorage.getItem(NamespaceKey)
return namespaceId || undefined
return n ? JSON.parse(n) : { id: 0, name: '', role: '' }
}
export function setNamespaceId(namespaceId: string): void {
sessionStorage.setItem(NamespaceKey, namespaceId)
localStorage.setItem(NamespaceKey, namespaceId)
export function getNamespaceId(): number {
const namespaceId = getNamespace().id
return namespaceId
}
export function removeNamespaceId(): void {
export function setNamespace(namespace: Namespace): void {
sessionStorage.setItem(NamespaceKey, JSON.stringify(namespace))
localStorage.setItem(NamespaceKey, JSON.stringify(namespace))
}
export function removeNamespace(): void {
sessionStorage.removeItem(NamespaceKey)
localStorage.removeItem(NamespaceKey)
}
export function getNamespaceList(): Array<Namespace> {
const namespaceList = localStorage.getItem(NamespaceListKey)
return namespaceList ? JSON.parse(namespaceList) : []
}
export function setNamespaceList(namespaceList: Array<Namespace>): void {
localStorage.setItem(NamespaceListKey, JSON.stringify(namespaceList))
}

View File

@ -215,7 +215,6 @@ export default defineComponent({
.then(() => {
this.getList()
this.getTotal()
ElMessage.success('Need to login again')
})
.finally(() => {
this.formProps.disabled = this.dialogVisible = false

View File

@ -54,7 +54,9 @@ export class xterm {
)
this.terminal.loadAddon(new AttachAddon(this.websocket))
this.websocket.onclose = function (evt) {
ElMessage.error(evt.reason)
if (evt.reason !== '') {
ElMessage.error(evt.reason)
}
}
}
public close(): void {