Skip to content

Commit 52342e6

Browse files
committed
feat: 新增Switch行为组件
1 parent 130e031 commit 52342e6

21 files changed

+416
-245
lines changed

app/admin/actions/status.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package actions
2+
3+
import (
4+
"github.com/quarkcloudio/quark-go/v3"
5+
"github.com/quarkcloudio/quark-go/v3/template/admin/resource/actions"
6+
"gorm.io/gorm"
7+
)
8+
9+
type StatusAction struct {
10+
actions.Switch
11+
}
12+
13+
// 更改状态,Status()
14+
func Status() *StatusAction {
15+
action := &StatusAction{}
16+
return action
17+
}
18+
19+
// 初始化
20+
func (p *StatusAction) Init(ctx *quark.Context) interface{} {
21+
22+
// 执行成功后刷新的组件
23+
p.Reload = "table"
24+
25+
// 选中时的内容
26+
p.CheckedChildren = "正常"
27+
28+
// 未选中时的内容
29+
p.UnCheckedChildren = "禁用"
30+
31+
// 设置字段名
32+
p.FieldName = "status"
33+
34+
// 设置字段值
35+
p.FieldValue = "1"
36+
37+
// 设置展示位置
38+
p.SetOnlyOnIndexTableRow(true)
39+
40+
return p
41+
}
42+
43+
// 行为接口接收的参数,当行为在表格行展示的时候,可以配置当前行的任意字段
44+
func (p *StatusAction) GetApiParams() []string {
45+
return []string{
46+
"id",
47+
}
48+
}
49+
50+
// 执行行为句柄
51+
func (p *StatusAction) Handle(ctx *quark.Context, query *gorm.DB) error {
52+
status := ctx.Query("status")
53+
if status == "" {
54+
return ctx.CJSONError("参数错误")
55+
}
56+
57+
var fieldStatus int
58+
if status == "0" || status == "false" {
59+
fieldStatus = 0
60+
} else {
61+
fieldStatus = 1
62+
}
63+
64+
err := query.Update("status", fieldStatus).Error
65+
if err != nil {
66+
return ctx.CJSONError(err.Error())
67+
}
68+
69+
return ctx.CJSONOk("操作成功")
70+
}

