优化多域的场景

This commit is contained in:
Yavolte 2025-07-23 16:19:37 +08:00
parent 3b130c9d14
commit b9c8112a7c
2 changed files with 10 additions and 9 deletions

View File

@ -387,7 +387,7 @@ func (m *Model) Search(w http.ResponseWriter, r *http.Request) {
childCtx := context.WithValue(r.Context(), RuntimeScopeKey, &types.RuntimeScope{ childCtx := context.WithValue(r.Context(), RuntimeScopeKey, &types.RuntimeScope{
Domain: domainName, Domain: domainName,
Request: r, Request: r,
User: m.valueLookup("user", w, r), User: m.valueLookup(types.FieldUser, w, r),
ModuleName: m.naming.ModuleName, ModuleName: m.naming.ModuleName,
TableName: m.naming.TableName, TableName: m.naming.TableName,
Scenario: types.ScenarioList, Scenario: types.ScenarioList,
@ -490,7 +490,7 @@ func (m *Model) Create(w http.ResponseWriter, r *http.Request) {
diffAttrs = make([]*types.DiffAttr, 0, 10) diffAttrs = make([]*types.DiffAttr, 0, 10)
childCtx := context.WithValue(r.Context(), RuntimeScopeKey, &types.RuntimeScope{ childCtx := context.WithValue(r.Context(), RuntimeScopeKey, &types.RuntimeScope{
Domain: domainName, Domain: domainName,
User: m.valueLookup("user", w, r), User: m.valueLookup(types.FieldUser, w, r),
Request: r, Request: r,
ModuleName: m.naming.ModuleName, ModuleName: m.naming.ModuleName,
TableName: m.naming.TableName, TableName: m.naming.TableName,
@ -588,7 +588,7 @@ func (m *Model) Update(w http.ResponseWriter, r *http.Request) {
childCtx := context.WithValue(r.Context(), RuntimeScopeKey, &types.RuntimeScope{ childCtx := context.WithValue(r.Context(), RuntimeScopeKey, &types.RuntimeScope{
Domain: domainName, Domain: domainName,
Request: r, Request: r,
User: m.valueLookup("user", w, r), User: m.valueLookup(types.FieldUser, w, r),
ModuleName: m.naming.ModuleName, ModuleName: m.naming.ModuleName,
TableName: m.naming.TableName, TableName: m.naming.TableName,
Scenario: types.ScenarioUpdate, Scenario: types.ScenarioUpdate,
@ -671,7 +671,7 @@ func (m *Model) Delete(w http.ResponseWriter, r *http.Request) {
} }
childCtx := context.WithValue(r.Context(), RuntimeScopeKey, &types.RuntimeScope{ childCtx := context.WithValue(r.Context(), RuntimeScopeKey, &types.RuntimeScope{
Domain: m.valueLookup(types.FieldDomain, w, r), Domain: m.valueLookup(types.FieldDomain, w, r),
User: m.valueLookup("user", w, r), User: m.valueLookup(types.FieldUser, w, r),
Request: r, Request: r,
ModuleName: m.naming.ModuleName, ModuleName: m.naming.ModuleName,
TableName: m.naming.TableName, TableName: m.naming.TableName,
@ -790,7 +790,7 @@ func (m *Model) Export(w http.ResponseWriter, r *http.Request) {
childCtx := context.WithValue(r.Context(), RuntimeScopeKey, &types.RuntimeScope{ childCtx := context.WithValue(r.Context(), RuntimeScopeKey, &types.RuntimeScope{
Domain: domainName, Domain: domainName,
Request: r, Request: r,
User: m.valueLookup("user", w, r), User: m.valueLookup(types.FieldUser, w, r),
ModuleName: m.naming.ModuleName, ModuleName: m.naming.ModuleName,
TableName: m.naming.TableName, TableName: m.naming.TableName,
Scenario: types.ScenarioExport, Scenario: types.ScenarioExport,
@ -988,7 +988,7 @@ __end:
if result.TotalCount > result.SuccessCount { if result.TotalCount > result.SuccessCount {
result.FailureFile = failureFile result.FailureFile = failureFile
} }
result.Duration = time.Now().Sub(tm) result.Duration = time.Since(tm)
m.getHook().afterImport(ctx, result) m.getHook().afterImport(ctx, result)
} }
@ -1022,7 +1022,7 @@ func (m *Model) Import(w http.ResponseWriter, r *http.Request) {
//这里用background的context //这里用background的context
childCtx := context.WithValue(context.Background(), RuntimeScopeKey, &types.RuntimeScope{ childCtx := context.WithValue(context.Background(), RuntimeScopeKey, &types.RuntimeScope{
Domain: domainName, Domain: domainName,
User: m.valueLookup("user", w, r), User: m.valueLookup(types.FieldUser, w, r),
ModuleName: m.naming.ModuleName, ModuleName: m.naming.ModuleName,
TableName: m.naming.TableName, TableName: m.naming.TableName,
Scenario: types.ScenarioImport, Scenario: types.ScenarioImport,
@ -1075,7 +1075,7 @@ func (m *Model) Import(w http.ResponseWriter, r *http.Request) {
fast, _ = strconv.ParseBool(qs.Get("__fast")) fast, _ = strconv.ParseBool(qs.Get("__fast"))
go m.importInternal(childCtx, domainName, schemas, filename, fast, extraFields) go m.importInternal(childCtx, domainName, schemas, filename, fast, extraFields)
m.response.Success(w, types.ImportResponse{ m.response.Success(w, types.ImportResponse{
UID: m.valueLookup("user", w, r), UID: m.valueLookup(types.FieldUser, w, r),
Status: "committed", Status: "committed",
}) })
} }
@ -1096,7 +1096,7 @@ func newModel(v any, db *gorm.DB, naming types.Naming) *Model {
Clauses: map[string]clause.Clause{}, Clauses: map[string]clause.Clause{},
} }
if err := model.statement.Parse(v); err == nil { if err := model.statement.Parse(v); err == nil {
if model.statement.Schema.PrimaryFieldDBNames != nil && len(model.statement.Schema.PrimaryFieldDBNames) > 0 { if model.statement != nil && model.statement.Schema != nil && len(model.statement.Schema.PrimaryFieldDBNames) > 0 {
model.primaryKey = model.statement.Schema.PrimaryFieldDBNames[0] model.primaryKey = model.statement.Schema.PrimaryFieldDBNames[0]
} }
} }

View File

@ -93,6 +93,7 @@ const (
) )
const ( const (
FieldUser = "user"
FieldDomain = "domain" FieldDomain = "domain"
) )