feat: 集成微信收款服务

This commit is contained in:
RockYang
2023-07-20 17:46:32 +08:00
parent 86bc063941
commit c4d7126c4d
9 changed files with 262 additions and 51 deletions

View File

@@ -0,0 +1,54 @@
package wexin
import (
"chatplus/store/model"
"github.com/eatmoreapple/openwechat"
"github.com/skip2/go-qrcode"
"gorm.io/gorm"
)
// MessageHandler 消息处理
func MessageHandler(msg *openwechat.Message, db *gorm.DB) {
sender, err := msg.Sender()
if err != nil {
return
}
// 只处理微信支付的推送消息
if sender.NickName == "微信支付" ||
msg.MsgType == openwechat.MsgTypeApp ||
msg.AppMsgType == openwechat.AppMsgTypeUrl {
// 解析支付金额
message, err := parseTransactionMessage(msg.Content)
if err == nil {
transaction := extractTransaction(message)
logger.Infof("解析到收款信息:%+v", transaction)
if transaction.Amount <= 0 {
return
}
var item model.Reward
res := db.Where("tx_id = ?", transaction.TransId).First(&item)
if res.Error == nil {
logger.Infof("当前交易 ID %s 己经存在!", transaction.TransId)
return
}
res = db.Create(&model.Reward{
TxId: transaction.TransId,
Amount: transaction.Amount,
Remark: transaction.Remark,
Status: false,
})
if res.Error != nil {
logger.Errorf("交易保存失败ID: %s", transaction.TransId)
}
}
}
}
// QrCodeCallBack 登录扫码回调,
func QrCodeCallBack(uuid string) {
logger.Info("请使用微信扫描下面二维码登录")
q, _ := qrcode.New("https://login.weixin.qq.com/l/"+uuid, qrcode.Medium)
logger.Info(q.ToString(true))
}

View File

@@ -0,0 +1,68 @@
package wexin
import (
"encoding/xml"
"strconv"
"strings"
)
// Message 转账消息
type Message struct {
XMLName xml.Name `xml:"msg"`
AppMsg struct {
Des string `xml:"des"`
Url string `xml:"url"`
} `xml:"appmsg"`
}
// Transaction 解析后的交易信息
type Transaction struct {
TransId string `json:"trans_id"` // 微信转账交易 ID
Amount float64 `json:"amount"` // 微信转账交易金额
Remark string `json:"remark"` // 转账备注
}
// 解析微信转账消息
func parseTransactionMessage(xmlData string) (*Message, error) {
var msg Message
if err := xml.Unmarshal([]byte(xmlData), &msg); err != nil {
return nil, err
}
return &msg, nil
}
// 导出交易信息
func extractTransaction(message *Message) Transaction {
var tx = Transaction{}
// 导出交易金额和备注
lines := strings.Split(message.AppMsg.Des, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if len(line) == 0 {
continue
}
// 解析收款金额
prefix := "收款金额¥"
if strings.HasPrefix(line, prefix) {
if value, err := strconv.ParseFloat(line[len(prefix):], 64); err == nil {
tx.Amount = value
continue
}
}
// 解析收款备注
prefix = "付款方备注"
if strings.HasPrefix(line, prefix) {
tx.Remark = line[len(prefix):]
break
}
}
// 解析交易 ID
index := strings.Index(message.AppMsg.Url, "trans_id=")
if index != -1 {
end := strings.LastIndex(message.AppMsg.Url, "&")
tx.TransId = strings.TrimSpace(message.AppMsg.Url[index+9 : end])
}
return tx
}

View File

@@ -0,0 +1,42 @@
package wexin
import (
logger2 "chatplus/logger"
"github.com/eatmoreapple/openwechat"
"gorm.io/gorm"
)
// 微信收款机器人服务
var logger = logger2.GetLogger()
type WeChatBot struct {
bot *openwechat.Bot
db *gorm.DB
}
func NewWeChatBot(db *gorm.DB) *WeChatBot {
bot := openwechat.DefaultBot(openwechat.Desktop)
// 注册消息处理函数
bot.MessageHandler = func(msg *openwechat.Message) {
MessageHandler(msg, db)
}
// 注册登陆二维码回调
bot.UUIDCallback = QrCodeCallBack
return &WeChatBot{
bot: bot,
db: db,
}
}
func (b *WeChatBot) Login() error {
// 创建热存储容器对象
reloadStorage := openwechat.NewJsonFileHotReloadStorage("storage.json")
// 执行热登录
err := b.bot.HotLogin(reloadStorage)
if err != nil {
logger.Error("login error: %v", err)
return b.bot.Login()
}
logger.Info("微信登录成功!")
return nil
}