Skip to content

Commit

Permalink
添加微信公众号支持
Browse files Browse the repository at this point in the history
  • Loading branch information
johlanse committed Jul 31, 2022
1 parent f73ec27 commit f0a0da6
Show file tree
Hide file tree
Showing 12 changed files with 385 additions and 78 deletions.
10 changes: 7 additions & 3 deletions conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,13 @@ type Config struct {
} `json:"retry" yaml:"retry"`

Wechat struct {
Token string `json:"token" yaml:"token"`
Secret string `json:"secret" yaml:"secret"`
AppID string `json:"app_id" yaml:"app_id"`
Enable bool `json:"enable" yaml:"enable"`
Token string `json:"token" yaml:"token"`
Secret string `json:"secret" yaml:"secret"`
AppID string `json:"app_id" yaml:"app_id"`
LoginTempID string `json:"login_temp_id" yaml:"login_temp_id"`
NormalTempID string `json:"normal_temp_id" yaml:"normal_temp_id"`
PushLoginWarn bool `json:"push_login_warn" yaml:"push_login_warn"`
} `json:"wechat" yaml:"wechat"`
// 专项答题可接受的最小值
SpecialMinScore int `json:"special_min_score" yaml:"special_min_score"`
Expand Down
19 changes: 19 additions & 0 deletions conf/config_default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ web:
# 网页端登录密码
password: admin

# 微信公众号测试号配置
wechat:
# 是否启用
enable: false
# 开发者平台设置的token
token: ""
# 开发者平台的secret
secret: ""
# 开发者平台的appId
app_id: ""
# 发送登录消息需要使用的消息模板
# 模板标题,随意 模板内容: 点我登录,然后在浏览器中打开!!
login_temp_id: ""
# 发送普通消息需要使用的消息模板
# 模板标题:随意 模板内容: {{data.DATA}}
normal_temp_id: "-6Q"
# xxqg会每隔两小时左右检查所有用户的ck有效性,若开启该选项,会在检查失败时推送提醒消息
push_login_warn: false

# 登录重试配置
retry:
# 重试次数
Expand Down
29 changes: 29 additions & 0 deletions docs/push.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
## 推送配置

