141 lines
3.6 KiB
Go
141 lines
3.6 KiB
Go
package dingtalk
|
|
|
|
import (
|
|
"ai_scheduler/internal/data/constants"
|
|
"ai_scheduler/internal/data/impl"
|
|
"ai_scheduler/internal/data/model"
|
|
"ai_scheduler/internal/entitys"
|
|
"ai_scheduler/internal/pkg"
|
|
"ai_scheduler/internal/pkg/l_request"
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gofiber/fiber/v2/log"
|
|
"xorm.io/builder"
|
|
)
|
|
|
|
type User struct {
|
|
dingUserImpl *impl.BotUserImpl
|
|
botConfigImpl *impl.BotConfigImpl
|
|
auth *Auth
|
|
logger log.Logger
|
|
}
|
|
|
|
func NewUser(
|
|
dingUserImpl *impl.BotUserImpl,
|
|
botConfig *impl.BotConfigImpl,
|
|
auth *Auth,
|
|
logger log.Logger,
|
|
) *User {
|
|
return &User{
|
|
dingUserImpl: dingUserImpl,
|
|
botConfigImpl: botConfig,
|
|
logger: logger,
|
|
auth: auth,
|
|
}
|
|
}
|
|
|
|
func (u *User) GetUserInfo(ctx context.Context, staffId string, botOption ...BotOption) (userInfo *entitys.DingTalkUserInfo, err error) {
|
|
if len(staffId) == 0 {
|
|
return
|
|
}
|
|
user, err := u.dingUserImpl.GetByStaffId(staffId)
|
|
if err != nil {
|
|
if !errors.Is(err, sql.ErrNoRows) {
|
|
return
|
|
}
|
|
}
|
|
//如果没有找到,则新增
|
|
if user == nil {
|
|
DingUserInfo, _err := u.GetUserInfoFromDingTalkWithBot(ctx, staffId, botOption...)
|
|
if _err != nil {
|
|
return nil, _err
|
|
}
|
|
dingUserDo := &model.AiBotUser{
|
|
StaffID: DingUserInfo.Userid,
|
|
Name: DingUserInfo.Name,
|
|
Title: DingUserInfo.Title,
|
|
Extension: DingUserInfo.Extension,
|
|
DeptIDList: DingUserInfo.DeptIdList,
|
|
IsBoss: int32(pkg.Ter(DingUserInfo.Boss == "true", constants.IsBossTrue, constants.IsBossFalse)),
|
|
IsSenior: int32(pkg.Ter(DingUserInfo.Boss == "true", constants.IsSeniorTrue, constants.IsSeniorFalse)),
|
|
HiredDate: time.Unix(int64(DingUserInfo.HiredDate), 0),
|
|
}
|
|
//deptIdList, _err := pkg.StringToSlice(DingUserInfo.DeptIdList)
|
|
//if err != nil {
|
|
// return nil, _err
|
|
//}
|
|
dingUserDo.DeptIDList = strings.Trim(dingUserDo.DeptIDList, "[]")
|
|
}
|
|
return
|
|
}
|
|
|
|
func (u *User) GetUserInfoFromDingTalkWithBot(ctx context.Context, staffId string, botOption ...BotOption) (userInfo Result, err error) {
|
|
botInfo := &Bot{}
|
|
for _, option := range botOption {
|
|
option(botInfo)
|
|
}
|
|
|
|
if botInfo.id == 0 && botInfo.botConfig == nil {
|
|
err = errors.New("botInfo is nil")
|
|
return
|
|
}
|
|
if botInfo.botConfig == nil {
|
|
cond := builder.NewCond()
|
|
cond = cond.And(builder.Eq{"bot_id": botInfo.id})
|
|
err = u.botConfigImpl.GetOneBySearchToStrut(&cond, botInfo.botConfig)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
}
|
|
var config entitys.DingTalkBot
|
|
err = json.Unmarshal([]byte(botInfo.botConfig.BotConfig), &config)
|
|
if err != nil {
|
|
log.Infof("初始化“%s”失败:%s", botInfo.botConfig.BotName, err.Error())
|
|
return
|
|
}
|
|
token, err := u.auth.GetAccessToken(ctx, config.ClientId, config.ClientSecret)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
return u.GetUserInfoFromDingTalk(ctx, token, staffId)
|
|
}
|
|
|
|
func (u *User) GetUserInfoFromDingTalk(ctx context.Context, token string, staffId string) (user Result, err error) {
|
|
if token == "" && staffId == "" {
|
|
err = errors.New("获取钉钉用户信息的必要参数不足")
|
|
return
|
|
}
|
|
|
|
req := l_request.Request{
|
|
Method: http.MethodPost,
|
|
Url: constants.GetDingTalkRequestUrl(constants.RequestUrlGetUserGet, map[string]string{
|
|
"access_token": token,
|
|
}),
|
|
Data: map[string]string{
|
|
"userid": staffId,
|
|
},
|
|
}
|
|
res, err := req.Send()
|
|
if err != nil {
|
|
return
|
|
}
|
|
var userInfoResResult UserInfoResResult
|
|
err = json.Unmarshal(res.Content, &userInfoResResult)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if userInfoResResult.Errcode != "0" {
|
|
fmt.Errorf("钉钉请求报错:%s", userInfoResResult.Errmsg)
|
|
}
|
|
return userInfoResResult.Result, err
|
|
}
|