package migumanage import ( "fmt" "github.com/gin-gonic/gin" "github.com/go-admin-team/go-admin-core/logger" "go-admin/app/admin/models" "go-admin/tools" "go-admin/tools/app" "net/http" "time" ) // 以下接口是回调接口 // SubscribeNotice 订购成功通知接口 // @Summary 订购成功通知接口 // @Tags 2024-咪咕-回调 // @Produce json // @Accept json // @Param request body models.NewSendDataReq true "订购成功通知接口" // @Success 200 {object} models.OppoSendDataResp // @Router /api/v1/notice/subscribe [get] func (e MiGuDeployService) SubscribeNotice(c *gin.Context) { fmt.Println("SubscribeNotice-start") logger.Info("订购成功通知-start") fmt.Println("URL:", c.Request.URL.String()) orderId := c.Query("orderId") // 对应linkId extData := c.Query("extData") // 对应outTradeNo status := c.Query("status") // status: 1:成功 不存在status也认为是订购成功通知 fmt.Println("orderId:", orderId) fmt.Println("extData:", extData) fmt.Println("status:", status) logger.Info(fmt.Sprintf("orderId is:%s, extData is:%s, status is:%s", orderId, extData, status)) if orderId == "" { // 没有返回linkId logger.Error("orderId is null") app.MiGuNoticeOK(c, "failed") return } if !(status == "1" || status == "") { logger.Error("status is", status) app.MiGuNoticeOK(c, fmt.Sprintf("failed,status is:%s", status)) return } err := e.MakeContext(c).MakeOrm().Errors if err != nil { fmt.Println("MakeContext err:", err) e.Logger.Error(err) app.MiGuNoticeOK(c, "failed") return } // 查询订单表是否有记录 var orderInfo models.MgOrder err = e.Orm.Table("mg_order").Where("external_order_id = ?", orderId).First(&orderInfo).Error if err != nil && err.Error() != "record not found" { logger.Errorf("SubscribeNotice query mg_order err:", err.Error()) app.MiGuNoticeOK(c, "failed") return } if err.Error() == "record not found" { // 订单表没有记录 if extData == "" { // 没有返回平台订单号 logger.Error("extData is null") app.MiGuNoticeOK(c, "failed") return } // 查询交易流水表 var logInfo models.MgTransactionLog err = e.Orm.Table("mg_transaction_log"). Where("out_trade_no = ? and verification_code <> ''", extData).First(&logInfo).Error if err != nil { logger.Errorf("SubscribeNotice query mg_transaction_log err:", err.Error()) app.MiGuNoticeOK(c, "failed") return } // 插入订单表 var inOrder models.MgOrder inOrder.CreatedAt = logInfo.CreatedAt inOrder.UpdatedAt = logInfo.UpdatedAt inOrder.ProductID = logInfo.ProductID inOrder.ChannelCode = logInfo.ChannelCode inOrder.OrderSerial = logInfo.OutTradeNo strTime := time.Now() inOrder.SubscribeTime = &strTime inOrder.PhoneNumber = logInfo.PhoneNumber inOrder.SM4PhoneNumber, _ = tools.SM4Encrypt(models.SM4KEy, logInfo.PhoneNumber) inOrder.ExternalOrderID = logInfo.LinkId inOrder.ChannelTradeNo = logInfo.ChannelTradeNo inOrder.State = models.SubscribeOK err = e.Orm.Create(&inOrder).Error if err != nil { logger.Info("Create MgOrder err:", err) app.MiGuError(c, http.StatusBadRequest, err, err.Error()) return } orderInfo.ChannelCode = inOrder.ChannelCode } else { // 订单表有记录 if orderInfo.State != 1 { err = e.Orm.Table("mg_order").Where("external_order_id = ?", orderId).Updates(map[string]interface{}{ "state": models.SubscribeOK, "subscribe_time": time.Now(), "updated_at": time.Now(), }).Error if err != nil { logger.Errorf("SubscribeNotice update mg_order err:", err.Error()) app.MiGuNoticeOK(c, "failed") return } } } // 判断是否为子渠道订购,是的话需要通知子渠道 if models.IsValidChannelEx(orderInfo.ChannelCode, e.Orm) { channelInfo, err := models.GetChannelInfoByChannelCode(orderInfo.ChannelCode, e.Orm) if err != nil { fmt.Println("SubscribeNotice GetChannelInfoByChannelCode err:", err) fmt.Println("SubscribeNotice sub_channel_code is:", orderInfo.ChannelCode) logger.Errorf("SubscribeNotice GetChannelInfoByChannelCode err, sub_channel_code is:", orderInfo.ChannelCode) } if channelInfo.SubscribeURL != "" { for i := 0; i < 3; i++ { resp, err := models.NoticeSubChannel(channelInfo.SubscribeURL, orderInfo.OrderSerial, orderInfo.ChannelTradeNo, "1") if err != nil { fmt.Println("NoticeSubChannel err:", err) fmt.Println("i is:", i) continue } if resp != "success" { continue } else { break } } } else { fmt.Println("SubscribeNotice SubscribeURL is null, sub_channel_code is:", orderInfo.ChannelCode) logger.Error("SubscribeNotice SubscribeURL is null, sub_channel_code is:", orderInfo.ChannelCode) } } fmt.Println("SubscribeNotice-end") logger.Info("订购成功通知-end") app.MiGuNoticeOK(c, "success") return } // UnsubscribeNotice 退订通知接口 // @Summary 退订通知接口 // @Tags 2024-咪咕-回调 // @Produce json // @Accept json // @Param request body models.NewSendDataReq true "退订通知接口" // @Success 200 {object} models.OppoSendDataResp // @Router /api/v1/notice/unsubscribe [get] func (e MiGuDeployService) UnsubscribeNotice(c *gin.Context) { fmt.Println("UnsubscribeNotice-start") logger.Info("退订通知-start") fmt.Println("URL:", c.Request.URL.String()) orderId := c.Query("orderId") // 对应linkId extData := c.Query("extData") // 对应outTradeNo status := c.Query("status") // status: 1:成功 不存在status也认为是订购成功通知 fmt.Println("orderId:", orderId) fmt.Println("extData:", extData) fmt.Println("status:", status) logger.Info(fmt.Sprintf("orderId is:%s, extData is:%s, status is:%s", orderId, extData, status)) if orderId == "" { // 没有返回linkId logger.Error("orderId is null") app.MiGuNoticeOK(c, "failed") return } if !(status == "2") { logger.Error("status is", status) app.MiGuNoticeOK(c, fmt.Sprintf("failed,status is:%s", status)) return } err := e.MakeContext(c).MakeOrm().Errors if err != nil { fmt.Println("MakeContext err:", err) e.Logger.Error(err) app.MiGuNoticeOK(c, "failed") return } // 查询是否有记录 var orderInfo models.MgOrder err = e.Orm.Table("mg_order").Where("external_order_id = ?", orderId).First(&orderInfo).Error if err != nil { logger.Errorf("UnsubscribeNotice query mg_order err:", err.Error()) app.MiGuNoticeOK(c, "failed") return } if orderInfo.State != 2 { // 订单不是退订状态才更新 err = e.Orm.Table("mg_order").Where("external_order_id = ?", orderId).Updates(map[string]interface{}{ "state": models.UnsubscribeOK, "unsubscribe_time": time.Now(), "updated_at": time.Now(), }).Error if err != nil { logger.Errorf("UnsubscribeNotice update mg_order err:", err.Error()) app.MiGuNoticeOK(c, "failed") return } } // 判断是否为子渠道订购,是的话需要通知子渠道 if models.IsValidChannelEx(orderInfo.ChannelCode, e.Orm) { channelInfo, err := models.GetChannelInfoByChannelCode(orderInfo.ChannelCode, e.Orm) if err != nil { fmt.Println("UnsubscribeNotice GetChannelInfoByChannelCode err:", err) fmt.Println("UnsubscribeNotice sub_channel_code is:", orderInfo.ChannelCode) logger.Errorf("UnsubscribeNotice GetChannelInfoByChannelCode err, sub_channel_code is:", orderInfo.ChannelCode) } if channelInfo.SubscribeURL != "" { for i := 0; i < 3; i++ { resp, err := models.NoticeSubChannel(channelInfo.SubscribeURL, orderInfo.OrderSerial, orderInfo.ChannelTradeNo, "2") if err != nil { continue } if resp != "success" { continue } else { break } } } else { fmt.Println("UnsubscribeNotice SubscribeURL is null, sub_channel_code is:", orderInfo.ChannelCode) logger.Error("UnsubscribeNotice SubscribeURL is null, sub_channel_code is:", orderInfo.ChannelCode) } } fmt.Println("UnsubscribeNotice-end") logger.Info("退订通知-end") app.MiGuNoticeOK(c, "success") return }