package api import ( "assetcenter/conf" "assetcenter/db/model" "assetcenter/db/repo" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" ) const ( CategoyScopeGlobal = "global" CategoyScopeQiye = "qiye" ) func AssetProfile(c *gin.Context, apictx *ApiSession) (interface{}, error) { var floatv float32 = 1 profile := map[string]interface{}{ "name": apictx.Svc.Conf.Name, "version": apictx.Svc.Conf.Version, "saveType": apictx.Svc.Conf.SaveType, "assets": apictx.Svc.Conf.Assets, "category": apictx.Svc.Conf.Category, "assetConf": map[string]interface{}{ "decorate": &model.AssetDecorateMesh{ Components: []*model.MeshMatConf{ &model.MeshMatConf{ Name: "xx", Index: 1, Material: &model.MatConfig{}, }, }, }, "texture": &model.AssetTexture{}, "env3d": &model.Env3d{}, "material": model.AssetMaterial{ Config: &model.MatConfig{ Uvtransform: &model.MaterailUv{}, CullFace: "", Version: 1, Type: "default", Albedo: &model.MatTextureColor{Color: &model.Vect3{}, Texture: &model.OssType{}, UseTexture: BoolValue(true)}, Roughness: &model.MatTextureFactor{Texture: &model.OssType{}, Factor: &floatv}, Normal: &model.MatNormal{Texture: &model.OssType{}, Factor: &floatv}, Metalness: &model.MatTextureFactor{Texture: &model.OssType{}, Factor: &floatv}, Opacity: &model.MatTextureFactorWithEnable{Enable: *BoolValue(false), Factor: &floatv, Texture: &model.OssType{}}, Specular: &model.MatTextureColor{Color: &model.Vect3{}, Texture: &model.OssType{}, UseTexture: BoolValue(true)}, }, }, "boxtpl": &model.Boxtpl{Components: []*model.BoxtplComponent{ &model.BoxtplComponent{ UV: &model.BoxtplCompUv{}, }, }}, }, } return profile, nil } func UploadAsset(c *gin.Context, apictx *ApiSession, asset *conf.AppAsset) (interface{}, error) { body := repo.RepoAssetCreateDefaultValue(asset) if body == nil { return nil, NewError("不支持的上传类型") } c.ShouldBindJSON(body.GetEntity()) return body.Upload(apictx.CreateRepoCtx(), apictx.User.ID) } func UpdateAsset(c *gin.Context, apictx *ApiSession, asset *conf.AppAsset) (interface{}, error) { body := repo.RepoAssetCreateDefaultValue(asset) if body == nil { return nil, NewError("不支持的资产类型") } c.ShouldBindJSON(body.GetEntity()) return body.Update(apictx.CreateRepoCtx()) } func DetailAsset(c *gin.Context, apictx *ApiSession, asset *conf.AppAsset) (interface{}, error) { id := c.Param("id") body := repo.RepoAssetCreateDefaultValue(asset) if body == nil { return nil, NewError("不支持的资产类型") } c.ShouldBindJSON(body.GetEntity()) return body.Detail(apictx.CreateRepoCtx(), id) } func RemoveAsset(c *gin.Context, apictx *ApiSession, asset *conf.AppAsset) (interface{}, error) { body := &struct { Id string }{} c.ShouldBindJSON(body) collectionName := "asset-" + asset.Type if len(body.Id) < 1 { return nil, NewError("id不能为空!") } return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), collectionName, body.Id) } func UpdateAssetState(c *gin.Context, apictx *ApiSession, asset *conf.AppAsset) (interface{}, error) { body := &struct { Id string State int }{} c.ShouldBindJSON(body) collectionName := "asset-" + asset.Type if len(body.Id) < 1 { return nil, NewError("id不能为空!") } return repo.RepoUpdateSetDocProps(apictx.CreateRepoCtx(), collectionName, body.Id, bson.M{"$set": bson.M{"state": body.State}}) } func UserAssetList(c *gin.Context, apictx *ApiSession, asset *conf.AppAsset) (interface{}, error) { page, size, query, fields := UtilQueryPageSize2(c) uid, _ := primitive.ObjectIDFromHex(apictx.User.ID) query["userId"] = uid collectionName := "asset-" + asset.Type // ParseCategories(query, apictx) project := []string{"name", "thumbnail", "createTime", "state", "categories", "meshState", "images", "file", "osgjs"} if len(fields) > 0 { project = fields } return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{ CollectName: collectionName, Page: page, Size: size, Query: query, Project: project, Sort: bson.M{"createTime": -1}, }) } func CreateAssetRouter(router *GinRouter) { assets := conf.AppConfig.Assets if len(assets) < 1 { return } for _, asset := range assets { targetAsset := &conf.AppAsset{Type: asset.Type, Name: asset.Name, IsMesh: asset.IsMesh, IsImage: asset.IsImage, DefaultCategory: asset.DefaultCategory} router.POSTJWT("/upload/"+asset.Type, func(c *gin.Context, apictx *ApiSession) (interface{}, error) { return UploadAsset(c, apictx, targetAsset) }) router.POSTJWT("/update/"+asset.Type, func(c *gin.Context, apictx *ApiSession) (interface{}, error) { return UpdateAsset(c, apictx, targetAsset) }) router.POSTJWT(asset.Type+"/state", func(c *gin.Context, apictx *ApiSession) (interface{}, error) { return UpdateAssetState(c, apictx, targetAsset) }) router.GET(asset.Type+"/detail/:id", func(c *gin.Context, apictx *ApiSession) (interface{}, error) { return DetailAsset(c, apictx, targetAsset) }) router.POSTJWT("/delete/"+asset.Type, func(c *gin.Context, apictx *ApiSession) (interface{}, error) { return RemoveAsset(c, apictx, targetAsset) }) router.GETJWT("/user/"+asset.Type+"/list", func(c *gin.Context, apictx *ApiSession) (interface{}, error) { return UserAssetList(c, apictx, targetAsset) }) router.GETJWT("/team/"+asset.Type+"/list", func(c *gin.Context, apictx *ApiSession) (interface{}, error) { return UserAssetList(c, apictx, targetAsset) }) router.GET("/platform/"+asset.Type+"/list", func(c *gin.Context, apictx *ApiSession) (interface{}, error) { return UserAssetList(c, apictx, targetAsset) }) } }