You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
paopao-ce/internal/service/wallet.go

55 lines
1.2 KiB

2 years ago
package service
import (
"time"
"github.com/gin-gonic/gin"
"github.com/rocboss/paopao-ce/global"
"github.com/rocboss/paopao-ce/internal/model"
"github.com/rocboss/paopao-ce/pkg/errcode"
2 years ago
)
type RechargeReq struct {
Amount int64 `json:"amount" form:"amount" binding:"required"`
}
func GetRechargeByID(id int64) (*model.WalletRecharge, error) {
return myDao.GetRechargeByID(id)
2 years ago
}
func CreateRecharge(userID, amount int64) (*model.WalletRecharge, error) {
return myDao.CreateRecharge(userID, amount)
2 years ago
}
func FinishRecharge(ctx *gin.Context, id int64, tradeNo string) error {
if ok, _ := global.Redis.SetNX(ctx, "PaoPaoRecharge:"+tradeNo, 1, time.Second*5).Result(); ok {
recharge, err := myDao.GetRechargeByID(id)
2 years ago
if err != nil {
return err
}
if recharge.TradeStatus != "TRADE_SUCCESS" {
// 标记为已付款
err := myDao.HandleRechargeSuccess(recharge, tradeNo)
defer global.Redis.Del(ctx, "PaoPaoRecharge:"+tradeNo)
2 years ago
if err != nil {
return err
}
}
}
return nil
}
func BuyPostAttachment(post *model.Post, user *model.User) error {
2 years ago
if user.Balance < post.AttachmentPrice {
return errcode.InsuffientDownloadMoney
}
// 执行购买
return myDao.HandlePostAttachmentBought(post, user)
2 years ago
}