147 lines
4.2 KiB
Go
147 lines
4.2 KiB
Go
|
package service
|
||
|
|
||
|
import (
|
||
|
"finclip-app-manager/domain/entity"
|
||
|
"finclip-app-manager/domain/entity/proto"
|
||
|
"finclip-app-manager/domain/repository"
|
||
|
"finclip-app-manager/infrastructure/config"
|
||
|
impl "finclip-app-manager/infrastructure/db/repo"
|
||
|
"finclip-app-manager/infrastructure/utility"
|
||
|
"finclip-app-manager/infrastructure/utils"
|
||
|
"net/http"
|
||
|
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"gitlab.finogeeks.club/finclip-backend/apm"
|
||
|
"gopkg.in/mgo.v2/bson"
|
||
|
)
|
||
|
|
||
|
type AppTempInfoService struct {
|
||
|
}
|
||
|
|
||
|
func NewAppTempInfoService() *AppTempInfoService {
|
||
|
var s AppTempInfoService
|
||
|
return &s
|
||
|
}
|
||
|
|
||
|
func (s AppTempInfoService) UpdateTempApp(c *gin.Context, appId string, req *proto.UpdateTempAppInfoReq) {
|
||
|
traceCtx := apm.ApmClient().TraceContextFromGin(c)
|
||
|
|
||
|
var (
|
||
|
rspData = make(map[string]interface{})
|
||
|
)
|
||
|
|
||
|
repo := impl.InitAppTempInfoRepo()
|
||
|
appTempInfo, err := repo.GetInfoByAppId(traceCtx, appId)
|
||
|
if err != nil {
|
||
|
log.Errorf("GetInfoByAppIdAndSeq err:%s", err.Error())
|
||
|
if repo.NotFound(err) {
|
||
|
utility.MakeLocRsp(c, http.StatusBadRequest, utility.FS_APP_SEQUENCE_NOT_FOUND, rspData)
|
||
|
return
|
||
|
}
|
||
|
utility.MakeLocRsp(c, http.StatusInternalServerError, utility.FS_DB_ERR, rspData)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if appTempInfo.CustomData.SourceFile == nil {
|
||
|
appTempInfo.CustomData.SourceFile = make([]entity.CustomDataSourceFile, 0)
|
||
|
}
|
||
|
appTempInfo.Name = req.AppName
|
||
|
appTempInfo.Logo = req.AppLogo
|
||
|
sourceFileInfo := entity.CustomDataSourceFile{
|
||
|
FileMd5: req.FileMd5,
|
||
|
Name: req.Name,
|
||
|
SourceFileUrl: req.SourceFileUrl,
|
||
|
UploadDate: req.UploadDate,
|
||
|
Url: req.Url,
|
||
|
EncryptedUrl: req.EncryptedUrl,
|
||
|
EncryptedFileMd5: req.EncryptedFileMd5,
|
||
|
Packages: convertPackages(req.Packages),
|
||
|
EncryptPackages: convertPackages(req.EncryptPackages),
|
||
|
}
|
||
|
|
||
|
appTempInfo.CustomData.SourceFile = append(appTempInfo.CustomData.SourceFile, sourceFileInfo)
|
||
|
log.Infof("new temp info:%+v", appTempInfo)
|
||
|
err = repo.UpdateInfo(traceCtx, appTempInfo)
|
||
|
if err != nil {
|
||
|
log.Errorf("UpdateInfo err:%s", err.Error())
|
||
|
utility.MakeLocRsp(c, http.StatusInternalServerError, utility.FS_DB_ERR, rspData)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
utility.MakeLocRsp(c, http.StatusOK, utility.OK, rspData)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (s AppTempInfoService) GenTempApp(c *gin.Context, req proto.CreateTempAppInfoReq) {
|
||
|
traceCtx := apm.ApmClient().TraceContextFromGin(c)
|
||
|
var (
|
||
|
rspData = make(map[string]interface{})
|
||
|
appId = bson.NewObjectId().Hex()
|
||
|
)
|
||
|
|
||
|
if req.ProjectType < 0 || req.ProjectType > 2 {
|
||
|
utility.MakeLocRsp(c, http.StatusBadRequest, utility.FS_PARAM_ERR, nil)
|
||
|
return
|
||
|
}
|
||
|
log.Infof("GenTempApp appId:[%s]", appId)
|
||
|
appTempInfo := repository.NewDefaultInfo(appId, req.ProjectType)
|
||
|
|
||
|
repo := impl.InitAppTempInfoRepo()
|
||
|
err := repo.Insert(traceCtx, appTempInfo)
|
||
|
if err != nil {
|
||
|
log.Errorf("GenTempApp db err:%s", err.Error())
|
||
|
utility.MakeLocRsp(c, http.StatusInternalServerError, utility.FS_DB_ERR, rspData)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
nowMs := utils.GetNowMs()
|
||
|
qrcodeInfo := &entity.QrCodeInfo{
|
||
|
Type: entity.QrCodeTypeTemporary,
|
||
|
Uuid: GenTemporaryQrCodeUuid(appId, entity.AppTempDefSequence),
|
||
|
AppId: appId,
|
||
|
Sequence: appTempInfo.Sequence,
|
||
|
ApiServer: config.Cfg.ApiServer,
|
||
|
CodeId: "", //codeid 和 appid 相同
|
||
|
StartParams: entity.AppStartParams{},
|
||
|
ExpireTime: nowMs + int64(config.Cfg.QRcodeExpireTime*1e3),
|
||
|
CreateTime: nowMs,
|
||
|
}
|
||
|
//插入二维码信息
|
||
|
qrCodeRepo := impl.InitQrCodeInfoRepo()
|
||
|
err = qrCodeRepo.GenInfo(traceCtx, qrcodeInfo)
|
||
|
if err != nil {
|
||
|
log.Errorf("gen temp qrcode info err:%s", err.Error())
|
||
|
utility.MakeLocRsp(c, http.StatusInternalServerError, utility.FS_DB_ERR, rspData)
|
||
|
return
|
||
|
}
|
||
|
log.Infof("GenTempApp appId:[%s]", appId)
|
||
|
rspData["appId"] = appId
|
||
|
rspData["sequence"] = entity.AppTempDefSequence
|
||
|
log.Infof("GenTempApp rsp:%+v", rspData)
|
||
|
utility.MakeLocRsp(c, http.StatusOK, utility.OK, rspData)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func convertPackages(old []proto.Package) []entity.Package {
|
||
|
n := len(old)
|
||
|
if n == 0 {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
res := make([]entity.Package, 0, n)
|
||
|
|
||
|
for _, val := range old {
|
||
|
res = append(res, entity.Package{
|
||
|
Root: val.Root,
|
||
|
Name: val.Name,
|
||
|
Pages: val.Pages,
|
||
|
Independent: val.Independent,
|
||
|
Filename: val.Filename,
|
||
|
FileUrl: val.FileUrl,
|
||
|
FileMd5: val.FileMd5,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
return res
|
||
|
}
|