Skip to content

Commit

Permalink
V 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dyk98 committed Mar 9, 2022
1 parent 6ac8bd0 commit a73b966
Show file tree
Hide file tree
Showing 81 changed files with 2,310 additions and 1,142 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/.idea
.idea
.DS_Store
.vscode

client/node_modules
client/.pnp
Expand All @@ -24,3 +25,5 @@ client/yarn-debug.log*
client/yarn-error.log*
client/yarn.lock
client/package-lock.json
client/.vscode
client/dist
Empty file removed .gitmodules
Empty file.
12 changes: 9 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
FROM node:12.22.1 as nodeBuilder

FROM node:16.14.0 as nodeBuilder
# 指定构建过程中的工作目录
WORKDIR /wxcloudrun-wxcomponent

Expand All @@ -17,7 +16,7 @@ WORKDIR /wxcloudrun-wxcomponent
COPY . /wxcloudrun-wxcomponent/

# 执行代码编译命令。操作系统参数为linux,编译后的二进制产物命名为main,并存放在当前目录下。
RUN GOOS=linux go build -o main .
RUN GOOS=linux GOARCH=amd64 go build -o main .

# 选用运行时所用基础镜像(GO语言选择原则:尽量体积小、包含基础linux内容的基础镜像)
FROM alpine:3.13
Expand All @@ -30,6 +29,13 @@ COPY --from=builder /wxcloudrun-wxcomponent/main /wxcloudrun-wxcomponent/
COPY --from=builder /wxcloudrun-wxcomponent/comm/config/server.conf /wxcloudrun-wxcomponent/comm/config/
COPY --from=nodeBuilder /wxcloudrun-wxcomponent/client/dist /wxcloudrun-wxcomponent/client/dist

