migu_server/app/admin/apis/migumanage/migu_external.go

380 lines
11 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/tools/app"
"net/http"
"time"
)
// 以下接口是封装转发后对外的接口
// SendCaptchaEx 下单接口(下发验证码)
// @Summary 下单接口(下发验证码)
// @Tags 2024-咪咕(对外)-下单
// @Produce json
// @Accept json
// @Param request body models.SendCaptchaReqEx true "下单接口(下发验证码)"
// @Success 200 {object} models.SendCaptchaResp
// @Router /api/v1/coupon_provider/send_captcha [post]
func (e MiGuDeployService) SendCaptchaEx(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.MiGuError(c, http.StatusInternalServerError, err, "初始化失败")
return
}
req := &models.SendCaptchaReqEx{}
if c.ShouldBindJSON(req) != nil {
logger.Errorf("SendCaptcha para err")
app.MiGuError(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
return
}
logger.Info("SendCaptcha req:", req)
// 判断渠道订单号是否过长
if len(req.OutTradeNo) > 64 {
app.MiGuError(c, http.StatusBadRequest, errors.New("outTradeNo length exceeded the limit64"), "length exceeded the limit64")
return
}
// 判断渠道编号
if !models.IsValidChannelEx(req.Channel, e.Orm) {
app.MiGuError(c, http.StatusBadRequest, errors.New("invalid channel"), "invalid channel")
return
}
// 判断skuCode
if !models.IsValidSkuCodeEx(req.SkuCode, e.Orm) {
app.MiGuError(c, http.StatusBadRequest, errors.New("invalid skuCode"), "invalid skuCode")
return
}
// 通过子渠道编号和sku查询主渠道编号及其sku
channel, sku, err := models.GetMainChannelCodeAndSkuCode(req.Channel, req.SkuCode, e.Orm)
if err != nil {
app.MiGuError(c, http.StatusBadRequest, err, "channel or skuCode err")
return
}
outTradeNo := models.GetOrderSerial(e.Orm)
miGuReq := &models.MiGuSendCaptchaReq{
Phone: req.Phone,
Channel: channel.MainChannelCode,
SkuCode: sku.SkuName,
OutTradeNo: outTradeNo,
}
resp, err := models.MiGuCaptchaRequest(miGuReq)
if err != nil {
logger.Errorf("SendCaptcha MiGuCaptchaRequest err:", err.Error())
app.MiGuError(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
sendLog.ChannelTradeNo = req.OutTradeNo
err = e.Orm.Create(&sendLog).Error
if err != nil {
logger.Info("Create MgTransactionLog err:", err)
app.MiGuError(c, http.StatusBadRequest, err, err.Error())
return
}
if resp.Data.LinkId != "" {
resp.Data.LinkId = outTradeNo
}
fmt.Println("SendCaptcha-end")
logger.Info("SendCaptcha-end")
app.MiGuOK(c, resp)
}
// SubmitOrderEx 提交接口(提交验证码)
// @Summary 提交接口(提交验证码)
// @Tags 2024-咪咕(对外)-下单
// @Produce json
// @Accept json
// @Param request body models.SubmitOrderReqEx true "提交接口(提交验证码)"
// @Success 200 {object} models.SubmitOrderResp
// @Router /api/v1/coupon_provider/submit_order [post]
func (e MiGuDeployService) SubmitOrderEx(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.MiGuError(c, http.StatusInternalServerError, err, "初始化失败")
return
}
req := &models.SubmitOrderReqEx{}
if c.ShouldBindJSON(req) != nil {
logger.Errorf("SendCaptcha para err")
app.MiGuError(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
return
}
logger.Info("SubmitOrder req:", req)
// 判断渠道订单号是否过长
if len(req.OutTradeNo) > 64 {
app.MiGuError(c, http.StatusBadRequest, errors.New("outTradeNo length exceeded the limit64"), "length exceeded the limit64")
return
}
// 判断渠道编号
if !models.IsValidChannelEx(req.Channel, e.Orm) {
app.MiGuError(c, http.StatusBadRequest, errors.New("invalid channel"), "invalid channel")
return
}
// 判断skuCode
if !models.IsValidSkuCodeEx(req.SkuCode, e.Orm) {
app.MiGuError(c, http.StatusBadRequest, errors.New("invalid skuCode"), "invalid skuCode")
return
}
// 查询是否有记录
var logInfo models.MgTransactionLog
err = e.Orm.Table("mg_transaction_log").Where("out_trade_no = ?", req.LinkId).First(&logInfo).Error
if err != nil {
logger.Errorf("SubmitOrder query mg_transaction_log err:", err.Error())
app.MiGuError(c, http.StatusBadRequest, err, err.Error())
return
}
if logInfo.LinkId == "" {
app.MiGuError(c, http.StatusBadRequest, errors.New("linkId is invalid"), "linkId is invalid")
return
}
// 通过子渠道编号和sku查询主渠道编号及其sku
channel, sku, err := models.GetMainChannelCodeAndSkuCode(req.Channel, req.SkuCode, e.Orm)
if err != nil {
app.MiGuError(c, http.StatusBadRequest, err, "channel or skuCode err")
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: channel.MainChannelCode,
SkuCode: sku.SkuName,
OutTradeNo: outTradeNo,
}
resp, err := models.MiGuCaptchaSubmit(miGuReq)
if err != nil {
logger.Errorf("SubmitOrder MiGuCaptchaSubmit err:", err.Error())
app.MiGuError(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.LinkId = resp.Data.LinkId
sendLog.PhoneNumber = req.Phone
sendLog.Result = resp.Code
sendLog.Reason = reason
sendLog.VerificationCode = req.SmsCode
orderTime := time.Now()
sendLog.OrderTime = &orderTime
sendLog.ChannelTradeNo = req.OutTradeNo
err = e.Orm.Create(&sendLog).Error
if err != nil {
logger.Info("Create MgTransactionLog err:", err)
app.MiGuError(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)
//orderInfo.ChannelTradeNo = req.OutTradeNo // 渠道订单号
//
//err = e.Orm.Create(&orderInfo).Error
//if err != nil {
// logger.Info("Create MgTransactionLog err:", err)
// app.MiGuError(c, http.StatusBadRequest, err, err.Error())
// return
//}
resp.Data.LinkId = outTradeNo
}
fmt.Println("SubmitOrder-end")
logger.Info("SubmitOrder-end")
app.MiGuOK(c, resp)
}
// CheckOrderEx 查询是否已经退订接口
// @Summary 查询是否已经退订接口
// @Tags 2024-咪咕(对外)-下单
// @Produce json
// @Accept json
// @Param request body models.CheckOrderReqEx true "查询是否已经退订接口"
// @Success 200 {object} models.CheckOrderResp
// @Router /api/v1/coupon_provider/order/check [post]
func (e MiGuDeployService) CheckOrderEx(c *gin.Context) {
fmt.Println("CheckOrderEx-start")
logger.Info("CheckOrderEx-start")
err := e.MakeContext(c).MakeOrm().Errors
if err != nil {
fmt.Println("CheckOrderEx MakeContext err:", err)
e.Logger.Error(err)
app.MiGuError(c, http.StatusInternalServerError, err, "初始化失败")
return
}
req := &models.CheckOrderReqEx{}
if c.ShouldBindJSON(req) != nil {
logger.Errorf("CheckOrderEx para err")
app.MiGuError(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
return
}
logger.Info("CheckOrderEx req:", req)
var resp models.CheckOrderResp
resp.Code = "0" //未退订
resp.Msg = "success"
// 查询是否有记录
var orderInfo models.MgOrder
err = e.Orm.Table("mg_order").Where("order_serial = ?", req.LinkId).First(&orderInfo).Error
if err != nil {
logger.Errorf("CheckOrderEx query mg_order err:", err.Error())
resp.Code = "404" // 没记录
app.MiGuOK(c, resp)
return
}
if orderInfo.State == 2 { // 已退订
resp.Code = "-1"
}
fmt.Println("CheckOrderEx-end")
logger.Info("CheckOrderEx-end")
app.MiGuOK(c, resp)
}
// CheckRightsInfoEx 查询是否已经退订接口(咪咕)
// @Summary 查询是否已经退订接口(咪咕)
// @Tags 2024-咪咕(对外)-下单
// @Produce json
// @Accept json
// @Param request body models.QueryRightsInfoReqEx true "查询是否已经退订接口(咪咕)"
// @Success 200 {object} models.QueryRightsInfoResp
// @Router /api/v1/coupon_provider/order/check_rights_info [post]
func (e MiGuDeployService) CheckRightsInfoEx(c *gin.Context) {
fmt.Println("CheckRightsInfoEx-start")
logger.Info("CheckRightsInfoEx-start")
err := e.MakeContext(c).MakeOrm().Errors
if err != nil {
fmt.Println("CheckOrderEx MakeContext err:", err)
e.Logger.Error(err)
app.MiGuError(c, http.StatusInternalServerError, err, "初始化失败")
return
}
req := &models.QueryRightsInfoReqEx{}
if c.ShouldBindJSON(req) != nil {
logger.Errorf("CheckRightsInfoEx para err")
app.MiGuError(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
return
}
logger.Info("CheckRightsInfoEx req:", req)
// 判断渠道编号
if !models.IsValidChannelEx(req.Channel, e.Orm) {
app.MiGuError(c, http.StatusBadRequest, errors.New("invalid channel"), "invalid channel")
return
}
// 判断skuCode
if !models.IsValidSkuCodeEx(req.SkuCode, e.Orm) {
app.MiGuError(c, http.StatusBadRequest, errors.New("invalid skuCode"), "invalid skuCode")
return
}
// 通过子渠道编号和sku查询主渠道编号及其sku
_, sku, err := models.GetMainChannelCodeAndSkuCode(req.Channel, req.SkuCode, e.Orm)
if err != nil {
app.MiGuError(c, http.StatusBadRequest, err, "channel or skuCode err")
return
}
reqQuery := models.QueryRightsInfoReq{
Mobile: req.Phone,
}
reqQuery.AppChannelList = append(reqQuery.AppChannelList, sku.ChannelCode)
resp, err := models.MiGuQueryRightsInfo(&reqQuery)
if err != nil {
logger.Errorf("CheckRightsInfoEx MiGuQueryRightsInfo err:", err.Error())
app.MiGuError(c, http.StatusBadRequest, err, err.Error())
return
}
fmt.Println("CheckRightsInfoEx-end")
logger.Info("CheckRightsInfoEx-end")
app.MiGuOK(c, resp)
}