正在显示
4 个修改的文件
包含
215 行增加
和
1 行删除
| @@ -176,3 +176,12 @@ func (gateway HttpLibAlliedCreationUser) OrgSearch(param ReqOrgSearch) (int, []* | @@ -176,3 +176,12 @@ func (gateway HttpLibAlliedCreationUser) OrgSearch(param ReqOrgSearch) (int, []* | ||
| 176 | err := gateway.FastDoRequest(url, method, param, &data) | 176 | err := gateway.FastDoRequest(url, method, param, &data) |
| 177 | return data.Count, data.Orgs, err | 177 | return data.Count, data.Orgs, err |
| 178 | } | 178 | } |
| 179 | + | ||
| 180 | +// TerminalReport 终端汇报 | ||
| 181 | +func (gateway HttpLibAlliedCreationUser) TerminalReport(param ReqTerminalReport) (DataTerminalReport, error) { | ||
| 182 | + url := gateway.Host() + "/terminal/report" | ||
| 183 | + method := "post" | ||
| 184 | + var data DataTerminalReport | ||
| 185 | + err := gateway.FastDoRequest(url, method, param, &data) | ||
| 186 | + return data, err | ||
| 187 | +} |
| @@ -163,3 +163,21 @@ type ( | @@ -163,3 +163,21 @@ type ( | ||
| 163 | Orgs []*models.Organization `json:"orgs"` | 163 | Orgs []*models.Organization `json:"orgs"` |
| 164 | } | 164 | } |
| 165 | ) | 165 | ) |
| 166 | + | ||
| 167 | +//返回组织列表 | ||
| 168 | +type ( | ||
| 169 | + ReqTerminalReport struct { | ||
| 170 | + TerminalType string `json:"terminalType"` | ||
| 171 | + TerminalId string `json:"terminalId"` | ||
| 172 | + Command string `json:"command"` | ||
| 173 | + Content string `json:"content"` | ||
| 174 | + Table string `json:"table"` | ||
| 175 | + | ||
| 176 | + CompanyId int64 `json:"companyId"` | ||
| 177 | + OrgId int64 `json:"orgId"` | ||
| 178 | + } | ||
| 179 | + | ||
| 180 | + DataTerminalReport struct { | ||
| 181 | + Response string `json:"response"` | ||
| 182 | + } | ||
| 183 | +) |
| 1 | +package service_gateway | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + "github.com/beego/beego/v2/client/httplib" | ||
| 6 | + "github.com/beego/beego/v2/server/web" | ||
| 7 | + "github.com/beego/beego/v2/server/web/context" | ||
| 8 | + "github.com/linmadan/egglib-go/log" | ||
| 9 | + "github.com/linmadan/egglib-go/utils/json" | ||
| 10 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant" | ||
| 11 | + "io/ioutil" | ||
| 12 | + "net/http" | ||
| 13 | + "strings" | ||
| 14 | +) | ||
| 15 | + | ||
| 16 | +type internalService interface { | ||
| 17 | + GetResponseData(result Response, data interface{}) error | ||
| 18 | + CreateRequest(url string, method string) *httplib.BeegoHTTPRequest | ||
| 19 | + Host() string | ||
| 20 | +} | ||
| 21 | + | ||
| 22 | +func RedirectInternalService(prefix string, svr internalService, log log.Logger) web.FilterFunc { | ||
| 23 | + return func(ctx *context.Context) { | ||
| 24 | + if !strings.HasPrefix(ctx.Request.RequestURI, prefix) { | ||
| 25 | + return | ||
| 26 | + } | ||
| 27 | + var err error | ||
| 28 | + var byteResult []byte | ||
| 29 | + var data = make(map[string]interface{}) | ||
| 30 | + defer func() { | ||
| 31 | + if err != nil { | ||
| 32 | + ctx.Output.SetStatus(http.StatusOK) | ||
| 33 | + ctx.Output.JSON(map[string]interface{}{ | ||
| 34 | + "msg": err.Error(), | ||
| 35 | + "code": 1, | ||
| 36 | + "data": struct{}{}, | ||
| 37 | + }, false, false) | ||
| 38 | + } | ||
| 39 | + }() | ||
| 40 | + method := strings.ToLower(ctx.Request.Method) | ||
| 41 | + url := strings.Replace(ctx.Request.RequestURI, prefix, "", 1) | ||
| 42 | + req := svr.CreateRequest(svr.Host()+url, method) | ||
| 43 | + log.Debug(method + " 请求url:" + svr.Host() + url) | ||
| 44 | + // 传递当前登录信息(可配置) | ||
| 45 | + req.Header("companyId", fmt.Sprintf("%v", constant.MANUFACTURE_DEFAULT_COMPANYID)) | ||
| 46 | + req.Header("orgId", fmt.Sprintf("%v", constant.MANUFACTURE_DEFAULT_ORGID)) | ||
| 47 | + req.Body(ctx.Input.RequestBody) | ||
| 48 | + response, err := req.Response() | ||
| 49 | + if err != nil { | ||
| 50 | + return | ||
| 51 | + } | ||
| 52 | + if response.StatusCode != http.StatusOK { | ||
| 53 | + err = fmt.Errorf("%v", response.Status) | ||
| 54 | + return | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + byteResult, err = ioutil.ReadAll(response.Body) | ||
| 58 | + if err != nil { | ||
| 59 | + return | ||
| 60 | + } | ||
| 61 | + defer response.Body.Close() | ||
| 62 | + | ||
| 63 | + // 透传非json数据 | ||
| 64 | + contentType := response.Header.Get("Content-Type") | ||
| 65 | + if contentType != "application/json" { | ||
| 66 | + copyResponse(ctx, response, byteResult) | ||
| 67 | + return | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + var result Response | ||
| 71 | + err = json.Unmarshal(byteResult, &result) | ||
| 72 | + if err != nil { | ||
| 73 | + return | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + err = svr.GetResponseData(result, &data) | ||
| 77 | + if err != nil { | ||
| 78 | + return | ||
| 79 | + } | ||
| 80 | + ctx.Output.SetStatus(http.StatusOK) | ||
| 81 | + ctx.Output.JSON(map[string]interface{}{ | ||
| 82 | + "msg": "成功", | ||
| 83 | + "code": 0, | ||
| 84 | + "data": data, | ||
| 85 | + }, false, false) | ||
| 86 | + } | ||
| 87 | +} | ||
| 88 | + | ||
| 89 | +func InvokeInternalService(ctx *context.Context, svr internalService, log log.Logger, wrapper func(ctx *context.Context, req *httplib.BeegoHTTPRequest)) (interface{}, error) { | ||
| 90 | + var err error | ||
| 91 | + var byteResult []byte | ||
| 92 | + method := strings.ToLower(ctx.Request.Method) | ||
| 93 | + url := strings.Replace(ctx.Request.RequestURI, "", "", 1) | ||
| 94 | + req := svr.CreateRequest(svr.Host()+url, method) | ||
| 95 | + log.Debug(method + " 请求url:" + svr.Host() + url) | ||
| 96 | + // 传递当前登录信息(可配置) | ||
| 97 | + //req.Header("companyId", fmt.Sprintf("%v", constant.MANUFACTURE_DEFAULT_COMPANYID)) | ||
| 98 | + //req.Header("orgId", fmt.Sprintf("%v", constant.MANUFACTURE_DEFAULT_ORGID)) | ||
| 99 | + //req.Body(ctx.Input.RequestBody) | ||
| 100 | + if wrapper != nil { | ||
| 101 | + wrapper(ctx, req) | ||
| 102 | + } | ||
| 103 | + response, err := req.Response() | ||
| 104 | + if err != nil { | ||
| 105 | + return nil, err | ||
| 106 | + } | ||
| 107 | + if response.StatusCode != http.StatusOK { | ||
| 108 | + err = fmt.Errorf("%v", response.Status) | ||
| 109 | + return nil, err | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + byteResult, err = ioutil.ReadAll(response.Body) | ||
| 113 | + if err != nil { | ||
| 114 | + return nil, err | ||
| 115 | + } | ||
| 116 | + defer response.Body.Close() | ||
| 117 | + var result Response | ||
| 118 | + err = json.Unmarshal(byteResult, &result) | ||
| 119 | + if err != nil { | ||
| 120 | + return nil, err | ||
| 121 | + } | ||
| 122 | + return ([]byte)(result.Data), nil | ||
| 123 | +} | ||
| 124 | + | ||
| 125 | +func copyResponse(ctx *context.Context, response *http.Response, data []byte) { | ||
| 126 | + for k, v := range response.Header { | ||
| 127 | + if len(v) == 0 { | ||
| 128 | + continue | ||
| 129 | + } | ||
| 130 | + ctx.Output.Header(k, strings.Join(v, ",")) | ||
| 131 | + } | ||
| 132 | + ctx.Output.SetStatus(response.StatusCode) | ||
| 133 | + ctx.Output.Body(data) | ||
| 134 | +} |
| 1 | package controllers | 1 | package controllers |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | + "fmt" | ||
| 5 | + "github.com/beego/beego/v2/server/web/context" | ||
| 4 | "github.com/linmadan/egglib-go/web/beego" | 6 | "github.com/linmadan/egglib-go/web/beego" |
| 7 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant" | ||
| 5 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" | 8 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" |
| 9 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/allied-lib/gateway/allied_creation_user" | ||
| 6 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/domainService" | 10 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/domainService" |
| 7 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log" | 11 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log" |
| 8 | "strings" | 12 | "strings" |
| @@ -36,6 +40,7 @@ func (controller *DeviceZKTecoController) PostCdata() { | @@ -36,6 +40,7 @@ func (controller *DeviceZKTecoController) PostCdata() { | ||
| 36 | log.Logger.Debug(err.Error() + "data:" + bodyList[1]) | 40 | log.Logger.Debug(err.Error() + "data:" + bodyList[1]) |
| 37 | } | 41 | } |
| 38 | } | 42 | } |
| 43 | + RedirectToUserModule(controller.Ctx, false) | ||
| 39 | controller.Response(data, nil) | 44 | controller.Response(data, nil) |
| 40 | } | 45 | } |
| 41 | 46 | ||
| @@ -46,9 +51,57 @@ func (controller *DeviceZKTecoController) GetCdata() { | @@ -46,9 +51,57 @@ func (controller *DeviceZKTecoController) GetCdata() { | ||
| 46 | 51 | ||
| 47 | func (controller *DeviceZKTecoController) GetRequest() { | 52 | func (controller *DeviceZKTecoController) GetRequest() { |
| 48 | //controller.Ctx.WriteString("C:11:DATA\tQUERY\tUSERINFO\tPIN=10086") | 53 | //controller.Ctx.WriteString("C:11:DATA\tQUERY\tUSERINFO\tPIN=10086") |
| 49 | - controller.Ctx.WriteString("OK") | 54 | + //controller.Ctx.WriteString("C:7EceWxtHB6:DATA QUERY USERINFO PIN=3") |
| 55 | + //controller.Ctx.WriteString("OK") | ||
| 56 | + RedirectToUserModule(controller.Ctx, true) | ||
| 50 | } | 57 | } |
| 51 | 58 | ||
| 52 | func (controller *DeviceZKTecoController) Ping() { | 59 | func (controller *DeviceZKTecoController) Ping() { |
| 53 | controller.Ctx.WriteString("OK") | 60 | controller.Ctx.WriteString("OK") |
| 54 | } | 61 | } |
| 62 | + | ||
| 63 | +func RedirectToUserModule(ctx *context.Context, useResult bool) { | ||
| 64 | + var svr = allied_creation_user.NewHttpLibAlliedCreationUser(constant.ALLIED_CREATION_USER_HOST) | ||
| 65 | + data := ctx.Input.RequestBody | ||
| 66 | + uri := ctx.Request.URL.Path | ||
| 67 | + sn := ctx.Input.Query("SN") | ||
| 68 | + table := "" | ||
| 69 | + cmd := uri | ||
| 70 | + switch uri { | ||
| 71 | + case "/zkteco/iclock/getrequest": // 获取下行命令 | ||
| 72 | + cmd = "getrequest" | ||
| 73 | + case "/zkteco/iclock/cdata": // 上报数据 | ||
| 74 | + cmd = "cdata" | ||
| 75 | + table = ctx.Input.Query("table") | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + param := allied_creation_user.ReqTerminalReport{ | ||
| 79 | + TerminalType: "zkteco", | ||
| 80 | + TerminalId: sn, | ||
| 81 | + Command: cmd, | ||
| 82 | + CompanyId: int64(constant.MANUFACTURE_DEFAULT_COMPANYID), | ||
| 83 | + OrgId: int64(constant.MANUFACTURE_DEFAULT_ORGID), | ||
| 84 | + Table: table, | ||
| 85 | + Content: string(data), | ||
| 86 | + } | ||
| 87 | + if !useResult { | ||
| 88 | + log.Logger.Debug(fmt.Sprintf("命令透传(sn:%v)-命令:%v 命令(内容):%v 成功", sn, cmd, string(data))) | ||
| 89 | + go func() { | ||
| 90 | + defer func() { | ||
| 91 | + if p := recover(); p != nil { | ||
| 92 | + log.Logger.Error(fmt.Sprintf("%v", p)) | ||
| 93 | + } | ||
| 94 | + }() | ||
| 95 | + svr.TerminalReport(param) | ||
| 96 | + }() | ||
| 97 | + return | ||
| 98 | + } | ||
| 99 | + response, err := svr.TerminalReport(param) | ||
| 100 | + if err != nil { | ||
| 101 | + log.Logger.Debug(fmt.Sprintf("命令透传(sn:%v)-命令:%v 命令(内容):%v 应答:%v 错误:%v", sn, cmd, string(data), response.Response, err.Error())) | ||
| 102 | + ctx.WriteString("OK") | ||
| 103 | + return | ||
| 104 | + } | ||
| 105 | + log.Logger.Debug(fmt.Sprintf("命令透传(sn:%v)-命令:%v 命令(内容):%v 应答:%v 成功", sn, cmd, string(data), response.Response)) | ||
| 106 | + ctx.WriteString(response.Response) | ||
| 107 | +} |
-
请 注册 或 登录 后发表评论