package script import ( "context" "encoding/json" "errors" "fmt" "net/http" "time" "finclip-app-manager/domain/entity" "finclip-app-manager/domain/repository" cache "finclip-app-manager/infrastructure/cache/redis" "finclip-app-manager/infrastructure/client/httpcall" "finclip-app-manager/infrastructure/config" impl "finclip-app-manager/infrastructure/db/repo" "finclip-app-manager/infrastructure/logger" "finclip-app-manager/infrastructure/utility" ) type AutoReviewApp struct { AppOperConfigRepo repository.IAppOperConfigRepo appRepo repository.AppRepository } func NewAutoReviewApp() *AutoReviewApp { return &AutoReviewApp{ AppOperConfigRepo: impl.InitAppOperConfigRepo(), appRepo: impl.InitAppRepo(), } } func (a *AutoReviewApp) ReviewApps(ctx context.Context) { fmt.Printf("start auto review app...\n") if config.Cfg.PublishEnv != entity.ENV_PRIVATE { fmt.Printf("no need auto review app\n") return } redisLockKey := "mop_auto_review_app_lock" getOperAdminInfo := false adminInfo := &GetOperAdminInfoRsp{} for { time.Sleep(60 * time.Second) isSuc, err := cache.TryLock(ctx, redisLockKey, 600) //可以多实例部署,但是任何使用只有一个实例可以工作 if !isSuc || err != nil { if err != nil { logger.GetLogger().Errorf("lock err:%s\n", err.Error()) } else { logger.GetLogger().Errorf("lock fail") } continue } //获取配置 configItem, _ := a.AppOperConfigRepo.Find(ctx) if configItem.AutoReviewApp == 0 { fmt.Printf("auto review app configItem.AutoReviewApp = %d\n", configItem.AutoReviewApp) cache.Unlock(ctx, redisLockKey) continue } if !getOperAdminInfo { adminInfo, err = a.GetOperAdminInfo(ctx) if err != nil || adminInfo == nil { cache.Unlock(ctx, redisLockKey) continue } else if adminInfo.Data.Info.ID == "" { logger.GetLogger().Errorf("adminInfo.Data.Info.ID empty") cache.Unlock(ctx, redisLockKey) continue } getOperAdminInfo = true } //获取审核中列表 pageNo := 1 pageSize := 20 for { _, list, err := a.appRepo.ListAppVers(ctx, pageNo, pageSize, "", "pendingReview", "") if err != nil { logger.GetLogger().Errorf("a.appRepo.ListAppVers err:%s\n", err.Error()) break } else if len(list) == 0 { fmt.Printf("auto review app a.appRepo.ListAppVers empty\n") break } a.DoReview(ctx, list, adminInfo.Data.Info.ID) pageNo += 1 } cache.Unlock(ctx, redisLockKey) fmt.Printf("auto review app success\n") } } func (a *AutoReviewApp) DoReview(ctx context.Context, list []entity.AppVersion, userId string) { for _, v := range list { var auditReq AuditAppInternalReq auditReq.AccountId = userId auditReq.AppId = v.AppID auditReq.Sequence = v.Sequence auditReq.Operation = "Approve" auditReq.Reason = "系统(自动审核)" auditReq.Platform = "oper" a.AuditAppInternal(ctx, &auditReq, userId) time.Sleep(1 * time.Second) } } type AuditAppInternalRsp struct { Errcode string `json:"errcode"` Error string `json:"error"` } type AuditAppInternalReq struct { AccountId string `json:"accountId"` AppId string `json:"appId"` Sequence int `json:"sequence"` Operation string `json:"operation"` Reason string `json:"reason"` Platform string `json:"platform"` } func (a *AutoReviewApp) AuditAppInternal(ctx context.Context, req *AuditAppInternalReq, accountId string) (*AuditAppInternalRsp, error) { var headers = map[string]string{ "Accept": "application/json, text/plain, */*", "Content-Type": "application/json", "X-Consumer-Custom-ID": accountId, "url-call": "internal", } url := "http://mop-audit-manage-svr:8080/api/v1/mop/mop-audit-manage-svr/operation/applications" client := httpcall.NewClient() rsp := AuditAppInternalRsp{} resp, err := client.Request(ctx).SetHeaders(headers).SetBody(req).SetResult(&rsp).Put(url) if err != nil { logger.GetLogger().Errorf("AuditAppInternal req error:%s", err.Error()) return nil, err } if resp.StatusCode() != http.StatusOK { json.Unmarshal([]byte(resp.Body()), &rsp) logger.GetLogger().Errorf("AuditAppInternal status code err,rsp:%s", resp.String()) return &rsp, errors.New("AuditAppInternal status code err") } logger.GetLogger().Debugf("AuditAppInternal rsp=%s", utility.InterfaceToJsonString(rsp)) return &rsp, nil } type MopOperAdminInfo struct { Info struct { ID string `json:"id"` Phone string `json:"phone"` Account string `json:"account"` CreateTime int64 `json:"createTime"` } `json:"info"` } type GetOperAdminInfoRsp struct { Errcode string `json:"errcode"` Error string `json:"error"` Data MopOperAdminInfo `json:"data"` } func (a *AutoReviewApp) GetOperAdminInfo(ctx context.Context) (*GetOperAdminInfoRsp, error) { var headers = map[string]string{ "Accept": "application/json, text/plain, */*", "Content-Type": "application/json", "url-call": "internal", } var result GetOperAdminInfoRsp client := httpcall.NewClient() url := "http://mop-account-system:8080/api/v1/mop/applets-ecol-account/operation/admin-info" rsp, err := client.Request(ctx).SetHeaders(headers).SetResult(&result).Get(url) if err != nil { logger.GetLogger().Errorf("goResty fail, rsp:%s, err:%s", rsp.String(), err.Error()) return nil, err } else if rsp.StatusCode() != 200 { logger.GetLogger().Errorf("goResty fail, httpCode:%d", rsp.StatusCode()) return nil, errors.New("status err") } return &result, nil }