作者 yangfu

feat: ck user

... ... @@ -176,3 +176,12 @@ func (gateway HttpLibAlliedCreationUser) OrgSearch(param ReqOrgSearch) (int, []*
err := gateway.FastDoRequest(url, method, param, &data)
return data.Count, data.Orgs, err
}
// TerminalReport 终端汇报
func (gateway HttpLibAlliedCreationUser) TerminalReport(param ReqTerminalReport) (DataTerminalReport, error) {
url := gateway.Host() + "/terminal/report"
method := "post"
var data DataTerminalReport
err := gateway.FastDoRequest(url, method, param, &data)
return data, err
}
... ...
... ... @@ -163,3 +163,21 @@ type (
Orgs []*models.Organization `json:"orgs"`
}
)
//返回组织列表
type (
ReqTerminalReport struct {
TerminalType string `json:"terminalType"`
TerminalId string `json:"terminalId"`
Command string `json:"command"`
Content string `json:"content"`
Table string `json:"table"`
CompanyId int64 `json:"companyId"`
OrgId int64 `json:"orgId"`
}
DataTerminalReport struct {
Response string `json:"response"`
}
)
... ...
package service_gateway
import (
"fmt"
"github.com/beego/beego/v2/client/httplib"
"github.com/beego/beego/v2/server/web"
"github.com/beego/beego/v2/server/web/context"
"github.com/linmadan/egglib-go/log"
"github.com/linmadan/egglib-go/utils/json"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
"io/ioutil"
"net/http"
"strings"
)
type internalService interface {
GetResponseData(result Response, data interface{}) error
CreateRequest(url string, method string) *httplib.BeegoHTTPRequest
Host() string
}
func RedirectInternalService(prefix string, svr internalService, log log.Logger) web.FilterFunc {
return func(ctx *context.Context) {
if !strings.HasPrefix(ctx.Request.RequestURI, prefix) {
return
}
var err error
var byteResult []byte
var data = make(map[string]interface{})
defer func() {
if err != nil {
ctx.Output.SetStatus(http.StatusOK)
ctx.Output.JSON(map[string]interface{}{
"msg": err.Error(),
"code": 1,
"data": struct{}{},
}, false, false)
}
}()
method := strings.ToLower(ctx.Request.Method)
url := strings.Replace(ctx.Request.RequestURI, prefix, "", 1)
req := svr.CreateRequest(svr.Host()+url, method)
log.Debug(method + " 请求url:" + svr.Host() + url)
// 传递当前登录信息(可配置)
req.Header("companyId", fmt.Sprintf("%v", constant.MANUFACTURE_DEFAULT_COMPANYID))
req.Header("orgId", fmt.Sprintf("%v", constant.MANUFACTURE_DEFAULT_ORGID))
req.Body(ctx.Input.RequestBody)
response, err := req.Response()
if err != nil {
return
}
if response.StatusCode != http.StatusOK {
err = fmt.Errorf("%v", response.Status)
return
}
byteResult, err = ioutil.ReadAll(response.Body)
if err != nil {
return
}
defer response.Body.Close()
// 透传非json数据
contentType := response.Header.Get("Content-Type")
if contentType != "application/json" {
copyResponse(ctx, response, byteResult)
return
}
var result Response
err = json.Unmarshal(byteResult, &result)
if err != nil {
return
}
err = svr.GetResponseData(result, &data)
if err != nil {
return
}
ctx.Output.SetStatus(http.StatusOK)
ctx.Output.JSON(map[string]interface{}{
"msg": "成功",
"code": 0,
"data": data,
}, false, false)
}
}
func InvokeInternalService(ctx *context.Context, svr internalService, log log.Logger, wrapper func(ctx *context.Context, req *httplib.BeegoHTTPRequest)) (interface{}, error) {
var err error
var byteResult []byte
method := strings.ToLower(ctx.Request.Method)
url := strings.Replace(ctx.Request.RequestURI, "", "", 1)
req := svr.CreateRequest(svr.Host()+url, method)
log.Debug(method + " 请求url:" + svr.Host() + url)
// 传递当前登录信息(可配置)
//req.Header("companyId", fmt.Sprintf("%v", constant.MANUFACTURE_DEFAULT_COMPANYID))
//req.Header("orgId", fmt.Sprintf("%v", constant.MANUFACTURE_DEFAULT_ORGID))
//req.Body(ctx.Input.RequestBody)
if wrapper != nil {
wrapper(ctx, req)
}
response, err := req.Response()
if err != nil {
return nil, err
}
if response.StatusCode != http.StatusOK {
err = fmt.Errorf("%v", response.Status)
return nil, err
}
byteResult, err = ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
defer response.Body.Close()
var result Response
err = json.Unmarshal(byteResult, &result)
if err != nil {
return nil, err
}
return ([]byte)(result.Data), nil
}
func copyResponse(ctx *context.Context, response *http.Response, data []byte) {
for k, v := range response.Header {
if len(v) == 0 {
continue
}
ctx.Output.Header(k, strings.Join(v, ","))
}
ctx.Output.SetStatus(response.StatusCode)
ctx.Output.Body(data)
}
... ...
package controllers
import (
"fmt"
"github.com/beego/beego/v2/server/web/context"
"github.com/linmadan/egglib-go/web/beego"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/allied-lib/gateway/allied_creation_user"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/domainService"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
"strings"
... ... @@ -36,6 +40,7 @@ func (controller *DeviceZKTecoController) PostCdata() {
log.Logger.Debug(err.Error() + "data:" + bodyList[1])
}
}
RedirectToUserModule(controller.Ctx, false)
controller.Response(data, nil)
}
... ... @@ -46,9 +51,57 @@ func (controller *DeviceZKTecoController) GetCdata() {
func (controller *DeviceZKTecoController) GetRequest() {
//controller.Ctx.WriteString("C:11:DATA\tQUERY\tUSERINFO\tPIN=10086")
controller.Ctx.WriteString("OK")
//controller.Ctx.WriteString("C:7EceWxtHB6:DATA QUERY USERINFO PIN=3")
//controller.Ctx.WriteString("OK")
RedirectToUserModule(controller.Ctx, true)
}
func (controller *DeviceZKTecoController) Ping() {
controller.Ctx.WriteString("OK")
}
func RedirectToUserModule(ctx *context.Context, useResult bool) {
var svr = allied_creation_user.NewHttpLibAlliedCreationUser(constant.ALLIED_CREATION_USER_HOST)
data := ctx.Input.RequestBody
uri := ctx.Request.URL.Path
sn := ctx.Input.Query("SN")
table := ""
cmd := uri
switch uri {
case "/zkteco/iclock/getrequest": // 获取下行命令
cmd = "getrequest"
case "/zkteco/iclock/cdata": // 上报数据
cmd = "cdata"
table = ctx.Input.Query("table")
}
param := allied_creation_user.ReqTerminalReport{
TerminalType: "zkteco",
TerminalId: sn,
Command: cmd,
CompanyId: int64(constant.MANUFACTURE_DEFAULT_COMPANYID),
OrgId: int64(constant.MANUFACTURE_DEFAULT_ORGID),
Table: table,
Content: string(data),
}
if !useResult {
log.Logger.Debug(fmt.Sprintf("命令透传(sn:%v)-命令:%v 命令(内容):%v 成功", sn, cmd, string(data)))
go func() {
defer func() {
if p := recover(); p != nil {
log.Logger.Error(fmt.Sprintf("%v", p))
}
}()
svr.TerminalReport(param)
}()
return
}
response, err := svr.TerminalReport(param)
if err != nil {
log.Logger.Debug(fmt.Sprintf("命令透传(sn:%v)-命令:%v 命令(内容):%v 应答:%v 错误:%v", sn, cmd, string(data), response.Response, err.Error()))
ctx.WriteString("OK")
return
}
log.Logger.Debug(fmt.Sprintf("命令透传(sn:%v)-命令:%v 命令(内容):%v 应答:%v 成功", sn, cmd, string(data), response.Response))
ctx.WriteString(response.Response)
}
... ...