### 微信公众号推送
配置config.yml的如下部分
```yaml
# 微信公众号测试号配置
wechat:
# 是否启用
enable: false
# 开发者平台设置的token
token: ""
# 开发者平台的secret
secret: ""
# 开发者平台的appId
app_id: ""
# 发送登录消息需要使用的消息模板
# 模板标题,随意 模板内容: 点我登录,然后在浏览器中打开!!
login_temp_id: ""
# 发送普通消息需要使用的消息模板
# 模板标题:随意 模板内容: {{data.DATA}}
normal_temp_id: ""
# xxqg会每隔两小时左右检查所有用户的ck有效性,若开启该选项,会在检查失败时推送提醒消息
push_login_warn: false
```
+ 前往微信[公众号开发者平台](http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login),手机微信扫码登录
+ 配置url为**http:ip:port/wx**,ip和端口在web项配置中配置
+ 设置token
+ 分别添加登录模板消息和普通模板消息,添加要求查看配置项注释
+ 在配置文件中配置所有内容,启动程序
### web推送
> 适用于部署在服务器上或者家里有公网IP的设备上
Expand Down
9 changes: 3 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ require (
github.com/google/uuid v1.3.0
github.com/guonaihong/gout v0.2.9
github.com/imroc/req/v3 v3.8.2
github.com/johlanse/wechat v0.0.0-20220731103216-3c9e7b56434f
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0
github.com/klauspost/compress v1.15.5
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
github.com/makiuchi-d/gozxing v0.1.1
github.com/mxschmitt/playwright-go v0.1400.0
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/robfig/cron/v3 v3.0.0
github.com/sirupsen/logrus v1.8.1
github.com/sirupsen/logrus v1.9.0
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816
github.com/tidwall/gjson v1.12.1
Expand All @@ -29,7 +30,6 @@ require (
)

require (
github.com/beevik/etree v1.1.0 // indirect
github.com/codegangsta/negroni v1.0.0 // indirect
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
Expand All @@ -44,7 +44,6 @@ require (
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/huoxue1/lorca v0.1.11 // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/json-iterator/go v1.1.10 // indirect
github.com/julienschmidt/httprouter v1.3.0 // indirect
Expand All @@ -58,8 +57,6 @@ require (
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
github.com/rosbit/go-wget v1.2.5 // indirect
github.com/rosbit/go-wx-api v0.5.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/ugorji/go/codec v1.2.5 // indirect
Expand All @@ -68,7 +65,7 @@ require (
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
golang.org/x/mod v0.3.0 // indirect
golang.org/x/net v0.0.0-20220111093109-d55c255bac03 // indirect
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.0.0-20201124115921-2c860bdd6e78 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
Expand Down
19 changes: 10 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f h1:2dk3eOnYllh+wUOuDhOoC2vUVoJF/5z478ryJ+wzEII=
github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f/go.mod h1:4a58ifQTEe2uwwsaqbh3i2un5/CBPg+At/qHpt18Tmk=
github.com/beevik/etree v1.1.0 h1:T0xke/WvNtMoCqgzPhkX2r4rjY3GDZFi+FjpRZY2Jbs=
github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A=
github.com/codegangsta/negroni v1.0.0 h1:+aYywywx4bnKXWvoWtRfJ91vC59NbEhEY03sZjQhbVY=
github.com/codegangsta/negroni v1.0.0/go.mod h1:v0y3T5G7Y1UlFfyxFn/QLRU4a2EuNau2iZY63YTKWo0=
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ=
Expand Down Expand Up @@ -55,10 +53,14 @@ github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/U
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/huoxue1/lorca v0.1.11 h1:B5x4Xta8rauGCERoCHY7nWg2z1GUrksf9ycZGHwLs4Q=
github.com/huoxue1/lorca v0.1.11/go.mod h1:eEI3aGkmMladREqFJwUhd8cLlSUgG13wvuNprVjaVaY=
github.com/imroc/req/v3 v3.8.2 h1:wFZ7B0dclCQyjClP5GwXRboUGIek5l0mCpodrGgT01c=
github.com/imroc/req/v3 v3.8.2/go.mod h1:3JIicOKEDHfCSYYNLb/ObZNpx64EV5y40VlHMwhUCzU=
github.com/johlanse/wechat v0.0.0-20220731101603-68047d1e93e2 h1:hbe3Fbjc+1Ne+fUzxRlxYCzZOi9kzwhacys9D1tXreo=
github.com/johlanse/wechat v0.0.0-20220731101603-68047d1e93e2/go.mod h1:Dwcvo5MQU6u7XKoClpxoUlJMBAJhES9Qz4d2TSSBrU8=
github.com/johlanse/wechat v0.0.0-20220731102434-854afb866cf2 h1:57tGW/IDYuqtbHZSXuVFRkwTRD6dmcOL/M86yK2MyTA=
github.com/johlanse/wechat v0.0.0-20220731102434-854afb866cf2/go.mod h1:Dwcvo5MQU6u7XKoClpxoUlJMBAJhES9Qz4d2TSSBrU8=
github.com/johlanse/wechat v0.0.0-20220731103216-3c9e7b56434f h1:wpe87qm/nBbQ8BT+mrYvEHnXRbn11+2iSYpjrj62QAQ=
github.com/johlanse/wechat v0.0.0-20220731103216-3c9e7b56434f/go.mod h1:dLGDxcVd4CFRQInD2S2aMm4CGobWAFixOEgFxXZJ6Sw=
github.com/jonboulle/clockwork v0.2.2 h1:UOGuzwb1PwsrDAObMuhUnj0p5ULPj8V/xJ7Kx9qUBdQ=
github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand Down Expand Up @@ -115,13 +117,11 @@ github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6O
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E=
github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rosbit/go-wget v1.2.5 h1:XQFUwTJR8HDwYvxYH8X0cq3BkN1BOMoADc92KKp9Pcs=
github.com/rosbit/go-wget v1.2.5/go.mod h1:8Tt92GKM/K9WI65yYkX0lwJDnRp64Vd082ojViWT6T4=
github.com/rosbit/go-wx-api v0.5.0 h1:aAjTHCd2o3b14O0JYx2b+qzbZh8ThlKWMD+QsQEK9vQ=
github.com/rosbit/go-wx-api v0.5.0/go.mod h1:+HyYx6Avz1sPXTN+JBGyW1mf/jToDkXoNZXHX7S2F8I=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down Expand Up @@ -163,7 +163,6 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200222125558-5a598a2470a0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220111093109-d55c255bac03 h1:0FB83qp0AzVJm+0wcIlauAjJ+tNdh7jLuacRYCIVv7s=
Expand All @@ -186,6 +185,8 @@ golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 h1:rm+CHSpPEEW2IsXUib1ThaHIjuBVZjxNgSKmBLFfD4c=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
Expand Down
19 changes: 15 additions & 4 deletions lib/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (c *Core) GenerateCode() (string, string, error) {
return codeURL, g.Result, err
}

func (c *Core) CheckQrCode(code string) (*model.User, bool, error) {
func (c *Core) CheckQrCode(code, pushID string) (*model.User, bool, error) {
client := req.C()
client.OnAfterResponse(func(client *req.Client, response *req.Response) error {
return nil
Expand Down Expand Up @@ -218,6 +218,7 @@ func (c *Core) CheckQrCode(code string) (*model.User, bool, error) {
UID: uid,
Token: response.Cookies()[0].Value,
LoginTime: time.Now().Unix(),
PushId: pushID,
}
err = model.AddUser(user)
if err != nil {
Expand All @@ -235,7 +236,7 @@ func (c *Core) CheckQrCode(code string) (*model.User, bool, error) {
* @return *model.User
* @return error
*/
func (c *Core) L(retryTimes int) (*model.User, error) {
func (c *Core) L(retryTimes int, pushID string) (*model.User, error) {
_, codeData, err := c.GenerateCode()
if err != nil {
return nil, err
Expand All @@ -247,7 +248,7 @@ func (c *Core) L(retryTimes int) (*model.User, error) {
client.SetCommonHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36")

for i := 0; i < 150; i++ {
user, b, err := c.CheckQrCode(codeData)
user, b, err := c.CheckQrCode(codeData, pushID)
if b && err == nil {
return user, err
}
Expand All @@ -258,10 +259,20 @@ func (c *Core) L(retryTimes int) (*model.User, error) {
// 等待几分钟后重新执行
time.Sleep(time.Duration(conf.GetConfig().Retry.Intervals) * time.Minute)
c.Push("text", fmt.Sprintf("登录超时,将进行第%d重新次登录", retryTimes))
return c.L(retryTimes - 1)
return c.L(retryTimes-1, pushID)
}

func (c *Core) initWindows() {
_, err := os.Stat("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe")
if err != nil {
if os.IsNotExist(err) && conf.GetConfig().EdgePath == "" {
log.Warningln("检测到edge浏览器不存在并且未配置edge_path,将再次运行时自动下载chrome浏览器")
c.initNotWindows()
return
}
err = nil
}

dir, err := os.Getwd()
if err != nil {
return
Expand Down
27 changes: 0 additions & 27 deletions lib/core_test.go
Original file line number Diff line number Diff line change
@@ -1,28 +1 @@
package lib

import (
"fmt"
"testing"
)

func TestName(t *testing.T) {
core := Core{}
core.Init()
cookies, err := core.Login()
if err != nil {
return
}
score, err := GetUserScore(cookies)
if err != nil {
return
}
fmt.Println(score)
}

func TestLogin(t *testing.T) {
core := Core{}
core.Push = func(kind string, message string) {
fmt.Println(message)
}
core.L()
}
2 changes: 1 addition & 1 deletion lib/tg.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func login(bot *Telegram, args []string) {
}
core.Init()
defer core.Quit()
_, err := core.L(config.Retry.Times)
_, err := core.L(config.Retry.Times, "")
if err != nil {
bot.SendMsg(err.Error())
return
Expand Down
27 changes: 15 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,22 @@ func main() {
os.Exit(1)
}

if config.Web.Enable {
engine := web.RouterInit()
go func() {
h := http.NewServeMux()
engine := web.RouterInit()
go func() {
h := http.NewServeMux()
if config.Web.Enable {
h.Handle("/", engine)
//h.HandleFunc("/wx", web.HandleWechat)
}
if config.Wechat.Enable {
h.HandleFunc("/wx", web.HandleWechat)
}
if config.Web.Enable || config.Wechat.Enable {
err := http.ListenAndServe(fmt.Sprintf("%s:%d", config.Web.Host, config.Web.Port), h)
if err != nil {
return
}
}()
}
}
}()

if config.StartWait > 0 {
log.Infoln(fmt.Sprintf("将等待%d秒后启动程序", config.StartWait))
Expand Down Expand Up @@ -159,7 +163,7 @@ func main() {
}()
}

if !config.TG.Enable && config.Cron == "" {
if !config.TG.Enable && config.Cron == "" && !config.Wechat.Enable {
log.Infoln("已采用普通学习模式")
do("normal")
} else {
Expand Down Expand Up @@ -190,7 +194,6 @@ func do(m string) {
var user *model.User
users, _ := model.Query()
study := func(core2 *lib.Core, u *model.User) {

defer func() {
err := recover()
if err != nil {
Expand Down Expand Up @@ -229,7 +232,7 @@ func do(m string) {
// 用户小于1时自动登录
if len(users) < 1 {
log.Infoln("未检测到有效用户信息,将采用登录模式")
u, err := core.L(config.Retry.Times)
u, err := core.L(config.Retry.Times, "")
if err != nil {
log.Errorln(err.Error())
return
Expand All @@ -242,7 +245,7 @@ func do(m string) {
study(core, u)
}
if len(users) < 1 {
user, err := core.L(config.Retry.Times)
user, err := core.L(config.Retry.Times, "")
if err != nil {
core.Push("msg", "登录超时")
return
Expand Down Expand Up @@ -278,7 +281,7 @@ func do(m string) {
}

if i == 0 {
u, err := core.L(config.Retry.Times)
u, err := core.L(config.Retry.Times, "")
if err != nil {
log.Errorln(err.Error())
return
Expand Down
1 change: 1 addition & 0 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func init() {
log.Errorln("用户数据库打开失败,请检查config目录权限")
log.Panicln(err.Error())
}

_, _ = db.Exec(`create table user
(
nick TEXT,
Expand Down
Loading

0 comments on commit f0a0da6

Please sign in to comment.