From 95c2c2e0c15162d8fe48dbc25c7c42d05c76b62c Mon Sep 17 00:00:00 2001 From: huang <1724659546@qq.com> Date: Tue, 30 Sep 2025 22:32:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/infra/models/user.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/internal/infra/models/user.go b/internal/infra/models/user.go index 9d94286..6212136 100644 --- a/internal/infra/models/user.go +++ b/internal/infra/models/user.go @@ -1,7 +1,10 @@ -// Package models 定义了应用的数据模型,例如用户、产品等。 package models import ( + "database/sql/driver" + "encoding/json" + "errors" + "golang.org/x/crypto/bcrypt" "gorm.io/gorm" ) @@ -15,6 +18,24 @@ type ContactInfo struct { Feishu string `json:"feishu,omitempty"` } +// Scan 实现 sql.Scanner 接口,用于从数据库读取 JSONB 数据 +func (ci *ContactInfo) Scan(value interface{}) error { + if value == nil { + *ci = ContactInfo{} // 如果数据库值为 NULL,则初始化为空结构体 + return nil + } + bytes, ok := value.([]byte) + if !ok { + return errors.New("type assertion to []byte failed for ContactInfo") + } + return json.Unmarshal(bytes, ci) +} + +// Value 实现 driver.Valuer 接口,用于将 ContactInfo 写入数据库为 JSONB 数据 +func (ci ContactInfo) Value() (driver.Value, error) { + return json.Marshal(ci) +} + // User 代表系统中的用户模型 type User struct { // gorm.Model 内嵌了 ID, CreatedAt, UpdatedAt, 和 DeletedAt