feat: message notify

This commit is contained in:
meilin.huang
2025-04-15 21:42:31 +08:00
parent 3c0292b56e
commit 1b40d345eb
104 changed files with 2681 additions and 288 deletions

View File

@@ -4,7 +4,7 @@ import "fmt"
const (
AppName = "mayfly-go"
Version = "v1.9.3"
Version = "v1.9.4"
)
func GetAppInfo() string {

View File

@@ -1,4 +1,4 @@
package httpclient
package httpx
import (
"bytes"
@@ -20,7 +20,7 @@ import (
// 默认超时
const DefTimeout = 60
type RequestWrapper struct {
type Req struct {
client http.Client
url string
method string
@@ -37,16 +37,16 @@ type MultipartFile struct {
}
// 创建一个请求
func NewRequest(url string) *RequestWrapper {
return &RequestWrapper{url: url, client: http.Client{}}
func NewReq(url string) *Req {
return &Req{url: url, client: http.Client{}}
}
func (r *RequestWrapper) Url(url string) *RequestWrapper {
func (r *Req) Url(url string) *Req {
r.url = url
return r
}
func (r *RequestWrapper) Header(name, value string) *RequestWrapper {
func (r *Req) Header(name, value string) *Req {
if r.header == nil {
r.header = make(map[string]string)
}
@@ -54,12 +54,12 @@ func (r *RequestWrapper) Header(name, value string) *RequestWrapper {
return r
}
func (r *RequestWrapper) Timeout(timeout int) *RequestWrapper {
func (r *Req) Timeout(timeout int) *Req {
r.timeout = timeout
return r
}
func (r *RequestWrapper) GetByQuery(queryMap collx.M) *ResponseWrapper {
func (r *Req) GetByQuery(queryMap collx.M) *Resp {
var params string
for k, v := range queryMap {
if params != "" {
@@ -71,13 +71,13 @@ func (r *RequestWrapper) GetByQuery(queryMap collx.M) *ResponseWrapper {
return r.Get()
}
func (r *RequestWrapper) Get() *ResponseWrapper {
func (r *Req) Get() *Resp {
r.method = "GET"
r.body = nil
return sendRequest(r)
}
func (r *RequestWrapper) PostJson(body string) *ResponseWrapper {
func (r *Req) PostJson(body string) *Resp {
buf := bytes.NewBufferString(body)
r.method = "POST"
r.body = buf
@@ -88,15 +88,15 @@ func (r *RequestWrapper) PostJson(body string) *ResponseWrapper {
return sendRequest(r)
}
func (r *RequestWrapper) PostObj(body any) *ResponseWrapper {
func (r *Req) PostObj(body any) *Resp {
marshal, err := json.Marshal(body)
if err != nil {
return &ResponseWrapper{err: errors.New("解析json obj错误")}
return &Resp{err: errors.New("解析json obj错误")}
}
return r.PostJson(string(marshal))
}
func (r *RequestWrapper) PostForm(params string) *ResponseWrapper {
func (r *Req) PostForm(params string) *Resp {
buf := bytes.NewBufferString(params)
r.method = "POST"
r.body = buf
@@ -107,7 +107,7 @@ func (r *RequestWrapper) PostForm(params string) *ResponseWrapper {
return sendRequest(r)
}
func (r *RequestWrapper) PostMulipart(files []MultipartFile, reqParams collx.M) *ResponseWrapper {
func (r *Req) PostMulipart(files []MultipartFile, reqParams collx.M) *Resp {
buf := &bytes.Buffer{}
// 文件写入 buf
writer := multipart.NewWriter(buf)
@@ -117,7 +117,7 @@ func (r *RequestWrapper) PostMulipart(files []MultipartFile, reqParams collx.M)
if uploadFile.FilePath != "" {
file, err := os.Open(uploadFile.FilePath)
if err != nil {
return &ResponseWrapper{err: err}
return &Resp{err: err}
}
defer file.Close()
reader = file
@@ -127,18 +127,18 @@ func (r *RequestWrapper) PostMulipart(files []MultipartFile, reqParams collx.M)
part, err := writer.CreateFormFile(uploadFile.FieldName, uploadFile.FileName)
if err != nil {
return &ResponseWrapper{err: err}
return &Resp{err: err}
}
io.Copy(part, reader)
}
// 如果有其他参数则写入body
for k, v := range reqParams {
if err := writer.WriteField(k, cast.ToString(v)); err != nil {
return &ResponseWrapper{err: err}
return &Resp{err: err}
}
}
if err := writer.Close(); err != nil {
return &ResponseWrapper{err: err}
return &Resp{err: err}
}
r.method = "POST"
@@ -150,8 +150,8 @@ func (r *RequestWrapper) PostMulipart(files []MultipartFile, reqParams collx.M)
return sendRequest(r)
}
func sendRequest(rw *RequestWrapper) *ResponseWrapper {
respWrapper := &ResponseWrapper{}
func sendRequest(rw *Req) *Resp {
respWrapper := &Resp{}
timeout := rw.timeout
if timeout > 0 {
rw.client.Timeout = time.Duration(timeout) * time.Second
@@ -166,37 +166,37 @@ func sendRequest(rw *RequestWrapper) *ResponseWrapper {
}
setRequestHeader(req, rw.header)
resp, err := rw.client.Do(req)
return &ResponseWrapper{resp: resp, err: err}
return &Resp{resp: resp, err: err}
}
func setRequestHeader(req *http.Request, header map[string]string) {
req.Header.Set("User-Agent", "golang/mayfly")
req.Header.Set("User-Agent", "golang/mayfly-go")
for k, v := range header {
req.Header.Set(k, v)
}
}
type ResponseWrapper struct {
type Resp struct {
resp *http.Response
err error
}
// 将响应体通过json解析转为指定结构体
func (r *ResponseWrapper) BodyToObj(objPtr any) error {
// BodyTo 将响应体通过json解析转为指定结构体
func (r *Resp) BodyTo(ptr any) error {
bodyBytes, err := r.BodyBytes()
if err != nil {
return err
}
err = json.Unmarshal(bodyBytes, &objPtr)
err = json.Unmarshal(bodyBytes, &ptr)
if err != nil {
return fmt.Errorf("解析响应体-json解析失败-%s", err.Error())
}
return nil
}
// 将响应体转为strings
func (r *ResponseWrapper) BodyToString() (string, error) {
// BodyToString 将响应体转为strings
func (r *Resp) BodyToString() (string, error) {
bodyBytes, err := r.BodyBytes()
if err != nil {
return "", err
@@ -204,21 +204,20 @@ func (r *ResponseWrapper) BodyToString() (string, error) {
return string(bodyBytes), nil
}
// 将响应体通过json解析转为map
func (r *ResponseWrapper) BodyToMap() (map[string]any, error) {
// BodyToMap 将响应体通过json解析转为map
func (r *Resp) BodyToMap() (map[string]any, error) {
var res map[string]any
return res, r.BodyToObj(&res)
return res, r.BodyTo(&res)
}
// 获取响应体的字节数组
func (r *ResponseWrapper) BodyBytes() ([]byte, error) {
resp, err := r.GetHttpResp()
// BodyBytes 获取响应体的字节数组
func (r *Resp) BodyBytes() ([]byte, error) {
bodyReader, err := r.BodyReader()
if err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
defer bodyReader.Close()
body, err := io.ReadAll(bodyReader)
if err != nil {
return nil, fmt.Errorf("读取响应体数据失败-%s", err.Error())
@@ -226,8 +225,18 @@ func (r *ResponseWrapper) BodyBytes() ([]byte, error) {
return body, err
}
// 获取http响应结果结构体
func (r *ResponseWrapper) GetHttpResp() (*http.Response, error) {
// BodyReader 获取响应体的reader
func (r *Resp) BodyReader() (io.ReadCloser, error) {
resp, err := r.GetHttpResp()
if err != nil {
return nil, err
}
return resp.Body, nil
}
// GetHttpResp 获取http响应结果结构体
func (r *Resp) GetHttpResp() (*http.Response, error) {
if r.err != nil {
return nil, fmt.Errorf("请求失败-%s", r.err.Error())
}

View File

@@ -1,4 +1,4 @@
package httpclient
package httpx
import (
"fmt"
@@ -12,20 +12,20 @@ type TestStruct struct {
}
func TestGet(t *testing.T) {
res, err := NewRequest("www.baidu.com").Get().BodyToString()
res, err := NewReq("www.baidu.com").Get().BodyToString()
fmt.Println(err)
fmt.Println(res)
}
func TestGetBodyToMap(t *testing.T) {
res, err := NewRequest("http://go.mayfly.run/api/syslogs?pageNum=1&pageSize=10").Get().BodyToMap()
res, err := NewReq("http://go.mayfly.run/api/syslogs?pageNum=1&pageSize=10").Get().BodyToMap()
fmt.Println(err)
fmt.Println(res["msg"])
fmt.Println(res["code"])
}
func TestGetQueryBodyToMap(t *testing.T) {
res, err := NewRequest("http://go.mayfly.run/api/syslogs").
res, err := NewReq("http://go.mayfly.run/api/syslogs").
Header("Authorization", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2OTUzOTQ5NTIsImlkIjoxLCJ1c2VybmFtZSI6ImFkbWluIn0.pGrczVZqk5nlId-FZPkjW_O5Sw3-2yjgzACp_j4JEXY").
GetByQuery(collx.M{"pageNum": 1, "pageSize": 10}).
BodyToMap()

View File

@@ -217,7 +217,7 @@ func (m Map[K, V]) Value() (driver.Value, error) {
return json.Marshal(m)
}
type Slice[T int | string | Map[string, any]] []T
type Slice[T int | uint64 | string | Map[string, any]] []T
func (s *Slice[T]) Scan(value any) error {
if v, ok := value.([]byte); ok && len(v) > 0 {
@@ -254,3 +254,19 @@ func (e ExtraData) GetExtraString(key string) string {
}
return cast.ToString(e.Extra[key])
}
// GetExtraInt 获取额外信息中的int类型字段值
func (e ExtraData) GetExtraInt(key string) int {
if e.Extra == nil {
return 0
}
return cast.ToInt(e.Extra[key])
}
// GetExtraBool 获取额外信息中的bool类型字段值
func (e ExtraData) GetExtraBool(key string) bool {
if e.Extra == nil {
return false
}
return cast.ToBool(e.Extra[key])
}

View File

@@ -59,8 +59,9 @@ func ArrayContains[T comparable](arr []T, el T) bool {
return false
}
// 数组转为map
// @param keyFunc key的主键
// ArrayToMap 数组转为map
//
// keyFunc key的主键
func ArrayToMap[T any, K comparable](arr []T, keyFunc func(val T) K) map[K]T {
res := make(map[K]T, len(arr))
for _, val := range arr {
@@ -70,7 +71,7 @@ func ArrayToMap[T any, K comparable](arr []T, keyFunc func(val T) K) map[K]T {
return res
}
// 数组映射,即将一数组元素通过映射函数转换为另一数组
// ArrayMap 数组映射,即将一数组元素通过映射函数转换为另一数组
func ArrayMap[T any, K any](arr []T, mapFunc func(val T) K) []K {
res := make([]K, len(arr))
for i, val := range arr {
@@ -79,6 +80,18 @@ func ArrayMap[T any, K any](arr []T, mapFunc func(val T) K) []K {
return res
}
// ArrayMapFilter 数组映射并过滤若mapFunc返回false则不映射该元素到新数组。
func ArrayMapFilter[T any, K any](arr []T, mapFilterFunc func(val T) (K, bool)) []K {
res := make([]K, 0)
for _, val := range arr {
mapRes, needMap := mapFilterFunc(val)
if needMap {
res = append(res, mapRes)
}
}
return res
}
// 将数组或切片按固定大小分割成小数组
func ArrayChunk[T any](arr []T, chunkSize int) [][]T {
var chunks [][]T

View File

@@ -8,9 +8,9 @@ import (
)
// json字符串转map
func ToMap(jsonStr string) map[string]any {
func ToMap(jsonStr string) (map[string]any, error) {
if jsonStr == "" {
return map[string]any{}
return map[string]any{}, nil
}
return ToMapByBytes([]byte(jsonStr))
}
@@ -21,13 +21,10 @@ func To[T any](jsonStr string, res T) (T, error) {
}
// json字节数组转map
func ToMapByBytes(bytes []byte) map[string]any {
func ToMapByBytes(bytes []byte) (map[string]any, error) {
var res map[string]any
err := json.Unmarshal(bytes, &res)
if err != nil {
logx.ErrorTrace("json字符串转map失败", err)
}
return res
return res, err
}
// 转换为json字符串