package proj import ( "context" "dashoo.cn/opms_libary/myerrors" "dashoo.cn/opms_libary/plugin/dingtalk" "encoding/base64" "fmt" "github.com/gogf/gf/util/gconv" "io" "net/http" "strings" projDao "dashoo.cn/micro/app/dao/proj" model "dashoo.cn/micro/app/model/proj" "dashoo.cn/micro/app/service" ) type businessFileService struct { *service.ContextService Dao *projDao.ProjBusinessFileDao } func NewBusinessFileService(ctx context.Context) (svc *businessFileService, err error) { svc = new(businessFileService) if svc.ContextService, err = svc.Init(ctx); err != nil { return nil, err } svc.Dao = projDao.NewProjBusinessFileDao(svc.Tenant) return svc, nil } func (p *businessFileService) GetList(busId int64) (fileList []*model.ProjBusinessFile, err error) { err = p.Dao.Where(p.Dao.C.BusId, busId).Order("id desc").Scan(&fileList) return } func (p *businessFileService) GetEntityById(id int64) (business *model.ProjBusinessFile, err error) { err = p.Dao.Where(projDao.ProjBusinessFile.C.Id, id).Scan(&business) return } func (p *businessFileService) Create(req *model.ProjBusinessFileReq) (err error) { businessData := new(model.ProjBusinessFile) if err = gconv.Struct(req, businessData); err != nil { return } service.SetCreatedInfo(businessData, p.GetCxtUserId(), p.GetCxtUserName()) _, err = p.Dao.Insert(businessData) return } func (p *businessFileService) DeleteByIds(ids []int64) (err error) { _, err = p.Dao.WhereIn(projDao.ProjBusinessFile.C.Id, ids).Delete() return } func (p businessFileService) DownloadDingTalkFile(id int) (string, error) { ent, err := p.Dao.WherePri(id).One() if err != nil { return "", err } if ent == nil { return "", myerrors.TipsError("附件不存在") } if !strings.HasPrefix(ent.FileUrl, "dingtalk") { return "", myerrors.TipsError("此附件不是钉钉附件") } fileInfo := strings.Split(ent.FileUrl, ":") if len(fileInfo) != 3 { return "", myerrors.TipsError("钉钉附件地址不合法") } spaceId := fileInfo[1] fileId := fileInfo[2] // res, err := dingtalk.Client.GetStorage().AddPermission(dingtalk.Client.Context.CorpId, spaceId, s.userInfo.DingtalkId, "EDITOR", "USER") // fmt.Println(res, err) // if err != nil { // return "", fmt.Errorf("设置权限异常 %s", err.Error()) // } resp, err := dingtalk.Client.GetStorage().QueryFileDownloadInfo(spaceId, fileId, p.CxtUser.DingtalkId) if err != nil { return "", myerrors.TipsError("获取文件下载信息异常") } fmt.Println(resp, err) req, err := http.NewRequest("GET", resp.HeaderSignatureInfo.ResourceUrls[0], nil) if err != nil { return "", fmt.Errorf("构建文件下载请求异常 %s", err.Error()) } for k, v := range resp.HeaderSignatureInfo.Headers { req.Header.Add(k, v) } fileresp, err := http.DefaultClient.Do(req) if err != nil { return "", fmt.Errorf("文件下载异常 %s", err.Error()) } data, err := io.ReadAll(fileresp.Body) if err != nil { return "", fmt.Errorf("读取下载内容异常 %s", err.Error()) } return base64.StdEncoding.EncodeToString(data), nil }