study_xxqg/push/wx.go

366 lines
7.7 KiB
Go
Raw Normal View History

2022-08-15 07:07:03 +00:00
package push
import (
2022-07-31 12:33:39 +00:00
"encoding/json"
"fmt"
"net/http"
2022-07-31 12:33:39 +00:00
"strings"
"sync"
"time"
2022-07-31 12:33:39 +00:00
"github.com/johlanse/wechat/mp"
"github.com/johlanse/wechat/mp/request"
log "github.com/sirupsen/logrus"
2022-08-03 10:04:52 +00:00
"github.com/johlanse/study_xxqg/conf"
"github.com/johlanse/study_xxqg/lib"
"github.com/johlanse/study_xxqg/model"
"github.com/johlanse/study_xxqg/utils"
"github.com/johlanse/study_xxqg/utils/update"
)
var (
2022-07-31 12:33:39 +00:00
wx *mp.WeiXin
lastNonce = ""
2022-08-15 07:07:03 +00:00
datas1 sync.Map
wxPush func(id, kind, message string)
)
2022-07-31 12:33:39 +00:00
const (
2022-08-15 07:07:03 +00:00
loginBtn = "loginBtn"
2022-07-31 12:33:39 +00:00
StartStudy = "start_study"
2022-08-12 01:32:37 +00:00
getUser = "get_user"
2022-07-31 12:33:39 +00:00
SCORE = "score"
checkUpdate = "check_update"
updateBtn = "updateBtn"
restart = "restart"
getOpenID = "get_open_id"
2022-07-31 12:33:39 +00:00
)
2022-08-12 01:32:37 +00:00
type WechatHandler func(id string)
var (
handlers sync.Map
)
func RegisterHandler(key string, action WechatHandler) {
handlers.Store(key, action)
}
2022-08-15 07:07:03 +00:00
func initWx() {
once := sync.Once{}
once.Do(initWechat)
}
2022-07-31 12:33:39 +00:00
func initWechat() {
config := conf.GetConfig()
2022-07-31 12:33:39 +00:00
if !config.Wechat.Enable {
return
}
2022-08-12 01:32:37 +00:00
if config.Wechat.SuperOpenID == "" {
log.Warningln("你还未配置super_open_id选项")
}
2022-08-12 01:32:37 +00:00
// 注册插件
2022-08-15 07:07:03 +00:00
RegisterHandler(loginBtn, handleLogin)
2022-08-12 01:32:37 +00:00
RegisterHandler(StartStudy, handleStartStudy)
RegisterHandler(getUser, handleGetUser)
RegisterHandler(SCORE, handleScore)
RegisterHandler(checkUpdate, handleCheckUpdate)
RegisterHandler(updateBtn, handleUpdate)
RegisterHandler(restart, handleRestart)
RegisterHandler(getOpenID, handleGetOpenID)
2022-08-12 01:32:37 +00:00
wx = mp.New(config.Wechat.Token, config.Wechat.AppID, config.Wechat.Secret, "123", "123")
2022-07-31 12:33:39 +00:00
err := wx.CreateMenu(&mp.Menu{Buttons: []mp.MenuButton{
{
Name: "登录",
Type: "click",
2022-08-15 07:07:03 +00:00
Key: loginBtn,
Url: "",
MediaId: "",
SubButtons: nil,
},
2022-07-31 12:33:39 +00:00
{
Name: "学习管理",
Type: "click",
Key: "study",
Url: "",
MediaId: "",
SubButtons: []mp.MenuButton{
{
2022-08-12 01:32:37 +00:00
Name: "开始学习",
Type: "click",
Key: StartStudy,
2022-07-31 12:33:39 +00:00
},
{
2022-08-12 01:32:37 +00:00
Name: "获取用户",
Type: "click",
Key: getUser,
2022-07-31 12:33:39 +00:00
},
{
2022-08-12 01:32:37 +00:00
Name: "积分查询",
Type: "click",
Key: SCORE,
2022-07-31 12:33:39 +00:00
},
},
},
{
2022-08-12 01:32:37 +00:00
Name: "关于",
Type: "click",
SubButtons: []mp.MenuButton{
{
2022-08-12 01:32:37 +00:00
Name: "检查更新",
Type: "click",
Key: checkUpdate,
},
{
2022-08-12 01:32:37 +00:00
Name: "重启程序",
Type: "click",
Key: restart,
},
{
2022-08-12 01:32:37 +00:00
Name: "更新程序",
Type: "click",
Key: updateBtn,
},
{
Name: "获取open_id",
Type: "click",
Key: getOpenID,
},
},
},
}})
2022-07-31 12:33:39 +00:00
if err != nil {
log.Errorln("设置自定义菜单出现异常" + err.Error())
return
}
wx.HandleFunc("eventCLICK", func(wx *mp.WeiXin, w http.ResponseWriter, r *request.WeiXinRequest, timestamp, nonce string) {
2022-07-31 12:33:39 +00:00
if lastNonce == nonce {
return
}
lastNonce = nonce
2022-08-12 01:32:37 +00:00
value, ok := handlers.Load(r.EventKey)
if !ok {
log.Warningln("未注册key为" + r.EventKey + "的调用方法")
return
2022-07-31 12:33:39 +00:00
}
2022-08-12 01:32:37 +00:00
go func() {
defer func() {
err := recover()
if err != nil {
log.Errorln("处理微信事件错误")
log.Errorln(err)
}
}()
(value.(WechatHandler))(r.FromUserName)
}()
})
}
func handleGetOpenID(id string) {
sendMsg(id, "你的open_id为"+id)
}
//
// handleCheckUpdate
// @Description: 检查更新
// @param id
//
func handleCheckUpdate(id string) {
about := utils.GetAbout()
sendMsg(id, about)
}
//
// handleUpdate
// @Description: 开始更新
// @param id
//
func handleUpdate(id string) {
if conf.GetConfig().Wechat.SuperOpenID != id {
sendMsg(id, "请联系管理员处理!")
return
}
update.SelfUpdate("", conf.GetVersion())
sendMsg(id, "检查更新已完成,即将重启程序")
utils.Restart()
}
//
// handleRestart
// @Description: 重启程序
// @param id
//
func handleRestart(id string) {
if conf.GetConfig().Wechat.SuperOpenID != id {
sendMsg(id, "请联系管理员处理!")
return
}
sendMsg(id, "即将重启程序")
utils.Restart()
}
//
// sendMsg
// @Description: 发送消息
// @param id
// @param message
//
2022-07-31 12:33:39 +00:00
func sendMsg(id, message string) {
m := map[string]interface{}{
"data": map[string]string{
"value": message,
},
}
data, _ := json.Marshal(m)
2022-08-15 07:07:03 +00:00
if wx == nil {
initWx()
}
2022-07-31 12:33:39 +00:00
_, err := wx.SendTemplateMessage(&mp.TemplateMessage{
ToUser: id,
TemplateId: conf.GetConfig().Wechat.NormalTempID,
URL: "",
TopColor: "",
RawJSONData: data,
})
if err != nil {
return
}
}
// HandleWechat
/* @Description:处理wechat的请求接口
2022-07-31 12:33:39 +00:00
* @param rep
* @param req
*/
func HandleWechat(rep http.ResponseWriter, req *http.Request) {
2022-08-12 01:46:33 +00:00
if wx == nil {
2022-08-15 07:07:03 +00:00
initWx()
2022-08-12 01:46:33 +00:00
}
wx.ServeHTTP(rep, req)
}
2022-07-31 12:33:39 +00:00
//
// handleLogin
// @Description: 用户登录
// @param id
//
2022-07-31 12:33:39 +00:00
func handleLogin(id string) {
2022-08-15 07:07:03 +00:00
core := &lib.Core{Push: func(id1 string, kind, message string) {
2022-07-31 12:33:39 +00:00
if kind == "flush" && strings.HasPrefix(message, "登录链接") {
l := strings.ReplaceAll(message, "登录链接:\r\n", "")
_, err := wx.SendTemplateMessage(&mp.TemplateMessage{
ToUser: id,
TemplateId: conf.GetConfig().Wechat.LoginTempID,
URL: l,
TopColor: "",
RawJSONData: nil,
})
if err != nil {
log.Errorln(err.Error())
return
}
}
}}
_, err := core.L(0, id)
if err != nil {
return
}
sendMsg(id, "登录成功")
}
//
// handleStartStudy
// @Description: 开始学习
// @param id
//
2022-07-31 12:33:39 +00:00
func handleStartStudy(id string) {
users, err := model.QueryByPushID(id)
if err != nil {
return
}
if users == nil {
log.Warningln("还未存在绑定的用户登录")
sendMsg(id, "你还没有已登陆的用户,请点击下方登录按钮登录!")
return
}
2022-08-15 07:07:03 +00:00
core := &lib.Core{ShowBrowser: conf.GetConfig().ShowBrowser, Push: func(id1 string, kind, msg string) {
2022-07-31 12:33:39 +00:00
}}
core.Init()
defer core.Quit()
for i, user := range users {
2022-08-15 07:07:03 +00:00
_, ok := datas1.Load(user.UID)
2022-07-31 12:33:39 +00:00
if ok {
log.Warningln("用户" + user.Nick + "已经在学习中了,跳过该用户")
continue
} else {
2022-08-15 07:07:03 +00:00
datas1.Store(user.UID, "")
2022-07-31 12:33:39 +00:00
}
sendMsg(id, fmt.Sprintf("开始学习第%d个用户用户名%v", i+1, user.Nick))
core.LearnArticle(user)
core.LearnVideo(user)
core.RespondDaily(user, "daily")
core.RespondDaily(user, "weekly")
core.RespondDaily(user, "special")
2022-08-15 07:07:03 +00:00
datas1.Delete(user.UID)
2022-07-31 12:33:39 +00:00
score, _ := lib.GetUserScore(user.ToCookies())
sendMsg(id, fmt.Sprintf("第%d个用户%v学习完成学习积分\n%v", i+1, user.Nick, lib.FormatScore(score)))
}
}
func handleGetUser(id string) {
users, err := model.Query()
if err != nil {
return
}
if users == nil {
log.Warningln("还未存在绑定的用户登录")
sendMsg(id, "你还没有已登陆的用户,请点击下方登录按钮登录!")
return
}
message := ""
config := conf.GetConfig()
2022-07-31 12:33:39 +00:00
for _, user := range users {
if config.Wechat.SuperOpenID == id {
message += fmt.Sprintf("%v ==> %v", user.Nick, time.Unix(user.LoginTime, 0).Format("2006-01-02"))
if user.PushId == id {
message += "(已绑定)\r\n"
}
} else {
if user.PushId == id {
message += fmt.Sprintf("%v ==> %v", user.Nick, time.Unix(user.LoginTime, 0).Format("2006-01-02"))
}
2022-07-31 12:33:39 +00:00
}
2022-07-31 12:33:39 +00:00
}
sendMsg(id, message)
}
func handleScore(id string) {
users, err := model.Query()
if err != nil {
return
}
if users == nil {
log.Warningln("还未存在绑定的用户登录")
sendMsg(id, "你还没有已登陆的用户,请点击下方登录按钮登录!")
return
}
config := conf.GetConfig()
2022-07-31 12:33:39 +00:00
for _, user := range users {
score, _ := lib.GetUserScore(user.ToCookies())
if config.Wechat.SuperOpenID == id {
sendMsg(id, "用户:"+user.Nick+"\n"+lib.FormatScore(score))
} else {
if user.PushId == id {
sendMsg(id, "用户:"+user.Nick+"\n"+lib.FormatScore(score))
}
}
2022-07-31 12:33:39 +00:00
}
}