/
list.go
289 lines (272 loc) · 7.3 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package list
import (
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/sirupsen/logrus"
"github.com/erda-project/erda/apistructs"
)
func GetOpsInfo(opsData interface{}) (*Meta, error) {
if opsData == nil {
err := fmt.Errorf("empty operation data")
return nil, err
}
var op Operation
cont, err := json.Marshal(opsData)
if err != nil {
logrus.Errorf("marshal inParams failed, content:%v, err:%v", opsData, err)
return nil, err
}
err = json.Unmarshal(cont, &op)
if err != nil {
logrus.Errorf("unmarshal move out request failed, content:%v, err:%v", cont, err)
return nil, err
}
meta := op.Meta
return &meta, nil
}
func RenItem(pro apistructs.ProjectDTO, orgDomain string) (ProItem, error) {
activeTime, err := CountActiveTime(pro.ActiveTime)
if err != nil {
return ProItem{}, err
}
opClick := Operation{
Key: apistructs.ClickOperation.String(),
Reload: false,
Show: false,
Command: Command{
Key: "goto",
Target: "project",
},
}
//opManage := GenerateOperation{
// Key: apistructs.ListProjectToManageOperationKey.String(),
// Reload: false,
// Text: "管理",
// Show: false,
// Command: Command{
// Key: "goto",
// Target: "https://terminus-org.dev.terminus.io/orgCenter/projects",
// },
//}
opExist := Operation{
Key: apistructs.ListProjectExistOperationKey.String(),
Reload: true,
Text: "退出",
Confirm: "退出当前项目后,将不再有项目协作权限,如要再次加入需要项目管理员邀请,请确认是否退出?",
Meta: Meta{
ID: pro.ID,
},
}
opApplyDeploy := Operation{
Key: apistructs.ApplyDeployProjectFilterOperation.String(),
Reload: false,
Text: "申请部署",
Meta: Meta{
ProjectId: pro.ID,
ProjectName: pro.DisplayName,
},
}
item := ProItem{
ID: strconv.Itoa(int(pro.ID)),
ProjectId: pro.ID,
Title: pro.DisplayName,
Description: pro.Desc,
PrefixImg: "frontImg_default_project_icon",
ExtraInfos: []ExtraInfos{
map[bool]ExtraInfos{
true: {
Icon: "unlock",
Text: "公开项目",
},
false: {
Icon: "lock",
Text: "私有项目",
},
}[pro.IsPublic],
{
Icon: "application-one",
Text: strconv.Itoa(pro.Stats.CountApplications),
Tooltip: "应用数",
},
{
Icon: "time",
Text: activeTime,
Tooltip: pro.ActiveTime,
},
},
Operations: map[string]interface{}{
apistructs.ClickOperation.String(): opClick,
apistructs.ListProjectExistOperationKey.String(): opExist,
},
}
if pro.Logo != "" {
item.PrefixImg = pro.Logo
}
// 解封状态
if pro.BlockStatus == "unblocking" {
item.ExtraInfos = append(item.ExtraInfos, ExtraInfos{
Icon: "link-cloud-faild",
Text: "解封处理中,请稍等",
Type: "warning",
Tooltip: "解封处理中,请稍等",
})
} else if pro.BlockStatus == "unblocked" {
item.ExtraInfos = append(item.ExtraInfos, ExtraInfos{
Icon: "link-cloud-sucess",
Text: "已解封",
Type: "success",
})
}
// 目前去掉管理按钮
//// Owner Lead PM
//if pro.CanManage {
// item.Operations[apistructs.ListProjectToManageOperationKey.String()] = opManage
//}
// 根据解封状态判断
if pro.CanManage && (pro.BlockStatus == "unblocked" || pro.BlockStatus == "unblocking" || pro.BlockStatus == "blocked") {
item.Operations[apistructs.ApplyDeployProjectFilterOperation.String()] = opApplyDeploy
}
return item, nil
}
func (i *ComponentList) RenderList() error {
orgID, err := strconv.ParseUint(i.ctxBdl.Identity.OrgID, 10, 64)
if err != nil {
return err
}
queryStr := ""
_, ok := i.State.Query["title"]
if ok {
queryStr = i.State.Query["title"].(string)
}
req := apistructs.ProjectListRequest{
OrgID: orgID,
Query: queryStr,
PageNo: int(i.State.PageNo),
PageSize: int(i.State.PageSize),
OrderBy: "activeTime",
}
projectDTO, err := i.ctxBdl.Bdl.ListMyProject(i.ctxBdl.Identity.UserID, req)
if err != nil {
return err
}
org, err := i.ctxBdl.Bdl.GetOrg(orgID)
if err != nil {
return err
}
i.Data.List = make([]ProItem, 0)
i.State.Total = 0
if projectDTO != nil {
for _, v := range projectDTO.List {
p, err := RenItem(v, org.Domain)
if err != nil {
return err
}
i.Data.List = append(i.Data.List, p)
}
i.State.Total = uint64(projectDTO.Total)
}
return nil
}
func (i *ComponentList) RenderExist(ops interface{}) error {
mate, err := GetOpsInfo(ops)
if err != nil {
return err
}
req := apistructs.MemberRemoveRequest{
Scope: apistructs.Scope{
Type: apistructs.ProjectScope,
ID: strconv.Itoa(int(mate.ID)),
},
UserIDs: []string{i.ctxBdl.Identity.UserID},
}
req.UserID = i.ctxBdl.Identity.UserID
return i.ctxBdl.Bdl.DeleteMember(req)
}
func (i *ComponentList) RenderChangePageSize(ops interface{}) error {
mate, err := GetOpsInfo(ops)
if err != nil {
return err
}
i.State.PageNo = 1
i.State.PageSize = mate.PageSize.PageSize
return nil
}
func (i *ComponentList) RenderChangePageNo(ops interface{}) error {
mate, err := GetOpsInfo(ops)
if err != nil {
return err
}
i.State.PageNo = mate.PageNo
return nil
}
func CountActiveTime(ActiveTimeStr string) (string, error) {
var subStr string
nowTime := time.Now()
activeTime, err := time.Parse("2006-01-02 15:04:05", ActiveTimeStr)
if err != nil {
return "", err
}
// 计算时间差值
sub := nowTime.Sub(activeTime)
sub = sub + 8*time.Hour
if int64(sub.Hours()) >= 24*30*12 {
subStr = strconv.FormatInt(int64(sub.Hours())/(24*30*12), 10) + " 年前"
} else if int64(sub.Hours()) >= 24*30 {
subStr = strconv.FormatInt(int64(sub.Hours())/(24*30), 10) + " 月前"
} else if int64(sub.Hours()) >= 24 {
subStr = strconv.FormatInt(int64(sub.Hours())/24, 10) + " 天前"
} else if int64(sub.Hours()) > 0 {
subStr = strconv.FormatInt(int64(sub.Hours()), 10) + " 小时前"
} else if int64(sub.Minutes()) > 0 {
subStr = strconv.FormatInt(int64(sub.Minutes()), 10) + " 分钟前"
} else {
subStr = "几秒前"
}
return subStr, nil
}
func (i *ComponentList) CheckVisible() (bool, error) {
// 项目列表不为空
if i.State.Total != 0 {
return true, nil
}
// 没有加搜索条件
if (i.State.Query != nil && i.State.Query["title"] == "") || i.State.Query == nil {
return false, nil
}
// 如果加了搜索条件则需要去除搜索条件再判断一次
orgID, err := strconv.ParseUint(i.ctxBdl.Identity.OrgID, 10, 64)
if err != nil {
return false, err
}
req := apistructs.ProjectListRequest{
OrgID: orgID,
PageNo: 1,
PageSize: 1,
OrderBy: "activeTime",
}
projectDTO, err := i.ctxBdl.Bdl.ListMyProject(i.ctxBdl.Identity.UserID, req)
if err != nil {
return false, err
}
if projectDTO == nil {
return false, nil
}
if projectDTO.Total == 0 {
return false, nil
}
return true, nil
}