package service import ( "context" "encoding/base64" "errors" "finclip-app-manager/domain/entity" "finclip-app-manager/domain/entity/proto" "finclip-app-manager/domain/entity/proto/apiproto" "finclip-app-manager/domain/repository" "finclip-app-manager/infrastructure/config" impl "finclip-app-manager/infrastructure/db/repo" "finclip-app-manager/infrastructure/utils" "fmt" ) const ( qrcodePrefix = "-f-" qrcodeSuffix = "--" ) type QrCodeInfoService struct { qrcodeRepo repository.IQrCodeInfoRepo buildInfoRepo repository.IAppBuildInfoRepo } func NewQrCodeInfoService() *QrCodeInfoService { return &QrCodeInfoService{ qrcodeRepo: impl.InitQrCodeInfoRepo(), buildInfoRepo: impl.InitBuildInfoRepo(), } } func (q *QrCodeInfoService) GenQrcodeInfo(ctx context.Context, req proto.GenQrcodeReq) (*entity.QrCodeInfo, error) { var ( info *entity.QrCodeInfo err error ) //首先查看是否已经存在uuid switch req.Type { case entity.QrCodeTypeReview: info, err = q.qrcodeRepo.GetReviewInfo(ctx, req.AppId, req.Sequence) case entity.QrCodeTypeRelease: info, err = q.qrcodeRepo.GetReleaseInfo(ctx, req.AppId) case entity.QrCodeTypeTrial: info, err = q.qrcodeRepo.GetTrialInfo(ctx, req.AppId) case entity.QrCodeTypeTemporary: info, err = q.qrcodeRepo.GetTemporaryInfo(ctx, req.AppId, req.Sequence) case entity.QrCodeTypeRomoteDebug: info, err = q.qrcodeRepo.GetRemoteDebugInfo(ctx, req.AppId, req.Sequence) default: return nil, errors.New("type err") } if err != nil { if !q.qrcodeRepo.NotFound(err) { return nil, err } //不存在则进行插入 info, err = q.insertQrcodeInfo(ctx, req) if err != nil { log.Errorf("insertQrcodeInfo err:%s", err.Error()) return nil, err } return info, nil } //ide 临时小程序更新二维码时需更新过期时间 if info.Type == entity.QrCodeTypeTemporary || info.Type == entity.QrCodeTypeRomoteDebug || info.Type == entity.QrCodeTypeReview { expTime := int64(0) if info.Type == entity.QrCodeTypeReview { expTime = utils.GetNowMs() + int64(config.GetConfig().ReviewQRcodeExpireTime*1e3) } else { expTime = utils.GetNowMs() + int64(config.GetConfig().QRcodeExpireTime*1e3) } err = q.qrcodeRepo.UpdateInfo(ctx, info.Uuid, map[string]interface{}{"expire_time": expTime}) if err != nil { return nil, err } info.ExpireTime = expTime if req.Type == entity.QrCodeTypeTemporary { path := req.StartParams.Path query := req.StartParams.Query appStartParams := entity.AppStartParams{} if len(query) > 0 { appStartParams.PathAndQuery = path + "?" + query } else { appStartParams.PathAndQuery = path } err = q.qrcodeRepo.UpdateStartParamsByUuid(ctx, info.Uuid, appStartParams) if err != nil { return nil, err } } } if req.ApiServer != info.ApiServer { err = q.qrcodeRepo.UpdateApiServer(ctx, info.Uuid, req.ApiServer) if err != nil { log.Errorf("GenQrcodeInfo UpdateApiServer err:%s", err.Error()) return nil, err } info.ApiServer = req.ApiServer } return info, nil } func (q *QrCodeInfoService) insertQrcodeInfo(ctx context.Context, req proto.GenQrcodeReq) (*entity.QrCodeInfo, error) { //首先查看是否已经存在uuid var ( qrCodeInfo *entity.QrCodeInfo nowMs = utils.GetNowMs() ) path := req.StartParams.Path query := req.StartParams.Query appStartParams := entity.AppStartParams{} if len(query) > 0 { appStartParams.PathAndQuery = path + "?" + query } else { appStartParams.PathAndQuery = path } switch req.Type { case entity.QrCodeTypeReview: //生成该版本的二维码信息 qrCodeInfo = &entity.QrCodeInfo{ Type: entity.QrCodeTypeReview, Uuid: GenReviewQrCodeUuid(req.AppId, req.Sequence), AppId: req.AppId, Sequence: req.Sequence, ApiServer: req.ApiServer, CodeId: req.CodeId, StartParams: appStartParams, CreateTime: nowMs, DebugInfo: req.DebugInfo, ExpireTime: nowMs + int64(config.GetConfig().ReviewQRcodeExpireTime*1e3), } case entity.QrCodeTypeRelease: qrCodeInfo = &entity.QrCodeInfo{ Type: entity.QrCodeTypeRelease, Uuid: GenReleaseQrCodeUuid(req.AppId), AppId: req.AppId, Sequence: req.Sequence, ApiServer: req.ApiServer, StartParams: appStartParams, ExpireTime: 0, CreateTime: nowMs, DebugInfo: req.DebugInfo, } case entity.QrCodeTypeTrial: qrCodeInfo = &entity.QrCodeInfo{ Type: entity.QrCodeTypeTrial, Uuid: GenTrialQrCodeUuid(req.AppId), AppId: req.AppId, Sequence: 0, ApiServer: req.ApiServer, CodeId: req.CodeId, //todo ccc StartParams: appStartParams, CreateTime: nowMs, DebugInfo: req.DebugInfo, } case entity.QrCodeTypeTemporary: qrCodeInfo = &entity.QrCodeInfo{ Type: entity.QrCodeTypeTemporary, Uuid: GenTemporaryQrCodeUuid(req.AppId, entity.AppTempDefSequence), AppId: req.AppId, Sequence: entity.AppTempDefSequence, ApiServer: req.ApiServer, CodeId: "", StartParams: appStartParams, ExpireTime: nowMs + int64(config.GetConfig().QRcodeExpireTime*1e3), CreateTime: nowMs, DebugInfo: req.DebugInfo, } case entity.QrCodeTypeRomoteDebug: qrCodeInfo = &entity.QrCodeInfo{ Type: entity.QrCodeTypeRomoteDebug, Uuid: GenRemoteDebugQrCodeUuid(req.AppId, entity.AppTempDefSequence), AppId: req.AppId, Sequence: entity.AppTempDefSequence, ApiServer: req.ApiServer, CodeId: "", StartParams: appStartParams, ExpireTime: nowMs + int64(config.GetConfig().QRcodeExpireTime*1e3), CreateTime: nowMs, DebugInfo: req.DebugInfo, } default: return nil, errors.New("type err") } return qrCodeInfo, q.qrcodeRepo.Insert(ctx, qrCodeInfo) } func (q *QrCodeInfoService) DeleteWechatQrcode(ctx context.Context, req apiproto.DeleteWechatInfoReq) error { //wechatT := db.NewTable(db.TableWechatInfo) //params := map[string]interface{}{"qrcodeUrl": "", "qrcodeDownloadUrl": "", "updated": time.Now().UnixNano() / 1e6} //wechatT.UpdateOne(ctx, bson.M{"appId": req.AppId}, bson.M{"$set": params}) //s.ResetWechatHint(ctx, req.AppId) err := hCaller.DeleteWechatQrcode(ctx, req) if err != nil { return err } return nil } func (q *QrCodeInfoService) GetQrcodeInfo(ctx context.Context, uuid string) (*entity.QrCodeInfo, error) { repo := impl.InitQrCodeInfoRepo() info, err := repo.GetInfoByUuid(ctx, uuid) if err != nil { if repo.NotFound(err) { return nil, entity.NotFoundErr } return nil, err } return info, nil } func (q *QrCodeInfoService) UpdateTrialStartParams(ctx context.Context, codeId string, params entity.AppStartParams) error { _, err := q.qrcodeRepo.GetInfoByCodeId(ctx, codeId) if err != nil { if !repository.NotFound(err) { return err } buildInfo, err := q.buildInfoRepo.GetInfoById(ctx, codeId) if err != nil { return err } newQrcodeInfo := &entity.QrCodeInfo{ Type: entity.QrCodeTypeTrial, Uuid: GenTrialQrCodeUuid(buildInfo.AppID), AppId: buildInfo.AppID, Sequence: 0, ApiServer: config.Cfg.ApiServer, CodeId: codeId, //todo ccc StartParams: params, CreateTime: utils.GetNowMs(), } err = q.qrcodeRepo.Insert(ctx, newQrcodeInfo) if err != nil { return err } } return q.qrcodeRepo.UpdateTrialStartParams(ctx, codeId, params) } func GenReviewQrCodeUuid(appId string, seq int) string { s := utils.Gen16Md5(fmt.Sprintf("type=%s&appId=%s&sequence=%d", entity.QrCodeTypeReview, appId, seq)) return UuidJoinFix(base64.RawURLEncoding.EncodeToString([]byte(s))) } func GenReleaseQrCodeUuid(appId string) string { s := utils.Gen16Md5(fmt.Sprintf("type=%s&appId=%s", entity.QrCodeTypeRelease, appId)) return UuidJoinFix(base64.RawURLEncoding.EncodeToString([]byte(s))) } func GenTrialQrCodeUuid(appId string) string { s := utils.Gen16Md5(fmt.Sprintf("type=%s&appId=%s", entity.QrCodeTypeTrial, appId)) return UuidJoinFix(base64.RawURLEncoding.EncodeToString([]byte(s))) } func GenTemporaryQrCodeUuid(appId string, seq int) string { s := utils.Gen16Md5(fmt.Sprintf("type=%s&appId=%s&sequence=%d", entity.QrCodeTypeTemporary, appId, seq)) return UuidJoinFix(base64.RawURLEncoding.EncodeToString([]byte(s))) } func GenRemoteDebugQrCodeUuid(appId string, seq int) string { s := utils.Gen16Md5(fmt.Sprintf("type=%s&appId=%s&sequence=%d", entity.QrCodeTypeRomoteDebug, appId, seq)) return UuidJoinFix(base64.RawURLEncoding.EncodeToString([]byte(s))) } func UuidJoinFix(id string) string { return qrcodePrefix + id + qrcodeSuffix }