Commit 3ecdce0d authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/optimize' into develop

parents f0034512 a42397af
......@@ -9,8 +9,11 @@ type CoinTicker struct {
Model
CoinName string `json:"coin_name"`
CoinId int `json:"coin_id"` //币种ID`
Ticker string `json:"ticker"`
PlatformId int `json:"platform_id"`
CoinInfo *Coin `gorm:"foreignkey:coin_id" json:"coin_info"`
}
type CoinTickerResp struct {
......@@ -40,7 +43,7 @@ func GetCoinTicker(id int) (*CoinTicker, error) {
func GetCoinsTicker(maps interface{}) ([]*CoinTicker, error) {
var ct []*CoinTicker
err := db.Where(maps).Find(&ct).Error
err := db.Preload("CoinInfo").Where(maps).Find(&ct).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
......@@ -65,6 +68,7 @@ func ExistCoin(coin_name string, platform_id int) (bool, error) {
func AddCoinTicker(data map[string]interface{}) (error) {
coinTicker := CoinTicker{
CoinName: data["coin_name"].(string),
CoinId: data["coin_id"].(int),
Ticker: data["ticker"].(string),
PlatformId: data["platform_id"].(int),
}
......
......@@ -4,6 +4,7 @@ import (
"bwallet/pkg/errno"
"bwallet/pkg/handler"
"bwallet/pkg/util"
"bwallet/service/coin_service"
"bwallet/service/coin_ticker_service"
"bwallet/service/wallet_service"
"bwallet/validate_service"
......@@ -34,8 +35,7 @@ func CoinsTicker(c *gin.Context) {
return
}
wallet, err := walletService.Get()
platform_id = wallet.ChainId
platform_id = user.UserInfo.PlatformId
} else {
if arg := c.Query("platform_id"); arg != "" {
platform_id = com.StrTo(c.Query("platform_id")).MustInt()
......@@ -76,6 +76,17 @@ func AddCoinTicker(c *gin.Context) {
}
}
coin_model := coin_service.Coin{Id: coin_ticker.CoinId}
exists, err := coin_model.ExistById()
if err != nil {
handler.SendResponse(c, errno.ErrCoinNotFound, nil)
return
}
if !exists {
handler.SendResponse(c, errno.ErrCoinNotFound, nil)
return
}
CoinTickerValidateService := coin_ticker_service.CoinTicker{
CoinName: coin_ticker.CoinName,
PlatformId: platform_id,
......@@ -88,6 +99,7 @@ func AddCoinTicker(c *gin.Context) {
CoinTickerService := coin_ticker_service.CoinTicker{
CoinName: strings.ToUpper(coin_ticker.CoinName),
CoinId: coin_ticker.CoinId,
Ticker: coin_ticker.Ticker,
PlatformId: platform_id,
}
......@@ -139,6 +151,7 @@ func UpdateCoinTicker(c *gin.Context) {
CoinTickerService := coin_ticker_service.CoinTicker{
CoinName: strings.ToUpper(coin_ticker.CoinName),
CoinId: coin_ticker.CoinId,
PlatformId: platform_id,
Ticker: coin_ticker.Ticker,
}
......
......@@ -2,13 +2,6 @@ package article_service
import (
"bwallet/models"
"bwallet/pkg/errno"
"bwallet/pkg/util"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"
)
......@@ -35,48 +28,6 @@ func (a *Article) Get() (*models.Article, error) {
return nil, err
}
if -1 == article.PlatformId {
/*
https://iapi.bishijie.com/article/list?app_id=8415d8c1c4f762d8&language=1&page=1&size=20&timestamp=1595918443&sign=3b5f70e637187a9b28d35b4cfccce6b8
*/
params := make(map[string]string)
params["app_id"] = "8415d8c1c4f762d8"
params["id"] = article.Content
params["language"] = "1"
params["timestamp"] = strconv.FormatInt(time.Now().UTC().Unix(), 10)
req, err := http.NewRequest(http.MethodGet, "https://iapi.bishijie.com/article/detail", nil)
if err != nil {
return nil, err
}
condition := make(url.Values)
condition.Add("app_id", params["app_id"])
condition.Add("id", article.Content)
condition.Add("language", params["language"])
condition.Add("timestamp", params["timestamp"])
sign := util.StringToMd5(condition.Encode() + "09395dc651588c375d066666e072a714")
condition.Add("sign", sign)
req.URL.RawQuery = condition.Encode()
r, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
result := map[string]interface{}{}
json.Unmarshal(body, &result)
if _, ok := result["code"]; ok {
return nil, errno.InternalServerError
}
article.Content = result["content"].(string)
}
return article, nil
}
......
......@@ -6,23 +6,48 @@ import (
type CoinTicker struct {
Id int
CoinId int
CoinName string
Ticker string
PlatformId int
}
func (ct *CoinTicker) GetAll() ([]*models.CoinTicker, error) {
type CoinTickerResp struct {
Id int `json:"id"`
CoinName string `json:"coin_name"`
CoinId int `json:"coin_id"`
CoinTicker string `json:"coin_ticker"`
Icon string `json:"icon"`
Platform string `json:"platform"`
Chain string `json:"chain"`
}
func (ct *CoinTicker) GetAll() ([]*CoinTickerResp, error) {
coins_ticker, err := models.GetCoinsTicker(ct.getMaps())
if err != nil {
return nil, err
}
return coins_ticker, nil
var coinsResp = []*CoinTickerResp{}
for _, val := range coins_ticker {
tmp := &CoinTickerResp{}
tmp.Id = val.ID
tmp.CoinName = val.CoinInfo.Name
tmp.CoinId = val.CoinId
tmp.CoinTicker = val.Ticker
tmp.Icon = val.CoinInfo.Icon
tmp.Platform = val.CoinInfo.Platform
tmp.Chain = val.CoinInfo.Chain
coinsResp = append(coinsResp, tmp)
}
return coinsResp, nil
}
func (t *CoinTicker) Add() error {
coinTicker := map[string]interface{}{
"coin_name": t.CoinName,
"coin_id": t.CoinId,
"ticker": t.Ticker,
"platform_id": t.PlatformId,
}
......
......@@ -4,4 +4,5 @@ type CoinTicker struct {
CoinName string `json:"coin_name" validate:"required"`
Ticker string `json:"ticker" validate:"required"`
PlatformId int `json:"platform_id" validate:"required"`
CoinId int `json:"coin_id" validate:"required"`
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment