From cd3a0068d3574f8cf5440ac07ac5f864dc6c8cf7 Mon Sep 17 00:00:00 2001 From: duancheng Date: Mon, 25 Nov 2024 14:14:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yaml | 8 ++++- log/2024-11-15.log | 4 +++ main.go | 5 +-- service/http.go | 83 ++++++++++++++++++++++++++++++++++++++++++++++ service/mqtt.go | 62 ++++++++++++++++++++++++---------- vo/httpVo.go | 20 +++++++++++ 6 files changed, 162 insertions(+), 20 deletions(-) create mode 100644 log/2024-11-15.log create mode 100644 service/http.go create mode 100644 vo/httpVo.go diff --git a/config.yaml b/config.yaml index 26b5162..ac8e5c8 100644 --- a/config.yaml +++ b/config.yaml @@ -23,7 +23,13 @@ ysepay: notifyUrl: "http://xxxxxxxxxxxxx/ysePay/notifyPay" CERTID: "826452972730006" busCode: "00510030" #业务代码 -MQTT: +orderMQTT: + topic: "pay" + ip: "101.200.127.15" #测试环境IP + port: 1883 + user: "yuledui" + passwd: "yuledui" +driverMQTT: topic: "pay" ip: "101.200.127.15" #测试环境IP port: 1883 diff --git a/log/2024-11-15.log b/log/2024-11-15.log new file mode 100644 index 0000000..8864221 --- /dev/null +++ b/log/2024-11-15.log @@ -0,0 +1,4 @@ +2024-11-15 08:45:33.783 http.go:51: invalid character '<' looking for beginning of value +2024-11-15 08:45:33.784 mqtt.go:59: 订单MQTT连接地址: mqtt://101.200.127.15:1883 +2024-11-15 08:54:20.774 http.go:53: invalid character '<' looking for beginning of value +2024-11-15 08:54:20.774 mqtt.go:59: 订单MQTT连接地址: mqtt://101.200.127.15:1883 diff --git a/main.go b/main.go index 7979ad3..ccead95 100644 --- a/main.go +++ b/main.go @@ -7,8 +7,9 @@ import ( ) func main() { - - err := service.MQTTService.InitMQTT() //初始化连接订单MQTT + + service.CatService.SignCat() + err := service.MQTTService.InitOrderMQTT() //初始化连接订单MQTT if err != nil { fmt.Println(err) return diff --git a/service/http.go b/service/http.go new file mode 100644 index 0000000..97511ce --- /dev/null +++ b/service/http.go @@ -0,0 +1,83 @@ +package service + +import ( + "bytes" + "crypto/md5" + "encoding/json" + "fmt" + "io" + "math/rand" + "net/http" + "sort" + "strings" + "zhaoCaiMao/vo" + + "github.com/gogf/gf/v2/frame/g" +) + +type Cat struct { +} + +var CatService Cat +var letters = []rune("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") + +// 招财猫签到 +func (t *Cat) SignCat() error { + req := vo.SignReq{} + req.Vendor = "LANDI" + req.Nonce = randStr(22) + req.Sn = "LDNS3022890N637808" + req.SignMethod = "md5" + req.TerminalInfo = "测试请求" + req.Sign = t.sign(&req) + fmt.Println(req.Sign) + jsonBody, err := json.Marshal(&req) + if err != nil { + g.Log().Line().Print(nil, err) + return err + } + r, err := http.Post("https://rmb.pingan.com.cn:443", "application/json", bytes.NewBuffer(jsonBody)) + if err != nil { + g.Log().Line().Print(nil, err) + return err + } + defer r.Body.Close() + body, err := io.ReadAll(r.Body) + fmt.Println(string(body)) + if err != nil { + g.Log().Line().Print(nil, err) + return err + } + resp := vo.SignResp{} + err = json.Unmarshal(body, &resp) + if err != nil { + g.Log().Line().Print(nil, err) + return err + } + fmt.Println(resp) + return nil + +} +func (t *Cat) sign(req *vo.SignReq) string { + parts := []string{} + parts = append(parts, fmt.Sprintf("%s=%s", "nonce", req.Nonce)) + parts = append(parts, fmt.Sprintf("%s=%s", "signMethod", req.SignMethod)) + parts = append(parts, fmt.Sprintf("%s=%s", "sn", req.Sn)) + parts = append(parts, fmt.Sprintf("%s=%s", "terminalInfo", req.TerminalInfo)) + parts = append(parts, fmt.Sprintf("%s=%s", "vendor", req.Vendor)) + sort.Strings(parts) + data := strings.Join(parts, "&") + data += fmt.Sprintf("&key=(%s)", "49df1a070e754da391493bfef5139282") + fmt.Println(data) + hash := md5.New() + io.WriteString(hash, data) + md5Sum := hash.Sum(nil) + return fmt.Sprintf("%x", md5Sum) +} +func randStr(n int) string { + b := make([]rune, n) + for i := range b { + b[i] = letters[rand.Intn(len(letters))] + } + return string(b) +} diff --git a/service/mqtt.go b/service/mqtt.go index 6357511..181694e 100644 --- a/service/mqtt.go +++ b/service/mqtt.go @@ -1,10 +1,10 @@ package service import ( + "encoding/json" "errors" "fmt" "time" - "encoding/json" "zhaoCaiMao/vo" mqtt "github.com/eclipse/paho.mqtt.golang" @@ -18,9 +18,37 @@ type MQTT struct { var MQTTService MQTT -func (t *MQTT) InitMQTT() error { - ip := g.Cfg().MustGet(nil, "MQTT.ip") //订单MQTT - port := g.Cfg().MustGet(nil, "MQTT.port") +/* + func (t *MQTT) InitDriverMQTT() error { + ip := g.Cfg().MustGet(nil, "driverMQTT.ip") //订单MQTT + port := g.Cfg().MustGet(nil, "driverMQTT.port") + // 创建MQTT客户端选项 + var broker = server_ip + var port = server_port + opts := mqtt.NewClientOptions() // 创建MQTT客户端选项 + opts.AddBroker(fmt.Sprintf("tcp://%s:%d", broker, port)) // 添加MQTT broker地址和端口 + userName := "Signature" + "|" + AccessKey_ID + "|" + InstanceId + password := HmacSha1(AccessKey_Secret, ClientId) + opts.SetClientID(ClientId) // 设置客户端ID + opts.SetUsername(userName) // 设置用户名(用于MQTT的身份验证) + opts.SetPassword(string(Decode(password))) // 设置密码(用于MQTT的身份验证) + opts.SetDefaultPublishHandler(AliMessagePubHandler) // 设置默认的消息发布处理函数 + opts.SetAutoReconnect(true) //客户端与MQTT代理的连接断开时,是否自动尝试重新连接 + opts.SetMaxReconnectInterval(3) //客户端在两次连续重连尝试之间的最大时间间隔秒 + opts.OnConnect = AliConnectHandler // 设置连接成功的处理函数 + opts.OnConnectionLost = AliConnectLostHandler // 设置连接丢失的处理函数 + fmt.Println("opts:", opts) + alimc = mqtt.NewClient(opts) // 创建新的MQTT客户端实例 + if token := alimc.Connect(); token.Wait() && token.Error() != nil { // 尝试连接到MQTT broker + fmt.Println("-------------------------") + fmt.Println("链接异常:", token.Error().Error()) + panic(token.Error()) // 如果连接失败,则抛出异常 + } + } +*/ +func (t *MQTT) InitOrderMQTT() error { + ip := g.Cfg().MustGet(nil, "orderMQTT.ip") //订单MQTT + port := g.Cfg().MustGet(nil, "orderMQTT.port") /* user := g.Cfg().MustGet(nil, "MQTT.user") @@ -45,7 +73,7 @@ func (t *MQTT) InitMQTT() error { g.Log().Line().Error(nil, err) return err } - payTopic := g.Cfg().MustGet(nil, "MQTT.topic").String() + payTopic := g.Cfg().MustGet(nil, "orderMQTT.topic").String() //订阅支付订单消息 token = t.client.Subscribe(payTopic, 2, nil) if !token.Wait() { @@ -58,13 +86,12 @@ func (t *MQTT) reciveOrderMQTTHandler(client mqtt.Client, msg mqtt.Message) { fmt.Println(logMsg) g.Log().Line().Print(nil, logMsg) mqttOrder := vo.MQTTOrder{} - err := json.Unmarshal(msg.Payload(),&mqttOrder) + err := json.Unmarshal(msg.Payload(), &mqttOrder) if err != nil { g.Log().Line().Print(nil, err) - return + return } - t.createMsgToCat(&mqttOrder) - + //t.createMsgToCat(&mqttOrder) //to-----发送到招财猫设备 @@ -75,17 +102,18 @@ func (t *MQTT) connectLostHandler(client mqtt.Client, err error) { g.Log().Line().Print(nil, "订单MQTT 重新连接成功") token.Wait() } -//组织发送招财猫的报文 -func (t *MQTT) createMsgToCat(mqttOrder *vo.MQTTOrder) { + +/* +// 组织发送招财猫的报文 +func (t *MQTT) createMsgToCat(mqttOrder *vo.MQTTOrder) { mqttOrder. - mqttDevice := vo.MQTTDevice{} + mqttDevice := vo.MQTTDevice{} mqttDevice.msgId = mqttOrder.msgId - if mqttOrder.Channel == 1 { //微信渠道 + if mqttOrder.Channel == 1 { //微信渠道 mqttdevice.Type = 1 - }else { //支付宝渠道 + } else { //支付宝渠道 mqttdevice.Type = 2 } - - -} \ No newline at end of file +} +*/ diff --git a/vo/httpVo.go b/vo/httpVo.go new file mode 100644 index 0000000..90b3858 --- /dev/null +++ b/vo/httpVo.go @@ -0,0 +1,20 @@ +package vo + +type SignReq struct { + Vendor string `json:"vendor"` //厂商编号 + Sn string `json:"sn"` //机身号 + SignMethod string `json:"signMethod"` //签名方式,默认md5 + Nonce string `json:"nonce"` //随机数 + TerminalInfo string `json:"terminalInfo"` //设备信息 + Sign string `json:"sign"` //签名 +} +type SignResp struct { + Code string `json:"code"` //错误码 + Msg string `json:"msg"` //错误描述 + Host string `json:"host"` //服务器地址 + Port int `json:"Port"` //端口 + ClientId string `json:"clientId"` //客户端ID + Username string `json:"username"` //用户名 + Password string `json:"password"` //密码 + Topic string `json:"topic"` //订阅消息主题名 +}