# 设置时区
RUN apk --update add tzdata && \
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo "Asia/Shanghai" > /etc/timezone && \
apk del tzdata && \
rm -rf /var/cache/apk/*

# 设置release模式
ENV GIN_MODE release

Expand Down
2 changes: 1 addition & 1 deletion api/admin/authorizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type authorizerInfo struct {
}
type getAuthorizerListResp struct {
TotalCount int `json:"total_count"`
List []authorizerInfo `json:list`
List []authorizerInfo `json:"list"`
}

func pullAuthorizerListHandler(c *gin.Context) {
Expand Down
6 changes: 3 additions & 3 deletions api/admin/callbackrecords.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type getWxBizRecordsReq struct {
Limit int `form:"limit"`
}

func wxComponentRecordsHandler(c *gin.Context) {
func getWxComponentRecordsHandler(c *gin.Context) {
var req getWxComponentRecordsReq
if err := c.ShouldBindQuery(&req); err != nil {
c.JSON(http.StatusOK, errno.ErrInvalidParam.WithData(err.Error()))
Expand All @@ -50,7 +50,7 @@ func wxComponentRecordsHandler(c *gin.Context) {
c.JSON(http.StatusOK, errno.OK.WithData(gin.H{"total": total, "records": records}))
}

func wxBizRecordsHandler(c *gin.Context) {
func getWxBizRecordsHandler(c *gin.Context) {
var req getWxBizRecordsReq
if err := c.ShouldBindQuery(&req); err != nil {
c.JSON(http.StatusOK, errno.ErrInvalidParam.WithData(err.Error()))
Expand All @@ -71,7 +71,7 @@ func wxBizRecordsHandler(c *gin.Context) {
c.JSON(http.StatusOK, errno.OK.WithData(gin.H{"total": total, "records": records}))
}

func wxCallBackConfigHandler(c *gin.Context) {
func getWxCallBackConfigHandler(c *gin.Context) {
c.JSON(http.StatusOK, errno.OK.WithData(gin.H{
"envId": wxbase.GetEnvId(),
"service": wxbase.GetService(),
Expand Down
207 changes: 207 additions & 0 deletions api/admin/callbackrules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
package admin

import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"

"github.com/WeixinCloud/wxcloudrun-wxcomponent/comm/errno"
"github.com/WeixinCloud/wxcloudrun-wxcomponent/comm/httputils"
"github.com/WeixinCloud/wxcloudrun-wxcomponent/comm/log"
"github.com/WeixinCloud/wxcloudrun-wxcomponent/db/dao"
"github.com/WeixinCloud/wxcloudrun-wxcomponent/db/model"
"github.com/gin-gonic/gin"
)

type getCallBackProxyRuleListReq struct {
Offset int `form:"offset"`
Limit int `form:"limit"`
Type int `form:"type"`
}

type callBackProxyRule struct {
ID int32 `json:"id"`
Name string `json:"name"`
InfoType string `json:"infoType"`
MsgType string `json:"msgType"`
Event string `json:"event"`
Open int `json:"open"`
Data model.HttpProxyConfig `json:"data"`
CreateTime int64 `json:"createTime"`
UpdateTime int64 `json:"updateTime"`
}

func getCallBackProxyRuleListHandler(c *gin.Context) {
var req getCallBackProxyRuleListReq
if err := c.ShouldBindQuery(&req); err != nil {
c.JSON(http.StatusOK, errno.ErrInvalidParam.WithData(err.Error()))
return
}
dbValue, total, err := dao.GetWxCallBackRuleList(req.Offset, req.Limit, req.Type)
if err != nil {
c.JSON(http.StatusOK, errno.ErrSystemError.WithData(err.Error()))
return
}
res := make([]callBackProxyRule, 0, 10)
for _, v := range dbValue {
var proxyConfig model.HttpProxyConfig
if err = json.Unmarshal([]byte(v.Info), &proxyConfig); err != nil {
log.Errorf("Unmarshal err, %v", err)
} else {
res = append(res, callBackProxyRule{
ID: v.ID,
Name: v.Name,
InfoType: v.InfoType,
MsgType: v.MsgType,
Event: v.Event,
Open: v.Open,
Data: proxyConfig,
CreateTime: v.CreateTime.UnixNano() / 1e6,
UpdateTime: v.UpdateTime.UnixNano() / 1e6,
})
}
}
c.JSON(http.StatusOK, errno.OK.WithData(gin.H{
"total": total,
"rules": res,
}))
}

func updateCallBackProxyRuleHandler(c *gin.Context) {
var req callBackProxyRule
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusOK, errno.ErrInvalidParam.WithData(err.Error()))
return
}
if req.InfoType == "" && req.MsgType == "" && req.Event == "" {
c.JSON(http.StatusOK, errno.ErrInvalidParam.WithData("消息推送类型为空"))
return
}
value, _ := json.Marshal(req.Data)
if err := dao.UpdateWxCallBackRule(&model.WxCallbackRule{
ID: req.ID,
Name: req.Name,
InfoType: req.InfoType,
MsgType: req.MsgType,
Event: req.Event,
Open: req.Open,
Type: model.PROXYTYPE_HTTP,
Info: string(value),
}); err != nil {
c.JSON(http.StatusOK, errno.ErrSystemError.WithData(err.Error()))
return
}
c.JSON(http.StatusOK, errno.OK)
}

func addCallBackProxyRuleHandler(c *gin.Context) {
var req callBackProxyRule
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusOK, errno.ErrInvalidParam.WithData(err.Error()))
return
}
if req.InfoType == "" && req.MsgType == "" && req.Event == "" {
c.JSON(http.StatusOK, errno.ErrInvalidParam.WithData("消息推送类型为空"))
return
}
value, _ := json.Marshal(req.Data)
if err := dao.AddWxCallBackRule(&model.WxCallbackRule{
Name: req.Name,
InfoType: req.InfoType,
MsgType: req.MsgType,
Event: req.Event,
Open: req.Open,
Type: model.PROXYTYPE_HTTP,
Info: string(value),
}); err != nil {
if strings.Contains(err.Error(), "Error 1062") {
c.JSON(http.StatusOK, errno.ErrInvalidParam.WithData("该事件已存在转发规则"))
return
}
c.JSON(http.StatusOK, errno.ErrSystemError.WithData(err.Error()))
return
}
c.JSON(http.StatusOK, errno.OK)
}

type callBackProxyRuleId struct {
ID int32 `form:"id"`
}

func delCallBackProxyRuleHandler(c *gin.Context) {
var req callBackProxyRuleId
if err := c.ShouldBindQuery(&req); err != nil {
c.JSON(http.StatusOK, errno.ErrInvalidParam.WithData(err.Error()))
return
}
if err := dao.DelWxCallBackRule(req.ID); err != nil {
c.JSON(http.StatusOK, errno.ErrSystemError.WithData(err.Error()))
return
}
c.JSON(http.StatusOK, errno.OK)
}

func testCallbackRuleHandler(c *gin.Context) {
var req callBackProxyRuleId
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusOK, errno.ErrInvalidParam.WithData(err.Error()))
return
}
if record, err := dao.GetWxCallBackRuleById(req.ID); err != nil {
c.JSON(http.StatusOK, errno.ErrSystemError.WithData(err.Error()))
return
} else {
if record.Open != 0 && record.Type == model.PROXYTYPE_HTTP {
var proxyConfig model.HttpProxyConfig
if err = json.Unmarshal([]byte(record.Info), &proxyConfig); err != nil {
log.Errorf("Unmarshal err, %v", err)
c.JSON(http.StatusOK, errno.ErrSystemError.WithData(err.Error()))
return
}
resp, err := httputils.PostJson(fmt.Sprintf("http:https://127.0.0.1:%d%s", proxyConfig.Port,
strings.Replace(proxyConfig.Path, "$APPID$", "wxtestappid", -1)),
genWxCallBackReq(record))
if err != nil {
log.Error(err)
c.JSON(http.StatusOK, errno.ErrRequestErr.WithData(err.Error()))
return
}
c.JSON(http.StatusOK, errno.OK.WithData(string(resp)))
return
} else {
c.JSON(http.StatusOK, errno.ErrInvalidStatus.WithData("该规则未启用或类型异常"))
return
}
}
}

type wxCallBackReq struct {
CreateTime int64 `json:"CreateTime"`
ToUserName string `json:"ToUserName,omitempty"`
FromUserName string `json:"FromUserName,omitempty"`
InfoType string `json:"InfoType,omitempty"`
MsgType string `json:"MsgType,omitempty"`
Event string `json:"Event,omitempty"`
Data string `json:"Data,omitempty"`
}

func genWxCallBackReq(rule *model.WxCallbackRule) *wxCallBackReq {
if rule.InfoType != "" {
return &wxCallBackReq{
CreateTime: time.Now().UnixNano() / 1e6,
InfoType: rule.InfoType,
Data: "TestData",
}
} else {
return &wxCallBackReq{
CreateTime: time.Now().UnixNano() / 1e6,
MsgType: rule.MsgType,
Event: rule.Event,
ToUserName: "TestUserName1",
FromUserName: "TestUserName2",
Data: "TestData",
}
}
}
4 changes: 1 addition & 3 deletions api/admin/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ import (

// InitAdmin 初始化管理员
func InitAdmin(username, password string) error {
err := dao.AddUserRecord(username, password)
if err != nil {
if err := dao.AddUserRecordIfNeeded(username, password); err != nil {
log.Errorf("InitAuth err %v", err)
return err
}
log.Debugf("SaveUser user[%s] pwd[%s] Succ ", username, password)
return nil
}

Expand Down
31 changes: 31 additions & 0 deletions api/admin/proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package admin

import (
"net/http"

"github.com/WeixinCloud/wxcloudrun-wxcomponent/api/proxy"
"github.com/WeixinCloud/wxcloudrun-wxcomponent/comm/errno"
"github.com/gin-gonic/gin"
)

func getProxyHandler(c *gin.Context) {
c.JSON(http.StatusOK, errno.OK.WithData(proxy.GetProxyConfig()))
}

type updateProxyReq struct {
Open bool `json:"open"`
Port int `json:"port"`
}

func updateProxyHandler(c *gin.Context) {
var req updateProxyReq
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusOK, errno.ErrInvalidParam.WithData(err.Error()))
return
}
if err := proxy.SetProxyConfig(req.Open, req.Port, ""); err != nil {
c.JSON(http.StatusOK, errno.ErrSystemError.WithData(err.Error()))
return
}
c.JSON(http.StatusOK, errno.OK)
}
26 changes: 18 additions & 8 deletions api/admin/routers.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
package admin

import (
"github.com/WeixinCloud/wxcloudrun-wxcomponent/api/innerservice"
"github.com/WeixinCloud/wxcloudrun-wxcomponent/middleware"
"github.com/gin-gonic/gin"
)

// Routers 路由
func Routers(e *gin.Engine) {
func Routers(e *gin.RouterGroup) {
// auth
e.PUT("/auth", authHandler)

g := e.Group("/admin", middleware.JWTMiddleWare)

// 第三方token
g.GET("/component-access-token", componentAccessTokenHandler)
g.GET("/cloudbase-access-token", cloudbaseAccessTokenHandler)
g.GET("/authorizer-access-token", authorizerAccessTokenHandler)
g.GET("/ticket", ticketHandler)
g.GET("/cloudbase-access-token", getCloudbaseAccessTokenHandler)
g.GET("/component-access-token", innerservice.GetComponentAccessTokenHandler)
g.GET("/authorizer-access-token", innerservice.GetAuthorizerAccessTokenHandler)
g.GET("/ticket", innerservice.GetTicketHandler)

// 消息与事件
g.GET("/wx-component-records", wxComponentRecordsHandler)
g.GET("/wx-biz-records", wxBizRecordsHandler)
g.GET("/callback-config", wxCallBackConfigHandler)
g.GET("/wx-component-records", getWxComponentRecordsHandler)
g.GET("/wx-biz-records", getWxBizRecordsHandler)
g.GET("/callback-config", getWxCallBackConfigHandler)
g.GET("/callback-proxy-rule-list", getCallBackProxyRuleListHandler)
g.POST("/callback-proxy-rule", updateCallBackProxyRuleHandler)
g.PUT("/callback-proxy-rule", addCallBackProxyRuleHandler)
g.DELETE("/callback-proxy-rule", delCallBackProxyRuleHandler)
g.POST("/callback-test", testCallbackRuleHandler)

// 小程序管理
g.POST("/pull-authorizer-list", pullAuthorizerListHandler)
Expand All @@ -38,4 +44,8 @@ func Routers(e *gin.Engine) {

// 刷新token
g.GET("/refresh-auth", refreshAuthHandler)

// 转发设置
g.GET("/proxy", getProxyHandler)
g.POST("/proxy", updateProxyHandler)
}
Loading

0 comments on commit a73b966

Please sign in to comment.