package fms import ( "context" "encoding/json" "fmt" jsoniter "github.com/json-iterator/go" "hongze/fms_api/global" "hongze/fms_api/models/fms" "hongze/fms_api/services/alarm_msg" "hongze/fms_api/utils" "strconv" "time" ) const ( CurrencyRateApiHost = "https://jisuhuilv.market.alicloudapi.com" CurrencyRateExchangeSingle = "/exchange/single" CurrencyRateApiAppCode = "22553c4ba74545568aba70ac6cfd441d" CurrencyRateApiAppKey = "203889624" CurrencyRateApiAppSecret = "voampGGl0yGNx5xA1sZdmZlV2EiBct2P" ) type CurrencyRateApiResp struct { Status int `json:"status"` Msg string `json:"msg"` Result struct { Currency string `json:"currency"` Name string `json:"name"` List map[string]struct { Name string `json:"name"` Rate string `json:"rate"` UpdateTime string `json:"updatetime"` } `json:"list"` } `json:"result"` } type CurrencyRateItem struct { Name string `json:"name"` Code string `json:"code"` Rate float64 `json:"rate"` UpdateTime time.Time `json:"update_time"` } // CurlCurrencyRateApi 请求货币汇率接口 func CurlCurrencyRateApi() (resList []*CurrencyRateItem, err error) { defer func() { if err != nil { go alarm_msg.SendAlarmMsg("请求汇率接口失败, Err: "+err.Error(), 3) } }() // 请求汇率接口 query := "?currency=CNY" reqUrl := fmt.Sprint(CurrencyRateApiHost, CurrencyRateExchangeSingle, query) auth := fmt.Sprintf("APPCODE %s", CurrencyRateApiAppCode) resultByte, e := utils.PublicGetRequest(reqUrl, auth) if e != nil { err = fmt.Errorf("HttpCurrencyRateApi errMsg: %s", e.Error()) return } if len(resultByte) == 0 { err = fmt.Errorf("HttpCurrencyRateApi empty data") return } // 汇率结果 results := new(CurrencyRateApiResp) if e := json.Unmarshal(resultByte, &results); e != nil { err = fmt.Errorf("rate api result unmarshal err: %s", e.Error()) return } if results.Status != 0 { err = fmt.Errorf("curl rate api errMsg: %s", results.Msg) return } list := results.Result.List if list == nil { err = fmt.Errorf("rate api list is empty") return } resList = make([]*CurrencyRateItem, 0) for k, v := range list { item := new(CurrencyRateItem) item.Code = k item.Name = v.Name f, e := strconv.ParseFloat(v.Rate, 64) if e != nil { err = fmt.Errorf("rate api rate parse err: %v", e) return } item.Rate = f t, e := time.ParseInLocation(utils.FormatDateTime, v.UpdateTime, time.Local) if e != nil { global.LOG.Errorf("rate api time parse err:", e) continue } item.UpdateTime = t resList = append(resList, item) } return } // GetTodayCurrencyRateList 获取今日货币及汇率列表 func GetTodayCurrencyRateList() (list []*fms.CurrencyUnitItem, err error) { list = make([]*fms.CurrencyUnitItem, 0) // 货币列表 ob := new(fms.CurrencyUnit) cond := `enable = 1` pars := make([]interface{}, 0) currencyList, e := ob.List(cond, pars) if e != nil { err = fmt.Errorf("获取货币列表失败, Err: %s", e.Error()) return } rateMap := make(map[string]float64) rateList := make([]*CurrencyRateItem, 0) // 读取缓存 cacheJson, _ := global.Redis.Get(context.TODO(), utils.CACHE_KEY_CURRENCY_RMB_RATE).Result() if cacheJson != "" { if e = jsoniter.UnmarshalFromString(cacheJson, &rateList); e != nil { err = fmt.Errorf("解析汇率缓存数据失败, Err: %s", e.Error()) return } } // 请求汇率接口 if cacheJson == "" { rateList, e = CurlCurrencyRateApi() if e != nil { err = fmt.Errorf("请求汇率接口失败, Err: %s", e.Error()) return } if len(rateList) == 0 { err = fmt.Errorf("汇率接口返回数据为空") return } newCache, e := jsoniter.MarshalToString(rateList) if e != nil { err = fmt.Errorf("写入汇率缓存失败, Err: %s", e.Error()) return } now := time.Now() todayLast := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, time.Local) sub := todayLast.Sub(now) _ = global.Redis.SetEX(context.TODO(), utils.CACHE_KEY_CURRENCY_RMB_RATE, newCache, sub) } for i := range rateList { rateMap[rateList[i].Code] = rateList[i].Rate } for i := range currencyList { r := rateMap[currencyList[i].Code] if currencyList[i].Code == fms.BaseCurrencyCode { r = 1 } list = append(list, &fms.CurrencyUnitItem{ Name: currencyList[i].Name, UnitName: currencyList[i].UnitName, Code: currencyList[i].Code, Enable: currencyList[i].Enable, RMBRate: r, FlagImg: currencyList[i].FlagImg, }) } return }