feat:返回消息

dev v1.0.2
Chris 1 year ago
parent ce5ada70f0
commit f393a49328

2
cache/define.go vendored

@ -16,7 +16,7 @@ type ICache interface {
Forever(key string, val any)
}
// Cache 定义范型
// Cache 定义范型T为保存的数据类型
type Cache[T any] struct {
ICache ICache
}

10
cache/location.go vendored

@ -4,7 +4,9 @@ import (
"fmt"
"gitea.codecodify.com/golang/tool/helper"
"github.com/patrickmn/go-cache"
"io/fs"
"log"
"os"
"time"
)
@ -14,6 +16,8 @@ func init() {
const EmptyName = ""
const cacheDir = "./cacheTemp"
// location 使用cache2go实现本地缓存
type location struct {
Name string
@ -68,7 +72,11 @@ func getCache() *cache.Cache {
func getFile(name string) string {
name = helper.Ternary(len(name) > 0, name+".cache", "cache.cache").(string)
return fmt.Sprintf("%s/%s", helper.GetCurrentAbPath(), name)
_, err := os.Stat(cacheDir)
if os.IsNotExist(err) {
os.Mkdir(cacheDir, fs.ModePerm)
}
return fmt.Sprintf("%s/%s", cacheDir, name)
}
func (l location) saveFile() error {

@ -0,0 +1,37 @@
package response
import "encoding/json"
// 返回码
const (
Success = 0
Fail = -1
)
// Response 统一响应结构
type Response[T any] struct {
Errcode int `json:"errcode"` // 返回码
Errmsg string `json:"errmsg"` // 对返回码的文本描述内容
Data T `json:"data"` // 返回数据
}
// NewResponse 创建响应
func NewResponse[T any](errcode int, errmsg string, data T) Response[T] {
return Response[T]{Errcode: errcode, Errmsg: errmsg, Data: data}
}
// SuccessResponse 成功返回
func SuccessResponse[T any](data T) Response[T] {
return NewResponse[T](Success, "success", data)
}
// FailResponse 失败返回
func FailResponse[T any](errmsg string) Response[T] {
return NewResponse[T](Fail, errmsg, nil)
}
// Bytes 将响应转换为[]byte
func (r Response[T]) Bytes() []byte {
b, _ := json.Marshal(r)
return b
}

@ -11,7 +11,7 @@ import (
// Message 定义消息范型, 统一发送消息入口, 为了类型安全, 需要实现Req接口
type Message[T Req] struct{}
const title = "【企业维系消息通知】"
const title = "【企业微信消息通知】"
// Send 发送消息
func (m *Message[T]) Send(body T, token string) (Resp, error) {

Loading…
Cancel
Save