Skip to content

Commit

Permalink
Merge pull request #7 from WeixinCloud/dev
Browse files Browse the repository at this point in the history
v2.1.0
  • Loading branch information
maoyutao committed Apr 14, 2022
2 parents c03ed1e + bd5cc50 commit 9098ffb
Show file tree
Hide file tree
Showing 59 changed files with 2,783 additions and 564 deletions.
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

cd client && npm run check-host
11 changes: 4 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ WORKDIR /wxcloudrun-wxcomponent
# 将当前目录(dockerfile所在目录)下所有文件都拷贝到工作目录下
COPY . /wxcloudrun-wxcomponent/

RUN cd /wxcloudrun-wxcomponent/client && npm install --registry=https://mirrors.cloud.tencent.com/npm/ && npm run build
RUN cd /wxcloudrun-wxcomponent/client && npm install --registry=https://registry.npm.taobao.org && npm run build

FROM golang:1.17.1-alpine3.14 as builder

Expand All @@ -21,12 +21,6 @@ RUN GOOS=linux GOARCH=amd64 go build -o main .
# 选用运行时所用基础镜像(GO语言选择原则:尽量体积小、包含基础linux内容的基础镜像)
FROM alpine:3.13

# 容器默认时区为UTC,如需使用上海时间请启用以下时区设置命令
# RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo Asia/Shanghai > /etc/timezone

# 使用 HTTPS 协议访问容器云调用证书安装
RUN apk add ca-certificates

# 指定运行时的工作目录
WORKDIR /wxcloudrun-wxcomponent

