-
Notifications
You must be signed in to change notification settings - Fork 1
/
declare.go
89 lines (82 loc) · 1.92 KB
/
declare.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
package article
import (
"github.com/webx-top/echo"
"github.com/admpub/nging/v5/application/library/common"
"github.com/admpub/webx/application/middleware/sessdata"
"github.com/admpub/webx/application/model/official"
)
// ClickFlow 表态
func ClickFlow(c echo.Context, typ string, targetType string) error {
customer := sessdata.Customer(c)
data := c.Data()
if customer == nil {
data.SetError(common.ErrUserNotLoggedIn)
return c.JSON(data)
}
var id interface{}
targetID := c.Formx(`id`).Uint64()
targetSN := c.Formx(`sn`).String()
if targetID == 0 {
if len(targetSN) > 0 {
id = targetSN
} else {
data.SetInfo(c.T(`id无效`), 0)
return c.JSON(data)
}
} else {
id = targetID
}
target, ok := official.ClickFlowTargets[targetType]
if !ok {
return c.E(`不支持的目标类型: %s`, targetType)
}
after, idGetter, err := target.Do(c, id)
if err != nil {
data.SetError(err)
return c.JSON(data)
}
if idGetter != nil {
targetID = idGetter()
}
clickFlowM := official.NewClickFlow(c)
clickFlowM.TargetType = targetType
clickFlowM.TargetId = targetID
clickFlowM.OwnerId = customer.Id
clickFlowM.OwnerType = `customer`
clickFlowM.Type = typ
switch typ {
case `like`:
_, err = clickFlowM.Add()
case `hate`:
_, err = clickFlowM.Add()
default:
err = c.E(`类型无效`)
}
if err == nil {
if after != nil {
err = after(typ)
}
}
if err != nil {
data.SetError(err)
} else {
data.SetInfo(c.T(`表态成功`))
}
return c.JSON(data)
}
// ArticleLike 新闻:喜欢
func ArticleLike(c echo.Context) error {
return ClickFlow(c, `like`, `article`)
}
// ArticleHate 新闻:不喜欢
func ArticleHate(c echo.Context) error {
return ClickFlow(c, `hate`, `article`)
}
// CommentLike 评论:喜欢
func CommentLike(c echo.Context) error {
return ClickFlow(c, `like`, `comment`)
}
// CommentHate 评论:不喜欢
func CommentHate(c echo.Context) error {
return ClickFlow(c, `hate`, `comment`)
}