Browse Source

背景图\二维码

k.zhang 3 tháng trước cách đây
mục cha
commit
b400f084b1

+ 5 - 0
apis/shanghu/base.go

@@ -85,6 +85,11 @@ func InitShangHuRouter(engine *gin.RouterGroup) {
 		activeV1.POST("/active/draw/list", DrawLog)                       //中奖列表
 		activeV1.POST("/draw/verification/code", DrawLogVerificationCode) //奖品核销码                                           //中奖核销码
 		activeV1.POST("/draw/cancel", DrawCancel)                         //核销 奖品与拼团
+		activeV1.POST("/active/config/whxy", UpdateActiveConfigWHXY)      //二维码坐标
+		activeV1.POST("/active/qr", GetClientActiveQR)                    //获取二维码
+		activeV1.POST("/active/unified/order", GroupByUnifiedOrder)       //拼团购买
+		//activeV1.POST("")                                                 //支付回调
+		//
 		//activeV1.POST("")                                                 //拼团购买
 		//待核销金额
 		//

+ 227 - 0
apis/shanghu/merchant.active.config.go

@@ -1,14 +1,22 @@
 package shanghu
 
 import (
+	"duoduo/apis/common"
+	"duoduo/apis/pdd"
 	"duoduo/apis/shanghu/models"
 	"duoduo/models/shanghu"
 	"duoduo/tools"
 	"duoduo/tools/app"
+	"encoding/base64"
+	"encoding/json"
 	"errors"
 	"fmt"
 	"github.com/gin-gonic/gin"
+	"github.com/go-pay/gopay"
+	"github.com/go-pay/gopay/wechat"
+	"github.com/shopspring/decimal"
 	"math/rand"
+	"strconv"
 	"time"
 )
 
@@ -152,6 +160,7 @@ func ActiveConfigInfo(c *gin.Context) {
 	outData.ActivityEndTime = tools.TimeToStr(configInfo.ActivityEndTime)
 	outData.ActivityStartTime = tools.TimeToStr(configInfo.ActivityStartTime)
 	outData.ActiveName = configInfo.ActiveName
+	outData.BackgroundImage = configInfo.BackgroundImage
 
 	//中奖商品
 	drawInfoSql.ActiveConfigID = configInfo.ID
@@ -232,6 +241,43 @@ func ActiveConfigInfo(c *gin.Context) {
 
 }
 
+// whxy
+func UpdateActiveConfigWHXY(c *gin.Context) {
+	var inData models.ActiveConfigWHXYRequest
+	var sqlData shanghu.MerchantActiveConfig
+	var whxy []models.WHXYStr
+
+	err := c.ShouldBindJSON(&inData)
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+
+	err = json.Unmarshal([]byte(inData.WHXY), &whxy)
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+	for i := 0; i < len(whxy); i++ {
+		if !whxy[i].Y.IsZero() {
+			sqlData.W = whxy[i].W.String()
+			sqlData.X = whxy[i].X.String()
+			sqlData.Y = whxy[i].Y.String()
+			sqlData.H = whxy[i].H.String()
+		}
+	}
+	sqlData.ID = inData.ActiveConfigId
+
+	err = sqlData.UpdateMerchantWHXY()
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+
+	app.OK(c, nil, app.Success)
+
+}
+
 // 活动列表
 func ActiveConfigList(c *gin.Context) {
 	var inData models.ActiveConfigListRequest
@@ -562,3 +608,184 @@ func subStockAddLog(drawProductInfo shanghu.MerchantActiveDrawProduct, clientOpe
 
 	return nil
 }
+
+func GetClientActiveQR(c *gin.Context) {
+	var inData models.ClientActiveQRRequest
+	//var sqlData shanghu.MerchantClientCard
+	var outData models.ClientCardQRReply
+	var qr models.QRRequest
+	var getQR models.GetQRRequest
+
+	err := c.ShouldBindJSON(&inData)
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+	//	sqlData.ID = inData.ActiveConfigId
+
+	val, err := pdd.DuoDuoGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx8595c589dd736486&secret=668f87d2bc24199688e53ee8a88434b8")
+	if err != nil {
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+
+	err = tools.JsonUnmarshal(val, &outData)
+	if err != nil {
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+	if outData.AccessToken == "" {
+		app.Error(c, 500, err, "token error")
+		return
+	}
+	url := "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + outData.AccessToken
+
+	qr.CheckPath = true
+	qr.Page = "pages/home/home"
+	qr.EnvVersion = "release"
+	qr.Scene = strconv.FormatInt(inData.ActiveConfigId, 10)
+
+	data, err := json.Marshal(&qr)
+	if err != nil {
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+	reply, err := common.Post(data, url)
+	if err != nil {
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+	//err = json.Unmarshal(reply, &getQR)
+	//if err != nil {
+	//	app.Error(c, 500, err, err.Error())
+	//	return
+	//}
+
+	if getQR.Errcode != 0 {
+		app.Error(c, 500, err, getQR.Errmsg)
+		return
+	}
+
+	//fmt.Println(string(reply))
+
+	encoded := base64.StdEncoding.EncodeToString(reply)
+
+	app.OK(c, encoded, app.Success)
+
+}
+
+func GroupByUnifiedOrder(c *gin.Context) {
+	var inData models.GroupBuyUnifiedOrderRequest
+	var sqlData shanghu.ClientActivePayTrans
+	var activeConfig shanghu.MerchantActiveConfig
+	var outData models.GroupBuyUnifiedOrderReply
+
+	err := c.ShouldBindJSON(&inData)
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+
+	//校验活动id
+	activeConfig.ID = inData.ActiveConfigId
+
+	activeConfigInfo, err := activeConfig.GetConfigInfoById()
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+	if activeConfigInfo.DrawMode != 1 { //虚拟开团
+		app.Error(c, 500, errors.New("非虚拟开团,不允许下单"), "非虚拟开团,不允许下单.")
+		return
+	}
+
+	if inData.ClientOpenId == "" {
+		app.OK(c, nil, app.Success)
+		return
+	}
+	//校验 防止同一笔记录存在
+	sqlData.RequestID = inData.RequestId
+	if sqlData.GetRequestNum() > 0 {
+		app.Error(c, 400, errors.New("交易已存在"), "交易已存在")
+		return
+	}
+
+	//校验金额
+	//检查微信相关参数
+	if !inData.Amount.GreaterThan(decimal.NewFromInt(0)) {
+		app.Error(c, 400, errors.New("amount:金额必须大于0"), "amount:金额必须大于0")
+		return
+
+	}
+	if inData.Amount.Round(2).String() != inData.Amount.String() {
+		app.Error(c, 400, errors.New("total_fee:金额最多只能保留两位小数"), "total_fee:金额最多只能保留两位小数")
+		return
+	}
+
+	//创建支付记录
+	sqlData.ClientOpenID = inData.ClientOpenId
+	sqlData.RequestID = inData.RequestId
+	sqlData.CreatedAt = time.Now()
+	sqlData.UpdatedAt = time.Now()
+	sqlData.Amount = inData.Amount
+	sqlData.OutTradeNo = strconv.FormatInt(inData.ActiveConfigId, 10) + strconv.FormatInt(time.Now().UnixNano(), 10)
+	sqlData.Status = 1 //未支付
+	sqlData.ActiveConfigID = inData.ActiveConfigId
+	sqlData.InvitationCode = inData.InvitationCode
+	_, err = sqlData.Create()
+	if err != nil {
+		app.Error(c, 400, err, "创建支付失败")
+		return
+	}
+	fmt.Println(sqlData.OutTradeNo)
+	bm := make(gopay.BodyMap)
+	bm.Set("nonce_str", common.GetRandomString(32))
+	bm.Set("body", "商户卡")
+	bm.Set("out_trade_no", sqlData.OutTradeNo)
+	bm.Set("total_fee", inData.Amount.Mul(decimal.NewFromInt(100)).IntPart())
+	bm.Set("spbill_create_ip", "127.0.0.1")
+	bm.Set("notify_url", "https://tao1024.com/v1/client/pay/callback")
+	bm.Set("device_info", "WEB")
+	bm.Set("trade_type", "JSAPI")
+	bm.Set("sign_type", wechat.SignType_MD5)
+	bm.Set("openid", inData.ClientOpenId)
+
+	client := NewWechatService()
+	//请求支付下单,成功后得到结果
+	wxResp, err := client.UnifiedOrder(c, bm)
+	if err != nil {
+		app.Error(c, 400, err, "下单失败")
+		return
+	}
+
+	if wxResp.ReturnCode != "SUCCESS" {
+		app.Error(c, 400, errors.New(wxResp.ReturnMsg), "下单失败")
+		return
+	}
+	if wxResp.ResultCode != "SUCCESS" {
+		app.Error(c, 400, errors.New(wxResp.ErrCode+"--"+wxResp.ErrCodeDes), "下单失败")
+		return
+	}
+
+	timestamp := strconv.FormatInt(time.Now().Unix(), 10)
+	pac := "prepay_id=" + wxResp.PrepayId
+	paySign := wechat.GetMiniPaySign("wx8595c589dd736486", wxResp.NonceStr, pac, wechat.SignType_MD5, timestamp, "33c424fAa69942086f82A003e283E9C8")
+
+	outData.Timestamp = timestamp
+	outData.NonceStr = wxResp.NonceStr
+	outData.Package = pac
+	outData.PaySign = paySign
+	outData.SignType = wechat.SignType_MD5
+
+	//merchant, count, err := sqlData.GetOpenIdList(pageSize, pageIndex)
+	//if err != nil {
+	//	app.Error(c, 500, err, err.Error())
+	//	return
+	//}
+	app.OK(c, outData, app.Success)
+}
+
+func Pay(c *gin.Context) {
+	// 获取参数
+	//
+}

+ 33 - 6
apis/shanghu/models/active.config.go

@@ -22,14 +22,15 @@ type ActiveConfigRequest struct {
 }
 
 type ActiveConfigReply struct {
-	MerchantName      string         `json:"merchant_name"` //商家名称
-	DrawOneBiZhong    int64          `json:"draw_one_bi_zhong"`
-	GroupBuyUrl       string         `json:"group_buy_url"`
+	MerchantName      string         `json:"merchant_name"`       //商家名称
+	DrawOneBiZhong    int64          `json:"draw_one_bi_zhong"`   //
+	GroupBuyUrl       string         `json:"group_buy_url"`       //
 	GroupBuyMode      int            `json:"group_buy_mode"`      // 0-不开团 1-虚拟开团 2-真实开团
 	DrawMode          int            `json:"draw_mode"`           //抽奖模式 0-不抽奖,1-盲盒
-	ActivityEndTime   string         `json:"activity_end_time"`   // 活动结束时间
-	ActivityStartTime string         `json:"activity_start_time"` // 活动开始时间
-	ActiveName        string         `json:"active_name"`         // 活动名称
+	ActivityEndTime   string         `json:"activity_end_time"`   //活动结束时间
+	ActivityStartTime string         `json:"activity_start_time"` //活动开始时间
+	ActiveName        string         `json:"active_name"`         //活动名称
+	BackgroundImage   string         `json:"background_image"`    // 海报
 	DrawProduct       []DrawProduct  `json:"draw_product"`        //中奖商品
 	DrawGroupBuy      []DrawGroupBuy `json:"draw_group_buy"`      //活动拼团
 }
@@ -102,3 +103,29 @@ const (
 	NotClaimed = 2 //未兑奖
 	Claimed    = 3 //已兑奖
 )
+
+type ActiveConfigWHXYRequest struct {
+	WHXY           string `json:"whxy"`           //坐标
+	ActiveConfigId int64  `json:"activeConfigId"` //
+}
+
+type ClientActiveQRRequest struct {
+	ActiveConfigId int64 `json:"active_config_id"` //
+}
+
+type GroupBuyUnifiedOrderRequest struct {
+	RequestId      string          `json:"request_id"`      //request id
+	OutTradeNo     string          `json:"out_trade_no"`    //交易id
+	ActiveConfigId int64           `json:"activeConfigId"`  //
+	ClientOpenId   string          `json:"client_open_id"`  //openid
+	Amount         decimal.Decimal `json:"amount"`          //交易金额
+	InvitationCode string          `json:"invitation_code"` //邀请码
+}
+
+type GroupBuyUnifiedOrderReply struct {
+	Timestamp string `json:"timestamp"` // 时间戳
+	NonceStr  string `json:"nonce_str"` // 随机字符串,长度为32个字符以下
+	Package   string `json:"package"`   // prepay_id 参数值
+	PaySign   string `json:"pay_sign"`  // 签名
+	SignType  string `json:"sign_type"` // 签名类型
+}

+ 1 - 1
apis/shanghu/pay.go

@@ -60,7 +60,7 @@ func UnifiedOrder(c *gin.Context) {
 	}
 
 	//校验金额
-	// 检查微信相关参数
+	//检查微信相关参数
 	if !inData.Amount.GreaterThan(decimal.NewFromInt(0)) {
 		app.Error(c, 400, errors.New("amount:金额必须大于0"), "amount:金额必须大于0")
 		return

+ 55 - 0
models/shanghu/client.active.pay.trans.go

@@ -0,0 +1,55 @@
+package shanghu
+
+import (
+	orm "duoduo/database"
+	"github.com/shopspring/decimal"
+	"time"
+)
+
+type ClientActivePayTrans struct {
+	ID             int64           `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"`    // 主键
+	RequestID      string          `gorm:"column:request_id;type:varchar(255)" json:"request_id"`             // 请求id,幂等性
+	OutTradeNo     string          `gorm:"column:out_trade_no;type:varchar(255)" json:"out_trade_no"`         // 交易id
+	ActiveConfigID int64           `gorm:"column:active_config_id;type:bigint(20)" json:"active_config_id"`   // 活动id
+	ClientOpenID   string          `gorm:"column:client_open_id;type:varchar(255)" json:"client_open_id"`     // 客户端openid
+	Status         int             `gorm:"column:status;type:int(11)" json:"status"`                          // 1-未支付 2-支付成功 3-取消支付 4-退款
+	ThirdTradeNo   string          `gorm:"column:third_trade_no;type:varchar(255)" json:"third_trade_no"`     // 微信交易id
+	AccountStatus  int             `gorm:"column:account_status;type:int(11)" json:"account_status"`          // 分账状态 99-分账成功 2-分账失败 1-未分账 3-分账中
+	PayTime        time.Time       `gorm:"column:pay_time;type:datetime(3)" json:"pay_time"`                  // 支付时间
+	InvitationCode string          `gorm:"column:invitation_code;type:varchar(25)" json:"invitation_code"`    // 邀请码
+	Amount         decimal.Decimal `gorm:"column:amount;type:decimal(10,2)" json:"amount"`                    // 交易金额
+	AccountErrLog  string          `gorm:"column:account_err_log;type:varchar(255)" json:"account_err_log"`   // 分账err日志
+	GroupBuyID     int64           `gorm:"column:group_buy_id;type:bigint(20)" json:"group_buy_id"`           // 团购id
+	SeckillID      int64           `gorm:"column:seckill_id;type:bigint(20)" json:"seckill_id"`               // 秒杀id
+	CreateBy       int64           `gorm:"column:create_by;type:bigint(20)" json:"create_by"`                 // 创建者
+	UpdateBy       int64           `gorm:"column:update_by;type:bigint(20)" json:"update_by"`                 // 更新者
+	CreatedAt      time.Time       `gorm:"column:created_at;type:datetime(3)" json:"created_at"`              // 创建时间
+	UpdatedAt      time.Time       `gorm:"column:updated_at;type:datetime(3)" json:"updated_at"`              // 最后更新时间
+	DeletedAt      time.Time       `gorm:"column:deleted_at;type:datetime(3);default:null" json:"deleted_at"` // 删除时间
+}
+
+func (m *ClientActivePayTrans) TableName() string {
+	return "client_active_pay_trans"
+}
+
+func (m *ClientActivePayTrans) GetRequestNum() int {
+	var count int
+
+	tableCount := orm.ShMysql.Table(m.TableName()).Where("request_id = ? ", m.RequestID)
+	tableCount.Count(&count)
+	return count
+
+}
+
+func (m *ClientActivePayTrans) Create() (ClientActivePayTrans, error) {
+	var doc ClientActivePayTrans
+	var err error
+
+	doc = *m
+	err = orm.ShMysql.Table(m.TableName()).Create(&doc).Error
+	if err != nil {
+		return doc, err
+	}
+
+	return doc, nil
+}

+ 16 - 0
models/shanghu/merchant.active.config.go

@@ -2,6 +2,7 @@ package shanghu
 
 import (
 	orm "duoduo/database"
+	"duoduo/tools"
 	"time"
 )
 
@@ -75,3 +76,18 @@ func (m *MerchantActiveConfig) GetActiveConfigList(pageSize int, pageIndex int)
 	table.Count(&count)
 	return doc, count, nil
 }
+
+func (m *MerchantActiveConfig) UpdateMerchantWHXY() error {
+
+	if err := orm.ShMysql.Table(m.TableName()).Model(&m).Where("id = ? ", m.ID).Updates(
+		map[string]interface{}{
+			"w":          m.W,
+			"h":          m.H,
+			"x":          m.X,
+			"y":          m.Y,
+			"updated_at": tools.GetCurrntTimeStr()}).Error; err != nil {
+		return err
+	}
+
+	return nil
+}