From 301c0e59830f3108f1122b756adff82b3b52d3d7 Mon Sep 17 00:00:00 2001 From: dlcios Date: Fri, 24 Nov 2023 16:43:54 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BF=E5=AE=A2=E7=B3=BB=E7=BB=9F=E6=8E=A5?= =?UTF-8?q?=E5=85=A5=E9=92=89=E9=92=89=E6=B6=88=E6=81=AF=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/v1/visitor.go | 24 ++++ go.mod | 3 +- go.sum | 22 ++- internal/controller/visitor.go | 16 +++ internal/dao/internal/visitor_record.go | 4 + internal/model/do/visitor_record.go | 2 + internal/model/entity/visitor_record.go | 2 + internal/model/visitor_record.go | 46 +++++++ internal/service/visitor.go | 171 ++++++++++++++++++++++++ main.go | 1 + 10 files changed, 287 insertions(+), 4 deletions(-) diff --git a/api/v1/visitor.go b/api/v1/visitor.go index f8bdfee..7518228 100644 --- a/api/v1/visitor.go +++ b/api/v1/visitor.go @@ -12,7 +12,9 @@ type VisitorAccessReq struct { VisitorPhone string `p:"visitor_phone" v:"required|phone#请填写拜访人手机|请填写正确的手机号码"` VisitorDepartment string `p:"visitor_department" v:"required|max-length:30#请填写来访单位|请填写正确的来访单位"` DstDepartment string `p:"dst_department" v:"required|max-length:20#请填写拜访人部门|请填写正确的拜访人部门"` + DstDepartmentId int `p:"dst_department_id"` DstName string `p:"dst_name" v:"required|max-length:10#访客名称不能为空|拜访人姓名过长"` + DstUserId string `p:"dst_user_id" v:"required|max-length:64#拜访人不存在|拜访人不存在"` DstLocation string `p:"dst_location" v:"required|max-length:10#拜访人地址不能为空|拜访人地址非法"` Reason string `p:"reason" v:"required|max-length:255#请填写来访事由|来访事由过长"` VehicleNumber string `p:"vehicle_number"` @@ -58,3 +60,25 @@ type VisitorListRes struct { Current int `json:"current"` Total int `json:"total"` } + +type GetDepartmentListReq struct { + g.Meta `path:"/get_department_list" tags:"department list" method:"post" summary:"department list"` + DeptId int `json:"dept_id"` +} + +type GetDepartmentListRes struct { + g.Meta `mime:"text/html" example:"string"` + List []*model.Dept `json:"list"` +} + +type GetDingUserReq struct { + g.Meta `path:"/get_user_list" tags:"user id list" method:"post" summary:"user id list"` + DeptId int `json:"dept_id"` + PageNum int `json:"cursor"` + PageSize int `json:"page_size"` +} + +type GetDingUserRes struct { + g.Meta `mime:"text/html" example:"string"` + List *model.UserResult `json:"list"` +} diff --git a/go.mod b/go.mod index a328bca..93fc3a5 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,6 @@ go 1.15 require ( github.com/gogf/gf/contrib/drivers/mysql/v2 v2.5.6 - github.com/gogf/gf/v2 v2.5.6 + github.com/gogf/gf/contrib/nosql/redis/v2 v2.5.7 + github.com/gogf/gf/v2 v2.5.7 ) diff --git a/go.sum b/go.sum index 0e5bac0..03b224d 100644 --- a/go.sum +++ b/go.sum @@ -1,14 +1,23 @@ github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0= github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= +github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= +github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= +github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/clbanning/mxj/v2 v2.7.0 h1:WA/La7UGCanFe5NpHF0Q3DNtnCsVoxbPKuyBNHWRyME= github.com/clbanning/mxj/v2 v2.7.0/go.mod h1:hNiWqW14h+kc+MdF9C6/YoRfjEJoR3ou6tn/Qo+ve2s= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= -github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= @@ -19,8 +28,11 @@ github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrt github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/gogf/gf/contrib/drivers/mysql/v2 v2.5.6 h1:oR9F4LVoKa/fjf/o6Y/CQRNiYy35Bszo07WwvMWYMxo= github.com/gogf/gf/contrib/drivers/mysql/v2 v2.5.6/go.mod h1:gvHSRqCpv2c+N0gDHsEldHgU/yM9tcCBdIEKZ32/TaE= -github.com/gogf/gf/v2 v2.5.6 h1:a1UK1yUP3s+l+vPxmV91+8gTarAP9b1IEOw0W7LNl6E= +github.com/gogf/gf/contrib/nosql/redis/v2 v2.5.7 h1:HgOudyYp8F6iVC6YgbqKhPMKgxQJj4MO8VBdJEMPTpk= +github.com/gogf/gf/contrib/nosql/redis/v2 v2.5.7/go.mod h1:jiRz86SerTb+z4KD4LtxgVw3IEcsWRBnL40FvIAg/sY= github.com/gogf/gf/v2 v2.5.6/go.mod h1:17K/gBYrp0bHGC3XYC7bSPoywmZ6MrZHrZakTfh4eIQ= +github.com/gogf/gf/v2 v2.5.7 h1:h+JSoD6z3d2q0uGszvtahrSm4DiM2ECyNjyTwKIo8wE= +github.com/gogf/gf/v2 v2.5.7/go.mod h1:x2XONYcI4hRQ/4gMNbWHmZrNzSEIg20s2NULbzom5k0= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= @@ -33,8 +45,9 @@ github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxec github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= @@ -42,6 +55,8 @@ github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/redis/go-redis/v9 v9.2.1 h1:WlYJg71ODF0dVspZZCpYmoF1+U1Jjk9Rwd7pq6QmlCg= +github.com/redis/go-redis/v9 v9.2.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= @@ -81,6 +96,7 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/internal/controller/visitor.go b/internal/controller/visitor.go index b8a37db..cbc9324 100644 --- a/internal/controller/visitor.go +++ b/internal/controller/visitor.go @@ -44,3 +44,19 @@ func (c *cVisitor) VisitorList(ctx context.Context, req *v1.VisitorListReq) (res } return } + +func (c *cVisitor) GetDepartmentList(ctx context.Context, req *v1.GetDepartmentListReq) (res *v1.GetDepartmentListRes, err error) { + res, err = service.Visitor.GetDingDepartmentList(ctx, req) + if err != nil { + return nil, gerror.Newf("系统繁忙,请稍后再试") + } + return +} + +func (c *cVisitor) GetDingUserList(ctx context.Context, req *v1.GetDingUserReq) (res *v1.GetDingUserRes, err error) { + res, err = service.Visitor.GetDingUserList(ctx, req) + if err != nil { + return nil, gerror.Newf("系统繁忙,请稍后再试") + } + return +} diff --git a/internal/dao/internal/visitor_record.go b/internal/dao/internal/visitor_record.go index 16b9a21..14c2181 100644 --- a/internal/dao/internal/visitor_record.go +++ b/internal/dao/internal/visitor_record.go @@ -25,7 +25,9 @@ type VisitorRecordColumns struct { VisitorIdentity string // 访客身份证号码 VisitorPhone string // 访客手机号 VisitorDepartment string // 来访单位 + DstDepartmentId string // 拜访部门id DstDepartment string // 拜访人部门 + DstUserId string // 拜访人用户id DstName string // 拜访人姓名 DstLocation string // 地址(楼宇和层数) Reason string // 来访事由 @@ -44,7 +46,9 @@ var visitorRecordColumns = VisitorRecordColumns{ VisitorIdentity: "visitor_identity", VisitorPhone: "visitor_phone", VisitorDepartment: "visitor_department", + DstDepartmentId: "dst_department_id", DstDepartment: "dst_department", + DstUserId: "dst_user_id", DstName: "dst_name", DstLocation: "dst_location", Reason: "reason", diff --git a/internal/model/do/visitor_record.go b/internal/model/do/visitor_record.go index c8613f5..25a4b34 100644 --- a/internal/model/do/visitor_record.go +++ b/internal/model/do/visitor_record.go @@ -17,7 +17,9 @@ type VisitorRecord struct { VisitorIdentity interface{} // 访客身份证号码 VisitorPhone interface{} // 访客手机号 VisitorDepartment interface{} // 来访单位 + DstDepartmentId interface{} // 拜访部门id DstDepartment interface{} // 拜访人部门 + DstUserId interface{} // 拜访人用户id DstName interface{} // 拜访人姓名 DstLocation interface{} // 地址(楼宇和层数) Reason interface{} // 来访事由 diff --git a/internal/model/entity/visitor_record.go b/internal/model/entity/visitor_record.go index c70b1b7..b32ce36 100644 --- a/internal/model/entity/visitor_record.go +++ b/internal/model/entity/visitor_record.go @@ -15,7 +15,9 @@ type VisitorRecord struct { VisitorIdentity string `json:"visitorIdentity" ` // 访客身份证号码 VisitorPhone string `json:"visitorPhone" ` // 访客手机号 VisitorDepartment string `json:"visitorDepartment" ` // 来访单位 + DstDepartmentId string `json:"dstDepartmentId" ` // 拜访部门id DstDepartment string `json:"dstDepartment" ` // 拜访人部门 + DstUserId string `json:"dstUserId" ` // 拜访人用户id DstName string `json:"dstName" ` // 拜访人姓名 DstLocation string `json:"dstLocation" ` // 地址(楼宇和层数) Reason string `json:"reason" ` // 来访事由 diff --git a/internal/model/visitor_record.go b/internal/model/visitor_record.go index 9ad42d5..14b3e00 100644 --- a/internal/model/visitor_record.go +++ b/internal/model/visitor_record.go @@ -15,7 +15,9 @@ type VisitorRecord struct { VisitorPhone string `json:"visitorPhone" ` // 访客手机号 VisitorDepartment string `json:"visitorDepartment" ` // 来访单位 DstDepartment string `json:"dstDepartment" ` // 拜访人部门 + DstDepartmentId int `json:"dst_dept_id" ` // 拜访人部门id DstName string `json:"dstName" ` // 拜访人姓名 + DstUserId string `json:"dst_user_id" ` // 拜访人用户id DstLocation string `json:"dstLocation" ` // 地址(楼宇和层数) Reason string `json:"reason" ` // 来访事由 VehicleNumber string `json:"vehicleNumber" ` // 车牌号码 @@ -25,3 +27,47 @@ type VisitorRecord struct { CreatedAt *gtime.Time `json:"createdAt" ` // 创建时间 UpdatedAt *gtime.Time `json:"updatedAt" ` // 更新时间 } + +type AccessTokenResp struct { + AccessToken string `json:"access_token"` + ExpiresIn int `json:"expires_in"` + ErrMsg string `json:"errmsg"` + ErrCode int `json:"errcode"` +} + +type DeptListResp struct { + ErrMsg string `json:"errmsg"` + ErrCode int `json:"errcode"` + Result []*Dept `json:"result"` + RequestId string `json:"request_id"` +} + +type Dept struct { + DeptId int `json:"dept_id"` + Name string `json:"name"` + ParentId int `json:"parent_id"` +} + +type UserListResp struct { + ErrMsg string `json:"errmsg"` + ErrCode int `json:"errcode"` + Result *UserResult `json:"result"` + RequestId string `json:"request_id"` +} + +type UserResult struct { + HasMore bool `json:"has_more"` + List []*User `json:"list"` +} + +type User struct { + UserId string `json:"userid"` + Name string `json:"name"` +} + +type DingMsgResp struct { + ErrMsg string `json:"errmsg"` + ErrCode int `json:"errcode"` + TaskId int `json:"task_id"` + RequestId string `json:"request_id"` +} diff --git a/internal/service/visitor.go b/internal/service/visitor.go index 042c8fc..8f1d325 100644 --- a/internal/service/visitor.go +++ b/internal/service/visitor.go @@ -2,6 +2,7 @@ package service import ( "context" + "encoding/json" "fmt" "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/frame/g" @@ -27,6 +28,171 @@ type IVisitor interface { VisitorStatistic(ctx context.Context, req *v1.VisitorAccessReq) (res *v1.VisitorAccessRes, err error) GetDiffDays(t1, t2 time.Time) int VisitorList(ctx context.Context, req *v1.VisitorListReq) (res *v1.VisitorListRes, err error) + GetDingDepartmentList(ctx context.Context, req *v1.GetDepartmentListReq) (res *v1.GetDepartmentListRes, err error) + GetDingUserList(ctx context.Context, req *v1.GetDingUserReq) (res *v1.GetDingUserRes, err error) +} + +func (s *visitorService) sendDingMsgByUserId(ctx context.Context, userId string, departmentName string, visitorName string, visitorPhone string, visitAt int) (err error) { + accessToken, err := s.getAccessToken(ctx) + if err != nil || accessToken == "" { + return gerror.Newf(fmt.Sprintf("%s 发送钉钉通知失败, err:获取access_token失败", userId)) + } + + agentId, _ := g.Cfg().Get(ctx, "ding.agentId") + + if agentId.String() == "" { + return gerror.Newf(fmt.Sprintf("%s 发送钉钉通知失败, agentId:%s, appKey:%s, secret:%s", userId, agentId)) + } + + param := g.Map{ + "agent_id": agentId, + "userid_list": userId, + "msg": g.Map{ + "msgtype": "text", + "text": g.Map{ + "content": fmt.Sprintf("来自 %s 访客 %s (电话:%s),将于 %s 来访,请做好接待工作 ^_^ ", departmentName, visitorName, visitorPhone, gtime.New(visitAt).Format("Y-m-d H:i:s")), + }, + }, + } + + url := fmt.Sprintf("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token=%s", accessToken) + r, err := g.Client().Post(ctx, url, param) + if err != nil { + return err + } + + defer r.Close() + var dingMsgResp model.DingMsgResp + resStr := r.ReadAllString() + + err = json.Unmarshal(gconv.Bytes(resStr), &dingMsgResp) + if err != nil { + return err + } + + if dingMsgResp.ErrCode != 0 { + return gerror.Newf(dingMsgResp.ErrMsg) + } + + return +} + +func (s *visitorService) GetDingUserList(ctx context.Context, req *v1.GetDingUserReq) (res *v1.GetDingUserRes, err error) { + accessToken, err := s.getAccessToken(ctx) + if err != nil || accessToken == "" { + return nil, gerror.Newf("获取用户列表失败") + } + + condition := g.Map{ + "language": "zh_CN", + "cursor": req.PageNum, + "size": req.PageSize, + "dept_id": req.DeptId, + "contain_access_limit": true, + } + + url := fmt.Sprintf("https://oapi.dingtalk.com/topapi/user/listsimple?access_token=%s", accessToken) + r, err := g.Client().Post(ctx, url, condition) + if err != nil { + return nil, err + } + + defer r.Close() + var userResp model.UserListResp + resStr := r.ReadAllString() + + err = json.Unmarshal(gconv.Bytes(resStr), &userResp) + if err != nil { + return nil, nil + } + + if userResp.ErrCode != 0 { + return nil, gerror.Newf(userResp.ErrMsg) + } + + res = &v1.GetDingUserRes{ + List: userResp.Result, + } + return res, nil +} + +func (s *visitorService) getAccessToken(ctx context.Context) (accessToken string, err error) { + cacheKey, _ := g.Cfg().Get(ctx, "ding.cacheKey") + + //有缓存 + cache, _ := g.Redis().Do(ctx, "GET", cacheKey) + + if cache.String() != "" { + return cache.String(), nil + } + + //无缓存 + key, _ := g.Cfg().Get(ctx, "ding.key") + secret, _ := g.Cfg().Get(ctx, "ding.secret") + url := fmt.Sprintf("https://oapi.dingtalk.com/gettoken?appkey=%s&appsecret=%s", key, secret) + r, err := g.Client().Get(ctx, url) + if err != nil { + return "", err + } + defer r.Close() + var accessTokenRes model.AccessTokenResp + resStr := r.ReadAllString() + + err = json.Unmarshal(gconv.Bytes(resStr), &accessTokenRes) + if err != nil { + return "", nil + } + + if accessTokenRes.ErrCode != 0 { + return "", gerror.Newf(accessTokenRes.ErrMsg) + } + + if accessTokenRes.AccessToken != "" && accessTokenRes.ExpiresIn > 0 { + _, _ = g.Redis().Do(ctx, "SET", cacheKey, accessTokenRes.AccessToken) + _, _ = g.Redis().Do(ctx, "EXPIRE", cacheKey, accessTokenRes.ExpiresIn-60) + } + + return accessTokenRes.AccessToken, nil +} + +func (s *visitorService) GetDingDepartmentList(ctx context.Context, req *v1.GetDepartmentListReq) (res *v1.GetDepartmentListRes, err error) { + accessToken, err := s.getAccessToken(ctx) + if err != nil || accessToken == "" { + return nil, gerror.Newf("获取部门列表失败") + } + + condition := g.Map{ + "language": "zh_CN", + } + + if req.DeptId != 0 { + condition["dept_id"] = req.DeptId + } + + url := fmt.Sprintf("https://oapi.dingtalk.com/topapi/v2/department/listsub?access_token=%s", accessToken) + r, err := g.Client().Post(ctx, url, condition) + if err != nil { + return nil, err + } + + defer r.Close() + var deptResp model.DeptListResp + resStr := r.ReadAllString() + + err = json.Unmarshal(gconv.Bytes(resStr), &deptResp) + + if err != nil { + return nil, nil + } + + if deptResp.ErrCode != 0 { + return nil, gerror.Newf(deptResp.ErrMsg) + } + + res = &v1.GetDepartmentListRes{ + List: deptResp.Result, + } + return res, nil } func (s *visitorService) VisitorList(ctx context.Context, req *v1.VisitorListReq) (res *v1.VisitorListRes, err error) { @@ -90,6 +256,11 @@ func (s *visitorService) VisitorAccess(ctx context.Context, req *v1.VisitorAcces return gerror.Newf("提交失败,请重试") } + err = s.sendDingMsgByUserId(ctx, req.DstUserId, req.VisitorDepartment, req.VisitorName, req.VisitorPhone, req.VisitAt) + if err != nil { + g.Log().Info(ctx, fmt.Sprintf("sendDingMsgByUserId err------------------ %s", err.Error())) + } + return nil } diff --git a/main.go b/main.go index 201887a..57e02df 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( _ "vistor/internal/packed" _ "github.com/gogf/gf/contrib/drivers/mysql/v2" + _ "github.com/gogf/gf/contrib/nosql/redis/v2" "github.com/gogf/gf/v2/os/gctx" "vistor/internal/cmd"