app/admin/resources/department.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (p *Department) Searches(ctx *quark.Context) []interface{} {
9999
func (p *Department) Actions(ctx *quark.Context) []interface{} {
100100
return []interface{}{
101101
actions.CreateModal(),
102-
actions.ChangeStatus(),
102+
actions.Status(),
103103
actions.EditModal(),
104104
actions.DeleteSpecial(),
105105
actions.BatchDelete(),

template/admin/component/action/action.go

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,31 @@ import (
88

99
type Component struct {
1010
component.Element
11-
Label interface{} `json:"label"`
12-
Block bool `json:"block"`
13-
Danger bool `json:"danger"`
14-
Disabled bool `json:"disabled"`
15-
Ghost bool `json:"ghost"`
16-
Icon interface{} `json:"icon"`
17-
Shape string `json:"shape"`
18-
Size string `json:"size"`
19-
Type string `json:"type"`
20-
ActionType string `json:"actionType"`
21-
SubmitForm any `json:"submitForm"`
22-
Href string `json:"href"`
23-
Target string `json:"target"`
24-
Modal interface{} `json:"modal"`
25-
Drawer interface{} `json:"drawer"`
26-
ConfirmTitle string `json:"confirmTitle"`
27-
ConfirmText string `json:"confirmText"`
28-
ConfirmType string `json:"confirmType"`
29-
Api string `json:"api"`
30-
Reload string `json:"reload"`
31-
WithLoading bool `json:"withLoading"`
11+
Label interface{} `json:"label"`
12+
Block bool `json:"block"`
13+
Danger bool `json:"danger"`
14+
Disabled bool `json:"disabled"`
15+
Ghost bool `json:"ghost"`
16+
Icon interface{} `json:"icon"`
17+
Shape string `json:"shape"`
18+
Size string `json:"size"`
19+
Type string `json:"type"`
20+
ActionType string `json:"actionType"`
21+
SubmitForm any `json:"submitForm"`
22+
Href string `json:"href"`
23+
Target string `json:"target"`
24+
Modal interface{} `json:"modal"`
25+
Drawer interface{} `json:"drawer"`
26+
CheckedChildren interface{} `json:"checkedChildren,omitempty"` // 选中时的内容
27+
UnCheckedChildren interface{} `json:"unCheckedChildren,omitempty"` // 自定义的选择框后缀图标
28+
FieldName interface{} `json:"fieldName,omitempty"` // 字段名称
29+
FieldValue interface{} `json:"fieldValue,omitempty"` // 字段值
30+
ConfirmTitle string `json:"confirmTitle"`
31+
ConfirmText string `json:"confirmText"`
32+
ConfirmType string `json:"confirmType"`
33+
Api string `json:"api"`
34+
Reload string `json:"reload"`
35+
WithLoading bool `json:"withLoading"`
3236
}
3337

3438
// 初始化组件
@@ -237,6 +241,34 @@ func (p *Component) SetDrawer(callback interface{}) *Component {
237241
return p
238242
}
239243

244+
// 选中时的内容
245+
func (p *Component) SetCheckedChildren(checkedChildren interface{}) *Component {
246+
p.CheckedChildren = checkedChildren
247+
248+
return p
249+
}
250+
251+
// 未选中时的内容
252+
func (p *Component) SetUnCheckedChildren(unCheckedChildren interface{}) *Component {
253+
p.UnCheckedChildren = unCheckedChildren
254+
255+
return p
256+
}
257+
258+
// 获取字段名称
259+
func (p *Component) SetFieldName(fieldName interface{}) *Component {
260+
p.FieldName = fieldName
261+
262+
return p
263+
}
264+
265+
// 获取字段值
266+
func (p *Component) SetFieldValue(fieldValue interface{}) *Component {
267+
p.FieldValue = fieldValue
268+
269+
return p
270+
}
271+
240272
// 设置行为前的确认操作
241273
func (p *Component) SetWithConfirm(title string, text string, confirmType string) *Component {
242274
p.ConfirmTitle = title
@@ -267,10 +299,3 @@ func (p *Component) SetWithLoading(loading bool) *Component {
267299

268300
return p
269301
}
270-
271-
// 组件json序列化
272-
func (p *Component) JsonSerialize() *Component {
273-
p.Component = "action"
274-
275-
return p
276-
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package actions
2+
3+
import "github.com/quarkcloudio/quark-go/v3"
4+
5+
type Switch struct {
6+
Action
7+
CheckedChildren interface{} `json:"checkedChildren,omitempty"` // 选中时的内容
8+
UnCheckedChildren interface{} `json:"unCheckedChildren,omitempty"` // 自定义的选择框后缀图标
9+
FieldName interface{} `json:"fieldName,omitempty"` // 字段名称
10+
FieldValue interface{} `json:"fieldValue,omitempty"` // 字段值
11+
}
12+
13+
// 初始化
14+
func (p *Switch) New(ctx *quark.Context) interface{} {
15+
p.ActionType = "switch"
16+
return p
17+
}
18+
19+
// 选中时的内容
20+
func (p *Switch) GetCheckedChildren() interface{} {
21+
return p.CheckedChildren
22+
}
23+
24+
// 自定义的选择框后缀图标
25+
func (p *Switch) GetUnCheckedChildren() interface{} {
26+
return p.UnCheckedChildren
27+
}
28+
29+
// 获取字段名称
30+
func (p *Switch) GetFieldName() interface{} {
31+
return p.FieldName
32+
}
33+
34+
// 获取字段值
35+
func (p *Switch) GetFieldValue() interface{} {
36+
return p.FieldValue
37+
}

template/admin/resource/resolves_actions.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,28 @@ func (p *Template) BuildAction(ctx *quark.Context, item interface{}) interface{}
548548
}
549549

550550
return getAction
551+
552+
case "switch":
553+
switchActioner := item.(types.Switcher)
554+
555+
// 选中时的内容
556+
checkedChildren := switchActioner.GetCheckedChildren()
557+
558+
// 自定义的选择框后缀图标
559+
unCheckedChildren := switchActioner.GetUnCheckedChildren()
560+
561+
// 获取字段名称
562+
fieldName := switchActioner.GetFieldName()
563+
564+
// 获取字段值
565+
fieldValue := switchActioner.GetFieldValue()
566+
567+
// 设置跳转链接
568+
getAction = getAction.
569+
SetCheckedChildren(checkedChildren).
570+
SetUnCheckedChildren(unCheckedChildren).
571+
SetFieldName(fieldName).
572+
SetFieldValue(fieldValue)
551573
}
552574

553575
if confirmTitle != "" {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package types
2+
3+
type Switcher interface {
4+
Actioner
5+
6+
// 选中时的内容
7+
GetCheckedChildren() interface{}
8+
9+
// 自定义的选择框后缀图标
10+
GetUnCheckedChildren() interface{}
11+
12+
// 获取字段名称
13+
GetFieldName() interface{}
14+
15+
// 获取字段值
16+
GetFieldValue() interface{}
17+
}

web/app/admin/138.async.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/app/admin/240.async.js renamed to web/app/admin/143.async.js

Lines changed: 26 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/app/admin/404.async.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)