This commit is contained in:
duancheng 2024-11-14 10:07:39 +08:00
commit 4640e095b8
10 changed files with 290 additions and 0 deletions

31
config.yaml Normal file
View File

@ -0,0 +1,31 @@
server:
address: ":8001"
openapiPath: "/api.json"
swaggerPath: "/swagger"
logger:
path: "log"
level: "all"
stdout: false
#rotateExpire: 1
#rotateBackupLimit: 1
database:
logger:
path: "log/sqlLog"
level: "all"
stdout: true
default:
link: "pgsql:root:root@tcp(101.200.127.15:5431)/yuledui"
debug: true
weixin:
appId: "wx5078c9d7b9eca030" #"wx231bd3f08954da9e" #微信小程序appid
ysepay:
url: "http://127.0.0.1:8080/pay"
notifyUrl: "http://xxxxxxxxxxxxx/ysePay/notifyPay"
CERTID: "826452972730006"
busCode: "00510030" #业务代码
MQTT:
topic: "pay"
ip: "101.200.127.15" #测试环境IP
port: 1883
user: "yuledui"
passwd: "yuledui"

30
go.mod Normal file
View File

@ -0,0 +1,30 @@
module zhaoCaiMao
go 1.22.3
require (
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/clbanning/mxj/v2 v2.7.0 // indirect
github.com/eclipse/paho.mqtt.golang v1.5.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogf/gf/v2 v2.7.4 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/grokify/html-strip-tags-go v0.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
go.opentelemetry.io/otel v1.14.0 // indirect
go.opentelemetry.io/otel/sdk v1.14.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

53
go.sum Normal file
View File

@ -0,0 +1,53 @@
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/clbanning/mxj/v2 v2.7.0 h1:WA/La7UGCanFe5NpHF0Q3DNtnCsVoxbPKuyBNHWRyME=
github.com/clbanning/mxj/v2 v2.7.0/go.mod h1:hNiWqW14h+kc+MdF9C6/YoRfjEJoR3ou6tn/Qo+ve2s=
github.com/eclipse/paho.mqtt.golang v1.5.0 h1:EH+bUVJNgttidWFkLLVKaQPGmkTUfQQqjOsyvMGvD6o=
github.com/eclipse/paho.mqtt.golang v1.5.0/go.mod h1:du/2qNQVqJf/Sqs4MEL77kR8QTqANF7XU7Fk0aOTAgk=
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/gogf/gf/v2 v2.7.4 h1:cGHUBO5Jr8ty21GN5EO+S2rFYhprdcqnwS7PnWL7+t4=
github.com/gogf/gf/v2 v2.7.4/go.mod h1:EBXneAg/wes86rfeh68XC0a2JBNQylmT7Sp6/8Axk88=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grokify/html-strip-tags-go v0.1.0 h1:03UrQLjAny8xci+R+qjCce/MYnpNXCtgzltlQbOBae4=
github.com/grokify/html-strip-tags-go v0.1.0/go.mod h1:ZdzgfHEzAfz9X6Xe5eBLVblWIxXfYSQ40S/VKrAOGpc=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
go.opentelemetry.io/otel v1.14.0 h1:/79Huy8wbf5DnIPhemGB+zEPVwnN6fuQybr/SRXa6hM=
go.opentelemetry.io/otel v1.14.0/go.mod h1:o4buv+dJzx8rohcUeRmWUZhqupFvzWis188WlggnNeU=
go.opentelemetry.io/otel/sdk v1.14.0 h1:PDCppFRDq8A1jL9v6KMI6dYesaq+DFcDZvjsoGvxGzY=
go.opentelemetry.io/otel/sdk v1.14.0/go.mod h1:bwIC5TjrNG6QDCHNWvW4HLHtUQ4I+VQDsnjhvyZCALM=
go.opentelemetry.io/otel/trace v1.14.0 h1:wp2Mmvj41tDsyAJXiWDWpfNsOiIyd38fy85pyKcFq/M=
go.opentelemetry.io/otel/trace v1.14.0/go.mod h1:8avnQLK+CG77yNLUae4ea2JDQ6iT+gozhnZjy/rw9G8=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

12
log/2024-11-13.log Normal file
View File

@ -0,0 +1,12 @@
2024-11-13 14:41:57.736 mqtt.go:29: 订单MQTT连接地址: mqtt://101.200.127.15:1883
2024-11-13 14:45:12.488 mqtt.go:29: 订单MQTT连接地址: mqtt://101.200.127.15:1883
2024-11-13 14:46:31.681 mqtt.go:29: 订单MQTT连接地址: mqtt://101.200.127.15:1883
2024-11-13 14:50:46.964 mqtt.go:29: 订单MQTT连接地址: mqtt://101.200.127.15:1883
2024-11-13 15:15:01.049 mqtt.go:29: 订单MQTT连接地址: mqtt://101.200.127.15:1883
2024-11-13 15:15:17.725 mqtt.go:29: 订单MQTT连接地址: mqtt://101.200.127.15:1883
2024-11-13 16:00:22.037 mqtt.go:29: 订单MQTT连接地址: mqtt://101.200.127.15:1883
2024-11-13 16:11:09.299 mqtt.go:29: 订单MQTT连接地址: mqtt://101.200.127.15:1883
2024-11-13 16:13:25.523 mqtt.go:29: 订单MQTT连接地址: mqtt://101.200.127.15:1883
2024-11-13 16:13:30.792 mqtt.go:57: Received message: {"topic":"pay","msgId":"pyacyq0e340d5kwf8i7patsb004f0bjw","orderId":"pyacyq0h680d5gm4478bes82004m352r","orderEndTime":1712331159,"price":"65.3","amount":"65.2","bean":"0.1","shopId":"aaaaaaaaaa","openId":"aaaaaaaaaaaaaaaaabbbbbbbb","channel":1,"mobile":"15963264270","sendTime":1731485610} from topic: pay
2024-11-13 16:14:25.865 mqtt.go:29: 订单MQTT连接地址: mqtt://101.200.127.15:1883

19
log/2024-11-14.log Normal file
View File

@ -0,0 +1,19 @@
2024-11-14 09:19:28.834 mqtt.go:29: 订单MQTT连接地址: mqtt://101.200.127.15:1883
2024-11-14 09:21:14.113 mqtt.go:29: 订单MQTT连接地址: mqtt://101.200.127.15:1883
2024-11-14 09:21:47.784 mqtt.go:57: Received message: {
"topic": "pay",
"msgId": "pyacyq0e340d5kvuyeignq0700ocx8hj",
"orderId": "pyacyq0h680d5gm4478bes82004m352r",
"orderEndTime": 1712331159,
"price": "65.3",
"amount": "65.2",
"bean": "0.1",
"shopId": "aaaaaaaaaa",
"openId": "ccccccccccccc",
"channel": 1,
"mobile": "15963264270",
"sendTime": 1731484021
} from topic: pay
2024-11-14 09:22:02.872 mqtt.go:29: 订单MQTT连接地址: mqtt://101.200.127.15:1883
2024-11-14 09:38:10.793 mqtt.go:31: 订单MQTT连接地址: mqtt://101.200.127.15:1883

21
main.go Normal file
View File

@ -0,0 +1,21 @@
package main
import (
"fmt"
"time"
"zhaoCaiMao/service"
)
func main() {
err := service.MQTTService.InitMQTT() //初始化连接订单MQTT
if err != nil {
fmt.Println(err)
return
}
fmt.Println("初始化完成,启动成功")
for {
time.Sleep(time.Minute)
}
}

91
service/mqtt.go Normal file
View File

@ -0,0 +1,91 @@
package service
import (
"errors"
"fmt"
"time"
"encoding/json"
"zhaoCaiMao/vo"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/guid"
)
type MQTT struct {
client mqtt.Client
}
var MQTTService MQTT
func (t *MQTT) InitMQTT() error {
ip := g.Cfg().MustGet(nil, "MQTT.ip") //订单MQTT
port := g.Cfg().MustGet(nil, "MQTT.port")
/*
user := g.Cfg().MustGet(nil, "MQTT.user")
passwd := g.Cfg().MustGet(nil, "MQTT.passwd")
*/
opts := mqtt.NewClientOptions()
mqttAddress := fmt.Sprintf("mqtt://%s:%d", ip.String(), port.Int())
g.Log().Line().Print(nil, "订单MQTT连接地址:", mqttAddress)
opts.AddBroker(mqttAddress)
opts.SetKeepAlive(time.Hour * 24)
opts.SetPingTimeout(time.Second * 30)
opts.SetClientID(guid.S())
//opts.SetUsername(user.String()) //使用用户名和密码
//opts.SetPassword(passwd.String())
opts.SetDefaultPublishHandler(t.reciveOrderMQTTHandler) // 设置消息回调处理函数
//opts.OnConnect = connectHandler
opts.OnConnectionLost = t.connectLostHandler
t.client = mqtt.NewClient(opts)
token := t.client.Connect()
if token.Wait() && token.Error() != nil {
err := errors.New("订单MQTT初始化失败:" + token.Error().Error())
g.Log().Line().Error(nil, err)
return err
}
payTopic := g.Cfg().MustGet(nil, "MQTT.topic").String()
//订阅支付订单消息
token = t.client.Subscribe(payTopic, 2, nil)
if !token.Wait() {
return token.Error()
}
return nil
}
func (t *MQTT) reciveOrderMQTTHandler(client mqtt.Client, msg mqtt.Message) {
logMsg := fmt.Sprintf("Received message: %s from topic: %s\n", msg.Payload(), msg.Topic())
fmt.Println(logMsg)
g.Log().Line().Print(nil, logMsg)
mqttOrder := vo.MQTTOrder{}
err := json.Unmarshal(msg.Payload(),&mqttOrder)
if err != nil {
g.Log().Line().Print(nil, err)
return
}
t.createMsgToCat(&mqttOrder)
//to-----发送到招财猫设备
}
func (t *MQTT) connectLostHandler(client mqtt.Client, err error) {
token := t.client.Connect()
g.Log().Line().Print(nil, "订单MQTT 重新连接成功")
token.Wait()
}
//组织发送招财猫的报文
func (t *MQTT) createMsgToCat(mqttOrder *vo.MQTTOrder) {
mqttOrder.
mqttDevice := vo.MQTTDevice{}
mqttDevice.msgId = mqttOrder.msgId
if mqttOrder.Channel == 1 { //微信渠道
mqttdevice.Type = 1
}else { //支付宝渠道
mqttdevice.Type = 2
}
}

27
vo/mqttVo.go Normal file
View File

@ -0,0 +1,27 @@
package vo
//从订单MQTT中获取的消息
type MQTTOrder struct {
Topic string `json:"topic"` //主题
MsgId string `json:"msgId"` //消息ID
OrderId string `json:"orderId"` //订单号
OrderEndTime int64 `json:"orderEndTime"` //支付完成时间unix时间戳
Price string `json:"price"` //订单原价小数点后6位
Amount string `json:"amount"` //支付金额小数点后6位
Bean string `json:"bean"` //抵扣金豆
ShopId string `json:"shopId"` //店铺ID
OpenId string `json:"openId"` //微信或支付宝用户Id
Channel int `json:"channel"` //1微信2支付宝
Mobile string `json:"mobile"` //付款用户手机号
SendTime int64 `json:"sendTime"` //发送消息时间,unix时间戳
}
//发送到机器猫的MQTT消息
type MQTTDevice struct {
MsgId string `json:"msgId"` //消息编号
Type string `json:"type"` //1支付宝 2微信 3银联云闪付 99文字转语音播 100测试播报语音
Template string `json:"template"` //tts语音播板
Amount string `json:"amount"` //播报金额
Nonce string `json:"nonce"` //随机数
Sign string `json:"sign"` //签名
}

6
招财猫.txt Normal file
View File

@ -0,0 +1,6 @@
服务器名称: 阿里云
服务器地址: mqtt-cn-oew201vkk05.mqtt.aliyuncs.com
服务器端口: 1883
实例IDmqtt-cn-oew201vkk05
Topic: payment
Group IDGID_payment