调整request的方法,增加类型转换的方法
This commit is contained in:
104
docs/factory.md
104
docs/factory.md
@@ -32,7 +32,7 @@
|
||||
| **金额计算** | `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)` |
|
||||
| **HTTP请求** | `ParseJSON()`, `ConvertInt()`, `GetTimezone()` 等 | `fac.ParseJSON(r, &req)` |
|
||||
|
||||
### 🔧 高级功能:Get方法(仅在必要时使用)
|
||||
|
||||
@@ -286,11 +286,15 @@ import "net/http"
|
||||
var req UserRequest
|
||||
fac.ParseJSON(r, &req)
|
||||
|
||||
// 获取查询参数
|
||||
id := fac.GetQueryInt64(r, "id", 0)
|
||||
uid := fac.GetQueryUint64(r, "uid", 0)
|
||||
userId := fac.GetQueryUint32(r, "user_id", 0)
|
||||
keyword := fac.GetQuery(r, "keyword", "")
|
||||
// 获取查询参数(使用类型转换方法)
|
||||
id := fac.ConvertInt64(r.URL.Query().Get("id"), 0)
|
||||
uid := fac.ConvertUint64(r.URL.Query().Get("uid"), 0)
|
||||
userId := fac.ConvertUint32(r.URL.Query().Get("user_id"), 0)
|
||||
keyword := r.URL.Query().Get("keyword") // 字符串直接获取
|
||||
|
||||
// 获取表单参数
|
||||
age := fac.ConvertInt(r.FormValue("age"), 0)
|
||||
isActive := fac.ConvertBool(r.FormValue("is_active"), false)
|
||||
|
||||
// 获取时区(需要配合middleware.Timezone使用)
|
||||
timezone := fac.GetTimezone(r)
|
||||
@@ -657,34 +661,74 @@ fac.Success(w, user, "获取成功") // 自定义消息
|
||||
- `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位整数参数。
|
||||
|
||||
#### GetQueryUint64(r *http.Request, key string, defaultValue uint64) uint64
|
||||
|
||||
获取查询64位无符号整数参数。
|
||||
|
||||
#### GetQueryUint32(r *http.Request, key string, defaultValue uint32) uint32
|
||||
|
||||
获取查询32位无符号整数参数。
|
||||
|
||||
#### GetFormValue(r *http.Request, key, defaultValue string) string
|
||||
|
||||
获取表单值。
|
||||
|
||||
#### GetTimezone(r *http.Request) string
|
||||
|
||||
从请求的context中获取时区(需要配合middleware.Timezone使用)。
|
||||
|
||||
### 类型转换方法(黑盒模式)
|
||||
|
||||
#### ConvertInt(value string, defaultValue int) int
|
||||
|
||||
将字符串转换为int类型。
|
||||
|
||||
**参数:**
|
||||
- `value`: 待转换的字符串
|
||||
- `defaultValue`: 转换失败或字符串为空时返回的默认值
|
||||
|
||||
**示例:**
|
||||
```go
|
||||
// 从查询参数获取整数
|
||||
id := fac.ConvertInt(r.URL.Query().Get("id"), 0)
|
||||
|
||||
// 从表单获取整数
|
||||
age := fac.ConvertInt(r.FormValue("age"), 0)
|
||||
```
|
||||
|
||||
#### ConvertInt64(value string, defaultValue int64) int64
|
||||
|
||||
将字符串转换为int64类型。
|
||||
|
||||
**示例:**
|
||||
```go
|
||||
id := fac.ConvertInt64(r.URL.Query().Get("id"), 0)
|
||||
```
|
||||
|
||||
#### ConvertUint64(value string, defaultValue uint64) uint64
|
||||
|
||||
将字符串转换为uint64类型。
|
||||
|
||||
**示例:**
|
||||
```go
|
||||
uid := fac.ConvertUint64(r.URL.Query().Get("uid"), 0)
|
||||
```
|
||||
|
||||
#### ConvertUint32(value string, defaultValue uint32) uint32
|
||||
|
||||
将字符串转换为uint32类型。
|
||||
|
||||
**示例:**
|
||||
```go
|
||||
userId := fac.ConvertUint32(r.URL.Query().Get("user_id"), 0)
|
||||
```
|
||||
|
||||
#### ConvertBool(value string, defaultValue bool) bool
|
||||
|
||||
将字符串转换为bool类型。
|
||||
|
||||
**示例:**
|
||||
```go
|
||||
isActive := fac.ConvertBool(r.URL.Query().Get("is_active"), false)
|
||||
```
|
||||
|
||||
#### ConvertFloat64(value string, defaultValue float64) float64
|
||||
|
||||
将字符串转换为float64类型。
|
||||
|
||||
**示例:**
|
||||
```go
|
||||
price := fac.ConvertFloat64(r.URL.Query().Get("price"), 0.0)
|
||||
```
|
||||
|
||||
### 日期时间工具方法(黑盒模式)
|
||||
|
||||
#### Now(timezone ...string) time.Time
|
||||
|
||||
Reference in New Issue
Block a user