13 changed files with 445 additions and 23 deletions
-
9api/v1/couponusers/couponUsers.go
-
2go.mod
-
1go.sum
-
10internal/cmd/cmd.go
-
8internal/consts/consts.go
-
122internal/controller/couponusers/couponUsers.go
-
2internal/dao/internal/coupon_users.go
-
168internal/logic/couponusers/couponUsers.go
-
1internal/model/do/coupon_users.go
-
9internal/model/dto/Result.go
-
1internal/model/entity/coupon_users.go
-
6internal/service/couponusers.go
-
129utility/utility.go
@ -1 +1,9 @@ |
|||||
package consts |
package consts |
||||
|
|
||||
|
const ( |
||||
|
URL_KEY = "jingwang:cms:env" //Redis中的键
|
||||
|
//内容: http://39.101.133.168:8828/hljw
|
||||
|
//测试环境解析token接口: http://39.101.133.168:8828/hljw/api/v2/member/info
|
||||
|
//正式环境解析token接口: http://api.homilychart.com:8828/hljw/api/v2/member/info
|
||||
|
URL_HASH_KEY = "HLJW_BASE_URL" |
||||
|
) |
@ -0,0 +1,129 @@ |
|||||
|
package utility |
||||
|
|
||||
|
import ( |
||||
|
"CouponBackendGo/internal/consts" |
||||
|
"CouponBackendGo/internal/model/dto" |
||||
|
"encoding/json" |
||||
|
"errors" |
||||
|
"fmt" |
||||
|
"github.com/gogf/gf/v2/frame/g" |
||||
|
"github.com/gogf/gf/v2/os/gctx" |
||||
|
"io/ioutil" |
||||
|
"net/http" |
||||
|
"strconv" |
||||
|
"strings" |
||||
|
) |
||||
|
|
||||
|
// 查询数据库中的URL
|
||||
|
func selectBaseUrl(hashKey string) *dto.Result { |
||||
|
|
||||
|
//查询数据库
|
||||
|
//env表中存储着一些所需要的地址,值等,可能是需要查出来存放到redis中(value字段中是值)
|
||||
|
//key为"HLJW_URL"时,value为:http://39.101.133.168:8828/hljw,为测试环境时前置地址
|
||||
|
value, err := g.DB().Model("env").Where("`key` = ?", hashKey).Value("value") |
||||
|
if err != nil { |
||||
|
return dto.Error("数据库env查询失败") |
||||
|
} |
||||
|
if value.IsNil() || value.String() == "" { |
||||
|
return dto.Error("未找到对应数据") |
||||
|
} |
||||
|
return dto.SuccessWithData(value.String()) |
||||
|
} |
||||
|
|
||||
|
// 获取URL
|
||||
|
func getUrl(key, hashKey string) *dto.Result { |
||||
|
ctx := gctx.New() |
||||
|
|
||||
|
//测试环境: http://39.101.133.168:8828/hljw/api/v2/member/info
|
||||
|
//正式环境: http://api.homilychart.com:8828/hljw/api/v2/member/info
|
||||
|
|
||||
|
//1. 从Redis获取URL
|
||||
|
redisUrl, err := g.Redis().Do(ctx, "HGET", key, hashKey) |
||||
|
if err == nil && redisUrl.String() != "" { |
||||
|
return dto.SuccessWithMsgAndData(fmt.Sprintf("从Redis获取URL: %s", redisUrl.String()), redisUrl.String()) |
||||
|
} |
||||
|
|
||||
|
//2. 如果Redis中没有, 查询数据库
|
||||
|
urlResult := selectBaseUrl(hashKey) |
||||
|
if urlResult.Code != 200 { |
||||
|
return urlResult |
||||
|
} |
||||
|
url := urlResult.Data.(string) |
||||
|
|
||||
|
//3. 将URL存入Redis
|
||||
|
_, err = g.Redis().Do(ctx, "HSET", key, hashKey, url) |
||||
|
if err != nil { |
||||
|
return dto.Error("将数据存入Redis失败") |
||||
|
} |
||||
|
|
||||
|
return dto.SuccessWithData(url) |
||||
|
} |
||||
|
|
||||
|
// 获取jwcode
|
||||
|
func GetJwcodeJSON(token string) (int, error) { |
||||
|
|
||||
|
//获取基础URL 用于解析token的接口地址
|
||||
|
urlResult := getUrl(consts.URL_KEY, consts.URL_HASH_KEY) |
||||
|
if urlResult.Code != 200 { |
||||
|
return 0, errors.New(fmt.Sprintf("获取基础URL失败: %s", urlResult.Message)) |
||||
|
} |
||||
|
baseUrl := urlResult.Data.(string) |
||||
|
|
||||
|
//拼接完整的URL, 用于发送请求并解析token
|
||||
|
url := baseUrl + "/api/v2/member/info" |
||||
|
requestBody := strings.NewReader(`{"token":"` + token + `"}`) |
||||
|
|
||||
|
//创建HTTP请求(POST)
|
||||
|
req, err := http.NewRequest("POST", url, requestBody) |
||||
|
if err != nil { |
||||
|
return 0, fmt.Errorf("创建HTTP请求失败: %w", err) |
||||
|
} |
||||
|
//设置请求头
|
||||
|
req.Header.Set("Content-Type", "application/json") |
||||
|
|
||||
|
//发送请求
|
||||
|
client := &http.Client{} |
||||
|
resp, err := client.Do(req) |
||||
|
if err != nil { |
||||
|
return 0, fmt.Errorf("发送HTTP请求失败: %w", err) //
|
||||
|
} |
||||
|
//延时关闭响应体
|
||||
|
defer resp.Body.Close() |
||||
|
|
||||
|
//检查HTTP状态码
|
||||
|
if resp.StatusCode != http.StatusOK { |
||||
|
return 0, fmt.Errorf("HTTP请求失败, 状态码: %d", resp.StatusCode) |
||||
|
} |
||||
|
|
||||
|
//读取并解析响应体
|
||||
|
body, err := ioutil.ReadAll(resp.Body) |
||||
|
if err != nil { |
||||
|
return 0, fmt.Errorf("读取响应体失败: %w", err) |
||||
|
} |
||||
|
|
||||
|
//解析JSON数据
|
||||
|
var jsonResponse map[string]interface{} |
||||
|
err = json.Unmarshal(body, &jsonResponse) |
||||
|
if err != nil { |
||||
|
return 0, fmt.Errorf("解析JSON数据失败: %w", err) |
||||
|
} |
||||
|
|
||||
|
//提取data节点
|
||||
|
data, ok := jsonResponse["data"].(map[string]interface{}) |
||||
|
if !ok { |
||||
|
return 0, errors.New("响应体中没有有效的data节点") |
||||
|
} |
||||
|
|
||||
|
//提取jwcode字段节点并转换为整数
|
||||
|
jwcode, ok := data["jwcode"].(string) //首先尝试解析为字符串
|
||||
|
if ok { |
||||
|
jwcodeInt, err := strconv.Atoi(jwcode) //将字符串转换为整数
|
||||
|
if err != nil { |
||||
|
return 0, fmt.Errorf("解析jwcode字段为整数失败: %w", err) |
||||
|
} |
||||
|
return jwcodeInt, nil |
||||
|
} |
||||
|
|
||||
|
//如果jwcode不是字符串,返回错误日志
|
||||
|
return 0, errors.New("响应体中没有有效的jwcode字段") |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue