39 lines
756 B
Go
39 lines
756 B
Go
package db
|
|
|
|
import (
|
|
"base-framework/pkg/config"
|
|
"base-framework/pkg/router"
|
|
"base-framework/pkg/utils/response"
|
|
"database/sql"
|
|
"fmt"
|
|
)
|
|
|
|
// Client 封装数据库连接对象
|
|
type Client struct {
|
|
Conn *sql.DB
|
|
}
|
|
|
|
// NewClient 根据 context 获取 orgId 并返回 Client 对象
|
|
func NewClient(c *router.Context) *Client {
|
|
val, ok := c.Get("orgId")
|
|
fmt.Println(val)
|
|
if !ok {
|
|
response.Error(c).Code(response.CodeInvalidOrgCode).Send()
|
|
return nil
|
|
}
|
|
|
|
orgId, ok := val.(string)
|
|
if !ok || orgId == "" {
|
|
response.Error(c).Code(response.CodeInvalidOrgCode).Send()
|
|
return nil
|
|
}
|
|
|
|
conn, ok := config.GetDB(orgId)
|
|
if !ok || conn == nil {
|
|
response.Error(c).Code(response.CodeInvalidOrgCode).Send()
|
|
return nil
|
|
}
|
|
|
|
return &Client{Conn: conn}
|
|
}
|