232 lines
6.4 KiB
Go
232 lines
6.4 KiB
Go
package recharge
|
|
|
|
import (
|
|
"f5"
|
|
"fmt"
|
|
"jccommon"
|
|
"main/common"
|
|
"main/constant"
|
|
"main/service"
|
|
|
|
"main/mt"
|
|
"net/http"
|
|
"q5"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type RechargeApi struct {
|
|
}
|
|
|
|
func (ea *RechargeApi) RechargeList(c *gin.Context) {
|
|
rspObj := struct {
|
|
ErrCode int32 `json:"errcode"`
|
|
ErrMsg string `json:"errmsg"`
|
|
Rows []interface{} `json:"rows"`
|
|
}{}
|
|
|
|
mt.Table.Recharge.RawMetaTable.Traverse(func(tb *mt.Recharge) bool {
|
|
tmpmap := map[string]interface{}{}
|
|
tmpmap["goods_id"] = tb.GetId()
|
|
tmpmap["diamond"] = tb.GetDiamond()
|
|
tmpmap["price"] = tb.GetPrice()
|
|
tmpmap["max_buy_times"] = tb.GetMaxBuyTimes()
|
|
|
|
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"`
|
|
Num int32 `json:"num"`
|
|
AccountAddress string `json:"account_address"`
|
|
}{}
|
|
if err := c.ShouldBindJSON(&reqJson); err != nil {
|
|
f5.RspErr(c, 1, err.Error())
|
|
return
|
|
}
|
|
currencyMeta := mt.Table.RechargeCurrency.Get(reqJson.NetId)
|
|
if currencyMeta == nil {
|
|
f5.RspErr(c, 2, "server internal error")
|
|
return
|
|
}
|
|
if currencyMeta.GetExchangeRate() <= 0 ||
|
|
currencyMeta.GetExchangeRate() > constant.RECHARGE_CURRENCY_MAX_EXCHANGE_RAET {
|
|
f5.RspErr(c, 2, "server internal error")
|
|
return
|
|
}
|
|
if currencyMeta.GetCurrencyDecimal() <= 0 ||
|
|
currencyMeta.GetCurrencyDecimal() > constant.RECHARGE_CURRENCY_MAX_DECIMAL {
|
|
f5.RspErr(c, 2, "server internal error")
|
|
return
|
|
}
|
|
currencyContractMeta := mt.Table.Contract.GetByNetIdName(reqJson.NetId, currencyMeta.GetCurrencyName())
|
|
if currencyContractMeta == nil {
|
|
f5.RspErr(c, 2, "server internal error")
|
|
return
|
|
}
|
|
goodsMeta := mt.Table.Recharge.GetById(q5.ToInt64(reqJson.GoodsId))
|
|
if goodsMeta == nil {
|
|
f5.RspErr(c, 2, "goods id param error")
|
|
return
|
|
}
|
|
if reqJson.Num < 0 || reqJson.Num > goodsMeta.GetMaxBuyTimes() {
|
|
f5.RspErr(c, 2, "num param error")
|
|
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(reqJson.Num),
|
|
q5.ToInt64(goodsMeta.GetId()),
|
|
shortOrderId,
|
|
})
|
|
var err error
|
|
orderId, err = transId.ToString()
|
|
if err != nil {
|
|
f5.RspErr(c, 2, "server internal error")
|
|
return
|
|
}
|
|
}
|
|
price := q5.PowInt64(10, currencyMeta.GetCurrencyDecimal()) * q5.ToInt64(goodsMeta.GetPrice()) *
|
|
q5.ToInt64(reqJson.Num)
|
|
params := map[string]string{
|
|
"c": "BcService",
|
|
"a": "recharge",
|
|
"net_id": q5.ToString(reqJson.NetId),
|
|
"order_id": orderId,
|
|
"account_address": reqJson.AccountAddress,
|
|
"passport_address": passportAddress,
|
|
"amount": q5.ToString(price),
|
|
"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 = reqJson.Num
|
|
var diamond int64 = currencyMeta.GetExchangeRate() * q5.ToInt64(goodsMeta.GetDiamond()) * 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.RspErr(c, 500, "server internal error")
|
|
return
|
|
}
|
|
if q5.DecodeJson(rsp.GetRawData(), &jsonRspObj) != nil {
|
|
f5.RspErr(c, 500, "server internal error")
|
|
return
|
|
}
|
|
if jsonRspObj.ErrCode != 0 {
|
|
c.JSON(200, jsonRspObj)
|
|
return
|
|
}
|
|
if !service.AddRechargeOrder(
|
|
orderId,
|
|
q5.ToString(shortOrderId),
|
|
reqJson.NetId,
|
|
reqJson.AccountAddress,
|
|
passportAddress,
|
|
currencyContractMeta.GetName(),
|
|
currencyContractMeta.GetAddress(),
|
|
goodsMeta.GetId(),
|
|
itemNum,
|
|
q5.ToString(price),
|
|
diamond) {
|
|
f5.RspErr(c, 500, "server internal error")
|
|
return
|
|
}
|
|
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" 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)
|
|
}
|