作者 yangfu

feat: 同步用户到考勤机

INSERT INTO "users"."menu"("menu_id", "parent_id", "menu_name", "code", "access_code", "menu_type", "icon", "sort", "remark", "category", "parent_path", "is_publish", "enable_status", "link") VALUES (274, 42, '同步考勤机', 'ADMIN_SYSTEM-MANAGE_BASE_USER_SYNC', 'w1-1-1-1-9', 'button', '', 9, '同步考勤机', '7', '37,39,40,42', 1, 0, NULL);
... ...
... ... @@ -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"`
}
)
... ...
... ... @@ -8,7 +8,10 @@ import (
"github.com/bwmarrin/snowflake"
jsonlib "github.com/linmadan/egglib-go/utils/json"
"github.com/shopspring/decimal"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
"io"
"io/ioutil"
"reflect"
"strconv"
"strings"
... ... @@ -422,3 +425,21 @@ func Truncate(value float64, places int32) float64 {
rsp, _ := quantity.Float64()
return rsp
}
func Utf8ToGbk(s []byte) ([]byte, error) {
reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewEncoder())
d, e := ioutil.ReadAll(reader)
if e != nil {
return nil, e
}
return d, nil
}
func GbkToUtf8(s []byte) ([]byte, error) {
reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewDecoder())
d, e := ioutil.ReadAll(reader)
if e != nil {
return nil, e
}
return d, nil
}
... ...
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/infrastructure/utils"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
"strings"
"time"
"unicode/utf8"
)
type DeviceZKTecoController struct {
... ... @@ -21,7 +27,8 @@ func (controller *DeviceZKTecoController) PostCdata() {
data := &domain.DeviceZkTeco{
Sn: sn,
}
if len(bodyList) > 2 {
table := controller.Ctx.Input.Query("table")
if len(bodyList) > 2 && table == "ATTLOG" {
data.UserNo = bodyList[0]
//转成时间格式
mTime, err := time.ParseInLocation("2006-01-02 15:04:05", bodyList[1], time.Local)
... ... @@ -36,6 +43,7 @@ func (controller *DeviceZKTecoController) PostCdata() {
log.Logger.Debug(err.Error() + "data:" + bodyList[1])
}
}
RedirectToUserModule(controller.Ctx, false)
controller.Response(data, nil)
}
... ... @@ -46,9 +54,72 @@ 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")
case "/zkteco/iclock/devicecmd":
cmd = "devicecmd"
}
content := string(data)
if !utf8.Valid(data) {
utf8Content, _ := utils.GbkToUtf8(data) //ConvertToString(content, "gbk", "utf-8")
content = string(utf8Content)
}
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: content,
}
if !useResult {
log.Logger.Debug(fmt.Sprintf("命令透传(sn:%v)-命令:%v 命令(内容):%v 成功", sn, cmd, content))
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))
gbkData, _ := utils.Utf8ToGbk([]byte(response.Response))
ctx.Output.Header("Content-Type", "text/plain;charset=gbk")
ack := string(gbkData)
ctx.WriteString(ack)
}
func (controller *DeviceZKTecoController) DeviceCmd() {
//sn := controller.Ctx.Input.Query("SN")
RedirectToUserModule(controller.Ctx, false)
controller.Ctx.WriteString("OK")
}
... ...
... ... @@ -21,4 +21,5 @@ func init() {
web.Router("/zkteco/iclock/cdata", &controllers.DeviceZKTecoController{}, "Get:GetCdata")
web.Router("/zkteco/iclock/getrequest", &controllers.DeviceZKTecoController{}, "Get:GetRequest")
web.Router("/zkteco/iclock/ping", &controllers.DeviceZKTecoController{}, "Get:Ping")
web.Router("/zkteco/iclock/devicecmd", &controllers.DeviceZKTecoController{}, "Post:DeviceCmd")
}
... ...