341 lines
10 KiB
Go
341 lines
10 KiB
Go
package recharge
|
|
|
|
import (
|
|
"f5"
|
|
"fmt"
|
|
"jccommon"
|
|
"main/common"
|
|
"main/constant"
|
|
"main/service"
|
|
|
|
"main/mt"
|
|
"net/http"
|
|
"q5"
|
|
"strings"
|
|
|
|
"math/big"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type RechargeApi struct {
|
|
}
|
|
|
|
func (ea *RechargeApi) RechargeList(c *gin.Context) {
|
|
netId := q5.ToInt32(c.DefaultQuery("net_id", ""))
|
|
accountAddress := c.DefaultQuery("account_address", "")
|
|
rspObj := struct {
|
|
ErrCode int32 `json:"errcode"`
|
|
ErrMsg string `json:"errmsg"`
|
|
Contribution string `json:"contribution"`
|
|
Rows []interface{} `json:"rows"`
|
|
}{}
|
|
|
|
currencysMeta := mt.Table.Currency.GetByNetId(netId)
|
|
if currencysMeta == nil {
|
|
f5.RspErr(c, 2, "server internal error")
|
|
return
|
|
}
|
|
|
|
if contribution, err := service.Contribution.GetAddressContribution(accountAddress, true); err != nil {
|
|
f5.RspErr(c, 2, err.Error())
|
|
return
|
|
} else {
|
|
rspObj.Contribution = q5.ToString(contribution)
|
|
}
|
|
|
|
mt.Table.Recharge.Traverse(func(tb *mt.Recharge) bool {
|
|
tmpmap := map[string]interface{}{}
|
|
tmpmap["goods_id"] = tb.GetId()
|
|
tmpmap["diamond"] = tb.GetDiamond()
|
|
tmpmap["present_diamond"] = tb.GetPresentDiamond()
|
|
tmpmap["price"] = tb.GetPrice()
|
|
tmpmap["max_buy_times"] = tb.GetMaxBuyTimes()
|
|
tmpmap["can_email_buy"] = tb.GetCanEmailBuy()
|
|
currencyList := []struct {
|
|
Name string `json:"name"`
|
|
Address string `json:"address"`
|
|
}{}
|
|
currencysMeta.Range(
|
|
func(key string, val *mt.Currency) bool {
|
|
currency := val
|
|
p := q5.NewSliceElement(¤cyList)
|
|
p.Name = currency.GetCurrencyName()
|
|
p.Address = currency.GetContract().GetAddress()
|
|
return true
|
|
})
|
|
tmpmap["currency_list"] = currencyList
|
|
|
|
rspObj.Rows = append(rspObj.Rows, tmpmap)
|
|
|
|
return true
|
|
})
|
|
|
|
c.JSON(200, rspObj)
|
|
}
|
|
|
|
func (this *RechargeApi) Buy(c *gin.Context) {
|
|
passportAddress := c.MustGet("account_address").(string)
|
|
reqJson := struct {
|
|
NetId int32 `json:"net_id"`
|
|
GoodsId int32 `json:"goods_id"`
|
|
GoodsNum int32 `json:"goods_num"`
|
|
AccountAddress string `json:"account_address"`
|
|
CurrencyAddress string `json:"currency_address"`
|
|
}{}
|
|
if err := c.ShouldBindJSON(&reqJson); err != nil {
|
|
f5.RspErr(c, 1, err.Error())
|
|
return
|
|
}
|
|
this.internalBuy(c, reqJson.NetId, reqJson.GoodsId, reqJson.GoodsNum,
|
|
reqJson.AccountAddress, passportAddress, "", reqJson.CurrencyAddress)
|
|
}
|
|
|
|
func (this *RechargeApi) BuyWithEmail(c *gin.Context) {
|
|
passportAddress := c.MustGet("account_address").(string)
|
|
email := c.MustGet("email").(string)
|
|
reqJson := struct {
|
|
NetId int32 `json:"net_id"`
|
|
GoodsId int32 `json:"goods_id"`
|
|
GoodsNum int32 `json:"goods_num"`
|
|
AccountAddress string `json:"account_address"`
|
|
CurrencyAddress string `json:"currency_address"`
|
|
}{}
|
|
if err := c.ShouldBindJSON(&reqJson); err != nil {
|
|
f5.GetSysLog().Info("BuyWithEmail passport_address:%s err:%s",
|
|
passportAddress, err)
|
|
f5.RspErr(c, 1, err.Error())
|
|
return
|
|
}
|
|
f5.GetSysLog().Info("BuyWithEmail passport_address:%s email:%s reqJson:%s",
|
|
passportAddress, email, q5.EncodeJson(&reqJson))
|
|
if email == "" {
|
|
f5.RspErr(c, 1, "email is empty")
|
|
return
|
|
}
|
|
this.internalBuy(c, reqJson.NetId, reqJson.GoodsId, reqJson.GoodsNum,
|
|
reqJson.AccountAddress, passportAddress, email, reqJson.CurrencyAddress)
|
|
}
|
|
|
|
func (this *RechargeApi) internalBuy(c *gin.Context,
|
|
netId int32, goodsId int32, goodsNum int32, accountAddress string, passportAddress string,
|
|
email string, currencyAddress string) {
|
|
accountAddress = strings.ToLower(accountAddress)
|
|
passportAddress = strings.ToLower(passportAddress)
|
|
currencyMeta := mt.Table.Currency.GetByNetIdAddress(netId, currencyAddress)
|
|
if currencyMeta == nil {
|
|
f5.GetSysLog().Info("internalBuy error:1")
|
|
f5.RspErr(c, 2, "server internal error")
|
|
return
|
|
}
|
|
currencyContractMeta := currencyMeta.GetContract()
|
|
if currencyContractMeta == nil {
|
|
f5.GetSysLog().Info("internalBuy error:2")
|
|
f5.RspErr(c, 2, "server internal error")
|
|
return
|
|
}
|
|
goodsMeta := mt.Table.Recharge.GetById(q5.ToInt64(goodsId))
|
|
if goodsMeta == nil {
|
|
f5.GetSysLog().Info("internalBuy error:3")
|
|
f5.RspErr(c, 2, "goods id param error")
|
|
return
|
|
}
|
|
if goodsMeta.GetPrice() <= 0 {
|
|
f5.GetSysLog().Info("internalBuy error:4")
|
|
f5.RspErr(c, 2, "goods id param error")
|
|
return
|
|
}
|
|
if goodsNum <= 0 || goodsNum > goodsMeta.GetMaxBuyTimes() {
|
|
f5.GetSysLog().Info("internalBuy error:5")
|
|
f5.RspErr(c, 2, "num param error")
|
|
return
|
|
}
|
|
if email != "" && goodsMeta.GetCanEmailBuy() == 0 {
|
|
f5.GetSysLog().Info("internalBuy error:6")
|
|
f5.RspErr(c, 2, "cant email buy")
|
|
return
|
|
}
|
|
nowTime := q5.ToInt32(f5.GetApp().GetRealSeconds())
|
|
shortOrderId := f5.GetApp().NewLockNodeUuid()
|
|
orderId := ""
|
|
{
|
|
transId := jccommon.NewTransId()
|
|
transId.Init(
|
|
jccommon.TRANS_ID_FUNC_NORMAL,
|
|
nowTime,
|
|
0,
|
|
[3]int64{
|
|
q5.ToInt64(goodsNum),
|
|
q5.ToInt64(goodsMeta.GetId()),
|
|
shortOrderId,
|
|
})
|
|
var err error
|
|
orderId, err = transId.ToString()
|
|
if err != nil {
|
|
f5.GetSysLog().Info("internalBuy error:6")
|
|
f5.RspErr(c, 2, "server internal error")
|
|
return
|
|
}
|
|
}
|
|
srcPrice := q5.ToInt64(goodsMeta.GetPrice()) * q5.ToInt64(goodsNum)
|
|
bnPrice := big.NewInt(0)
|
|
{
|
|
var i, e = big.NewInt(10), big.NewInt(currencyMeta.GetCurrencyDecimal())
|
|
i.Exp(i, e, nil)
|
|
bnPrice = i.Mul(i, big.NewInt(srcPrice))
|
|
}
|
|
f5.GetSysLog().Info("recharge net_id:%d currency_name:%s decimal:%d srcPrice:%d bnPrice:%s",
|
|
netId, currencyMeta.GetCurrencyName(), currencyMeta.GetCurrencyDecimal(), srcPrice, bnPrice.String());
|
|
if bnPrice.Cmp(big.NewInt(0)) < 0 || bnPrice.Cmp(big.NewInt(srcPrice)) < 0 {
|
|
f5.GetSysLog().Info("internalBuy error:7")
|
|
f5.RspErr(c, 3, "server internal error")
|
|
return
|
|
}
|
|
if ok, err := service.User.InBlackList(accountAddress); err != nil {
|
|
f5.GetSysLog().Info("internalBuy error:8")
|
|
f5.RspErr(c, 500, "server internal error")
|
|
return
|
|
} else if ok {
|
|
f5.GetSysLog().Info("internalBuy error:9")
|
|
f5.RspErr(c, 501, "illegal user")
|
|
return
|
|
}
|
|
params := map[string]string{
|
|
"c": "BcService",
|
|
"a": "recharge",
|
|
"net_id": q5.ToString(netId),
|
|
"order_id": orderId,
|
|
"account_address": accountAddress,
|
|
"passport_address": passportAddress,
|
|
"amount": bnPrice.String(),
|
|
"currency_name": currencyContractMeta.GetName(),
|
|
"currency_address": currencyContractMeta.GetAddress(),
|
|
}
|
|
jsonRspObj := &struct {
|
|
ErrCode int32 `json:"errcode"`
|
|
ErrMsg string `json:"errmsg"`
|
|
Calls []jccommon.ContractCall `json:"calls"`
|
|
}{}
|
|
var itemNum int32 = goodsNum
|
|
var diamond int64 = q5.ToInt64(goodsMeta.GetDiamond()) * int64(itemNum)
|
|
var presentDiamond int64 = int64(goodsMeta.GetPresentDiamond()) * int64(itemNum)
|
|
url := fmt.Sprintf("%s/webapp/index.php", mt.Table.Web3SignCluster.RandElement().GetUrl())
|
|
f5.GetHttpCliMgr().SendGoStyleRequest(
|
|
url,
|
|
params,
|
|
func(rsp f5.HttpCliResponse) {
|
|
if rsp.GetErr() != nil {
|
|
f5.GetSysLog().Info("internalBuy error:a")
|
|
f5.RspErr(c, 500, "server internal error")
|
|
return
|
|
}
|
|
if q5.DecodeJson(rsp.GetRawData(), &jsonRspObj) != nil {
|
|
f5.GetSysLog().Info("internalBuy error:b")
|
|
f5.RspErr(c, 500, "server internal error")
|
|
return
|
|
}
|
|
if jsonRspObj.ErrCode != 0 {
|
|
c.JSON(200, jsonRspObj)
|
|
return
|
|
}
|
|
if !service.AddRechargeOrder(
|
|
orderId,
|
|
q5.ToString(shortOrderId),
|
|
netId,
|
|
accountAddress,
|
|
passportAddress,
|
|
currencyContractMeta.GetName(),
|
|
currencyContractMeta.GetAddress(),
|
|
goodsMeta.GetId(),
|
|
itemNum,
|
|
bnPrice.String(),
|
|
srcPrice,
|
|
diamond,
|
|
presentDiamond,
|
|
email) {
|
|
f5.GetSysLog().Info("internalBuy error:c")
|
|
f5.RspErr(c, 500, "server internal error")
|
|
return
|
|
}
|
|
f5.GetSysLog().Info("recharge net_id:%d currency_name:%s decimal:%d srcPrice:%d bnPrice:%s calls.len:%d jsonRsp:%s",
|
|
netId,
|
|
currencyMeta.GetCurrencyName(),
|
|
currencyMeta.GetCurrencyDecimal(),
|
|
srcPrice,
|
|
bnPrice.String(),
|
|
len(jsonRspObj.Calls),
|
|
q5.EncodeJson(&jsonRspObj));
|
|
c.JSON(200, jsonRspObj)
|
|
})
|
|
}
|
|
|
|
func (ea *RechargeApi) RechargeQuery(c *gin.Context) {
|
|
account := strings.ToLower(c.Param("account_address"))
|
|
netId := q5.ToInt64(c.Param("net_id"))
|
|
reqJson := struct {
|
|
PageSize interface{} `json:"page_size"`
|
|
Cursor interface{} `json:"cursor"`
|
|
Search struct{} `json:"search"`
|
|
Filter struct{} `json:"filter"`
|
|
Sort struct {
|
|
Fields []struct{} `json:"fields"`
|
|
} `json:"sort"`
|
|
}{}
|
|
if err := c.ShouldBindJSON(&reqJson); err != nil {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"errcode": 1,
|
|
"errmsg": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
var pageSize int32 = 0xFFFF //q5.AdjustRangeValue(q5.SafeToInt32(reqJson.PageSize), 1, 20)
|
|
cursor := q5.SafeToInt64(reqJson.Cursor)
|
|
|
|
sql := fmt.Sprintf(`
|
|
SELECT * FROM t_recharge_order
|
|
WHERE idx > %d AND net_id = %d AND passport_address = ? AND pay_status = 1`,
|
|
cursor, netId)
|
|
|
|
params := []string{account}
|
|
subFilters := []f5.DbQueryFilter{}
|
|
|
|
orderBy := " ORDER BY createtime DESC "
|
|
|
|
rspObj := struct {
|
|
ErrCode int32 `json:"errcode"`
|
|
ErrMsg string `json:"errmsg"`
|
|
Page common.StreamPagination `json:"page"`
|
|
Rows []struct {
|
|
OrderID string `gorm:"column:order_id" json:"order_id"`
|
|
ShortOrderID string `gorm:"column:short_order_id" json:"short_order_id"`
|
|
TxHash string `gorm:"column:txhash" json:"txhash"`
|
|
NetID int64 `gorm:"column:net_id" json:"net_id"`
|
|
Currency string `gorm:"column:currency_address" json:"currency"`
|
|
CurrencyName string `gorm:"column:currency_name" json:"currency_name"`
|
|
Amount string `gorm:"column:price" json:"amount"`
|
|
Diamond string `gorm:"column:diamond+present_diamond" json:"diamond"`
|
|
Date int32 `gorm:"column:createtime" json:"createtime"`
|
|
Account string `gorm:"column:account_address" json:"account_address"`
|
|
Passport string `gorm:"column:passport_address" json:"passport_address"`
|
|
} `json:"rows"`
|
|
}{}
|
|
q5.NewSlice(&rspObj.Rows, 0, 10)
|
|
|
|
f5.GetGoStyleDb().StreamPageQuery(
|
|
constant.BCNFT_DB,
|
|
pageSize,
|
|
cursor,
|
|
sql,
|
|
params,
|
|
f5.GetDbFilter().Comp(subFilters...),
|
|
orderBy,
|
|
func(err error, pagination *f5.StreamPagination) {
|
|
rspObj.Page.FillPage(pagination)
|
|
},
|
|
func(ds *f5.DataSet) {
|
|
p := q5.NewSliceElement(&rspObj.Rows)
|
|
f5.UnmarshalModel(ds, p)
|
|
})
|
|
c.JSON(200, rspObj)
|
|
}
|