Expand All @@ -42,6 +36,9 @@ RUN apk --update add tzdata && \
apk del tzdata && \
rm -rf /var/cache/apk/*

# 兼容云托管开放接口服务
RUN apk add ca-certificates

# 设置release模式
ENV GIN_MODE release

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ go run main
+-----------------------+
| authorizers |
| comm |
| counter |
| user |
| wxcallback_biz |
| wxcallback_component |
Expand All @@ -97,6 +98,11 @@ go run main
- wxcallback_component: 推送给授权事件URL的消息
- wxcallback_rules: 消息转发规则
- wxtoken: component_access_token和authorizer_access_token
- counter: 登录失败计数
#### 命名格式
- 微信开放平台接口: 下划线
- 微管家前后端交互: 小驼峰
- 微信回调消息: 大驼峰

## License

Expand Down
36 changes: 26 additions & 10 deletions api/admin/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@ package admin
// 系统鉴权,登录,人员管理

import (
"fmt"
"net"
"net/http"
"strconv"
"strings"
"time"

"github.com/WeixinCloud/wxcloudrun-wxcomponent/comm/errno"
"github.com/WeixinCloud/wxcloudrun-wxcomponent/comm/log"
"github.com/astaxie/beego/validation"
"github.com/gin-gonic/gin"

"github.com/WeixinCloud/wxcloudrun-wxcomponent/comm/utils"
"github.com/WeixinCloud/wxcloudrun-wxcomponent/db/dao"
"github.com/WeixinCloud/wxcloudrun-wxcomponent/db/model"
)

const ErrHourlyLimit = 10

func checkAuth(req model.UserRecord) (int32, error) {
record, err := dao.GetUserRecord(req.Username, req.Password)
if err != nil {
Expand All @@ -36,21 +41,32 @@ func authHandler(c *gin.Context) {
return
}

valid := validation.Validation{}
ok, _ := valid.Valid(&req)

if !ok {
for _, err := range valid.Errors {
log.Debug(err.Key + " " + err.Message)
}
log.Error(valid.Errors)
c.JSON(http.StatusOK, errno.ErrAuthErr.WithData(valid.Errors))
ip, _, err := net.SplitHostPort(strings.TrimSpace(c.Request.RemoteAddr))
if err != nil {
log.Error(err.Error())
}
log.Info("Auth Ip: ", ip)
key := fmt.Sprintf("AUTH_%s_%d", ip, time.Now().Hour())
current, err := dao.GetCurrent(key)
if err != nil {
log.Error(err.Error())
c.JSON(http.StatusOK, errno.ErrSystemError.WithData(err.Error()))
return
}
log.Info("current: ", current)
if current >= ErrHourlyLimit {
c.JSON(http.StatusOK, errno.ErrAuthErrExceedLimit)
return
}

ID, err := checkAuth(req)
if err != nil {
log.Error(err.Error())
if err := dao.AddOne(key, ErrHourlyLimit); err != nil {
log.Error(err)
c.JSON(http.StatusOK, errno.ErrAuthErrExceedLimit)
return
}
c.JSON(http.StatusOK, errno.ErrAuthErr.WithData(err.Error()))
return
}
Expand Down
80 changes: 58 additions & 22 deletions api/admin/authorizer.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package admin

import (
"encoding/json"
"net/http"
"strconv"
"sync"
"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/comm/wx"
wxbase "github.com/WeixinCloud/wxcloudrun-wxcomponent/comm/wx/base"
Expand All @@ -18,19 +16,26 @@ import (
)

type getAuthorizerListReq struct {
ComponentAppid string `json:"component_appid"`
Offset int `json:"offset"`
Count int `json:"count"`
ComponentAppid string `wx:"component_appid"`
Offset int `wx:"offset"`
Count int `wx:"count"`
}

type authorizerInfo struct {
AuthorizerAppid string `json:"authorizer_appid"`
RefreshToken string `json:"refresh_token"`
AuthTime int64 `json:"auth_time"`
AuthorizerAppid string `wx:"authorizer_appid"`
RefreshToken string `wx:"refresh_token"`
AuthTime int64 `wx:"auth_time"`
}
type getAuthorizerListResp struct {
TotalCount int `json:"total_count"`
List []authorizerInfo `json:"list"`
TotalCount int `wx:"total_count"`
List []authorizerInfo `wx:"list"`
}

type getAuthorizerInfoResp struct {
model.Authorizer
RegisterType int `json:"registerType"`
AccountStatus int `json:"accountStatus"`
BasicConfig *wx.AuthorizerBasicConfig `json:"basicConfig"`
}

func pullAuthorizerListHandler(c *gin.Context) {
Expand Down Expand Up @@ -74,6 +79,18 @@ func pullAuthorizerListHandler(c *gin.Context) {
c.JSON(http.StatusOK, errno.OK)
}

func copyAuthorizerInfo(appinfo *wx.AuthorizerInfoResp, record *model.Authorizer) {
record.AppType = appinfo.AuthorizerInfo.AppType
record.ServiceType = appinfo.AuthorizerInfo.ServiceType.Id
record.NickName = appinfo.AuthorizerInfo.NickName
record.UserName = appinfo.AuthorizerInfo.UserName
record.HeadImg = appinfo.AuthorizerInfo.HeadImg
record.QrcodeUrl = appinfo.AuthorizerInfo.QrcodeUrl
record.PrincipalName = appinfo.AuthorizerInfo.PrincipalName
record.FuncInfo = appinfo.AuthorizationInfo.StrFuncInfo
record.VerifyInfo = appinfo.AuthorizerInfo.VerifyInfo.Id
}

func constructAuthorizerRecord(info authorizerInfo, record *model.Authorizer, wg *sync.WaitGroup) error {
defer wg.Done()
record.Appid = info.AuthorizerAppid
Expand All @@ -85,15 +102,7 @@ func constructAuthorizerRecord(info authorizerInfo, record *model.Authorizer, wg
log.Errorf("GetAuthorizerInfo fail %v", err)
return err
}
record.AppType = appinfo.AuthorizerInfo.AppType
record.ServiceType = appinfo.AuthorizerInfo.ServiceType.Id
record.NickName = appinfo.AuthorizerInfo.NickName
record.UserName = appinfo.AuthorizerInfo.UserName
record.HeadImg = appinfo.AuthorizerInfo.HeadImg
record.QrcodeUrl = appinfo.AuthorizerInfo.QrcodeUrl
record.PrincipalName = appinfo.AuthorizerInfo.PrincipalName
record.FuncInfo = appinfo.AuthorizationInfo.StrFuncInfo
record.VerifyInfo = appinfo.AuthorizerInfo.VerifyInfo.Id
copyAuthorizerInfo(&appinfo, record)
return nil
}

Expand All @@ -103,11 +112,11 @@ func getAuthorizerList(offset, count int, resp *getAuthorizerListResp) error {
Offset: offset,
Count: count,
}
_, respbody, err := httputils.PostWxJson("/cgi-bin/component/api_get_authorizer_list", req, true)
_, body, err := wx.PostWxJsonWithComponentToken("/cgi-bin/component/api_get_authorizer_list", "", req)
if err != nil {
return err
}
if err := json.Unmarshal(respbody, &resp); err != nil {
if err := wx.WxJson.Unmarshal(body, &resp); err != nil {
log.Errorf("Unmarshal err, %v", err)
return err
}
Expand All @@ -125,11 +134,38 @@ func getAuthorizerListHandler(c *gin.Context) {
c.JSON(http.StatusOK, errno.ErrInvalidParam.WithData(err.Error()))
return
}
if limit > 20 {
c.JSON(http.StatusOK, errno.ErrInvalidParam)
return
}
appid := c.DefaultQuery("appid", "")
records, total, err := dao.GetAuthorizerRecords(appid, offset, limit)
if err != nil {
c.JSON(http.StatusOK, errno.ErrSystemError.WithData(err.Error()))
return
}
c.JSON(http.StatusOK, errno.OK.WithData(gin.H{"total": total, "records": records}))
// 拉取最新的数据
wg := &sync.WaitGroup{}
wg.Add(len(records))
resp := make([]getAuthorizerInfoResp, len(records))
for i, record := range records {
go func(i int, record *model.Authorizer) {
defer wg.Done()
resp[i].Appid = record.Appid
resp[i].AuthTime = record.AuthTime
resp[i].RefreshToken = record.RefreshToken

var appinfo wx.AuthorizerInfoResp
if err := wx.GetAuthorizerInfo(record.Appid, &appinfo); err != nil {
log.Errorf("GetAuthorizerInfo fail %v", err)
return
}
copyAuthorizerInfo(&appinfo, &resp[i].Authorizer)
resp[i].RegisterType = appinfo.AuthorizerInfo.RegisterType
resp[i].AccountStatus = appinfo.AuthorizerInfo.AccountStatus
resp[i].BasicConfig = appinfo.AuthorizerInfo.BasicConfig
}(i, record)
}
wg.Wait()
c.JSON(http.StatusOK, errno.OK.WithData(gin.H{"total": total, "records": resp}))
}
8 changes: 4 additions & 4 deletions api/admin/callbackrules.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func getCallBackProxyRuleListHandler(c *gin.Context) {
Event: v.Event,
Open: v.Open,
Data: proxyConfig,
CreateTime: v.CreateTime.UnixNano() / 1e6,
UpdateTime: v.UpdateTime.UnixNano() / 1e6,
CreateTime: v.CreateTime.Unix(),
UpdateTime: v.UpdateTime.Unix(),
})
}
}
Expand Down Expand Up @@ -190,13 +190,13 @@ type wxCallBackReq struct {
func genWxCallBackReq(rule *model.WxCallbackRule) *wxCallBackReq {
if rule.InfoType != "" {
return &wxCallBackReq{
CreateTime: time.Now().UnixNano() / 1e6,
CreateTime: time.Now().Unix(),
InfoType: rule.InfoType,
Data: "TestData",
}
} else {
return &wxCallBackReq{
CreateTime: time.Now().UnixNano() / 1e6,
CreateTime: time.Now().Unix(),
MsgType: rule.MsgType,
Event: rule.Event,
ToUserName: "TestUserName1",
Expand Down
Loading

0 comments on commit 9098ffb

Please sign in to comment.