diff --git a/app/index.go b/app/index.go new file mode 100644 index 00000000..ce5a8796 --- /dev/null +++ b/app/index.go @@ -0,0 +1,26 @@ +package app + +import ( + "fmt" + "github.com/gin-gonic/gin" + "go-wechat/service" + "net/http" +) + +// Index +// @description: 首页 +// @param ctx +func Index(ctx *gin.Context) { + var result = gin.H{ + "msg": "success", + } + // 取出所有好友列表 + friends, groups, err := service.GetAllFriend() + if err != nil { + result["msg"] = fmt.Sprintf("数据获取失败: %s", err.Error()) + } + result["friends"] = friends + result["groups"] = groups + // 渲染页面 + ctx.HTML(http.StatusOK, "index.html", result) +} diff --git a/constant/wxid.go b/common/constant/wxid.go similarity index 100% rename from constant/wxid.go rename to common/constant/wxid.go diff --git a/common/types/datetime.go b/common/types/datetime.go new file mode 100644 index 00000000..b6d99a89 --- /dev/null +++ b/common/types/datetime.go @@ -0,0 +1,156 @@ +package types + +import ( + "database/sql/driver" + "fmt" + "strings" + "time" +) + +// 默认时间格式 +const dateTimeFormat = "2006-01-02 15:04:05.000" + +// 可能包含的时间格式 +var formatMap = map[string]string{ + "yyyy-mm-dd hh:mm:ss": "2006-01-02 15:04:05", + "yyyy-mm-dd hh:mm": "2006-01-02 15:04", + "yyyy-mm-dd hh": "2006-01-02 15:04", + "yyyy-mm-dd": "2006-01-02", + "yyyy-mm": "2006-01", + "mm-dd": "01-02", + "dd-mm-yy hh:mm:ss": "02-01-06 15:04:05", + "yyyy/mm/dd hh:mm:ss": "2006/01/02 15:04:05", + "yyyy/mm/dd hh:mm": "2006/01/02 15:04", + "yyyy/mm/dd hh": "2006/01/02 15", + "yyyy/mm/dd": "2006/01/02", + "yyyy/mm": "2006/01", + "mm/dd": "01/02", + "dd/mm/yy hh:mm:ss": "02/01/06 15:04:05", + "yyyy": "2006", + "mm": "01", + "hh:mm:ss": "15:04:05", + "mm:ss": "04:05", +} + +// DateTime 自定义时间类型 +type DateTime time.Time + +// Scan implements the Scanner interface. +func (dt *DateTime) Scan(value interface{}) error { + // mysql 内部日期的格式可能是 2006-01-02 15:04:05 +0800 CST 格式,所以检出的时候还需要进行一次格式化 + tTime, _ := time.Parse("2006-01-02 15:04:05 +0800 CST", value.(time.Time).String()) + *dt = DateTime(tTime) + return nil +} + +// Value implements the driver Valuer interface. +func (dt DateTime) Value() (dv driver.Value, err error) { + // 0001-01-01 00:00:00 属于空值,遇到空值解析成 null 即可 + if dt.String() == "0001-01-01 00:00:00.000" { + return nil, nil + } + dv, err = []byte(dt.Format(dateTimeFormat)), nil + return +} + +// 用于 fmt.Println 和后续验证场景 +func (dt DateTime) String() string { + return dt.Format(dateTimeFormat) +} + +// Format 格式化 +func (dt *DateTime) Format(fm string) string { + return time.Time(*dt).Format(fm) +} + +// AutoParse 假装是个自动解析时间的函数 +func (dt DateTime) AutoParse(timeStr string) (t time.Time, err error) { + // 循环匹配预设的时间格式 + for _, v := range formatMap { + // 尝试解析,没报错就是解析成功了 + t, err = time.ParseInLocation(v, timeStr, time.Local) + if err == nil { + // 错误为空,表示匹配上了 + return + } + } + return +} + +// After 时间比较 +func (dt *DateTime) After(now time.Time) bool { + return time.Time(*dt).After(now) +} + +// Before 时间比较 +func (dt *DateTime) Before(now time.Time) bool { + return time.Time(*dt).Before(now) +} + +// IBefore 时间比较 +func (dt *DateTime) IBefore(now DateTime) bool { + return dt.Before(time.Time(now)) +} + +// SubTime 对比 +func (dt DateTime) SubTime(t time.Time) time.Duration { + return dt.ToTime().Sub(t) +} + +// Sub 对比 +func (dt DateTime) Sub(t DateTime) time.Duration { + return dt.ToTime().Sub(t.ToTime()) +} + +// ToTime 转换为golang的时间类型 +func (dt DateTime) ToTime() time.Time { + return time.Time(dt) +} + +// IsNil 是否为空值 +func (dt DateTime) IsNil() bool { + return dt.Format(dateTimeFormat) == "0001-01-01 00:00:00.000" +} + +// Unix 实现Unix函数 +func (dt DateTime) Unix() int64 { + return dt.ToTime().Unix() +} + +// EndOfCentury 获取本世纪最后时间 +func (dt DateTime) EndOfCentury() DateTime { + yearEnd := time.Now().Local().Year()/100*100 + 99 + return DateTime(time.Date(yearEnd, 12, 31, 23, 59, 59, 999999999, time.Local)) +} + +// ======== 序列化 JSON ======== + +// MarshalJSON 时间到字符串 +func (dt DateTime) MarshalJSON() ([]byte, error) { + // 过滤掉空数据 + if dt.IsNil() { + return []byte("\"\""), nil + } + output := fmt.Sprintf(`"%s"`, dt.Format("2006-01-02 15:04:05")) + return []byte(output), nil +} + +// UnmarshalJSON 字符串到时间 +func (dt *DateTime) UnmarshalJSON(b []byte) (err error) { + if len(b) == 2 { + *dt = DateTime{} + return + } + // 解析指定的格式 + var now time.Time + if strings.HasPrefix(string(b), "\"") { + now, err = dt.AutoParse(string(b)[1 : len(b)-1]) + } else { + now, err = dt.AutoParse(string(b)) + } + if err != nil { + return + } + *dt = DateTime(now) + return +} diff --git a/config.yaml b/config.yaml index a0bf77ab..1f2d4c2a 100644 --- a/config.yaml +++ b/config.yaml @@ -16,7 +16,7 @@ mysql: db: wechat task: - enable: true + enable: false syncFriends: enable: true cron: '*/5 * * * *' # 五分钟一次 diff --git a/entity/friend.go b/entity/friend.go index 7c02bbc8..74c7040d 100644 --- a/entity/friend.go +++ b/entity/friend.go @@ -12,6 +12,7 @@ type Friend struct { Wxid string `json:"wxid"` // 微信原始Id EnableAi bool `json:"enableAI" gorm:"type:tinyint(1) default 0 not null"` // 是否使用AI EnableChatRank bool `json:"enableChatRank" gorm:"type:tinyint(1) default 0 not null"` // 是否使用聊天排行 + IsOk bool `json:"isOk" gorm:"type:tinyint(1) default 0 not null"` // 是否正常 } func (Friend) TableName() string { diff --git a/go.mod b/go.mod index c2c7651a..2e9c7df1 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.21 require ( github.com/duke-git/lancet/v2 v2.2.7 github.com/fsnotify/fsnotify v1.6.0 + github.com/gin-gonic/gin v1.9.1 github.com/go-co-op/gocron v1.34.1 github.com/go-resty/resty/v2 v2.8.0 github.com/sashabaranov/go-openai v1.17.5 @@ -14,13 +15,27 @@ require ( ) require ( + github.com/bytedance/sonic v1.9.1 // indirect + github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect + github.com/gabriel-vasile/mimetype v1.4.2 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.14.0 // indirect github.com/go-sql-driver/mysql v1.7.0 // indirect + github.com/goccy/go-json v0.10.2 // indirect github.com/google/uuid v1.3.1 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.4 // indirect + github.com/leodido/go-urn v1.2.4 // indirect github.com/magiconair/properties v1.8.7 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.1.0 // indirect github.com/robfig/cron/v3 v3.0.1 // indirect github.com/sagikazarmark/locafero v0.3.0 // indirect @@ -30,12 +45,17 @@ require ( github.com/spf13/cast v1.5.1 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/subosito/gotenv v1.6.0 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.11 // indirect go.uber.org/atomic v1.9.0 // indirect go.uber.org/multierr v1.9.0 // indirect + golang.org/x/arch v0.3.0 // indirect + golang.org/x/crypto v0.13.0 // indirect golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect golang.org/x/net v0.15.0 // indirect golang.org/x/sys v0.12.0 // indirect golang.org/x/text v0.13.0 // indirect + google.golang.org/protobuf v1.31.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 9ede1c71..e2df6c38 100644 --- a/go.sum +++ b/go.sum @@ -38,7 +38,13 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= +github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s= +github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= +github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams= +github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= @@ -63,15 +69,31 @@ github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0X github.com/frankban/quicktest v1.14.4/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= +github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= +github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= +github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= github.com/go-co-op/gocron v1.34.1 h1:g4Y1ePFin2z4Rbb5gTHNYfj36moY4kU9s0ZmGy/ZddQ= github.com/go-co-op/gocron v1.34.1/go.mod h1:NLi+bkm4rRSy1F8U7iacZOz0xPseMoIOnvabGoSe/no= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js= +github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= github.com/go-resty/resty/v2 v2.8.0 h1:J29d0JFWwSWrDCysnOK/YjsPMLQTx0TvgJEHVGvf2L8= github.com/go-resty/resty/v2 v2.8.0/go.mod h1:UCui0cMHekLrSntoMyofdSTaPpinlRHFtPpizuyDW2w= github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc= github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= @@ -97,6 +119,7 @@ github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvq github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= @@ -108,8 +131,10 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= @@ -140,9 +165,14 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk= +github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= @@ -153,10 +183,19 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= +github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= @@ -198,11 +237,16 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5 github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= +github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= @@ -218,6 +262,9 @@ go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= +golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k= +golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= @@ -226,6 +273,7 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck= golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= @@ -358,9 +406,11 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -525,6 +575,9 @@ google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2 google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= @@ -549,5 +602,6 @@ honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= +rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= diff --git a/main.go b/main.go index ab6ee78c..4df95f4d 100644 --- a/main.go +++ b/main.go @@ -1,15 +1,14 @@ package main import ( - "bytes" + "github.com/gin-gonic/gin" "go-wechat/config" - "go-wechat/handler" "go-wechat/initialization" + "go-wechat/router" "go-wechat/tasks" + "go-wechat/tcpserver" "go-wechat/utils" - "io" "log" - "net" "time" ) @@ -18,25 +17,6 @@ func init() { tasks.InitTasks() } -func process(conn net.Conn) { - // 处理完关闭连接 - defer func() { - log.Printf("处理完成: -> %s", conn.RemoteAddr()) - _ = conn.Close() - }() - - var buf bytes.Buffer - if _, err := io.Copy(&buf, conn); err != nil { - log.Printf("[%s]返回数据失败,错误信息: %v", conn.RemoteAddr(), err) - } - log.Printf("[%s]数据长度: %d", conn.RemoteAddr(), buf.Len()) - go handler.Parse(conn.RemoteAddr(), buf.Bytes()) - // 将接受到的数据返回给客户端 - if _, err := conn.Write([]byte("200 OK")); err != nil { - log.Printf("[%s]返回数据失败,错误信息: %v", conn.RemoteAddr(), err) - } -} - func main() { // 如果启用了自动配置回调,就设置一下 if config.Conf.Wechat.AutoSetCallback { @@ -45,21 +25,22 @@ func main() { utils.SetCallback(config.Conf.Wechat.Callback) } - // 建立 tcp 服务 - listen, err := net.Listen("tcp", "0.0.0.0:19099") - if err != nil { - log.Printf("listen failed, err:%v", err) - return - } + // 启动TCP服务 + go tcpserver.Start() - for { - // 等待客户端建立连接 - conn, err := listen.Accept() - if err != nil { - log.Printf("accept failed, err:%v", err) - continue - } - // 启动一个单独的 goroutine 去处理连接 - go process(conn) + // 启动HTTP服务 + app := gin.Default() + app.LoadHTMLGlob("views/*.html") + app.Static("/assets", "./views/assets") + app.StaticFile("/favicon.ico", "./views/wechat.ico") + // 404返回数据 + app.NoRoute(func(ctx *gin.Context) { + // 404直接跳转到首页 + ctx.Redirect(302, "/index.html") + }) + // 初始化路由 + router.Init(app) + if err := app.Run(":8080"); err != nil { + log.Panicf("服务启动失败:%v", err) } } diff --git a/router/router.go b/router/router.go new file mode 100644 index 00000000..e60eefb2 --- /dev/null +++ b/router/router.go @@ -0,0 +1,21 @@ +package router + +import ( + "github.com/gin-gonic/gin" + "go-wechat/app" +) + +// Init +// @description: 初始化路由 +// @param g +func Init(g *gin.Engine) { + g.GET("/", func(ctx *gin.Context) { + // 重定向到index.html + ctx.Redirect(302, "/index.html") + }) + + g.GET("/index.html", app.Index) // 首页 + g.GET("/test.html", func(ctx *gin.Context) { + ctx.HTML(200, "test.html", nil) + }) +} diff --git a/service/friend.go b/service/friend.go index f80cc870..548580c5 100644 --- a/service/friend.go +++ b/service/friend.go @@ -3,8 +3,37 @@ package service import ( "go-wechat/client" "go-wechat/entity" + "go-wechat/vo" + "strings" ) +// GetAllFriend +// @description: 取出所有好友 +// @return friends +// @return groups +// @return err +func GetAllFriend() (friends, groups []vo.FriendItem, err error) { + var records []vo.FriendItem + err = client.MySQL. + Table("t_friend AS tf"). + Joins("LEFT JOIN t_message AS tm ON tf.wxid = tm.from_user"). + Select("tf.*", "MAX(tm.create_at) AS last_active_time"). + Group("tf.wxid"). + Order("last_active_time DESC"). + Find(&records).Error + if err != nil { + return + } + for _, record := range records { + if strings.HasSuffix(record.Wxid, "@chatroom") { + groups = append(groups, record) + } else { + friends = append(friends, record) + } + } + return +} + // GetAllEnableAI // @description: 取出所有启用了AI的好友或群组 // @return []entity.Friend diff --git a/tasks/friends/friends.go b/tasks/friends/friends.go index 9abba0e4..69348f7d 100644 --- a/tasks/friends/friends.go +++ b/tasks/friends/friends.go @@ -4,8 +4,8 @@ import ( "encoding/json" "github.com/go-resty/resty/v2" "go-wechat/client" + "go-wechat/common/constant" "go-wechat/config" - "go-wechat/constant" "go-wechat/entity" "go-wechat/model" "gorm.io/gorm" @@ -38,6 +38,8 @@ func Sync() { tx := client.MySQL.Begin() defer tx.Commit() + nowIds := []string{} + for _, friend := range base.Data { if strings.Contains(friend.Wxid, "gh_") || strings.Contains(friend.Wxid, "@openim") { continue @@ -47,6 +49,7 @@ func Sync() { continue } log.Printf("昵称: %s -> 类型: %d -> 微信号: %s -> 微信原始Id: %s", friend.Nickname, friend.Type, friend.CustomAccount, friend.Wxid) + nowIds = append(nowIds, friend.Wxid) // 判断是否存在,不存在的话就新增,存在就修改一下名字 var count int64 @@ -87,6 +90,9 @@ func Sync() { } } + // 清理不在列表中的好友 + err = tx.Model(&entity.Friend{}).Where("wxid NOT IN (?)", nowIds).Update("is_ok", false).Error + log.Println("同步好友列表完成") } diff --git a/tcpserver/handle.go b/tcpserver/handle.go new file mode 100644 index 00000000..38dbb369 --- /dev/null +++ b/tcpserver/handle.go @@ -0,0 +1,31 @@ +package tcpserver + +import ( + "bytes" + "go-wechat/handler" + "io" + "log" + "net" +) + +// process +// @description: 处理连接 +// @param conn +func process(conn net.Conn) { + // 处理完关闭连接 + defer func() { + log.Printf("处理完成: -> %s", conn.RemoteAddr()) + _ = conn.Close() + }() + + var buf bytes.Buffer + if _, err := io.Copy(&buf, conn); err != nil { + log.Printf("[%s]返回数据失败,错误信息: %v", conn.RemoteAddr(), err) + } + log.Printf("[%s]数据长度: %d", conn.RemoteAddr(), buf.Len()) + go handler.Parse(conn.RemoteAddr(), buf.Bytes()) + // 将接受到的数据返回给客户端 + if _, err := conn.Write([]byte("200 OK")); err != nil { + log.Printf("[%s]返回数据失败,错误信息: %v", conn.RemoteAddr(), err) + } +} diff --git a/tcpserver/server.go b/tcpserver/server.go new file mode 100644 index 00000000..f4e3b201 --- /dev/null +++ b/tcpserver/server.go @@ -0,0 +1,28 @@ +package tcpserver + +import ( + "log" + "net" +) + +// Start +// @description: 启动服务 +func Start() { + // 建立 tcp 服务 + listen, err := net.Listen("tcp", "0.0.0.0:19099") + if err != nil { + log.Printf("listen failed, err:%v", err) + return + } + + for { + // 等待客户端建立连接 + conn, err := listen.Accept() + if err != nil { + log.Printf("accept failed, err:%v", err) + continue + } + // 启动一个单独的 goroutine 去处理连接 + go process(conn) + } +} diff --git a/views/index.html b/views/index.html new file mode 100644 index 00000000..fc54e98a --- /dev/null +++ b/views/index.html @@ -0,0 +1,110 @@ + + + + + 水群助手 + + + + + + + +
+ + {{ if ne .msg "success" }} + + {{ end }} + +
+ +
+ 好友列表 +
+
+ +
+ + + + + + + + + + + + + + + {{ range .friends }} + + + + + + + + + + {{ end }} + +
微信Id微信号昵称最后活跃时间是否在通讯录是否启用AI是否启用水群排行榜
{{ .Wxid }}{{ .CustomAccount }}{{ .Nickname }} + {{ if eq .LastActiveTime.IsNil true }} + 无数据 + {{ else }} + {{ .LastActiveTime }} + {{ end }} + {{ .IsOk }}{{ .EnableAi }}{{ .EnableChatRank }}
+
+
+
+
+ +
+ 群列表 +
+
+ +
+ + + + + + + + + + + + + + {{ range .groups }} + + + + + + + + + {{ end }} + +
群Id昵称最后活跃时间是否在通讯录是否启用AI是否启用水群排行榜
{{ .Wxid }}{{ .Nickname }} + {{ if eq .LastActiveTime.IsNil true }} + 无数据 + {{ else }} + {{ .LastActiveTime }} + {{ end }} + {{ .IsOk }}{{ .EnableAi }}{{ .EnableChatRank }}
+
+
+
+
+ + \ No newline at end of file diff --git a/views/static/js/index.js b/views/static/js/index.js new file mode 100644 index 00000000..adaee6c2 --- /dev/null +++ b/views/static/js/index.js @@ -0,0 +1 @@ +console.log("打开首页") \ No newline at end of file diff --git a/views/wechat.ico b/views/wechat.ico new file mode 100644 index 00000000..ea93e019 Binary files /dev/null and b/views/wechat.ico differ diff --git a/vo/friend.go b/vo/friend.go new file mode 100644 index 00000000..11c1e474 --- /dev/null +++ b/vo/friend.go @@ -0,0 +1,19 @@ +package vo + +import ( + "go-wechat/common/types" +) + +// FriendItem +// @description: 好友列表数据 +type FriendItem struct { + CustomAccount string // 微信号 + Nickname string // 昵称 + Pinyin string // 昵称拼音大写首字母 + PinyinAll string // 昵称全拼 + Wxid string // 微信原始Id + EnableAi bool // 是否使用AI + EnableChatRank bool // 是否使用聊天排行 + IsOk bool // 是否还在通讯库(群聊是要还在群里也算) + LastActiveTime types.DateTime // 最后活跃时间 +}