调整项目结构,factory只负责暴露方法,不实现业务细节

This commit is contained in:
2025-12-07 00:04:01 +08:00
parent b66f345281
commit 339920a940
23 changed files with 2165 additions and 1231 deletions

View File

@@ -26,6 +26,11 @@
| **邮件** | `SendEmail()` | `fac.SendEmail(to, subject, body)` |
| **短信** | `SendSMS()` | `fac.SendSMS(phones, params)` |
| **存储** | `UploadFile()`, `GetFileURL()` | `fac.UploadFile(ctx, key, file)` |
| **日期时间** | `Now()`, `ParseDateTime()`, `FormatDateTime()` 等 | `fac.Now("Asia/Shanghai")` |
| **金额计算** | `YuanToCents()`, `CentsToYuan()`, `FormatYuan()` | `fac.YuanToCents(100.5)` |
| **版本信息** | `GetVersion()` | `fac.GetVersion()` |
| **HTTP响应** | `Success()`, `Error()`, `SuccessPage()` | `fac.Success(w, data)` |
| **HTTP请求** | `ParseJSON()`, `GetQuery()`, `GetTimezone()` 等 | `fac.ParseJSON(r, &req)` |
### 🔧 高级功能Get方法仅在必要时使用
@@ -176,7 +181,83 @@ db.Find(&users)
db.Create(&user)
```
### 8. Redis操作获取客户端对象
### 8. 日期时间操作(黑盒模式
```go
// 获取当前时间
now := fac.Now("Asia/Shanghai")
// 解析时间
t, _ := fac.ParseDateTime("2024-01-01 12:00:00", "Asia/Shanghai")
// 格式化时间
str := fac.FormatDateTime(now)
// 时间计算
tomorrow := fac.AddDays(now, 1)
startOfDay := fac.StartOfDay(now, "Asia/Shanghai")
endOfDay := fac.EndOfDay(now, "Asia/Shanghai")
// Unix时间戳
unix := fac.ToUnix(now)
t2 := fac.FromUnix(unix, "Asia/Shanghai")
```
### 9. 金额计算(黑盒模式)
```go
// 元转分
cents := fac.YuanToCents(100.5) // 10050
// 分转元
yuan := fac.CentsToYuan(10050) // 100.5
// 格式化显示
str := fac.FormatYuan(10050) // "100.50"
```
### 10. 版本信息(黑盒模式)
```go
version := fac.GetVersion()
fmt.Println("当前版本:", version)
```
### 11. HTTP响应黑盒模式
```go
import "net/http"
// 成功响应
fac.Success(w, data)
fac.Success(w, data, "操作成功")
// 分页响应
fac.SuccessPage(w, users, total, page, pageSize)
// 错误响应
fac.Error(w, 1001, "用户不存在")
fac.SystemError(w, "系统错误")
```
### 12. HTTP请求解析黑盒模式
```go
import "net/http"
// 解析JSON
var req UserRequest
fac.ParseJSON(r, &req)
// 获取查询参数
id := fac.GetQueryInt64(r, "id", 0)
keyword := fac.GetQuery(r, "keyword", "")
// 获取时区需要配合middleware.Timezone使用
timezone := fac.GetTimezone(r)
```
### 13. Redis操作获取客户端对象
```go
import (
@@ -481,6 +562,222 @@ func main() {
**返回:** 配置对象
### HTTP响应方法黑盒模式
#### Success(w http.ResponseWriter, data interface{}, message ...string)
发送成功响应。
**参数:**
- `w`: HTTP响应写入器
- `data`: 响应数据
- `message`: 可选,成功消息(如果不提供,使用默认消息)
**示例:**
```go
fac.Success(w, user) // 使用默认消息
fac.Success(w, user, "获取成功") // 自定义消息
```
#### Error(w http.ResponseWriter, code int, message string)
发送错误响应。
**参数:**
- `w`: HTTP响应写入器
- `code`: 业务错误码
- `message`: 错误消息
#### SystemError(w http.ResponseWriter, message string)
发送系统错误响应HTTP 500
**参数:**
- `w`: HTTP响应写入器
- `message`: 错误消息
#### SuccessPage(w http.ResponseWriter, data interface{}, total int64, page, pageSize int, message ...string)
发送分页成功响应。
**参数:**
- `w`: HTTP响应写入器
- `data`: 响应数据列表
- `total`: 总记录数
- `page`: 当前页码
- `pageSize`: 每页大小
- `message`: 可选,成功消息
### HTTP请求方法黑盒模式
#### ParseJSON(r *http.Request, v interface{}) error
解析JSON请求体。
**参数:**
- `r`: HTTP请求
- `v`: 目标结构体指针
#### GetQuery(r *http.Request, key, defaultValue string) string
获取查询参数。
#### GetQueryInt(r *http.Request, key string, defaultValue int) int
获取查询整数参数。
#### GetQueryInt64(r *http.Request, key string, defaultValue int64) int64
获取查询64位整数参数。
#### GetFormValue(r *http.Request, key, defaultValue string) string
获取表单值。
#### GetTimezone(r *http.Request) string
从请求的context中获取时区需要配合middleware.Timezone使用
### 日期时间工具方法(黑盒模式)
#### Now(timezone ...string) time.Time
获取当前时间。
**参数:**
- `timezone`: 可选,时区字符串(如 "Asia/Shanghai"),不指定则使用默认时区
#### ParseDateTime(value string, timezone ...string) (time.Time, error)
解析日期时间字符串格式2006-01-02 15:04:05
#### ParseDate(value string, timezone ...string) (time.Time, error)
解析日期字符串格式2006-01-02
#### FormatDateTime(t time.Time, timezone ...string) string
格式化日期时间格式2006-01-02 15:04:05
#### FormatDate(t time.Time, timezone ...string) string
格式化日期格式2006-01-02
#### FormatTime(t time.Time, timezone ...string) string
格式化时间格式15:04:05
#### ToUnix(t time.Time) int64
转换为Unix时间戳
#### FromUnix(sec int64, timezone ...string) time.Time
从Unix时间戳创建时间。
#### ToUnixMilli(t time.Time) int64
转换为Unix毫秒时间戳。
#### FromUnixMilli(msec int64, timezone ...string) time.Time
从Unix毫秒时间戳创建时间。
#### AddDays(t time.Time, days int) time.Time
添加天数。
#### AddMonths(t time.Time, months int) time.Time
添加月数。
#### AddYears(t time.Time, years int) time.Time
添加年数。
#### StartOfDay(t time.Time, timezone ...string) time.Time
获取一天的开始时间00:00:00
#### EndOfDay(t time.Time, timezone ...string) time.Time
获取一天的结束时间23:59:59.999999999)。
#### StartOfMonth(t time.Time, timezone ...string) time.Time
获取月份的开始时间。
#### EndOfMonth(t time.Time, timezone ...string) time.Time
获取月份的结束时间。
#### StartOfYear(t time.Time, timezone ...string) time.Time
获取年份的开始时间。
#### EndOfYear(t time.Time, timezone ...string) time.Time
获取年份的结束时间。
#### DiffDays(t1, t2 time.Time) int
计算两个时间之间的天数差。
#### DiffHours(t1, t2 time.Time) int64
计算两个时间之间的小时差。
#### DiffMinutes(t1, t2 time.Time) int64
计算两个时间之间的分钟差。
#### DiffSeconds(t1, t2 time.Time) int64
计算两个时间之间的秒数差。
### 金额工具方法(黑盒模式)
#### GetMoneyCalculator() *tools.MoneyCalculator
获取金额计算器实例。
#### YuanToCents(yuan float64) int64
元转分。
**示例:**
```go
cents := fac.YuanToCents(100.5) // 返回 10050
```
#### CentsToYuan(cents int64) float64
分转元。
**示例:**
```go
yuan := fac.CentsToYuan(10050) // 返回 100.5
```
#### FormatYuan(cents int64) string
格式化显示金额分转元保留2位小数
**示例:**
```go
str := fac.FormatYuan(10050) // 返回 "100.50"
```
### 版本工具方法(黑盒模式)
#### GetVersion() string
获取版本号。
**说明:**
- 优先从环境变量 `DOCKER_TAG``VERSION` 中读取
- 如果没有设置环境变量,则使用默认版本号
## 设计优势
### 优势总结