|
|
@ -2,6 +2,7 @@ package service |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"context" |
|
|
|
"context" |
|
|
|
|
|
|
|
"encoding/json" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"github.com/gogf/gf/v2/errors/gerror" |
|
|
|
"github.com/gogf/gf/v2/errors/gerror" |
|
|
|
"github.com/gogf/gf/v2/frame/g" |
|
|
|
"github.com/gogf/gf/v2/frame/g" |
|
|
@ -27,6 +28,173 @@ type IVisitor interface { |
|
|
|
VisitorStatistic(ctx context.Context, req *v1.VisitorAccessReq) (res *v1.VisitorAccessRes, err error) |
|
|
|
VisitorStatistic(ctx context.Context, req *v1.VisitorAccessReq) (res *v1.VisitorAccessRes, err error) |
|
|
|
GetDiffDays(t1, t2 time.Time) int |
|
|
|
GetDiffDays(t1, t2 time.Time) int |
|
|
|
VisitorList(ctx context.Context, req *v1.VisitorListReq) (res *v1.VisitorListRes, err error) |
|
|
|
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("获取用户列表失败") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cursor := (req.PageNum - 1) * req.PageSize |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
condition := g.Map{ |
|
|
|
|
|
|
|
"language": "zh_CN", |
|
|
|
|
|
|
|
"cursor": cursor, |
|
|
|
|
|
|
|
"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) { |
|
|
|
func (s *visitorService) VisitorList(ctx context.Context, req *v1.VisitorListReq) (res *v1.VisitorListRes, err error) { |
|
|
@ -90,6 +258,11 @@ func (s *visitorService) VisitorAccess(ctx context.Context, req *v1.VisitorAcces |
|
|
|
return gerror.Newf("提交失败,请重试") |
|
|
|
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 |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|