-
Notifications
You must be signed in to change notification settings - Fork 152
/
wxtoken.go
45 lines (39 loc) · 1.25 KB
/
wxtoken.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
package innerservice
import (
"net/http"
"github.com/WeixinCloud/wxcloudrun-wxcomponent/comm/errno"
"github.com/WeixinCloud/wxcloudrun-wxcomponent/comm/wx"
wxbase "github.com/WeixinCloud/wxcloudrun-wxcomponent/comm/wx/base"
"github.com/gin-gonic/gin"
)
// GetTicketHandler 获取Ticket
func GetTicketHandler(c *gin.Context) {
ticket := wxbase.GetTicket()
if len(ticket) == 0 {
c.JSON(http.StatusOK, errno.ErrEmptyTicket)
return
}
c.JSON(http.StatusOK, errno.OK.WithData(gin.H{"ticket": ticket}))
}
// GetComponentAccessTokenHandler 获取ComponentAccessToken
func GetComponentAccessTokenHandler(c *gin.Context) {
token, err := wx.GetComponentAccessToken()
if err != nil {
if err.Error() == "empty ticket" {
c.JSON(http.StatusOK, errno.ErrEmptyTicket)
return
}
c.JSON(http.StatusOK, errno.ErrSystemError.WithData(err.Error()))
return
}
c.JSON(http.StatusOK, errno.OK.WithData(gin.H{"token": token}))
}
// GetAuthorizerAccessTokenHandler 获取AuthorizerAccessToken
func GetAuthorizerAccessTokenHandler(c *gin.Context) {
token, err := wx.GetAuthorizerAccessToken(c.Query("appid"))
if err != nil {
c.JSON(http.StatusOK, errno.ErrSystemError.WithData(err.Error()))
return
}
c.JSON(http.StatusOK, errno.OK.WithData(gin.H{"token": token}))
}