Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4963780a32 | ||
|
|
70ebaae3d2 | ||
|
|
9cd286fd92 | ||
|
|
f8ec441241 |
4
go.mod
4
go.mod
@@ -1,6 +1,6 @@
|
|||||||
module github.com/ayflying/utility_go
|
module github.com/ayflying/utility_go
|
||||||
|
|
||||||
go 1.23.0
|
go 1.24.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/apolloconfig/agollo/v4 v4.4.0
|
github.com/apolloconfig/agollo/v4 v4.4.0
|
||||||
@@ -79,6 +79,8 @@ require (
|
|||||||
google.golang.org/appengine v1.6.7 // indirect
|
google.golang.org/appengine v1.6.7 // indirect
|
||||||
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
|
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
|
||||||
google.golang.org/grpc v1.38.0 // indirect
|
google.golang.org/grpc v1.38.0 // indirect
|
||||||
|
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
|
||||||
|
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect
|
||||||
gopkg.in/ini.v1 v1.62.0 // indirect
|
gopkg.in/ini.v1 v1.62.0 // indirect
|
||||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -696,10 +696,14 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
|
|||||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||||
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
|
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
|
||||||
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||||
|
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=
|
||||||
|
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||||
|
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
|
||||||
|
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=
|
||||||
gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU=
|
gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU=
|
||||||
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package act
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/ayflying/utility_go/package/aycache"
|
"github.com/ayflying/utility_go/pgk"
|
||||||
"github.com/ayflying/utility_go/service"
|
"github.com/ayflying/utility_go/service"
|
||||||
"github.com/gogf/gf/v2/container/gvar"
|
"github.com/gogf/gf/v2/container/gvar"
|
||||||
"github.com/gogf/gf/v2/os/gctx"
|
"github.com/gogf/gf/v2/os/gctx"
|
||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Cache = aycache.New()
|
Cache = pgk.Cache("redis")
|
||||||
ActIdListIsShow map[int]func(uid int64) bool
|
ActIdListIsShow map[int]func(uid int64) bool
|
||||||
RedDotList map[string]func(uid int64) int32
|
RedDotList map[string]func(uid int64) int32
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,10 +2,10 @@ package gameAct
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/ayflying/utility_go/internal/game/act"
|
|
||||||
"github.com/ayflying/utility_go/internal/model/do"
|
"github.com/ayflying/utility_go/internal/model/do"
|
||||||
"github.com/ayflying/utility_go/internal/model/entity"
|
"github.com/ayflying/utility_go/internal/model/entity"
|
||||||
"github.com/ayflying/utility_go/package/aycache"
|
"github.com/ayflying/utility_go/package/aycache"
|
||||||
|
"github.com/ayflying/utility_go/pgk"
|
||||||
service2 "github.com/ayflying/utility_go/service"
|
service2 "github.com/ayflying/utility_go/service"
|
||||||
"github.com/ayflying/utility_go/tools"
|
"github.com/ayflying/utility_go/tools"
|
||||||
"github.com/gogf/gf/v2/container/gset"
|
"github.com/gogf/gf/v2/container/gset"
|
||||||
@@ -221,46 +221,10 @@ func (s *sGameAct) Save(actId int) (err error) {
|
|||||||
|
|
||||||
// 清空GetRedDot缓存
|
// 清空GetRedDot缓存
|
||||||
func (s *sGameAct) RefreshGetRedDotCache(uid int64) {
|
func (s *sGameAct) RefreshGetRedDotCache(uid int64) {
|
||||||
//cacheKey2 := fmt.Sprintf("gameAct:GetRedDot:%d", uid)
|
cacheKey := fmt.Sprintf("gameAct:GetRedDot:%s:%d", gtime.Now().Format("d"), uid)
|
||||||
cacheKey := fmt.Sprintf("gameAct:GetRedDot:%s:%d", gtime.Now().Format("Ymd"), uid)
|
_, err := pgk.Cache("redis").Remove(gctx.New(), cacheKey)
|
||||||
act.Cache.Remove(gctx.New(), cacheKey)
|
if err != nil {
|
||||||
|
g.Log().Error(ctx, err)
|
||||||
|
g.Dump(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
//func (s *sGameAct) GetRedDot(uid int64) (res map[string]int32, err error) {
|
|
||||||
// cacheKey := fmt.Sprintf("gameAct:GetRedDot:%s:%d", gtime.Now().Format("Ymd"), uid)
|
|
||||||
// if get, _ := act.Cache.Get(ctx, cacheKey); !get.IsEmpty() {
|
|
||||||
// err = get.Scan(&res)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// res = make(map[string]int32)
|
|
||||||
//
|
|
||||||
// //res["notice_count"] = 0
|
|
||||||
// //获取所有帖子红点
|
|
||||||
// for _, v := range communityNotice.Types {
|
|
||||||
// res[fmt.Sprintf("notice_%d", v)], err = service.CommunityNotice().Ping(uid, noticeV1.NoticeType(v))
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// //邮件红点
|
|
||||||
// res["mail_count"], err = service.GameMail().RedDot(uid)
|
|
||||||
//
|
|
||||||
// //act1可领取数量
|
|
||||||
// res["act1_count"], err = act1.New().RedDot(uid)
|
|
||||||
//
|
|
||||||
// //act2可领取数量
|
|
||||||
// res["act2_count"], err = act2.New().RedDot(uid)
|
|
||||||
//
|
|
||||||
// //成就红点
|
|
||||||
// res["act4_count"], err = act4.New().RedDot(uid)
|
|
||||||
//
|
|
||||||
// //广告点击
|
|
||||||
// res["act6_count"], err = act6.New().RedDot(uid)
|
|
||||||
//
|
|
||||||
// for k, v := range act.RedDotList {
|
|
||||||
// res[k] = v(uid)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// aycache.New().Set(ctx, cacheKey, res, time.Hour)
|
|
||||||
// return
|
|
||||||
//}
|
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
package drive
|
package drive
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/gogf/gf/v2/database/gredis"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"github.com/gogf/gf/v2/os/gcache"
|
"github.com/gogf/gf/v2/os/gcache"
|
||||||
|
"github.com/gogf/gf/v2/os/gctx"
|
||||||
)
|
)
|
||||||
|
|
||||||
var adapterRedisClient gcache.Adapter
|
var adapterRedisClient gcache.Adapter
|
||||||
@@ -11,7 +13,13 @@ var adapterRedisCache = gcache.New()
|
|||||||
func NewAdapterRedis() gcache.Adapter {
|
func NewAdapterRedis() gcache.Adapter {
|
||||||
|
|
||||||
if adapterRedisClient == nil {
|
if adapterRedisClient == nil {
|
||||||
adapterRedisClient = gcache.NewAdapterRedis(g.Redis("default"))
|
_cfg, _ := g.Cfg().Get(gctx.New(), "redis.default")
|
||||||
|
var cfg *gredis.Config
|
||||||
|
_cfg.Scan(&cfg)
|
||||||
|
redisObj, _ := gredis.New(cfg)
|
||||||
|
//adapterRedisClient = gcache.NewAdapterRedis(g.Redis("default"))
|
||||||
|
adapterRedisClient = gcache.NewAdapterRedis(redisObj)
|
||||||
|
|
||||||
adapterRedisCache.SetAdapter(adapterRedisClient)
|
adapterRedisCache.SetAdapter(adapterRedisClient)
|
||||||
}
|
}
|
||||||
return adapterRedisCache
|
return adapterRedisCache
|
||||||
|
|||||||
@@ -6,17 +6,17 @@ import (
|
|||||||
"github.com/gogf/gf/v2/os/gtime"
|
"github.com/gogf/gf/v2/os/gtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Mod struct {
|
type DingTalkMod struct {
|
||||||
DingTalkWebHook string
|
DingTalkWebHook string
|
||||||
}
|
}
|
||||||
|
|
||||||
func Load(webHook string) *Mod {
|
func DingTalkLoad(webHook string) *DingTalkMod {
|
||||||
return &Mod{
|
return &DingTalkMod{
|
||||||
DingTalkWebHook: webHook,
|
DingTalkWebHook: webHook,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Mod) Send(value string) {
|
func (m DingTalkMod) Send(value string) {
|
||||||
ctx := gctx.New()
|
ctx := gctx.New()
|
||||||
// 从配置中获取发送者名称
|
// 从配置中获取发送者名称
|
||||||
name, _ := g.Cfg().Get(ctx, "name")
|
name, _ := g.Cfg().Get(ctx, "name")
|
||||||
|
|||||||
51
pgk/notice/drive/email.go
Normal file
51
pgk/notice/drive/email.go
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
package drive
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
|
"github.com/gogf/gf/v2/os/gctx"
|
||||||
|
"gopkg.in/gomail.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MailMod struct {
|
||||||
|
Host string `json:"host" dc:"邮件服务器地址"`
|
||||||
|
Port int `json:"port" dc:"邮件服务器端口"`
|
||||||
|
User string `json:"user" dc:"邮件服务器用户名"`
|
||||||
|
Pass string `json:"pass" dc:"邮件服务器密码"`
|
||||||
|
From string `json:"from" dc:"邮件发送者"`
|
||||||
|
To string `json:"to" dc:"邮件接收者"`
|
||||||
|
Subject string `json:"subject" dc:"邮件主题"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func MailLoad(Host string, port int, to string, subject string) *MailMod {
|
||||||
|
return &MailMod{
|
||||||
|
Host: Host,
|
||||||
|
Port: port,
|
||||||
|
User: "root",
|
||||||
|
Pass: "root",
|
||||||
|
From: "root",
|
||||||
|
To: to,
|
||||||
|
Subject: subject,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m MailMod) Send(value string) {
|
||||||
|
// 创建一个新的消息
|
||||||
|
obj := gomail.NewMessage()
|
||||||
|
// 设置发件人
|
||||||
|
obj.SetHeader("From", m.From)
|
||||||
|
// 设置收件人
|
||||||
|
obj.SetHeader("To", m.To)
|
||||||
|
// 设置邮件主题
|
||||||
|
obj.SetHeader("Subject", m.Subject)
|
||||||
|
// 设置邮件正文
|
||||||
|
obj.SetBody("text/plain", value)
|
||||||
|
|
||||||
|
// 创建 SMTP 拨号器,这里需要提供 SMTP 服务器地址、端口、发件人邮箱和密码
|
||||||
|
d := gomail.NewDialer(m.Host, m.Port, m.User, m.Pass)
|
||||||
|
|
||||||
|
// 发送邮件
|
||||||
|
if err := d.DialAndSend(obj); err != nil {
|
||||||
|
g.Log().Error(gctx.New(), err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -9,11 +9,12 @@ type MessageV1 interface {
|
|||||||
Send(value string)
|
Send(value string)
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(typ v1.NoticeType, host string) MessageV1 {
|
func New(typ v1.NoticeType, host string, value ...interface{}) MessageV1 {
|
||||||
switch typ {
|
switch typ {
|
||||||
case v1.NoticeType_DINGTALK:
|
case v1.NoticeType_DINGTALK:
|
||||||
return drive.Load(host)
|
return drive.DingTalkLoad(host)
|
||||||
|
case v1.NoticeType_EMAIL:
|
||||||
|
return drive.MailLoad(host, value[0].(int), value[1].(string), value[2].(string))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
52
tools/random.go
Normal file
52
tools/random.go
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package tools
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
Rand *randMod
|
||||||
|
)
|
||||||
|
|
||||||
|
type randMod struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
// RandomAll 按权重随机选取 N 个不重复的元素
|
||||||
|
func (m *randMod) RandomAll(data map[int]int, n int) []int {
|
||||||
|
if n > len(data) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
// 复制权重映射,避免修改原始数据
|
||||||
|
remainingWeights := make(map[int]int)
|
||||||
|
for k, v := range data {
|
||||||
|
remainingWeights[k] = v
|
||||||
|
}
|
||||||
|
result := make([]int, 0, n)
|
||||||
|
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
totalWeight := 0
|
||||||
|
// 计算剩余元素的总权重
|
||||||
|
for _, weight := range remainingWeights {
|
||||||
|
totalWeight += weight
|
||||||
|
}
|
||||||
|
if totalWeight == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// 生成一个 0 到总权重之间的随机数
|
||||||
|
randomNum := rand.Intn(totalWeight)
|
||||||
|
currentWeight := 0
|
||||||
|
for key, weight := range remainingWeights {
|
||||||
|
currentWeight += weight
|
||||||
|
if randomNum < currentWeight {
|
||||||
|
// 将选中的元素添加到结果切片中
|
||||||
|
result = append(result, key)
|
||||||
|
// 从剩余权重映射中移除选中的元素
|
||||||
|
delete(remainingWeights, key)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
@@ -124,11 +124,11 @@ func (m *tools) Items2Map(items [][]int64) (list map[int64]int64) {
|
|||||||
// 该函数通过遍历切片,从后向前检查每个元素,如果找到与指定值相等的元素,则将其从切片中移除。
|
// 该函数通过遍历切片,从后向前检查每个元素,如果找到与指定值相等的元素,则将其从切片中移除。
|
||||||
// 这种从后向前的遍历方法可以避免因移除元素而导致的数组重新排列带来的额外计算。
|
// 这种从后向前的遍历方法可以避免因移除元素而导致的数组重新排列带来的额外计算。
|
||||||
// RemoveSlice 删除切片中的某个值
|
// RemoveSlice 删除切片中的某个值
|
||||||
func RemoveSlice[t Number](slice []t, value t) []t {
|
func RemoveSlice[t Number](slice []t, value ...t) []t {
|
||||||
// 从后向前遍历切片
|
// 从后向前遍历切片
|
||||||
for i := len(slice) - 1; i >= 0; i-- {
|
for i := len(slice) - 1; i >= 0; i-- {
|
||||||
// 检查当前元素是否等于需要移除的值
|
// 检查当前元素是否等于需要移除的值
|
||||||
if slice[i] == value {
|
if InArray(slice[i], value) {
|
||||||
// 如果相等,移除该元素
|
// 如果相等,移除该元素
|
||||||
// 使用append和切片操作符来实现移除操作,将i之前和i之后的元素合并到一起
|
// 使用append和切片操作符来实现移除操作,将i之前和i之后的元素合并到一起
|
||||||
slice = append(slice[:i], slice[i+1:]...)
|
slice = append(slice[:i], slice[i+1:]...)
|
||||||
|
|||||||
Reference in New Issue
Block a user