From b4803e41f5081d27178c9abc2cb185ab09273d9a Mon Sep 17 00:00:00 2001 From: johlanse Date: Sat, 12 Nov 2022 19:56:05 +0800 Subject: [PATCH] =?UTF-8?q?#152=20=E4=BF=AE=E5=A4=8D=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E5=85=AC=E4=BC=97=E5=8F=B7=E9=80=9A=E8=BF=87=E6=99=AE=E9=80=9A?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cli/shell.go | 25 +++++++++++++++ go.mod | 8 +++++ go.sum | 16 +++++++++ main.go | 12 +++---- model/wechat_user.go | 2 +- push/wx.go | 32 +++++++++--------- utils/stop/stop_other.go | 8 +++++ utils/stop/stop_windows.go | 66 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 145 insertions(+), 24 deletions(-) create mode 100644 utils/stop/stop_other.go create mode 100644 utils/stop/stop_windows.go diff --git a/cli/shell.go b/cli/shell.go index 3fe2a6a..596f2e1 100644 --- a/cli/shell.go +++ b/cli/shell.go @@ -27,6 +27,7 @@ func RunCli() { app.AddCommand(getUser()) app.AddCommand(addUser()) app.AddCommand(study()) + app.AddCommand(score()) grumble.Main(app) } @@ -70,6 +71,30 @@ func study() *grumble.Command { } } +func score() *grumble.Command { + return &grumble.Command{ + Name: "get score", + Aliases: []string{"score"}, + Help: "get score", + LongHelp: "查询用户积分", + Run: func(c *grumble.Context) error { + users, err := model.Query() + if err != nil { + return err + } + for _, user := range users { + score, err := lib.GetUserScore(user.ToCookies()) + if err != nil { + return err + } + _, _ = c.App.Println(user.Nick + "\n" + lib.FormatScore(score) + "\n\n") + } + + return err + }, + } +} + func addUser() *grumble.Command { return &grumble.Command{ Name: "add user", diff --git a/go.mod b/go.mod index 00192a4..71779c4 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/desertbit/grumble v1.1.3 github.com/dustin/go-humanize v1.0.0 github.com/fsnotify/fsnotify v1.5.4 + github.com/getlantern/systray v1.2.1 github.com/gin-contrib/gzip v0.0.6 github.com/gin-gonic/gin v1.8.1 github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.4.0-beta.0 @@ -38,6 +39,12 @@ require ( github.com/desertbit/go-shlex v0.1.1 // indirect github.com/desertbit/readline v1.5.1 // indirect github.com/fatih/color v1.13.0 // indirect + github.com/getlantern/context v0.0.0-20190109183933-c447772a6520 // indirect + github.com/getlantern/errors v0.0.0-20190325191628-abdb3e3e36f7 // indirect + github.com/getlantern/golog v0.0.0-20190830074920-4ef2e798c2d7 // indirect + github.com/getlantern/hex v0.0.0-20190417191902-c6586a6fe0b7 // indirect + github.com/getlantern/hidden v0.0.0-20190325191715-f02dbb02be55 // indirect + github.com/getlantern/ops v0.0.0-20190325191751-d70cb0d6f85f // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.0 // indirect github.com/go-playground/universal-translator v0.18.0 // indirect @@ -58,6 +65,7 @@ require ( github.com/mattn/go-isatty v0.0.16 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect github.com/pelletier/go-toml v1.9.5 // indirect github.com/pelletier/go-toml/v2 v2.0.5 // indirect github.com/pkg/errors v0.9.1 // indirect diff --git a/go.sum b/go.sum index 7423fcf..da42506 100644 --- a/go.sum +++ b/go.sum @@ -136,6 +136,20 @@ github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3 github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI= github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU= +github.com/getlantern/context v0.0.0-20190109183933-c447772a6520 h1:NRUJuo3v3WGC/g5YiyF790gut6oQr5f3FBI88Wv0dx4= +github.com/getlantern/context v0.0.0-20190109183933-c447772a6520/go.mod h1:L+mq6/vvYHKjCX2oez0CgEAJmbq1fbb/oNJIWQkBybY= +github.com/getlantern/errors v0.0.0-20190325191628-abdb3e3e36f7 h1:6uJ+sZ/e03gkbqZ0kUG6mfKoqDb4XMAzMIwlajq19So= +github.com/getlantern/errors v0.0.0-20190325191628-abdb3e3e36f7/go.mod h1:l+xpFBrCtDLpK9qNjxs+cHU6+BAdlBaxHqikB6Lku3A= +github.com/getlantern/golog v0.0.0-20190830074920-4ef2e798c2d7 h1:guBYzEaLz0Vfc/jv0czrr2z7qyzTOGC9hiQ0VC+hKjk= +github.com/getlantern/golog v0.0.0-20190830074920-4ef2e798c2d7/go.mod h1:zx/1xUUeYPy3Pcmet8OSXLbF47l+3y6hIPpyLWoR9oc= +github.com/getlantern/hex v0.0.0-20190417191902-c6586a6fe0b7 h1:micT5vkcr9tOVk1FiH8SWKID8ultN44Z+yzd2y/Vyb0= +github.com/getlantern/hex v0.0.0-20190417191902-c6586a6fe0b7/go.mod h1:dD3CgOrwlzca8ed61CsZouQS5h5jIzkK9ZWrTcf0s+o= +github.com/getlantern/hidden v0.0.0-20190325191715-f02dbb02be55 h1:XYzSdCbkzOC0FDNrgJqGRo8PCMFOBFL9py72DRs7bmc= +github.com/getlantern/hidden v0.0.0-20190325191715-f02dbb02be55/go.mod h1:6mmzY2kW1TOOrVy+r41Za2MxXM+hhqTtY3oBKd2AgFA= +github.com/getlantern/ops v0.0.0-20190325191751-d70cb0d6f85f h1:wrYrQttPS8FHIRSlsrcuKazukx/xqO/PpLZzZXsF+EA= +github.com/getlantern/ops v0.0.0-20190325191751-d70cb0d6f85f/go.mod h1:D5ao98qkA6pxftxoqzibIBBrLSUli+kYnJqrgBf9cIA= +github.com/getlantern/systray v1.2.1 h1:udsC2k98v2hN359VTFShuQW6GGprRprw6kD6539JikI= +github.com/getlantern/systray v1.2.1/go.mod h1:AecygODWIsBquJCJFop8MEQcJbWFfw/1yWbVabNgpCM= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/gin-contrib/gzip v0.0.6 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d4= github.com/gin-contrib/gzip v0.0.6/go.mod h1:QOJlmV2xmayAjkNS2Y8NQsMneuRShOU/kjovCXNuzzk= @@ -465,6 +479,8 @@ github.com/openzipkin-contrib/zipkin-go-opentracing v0.4.5/go.mod h1:/wsWhb9smxS github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw= github.com/openzipkin/zipkin-go v0.2.1/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4= github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4= +github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c h1:rp5dCmg/yLR3mgFuSOe4oEnDDmGLROTvMragMUXpTQw= +github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c/go.mod h1:X07ZCGwUbLaax7L0S3Tw4hpejzu63ZrrQiUe6W0hcy0= github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM= github.com/panjf2000/ants/v2 v2.5.0 h1:1rWGWSnxCsQBga+nQbA4/iY6VMeNoOIAM0ZWh9u3q2Q= github.com/panjf2000/ants/v2 v2.5.0/go.mod h1:cU93usDlihJZ5CfRGNDYsiBYvoilLvBF5Qp/BT2GNRE= diff --git a/main.go b/main.go index 7435866..a5435be 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + "github.com/johlanse/study_xxqg/utils/stop" "io" "math/rand" "net/http" @@ -24,7 +25,6 @@ import ( nested "github.com/Lyrics-you/sail-logrus-formatter/sailor" "github.com/huoxue1/xdaemon" - "github.com/johlanse/study_xxqg/cli" "github.com/johlanse/study_xxqg/conf" "github.com/johlanse/study_xxqg/lib/state" "github.com/johlanse/study_xxqg/utils" @@ -46,7 +46,9 @@ var ( var VERSION = "unknown" func init() { - runBack() + if runtime.GOOS != "windows" { + runBack() + } fmt.Printf("\033[1;31;40m%s\033[0m\n\n", "******************************************************************") fmt.Printf("\033[1;31;40m%s\033[0m\n\n", "软件仅可用户学习和个人使用,禁止用于任何商业活动!!!!") @@ -221,11 +223,7 @@ func main() { if now { do() } - if runtime.GOOS == "windows" { - cli.RunCli() - } else { - select {} - } + stop.Stop() } diff --git a/model/wechat_user.go b/model/wechat_user.go index 08c3fc5..57bb2f9 100644 --- a/model/wechat_user.go +++ b/model/wechat_user.go @@ -26,7 +26,7 @@ func UpdateWechatUser(user *WechatUser) error { return err } } else { - _, err := engine.Table(new(WechatUser)).Update(user) + _, err := engine.Where("open_id=?", user.OpenId).Update(user) if err != nil { return err } diff --git a/push/wx.go b/push/wx.go index abb940a..ea54093 100644 --- a/push/wx.go +++ b/push/wx.go @@ -1,7 +1,6 @@ package push import ( - "database/sql" "encoding/json" "fmt" "net/http" @@ -275,23 +274,19 @@ func handleTextPass(id, msg string) { */ func handleEventUseRequest(id, msg string) { user, err := model.FindWechatUser(id) - if err != nil { - if err == sql.ErrNoRows { - err := model.AddWechatUser(&model.WechatUser{ - OpenId: id, - Remark: "", - Status: 0, - LastRequestTime: time.Now().Unix(), - }) - if err != nil { - log.Errorln("添加用户出现错误" + err.Error()) - return - } - sendMsg(conf.GetConfig().Wechat.SuperOpenID, fmt.Sprintf("用户%v申请使用测试号,通过则回复信息:\n通过 %v\n\n拒绝则回复:\n拒绝 %v", id, id, id)) - } else { - log.Errorln("查询用户出现未知错误" + err.Error()) + if user.OpenId == "" { + + err := model.AddWechatUser(&model.WechatUser{ + OpenId: id, + Remark: "", + Status: 0, + LastRequestTime: time.Now().Unix(), + }) + if err != nil { + log.Errorln("添加用户出现错误" + err.Error()) return } + sendMsg(conf.GetConfig().Wechat.SuperOpenID, fmt.Sprintf("用户%v申请使用测试号,通过则回复信息:\n通过 %v\n\n拒绝则回复:\n拒绝 %v", id, id, id)) } else { if err != nil { @@ -627,6 +622,11 @@ func handleGetUser(id string, msg string) { } } + if message == "" { + log.Warningln("还未存在绑定的用户登录") + sendMsg(id, "你还没有已登陆的用户,请点击下方登录按钮登录!") + return + } sendMsg(id, message) } diff --git a/utils/stop/stop_other.go b/utils/stop/stop_other.go new file mode 100644 index 0000000..456911f --- /dev/null +++ b/utils/stop/stop_other.go @@ -0,0 +1,8 @@ +//go:build !windows +// +build !windows + +package stop + +func Stop() { + select {} +} diff --git a/utils/stop/stop_windows.go b/utils/stop/stop_windows.go new file mode 100644 index 0000000..2dbc55c --- /dev/null +++ b/utils/stop/stop_windows.go @@ -0,0 +1,66 @@ +//go:build windows + +package stop + +import ( + "fmt" + "github.com/getlantern/systray" + "github.com/johlanse/study_xxqg/cli" + "os" + "syscall" +) + +func Stop() { + cli.RunCli() + // systray.Run(onReady, onExit) +} + +func onReady() { + icon, _ := os.ReadFile("study.ico") + systray.SetIcon(icon) + systray.SetTitle("study xxqg") + systray.SetTooltip("study_xxqg") + mShow := systray.AddMenuItem("显示", "显示窗口") + mHide := systray.AddMenuItem("隐藏", "隐藏窗口") + systray.AddSeparator() + mQuit := systray.AddMenuItem("退出", "退出程序") + + kernel32 := syscall.NewLazyDLL("kernel32.dll") + user32 := syscall.NewLazyDLL("user32.dll") + // https://docs.microsoft.com/en-us/windows/console/getconsolewindow + getConsoleWindows := kernel32.NewProc("GetConsoleWindow") + // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindowasync + showWindowAsync := user32.NewProc("ShowWindowAsync") + consoleHandle, r2, err := getConsoleWindows.Call() + if consoleHandle == 0 { + fmt.Println("Error call GetConsoleWindow: ", consoleHandle, r2, err) + } + + go func() { + for { + select { + case <-mShow.ClickedCh: + mShow.Disable() + mHide.Enable() + r1, r2, err := showWindowAsync.Call(consoleHandle, 5) + if r1 != 1 { + fmt.Println("Error call ShowWindow @SW_SHOW: ", r1, r2, err) + } + case <-mHide.ClickedCh: + mHide.Disable() + mShow.Enable() + r1, r2, err := showWindowAsync.Call(consoleHandle, 0) + if r1 != 1 { + fmt.Println("Error call ShowWindow @SW_HIDE: ", r1, r2, err) + } + case <-mQuit.ClickedCh: + systray.Quit() + } + } + }() + +} + +func onExit() { + // clean up here +}