migu_music_server/app/admin/apis/migumanage/migu.go
2025-03-28 14:07:20 +08:00

302 lines
8.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package migumanage
import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/go-admin-team/go-admin-core/logger"
"go-admin/app/admin/models"
"go-admin/common/apis"
"go-admin/tools"
"go-admin/tools/app"
"net/http"
"time"
)
type MiGuDeployService struct {
apis.Api
}
// SendCaptcha 下单接口(下发验证码)
// @Summary 下单接口(下发验证码)
// @Tags 2024-咪咕-下单
// @Produce json
// @Accept json
// @Param request body models.SendCaptchaReq true "下单接口(下发验证码)"
// @Success 200 {object} models.SendCaptchaResp
// @Router /api/v1/migu/send_captcha [post]
func (e MiGuDeployService) SendCaptcha(c *gin.Context) {
fmt.Println("SendCaptcha-start")
logger.Info("SendCaptcha-start")
err := e.MakeContext(c).MakeOrm().Errors
if err != nil {
fmt.Println("MakeContext err:", err)
e.Logger.Error(err)
app.Error(c, http.StatusInternalServerError, err, "初始化失败")
return
}
req := &models.SendCaptchaReq{}
if c.ShouldBindJSON(req) != nil {
logger.Errorf("SendCaptcha para err")
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
return
}
logger.Info("SendCaptcha req:", req)
// 判断渠道编号
if !models.IsValidChannel(req.Channel) {
app.Error(c, http.StatusBadRequest, errors.New("invalid channel"), "invalid channel")
return
}
// 判断skuCode
if !models.IsValidSkuCode(req.SkuCode) {
app.Error(c, http.StatusBadRequest, errors.New("invalid skuCode"), "invalid skuCode")
return
}
outTradeNo := models.GetOrderSerial(e.Orm)
miGuReq := &models.MiGuSendCaptchaReq{
Phone: req.Phone,
Channel: req.Channel,
SkuCode: req.SkuCode,
OutTradeNo: outTradeNo,
}
resp, err := models.MiGuCaptchaRequest(miGuReq)
if err != nil {
logger.Errorf("SendCaptcha MiGuCaptchaRequest err:", err.Error())
app.Error(c, http.StatusBadRequest, err, err.Error())
return
}
var reason string
if resp.Msg != "" && resp.Data.LinkId != "" {
reason = fmt.Sprintf("%s;%s", resp.Msg, resp.Data.LinkId)
} else if resp.Msg != "" {
reason = resp.Msg
}
// 记录数据库
var sendLog models.MgTransactionLog
sendLog.ProductID = models.ProductID
sendLog.ChannelCode = req.Channel
sendLog.OutTradeNo = outTradeNo
sendLog.PhoneNumber = req.Phone
sendLog.LinkId = resp.Data.LinkId
sendLog.Result = resp.Code
sendLog.Reason = reason
err = e.Orm.Create(&sendLog).Error
if err != nil {
logger.Info("Create MgTransactionLog err:", err)
app.Error(c, http.StatusBadRequest, err, err.Error())
return
}
fmt.Println("SendCaptcha-end")
logger.Info("SendCaptcha-end")
app.MiGuOK(c, resp)
}
// SubmitOrder 提交接口(提交验证码)
// @Summary 提交接口(提交验证码)
// @Tags 2024-咪咕-下单
// @Produce json
// @Accept json
// @Param request body models.SubmitOrderReq true "提交接口(提交验证码)"
// @Success 200 {object} models.SubmitOrderResp
// @Router /api/v1/migu/submit_order [post]
func (e MiGuDeployService) SubmitOrder(c *gin.Context) {
fmt.Println("SubmitOrder-start")
logger.Info("SubmitOrder-start")
err := e.MakeContext(c).MakeOrm().Errors
if err != nil {
fmt.Println("SubmitOrder MakeContext err:", err)
e.Logger.Error(err)
app.Error(c, http.StatusInternalServerError, err, "初始化失败")
return
}
req := &models.SubmitOrderReq{}
if c.ShouldBindJSON(req) != nil {
logger.Errorf("SendCaptcha para err")
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
return
}
logger.Info("SubmitOrder req:", req)
// 判断渠道编号
if !models.IsValidChannel(req.Channel) {
app.Error(c, http.StatusBadRequest, errors.New("invalid channel"), "invalid channel")
return
}
// 判断skuCode
if !models.IsValidSkuCode(req.SkuCode) {
app.Error(c, http.StatusBadRequest, errors.New("invalid skuCode"), "invalid skuCode")
return
}
// 查询是否有记录
var logInfo models.MgTransactionLog
err = e.Orm.Table("mg_transaction_log").Where("link_id = ?", req.LinkId).First(&logInfo).Error
if err != nil {
logger.Errorf("SubmitOrder query mg_transaction_log err:", err.Error())
app.Error(c, http.StatusBadRequest, err, err.Error())
return
}
outTradeNo := logInfo.OutTradeNo
if logInfo.OutTradeNo == "" {
outTradeNo = models.GetOrderSerial(e.Orm)
}
miGuReq := &models.MiGuSubmitOrderReq{
LinkId: logInfo.LinkId,
SmsCode: req.SmsCode,
Phone: req.Phone,
Channel: req.Channel,
SkuCode: req.SkuCode,
OutTradeNo: outTradeNo,
}
resp, err := models.MiGuCaptchaSubmit(miGuReq)
if err != nil {
logger.Errorf("SubmitOrder MiGuCaptchaSubmit err:", err.Error())
app.Error(c, http.StatusBadRequest, err, err.Error())
return
}
var reason string
if resp.Msg != "" && resp.Data.LinkId != "" {
reason = fmt.Sprintf("%s;%s", resp.Msg, resp.Data.LinkId)
} else if resp.Msg != "" {
reason = resp.Msg
}
// 记录交易流水
var sendLog models.MgTransactionLog
sendLog.ProductID = models.ProductID
sendLog.ChannelCode = req.Channel
sendLog.OutTradeNo = logInfo.OutTradeNo
sendLog.PhoneNumber = req.Phone
sendLog.LinkId = req.LinkId
sendLog.Result = resp.Code
sendLog.Reason = reason
sendLog.VerificationCode = req.SmsCode
orderTime := time.Now()
sendLog.OrderTime = &orderTime
err = e.Orm.Create(&sendLog).Error
if err != nil {
logger.Info("Create MgTransactionLog err:", err)
app.Error(c, http.StatusBadRequest, err, err.Error())
return
}
if resp.Data.LinkId != "" { // 提交订阅成功则记录到订单表
// 记录到订单列表
var orderInfo models.MgOrder
orderInfo.ProductID = models.ProductID
orderInfo.ChannelCode = req.Channel
orderInfo.OrderSerial = logInfo.OutTradeNo
nowTime := time.Now()
orderInfo.SubscribeTime = &nowTime
orderInfo.PhoneNumber = req.Phone
orderInfo.SM4PhoneNumber, _ = tools.SM4Encrypt(models.SM4KEy, req.Phone)
orderInfo.ExternalOrderID = resp.Data.LinkId // 外部平台订单号咪咕返回的linkId)
err = e.Orm.Create(&orderInfo).Error
if err != nil {
logger.Info("Create MgTransactionLog err:", err)
//app.Error(c, http.StatusBadRequest, err, err.Error())
//return
}
}
fmt.Println("SubmitOrder-end")
logger.Info("SubmitOrder-end")
app.MiGuOK(c, resp)
}
// CheckOrder 查询是否已经退订接口
// @Summary 查询是否已经退订接口
// @Tags 2024-咪咕-下单
// @Produce json
// @Accept json
// @Param request body models.CheckOrderReq true "查询是否已经退订接口"
// @Success 200 {object} models.CheckOrderResp
// @Router /api/v1/migu/order/check [post]
func (e MiGuDeployService) CheckOrder(c *gin.Context) {
fmt.Println("CheckOrderReq-start")
logger.Info("CheckOrderReq-start")
err := e.MakeContext(c).MakeOrm().Errors
if err != nil {
fmt.Println("CheckOrderReq MakeContext err:", err)
e.Logger.Error(err)
app.Error(c, http.StatusInternalServerError, err, "初始化失败")
return
}
req := &models.CheckOrderReq{}
if c.ShouldBindJSON(req) != nil {
logger.Errorf("CheckOrderReq para err")
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
return
}
logger.Info("CheckOrderReq req:", req)
resp, err := models.MiGuCheckOrder(req)
if err != nil {
logger.Errorf("CheckOrderReq MiGuCaptchaSubmit err:", err.Error())
app.Error(c, http.StatusBadRequest, err, err.Error())
return
}
fmt.Println("CheckOrderReq-end")
logger.Info("CheckOrderReq-end")
app.MiGuOK(c, resp)
}
// CheckRightsInfo 查询是否已经退订接口(咪咕)
// @Summary 查询是否已经退订接口(咪咕)
// @Tags 2024-咪咕-下单
// @Produce json
// @Accept json
// @Param request body models.QueryRightsInfoReq true "查询是否已经退订接口(咪咕)"
// @Success 200 {object} models.QueryRightsInfoResp
// @Router /api/v1/migu/order/check_rights_info [post]
func (e MiGuDeployService) CheckRightsInfo(c *gin.Context) {
fmt.Println("CheckRightsInfo-start")
logger.Info("CheckRightsInfo-start")
req := &models.QueryRightsInfoReq{}
if c.ShouldBindJSON(req) != nil {
logger.Errorf("CheckOrderReq para err")
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
return
}
logger.Info("CheckRightsInfo req:", req)
resp, err := models.MiGuQueryRightsInfo(req)
if err != nil {
logger.Errorf("CheckRightsInfo MiGuQueryRightsInfo err:", err.Error())
app.Error(c, http.StatusBadRequest, err, err.Error())
return
}
fmt.Println("CheckRightsInfo-end")
logger.Info("CheckRightsInfo-end")
app.MiGuOK(c, resp)
}