hsun 2 лет назад
Родитель
Сommit
24ec66b254
4 измененных файлов с 60 добавлено и 38 удалено
  1. 5 1
      controller/contract/register.go
  2. 1 1
      controller/crm/contract.go
  3. 22 30
      services/fms/currency_rate.go
  4. 32 6
      utils/common.go

+ 5 - 1
controller/contract/register.go

@@ -2043,11 +2043,15 @@ func (rg *RegisterController) CurrencyList(c *gin.Context) {
 
 	respList := make([]*fms.CurrencyUnitItem, 0)
 	for i := range list {
+		r := rateMap[list[i].Code]
+		if list[i].Code == fms.BaseCurrencyCode {
+			r = 1
+		}
 		respList = append(respList, &fms.CurrencyUnitItem{
 			Name:    list[i].Name,
 			Code:    list[i].Code,
 			Enable:  list[i].Enable,
-			RMBRate: rateMap[list[i].Code],
+			RMBRate: r,
 		})
 	}
 	resp.OkData("获取成功", respList, c)

+ 1 - 1
controller/crm/contract.go

@@ -239,7 +239,7 @@ func (rg *ContractController) ServiceDetail(c *gin.Context) {
 		return
 	}
 
-	// 获取合同服务中的权限ID失败
+	// 获取合同服务中的权限ID, 填充contractDetail.Service.ChartPermissionIds字段
 	_, e = crmService.GetServicePermissionMap(contractDetail.Service)
 	if e != nil {
 		resp.FailMsg("获取失败", "获取合同服务中的权限ID失败, Err: "+e.Error(), c)

+ 22 - 30
services/fms/currency_rate.go

@@ -3,12 +3,8 @@ package fms
 import (
 	"encoding/json"
 	"fmt"
-	"hongze/fms_api/services/alarm_msg"
 	"hongze/fms_api/utils"
-	"io/ioutil"
-	"net/http"
 	"strconv"
-	"strings"
 	"time"
 )
 
@@ -42,16 +38,33 @@ type CurrencyRateItem struct {
 func GetCurrencyRateList() (resList []*CurrencyRateItem, err error) {
 	defer func() {
 		if err != nil {
-			go alarm_msg.SendAlarmMsg("请求汇率接口失败, Err: "+err.Error(), 3)
+			fmt.Println("请求汇率接口失败, Err: " + err.Error())
+			//go alarm_msg.SendAlarmMsg("请求汇率接口失败, Err: "+err.Error(), 3)
 		}
 	}()
-	// TODO:请求接口
-	//func HttpCurrencyRateApi(url, method, postData string, params ...string) ([]byte, error) {
+
+	// TODO:请求汇率接口
+	query := "?currency=CNY"
+	reqUrl := fmt.Sprint(CurrencyRateApiHost, CurrencyRateExchangeSingle, query)
+	auth := fmt.Sprintf("APPCODE %s", CurrencyRateApiAppCode)
+	resultByte, e := utils.PublicGetRequest(reqUrl, auth)
+	fmt.Println("reqUrl: ", reqUrl)
+	fmt.Println("PublicGetRequest err: ", e)
+	if e != nil {
+		err = fmt.Errorf("HttpCurrencyRateApi errMsg: %s", e.Error())
+		return
+	}
+	if len(resultByte) == 0 {
+		err = fmt.Errorf("HttpCurrencyRateApi empty data")
+		return
+	}
 
 	// 汇率结果
-	resultJson := `{"status":"0","msg":"ok","result":{"currency":"CNY","name":"人民币","list":{"HKD":{"name":"港币","rate":"1.2198","updatetime":"2015-10-26 16:56:22"},"USD":{"name":"美元","rate":"0.1574","updatetime":"2015-10-26 16:56:22"},"EUR":{"name":"欧元","rate":"0.1426","updatetime":"2015-10-26 16:56:22"}}}}`
+	//resultJson := `{"status":"0","msg":"ok","result":{"currency":"CNY","name":"人民币","list":{"HKD":{"name":"港币","rate":"1.2198","updatetime":"2015-10-26 16:56:22"},"USD":{"name":"美元","rate":"0.1574","updatetime":"2015-10-26 16:56:22"},"EUR":{"name":"欧元","rate":"0.1426","updatetime":"2015-10-26 16:56:22"}}}}`
 	results := new(CurrencyRateApiResp)
-	if e := json.Unmarshal([]byte(resultJson), &results); e != nil {
+	fmt.Println("result JSON: ", string(resultByte))
+	if e := json.Unmarshal(resultByte, &results); e != nil {
+		//if e := json.Unmarshal([]byte(resultJson), &results); e != nil {
 		err = fmt.Errorf("rate api result unmarshal err: %s", results.Msg)
 		return
 	}
@@ -85,24 +98,3 @@ func GetCurrencyRateList() (resList []*CurrencyRateItem, err error) {
 	}
 	return
 }
-
-func HttpCurrencyRateApi(url, method, postData string, params ...string) ([]byte, error) {
-	body := ioutil.NopCloser(strings.NewReader(postData))
-	client := &http.Client{}
-	req, err := http.NewRequest(method, url, body)
-	if err != nil {
-		return nil, err
-	}
-	contentType := "application/json;charset=utf-8"
-	if len(params) > 0 && params[0] != "" {
-		contentType = params[0]
-	}
-	req.Header.Set("Content-Type", contentType)
-	req.Header.Set("Authorization", fmt.Sprintf("APPCODE %s", CurrencyRateApiAppCode))
-	resp, err := client.Do(req)
-	defer func() {
-		_ = resp.Body.Close()
-	}()
-	b, err := ioutil.ReadAll(resp.Body)
-	return b, err
-}

+ 32 - 6
utils/common.go

@@ -13,6 +13,7 @@ import (
 	"image"
 	"image/png"
 	"io"
+	"io/ioutil"
 	"math"
 	"math/rand"
 	"net"
@@ -48,7 +49,7 @@ func GenToken(account string) (accessToken string, err error) {
 	return
 }
 
-//随机数种子
+// 随机数种子
 var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
 
 // GetRandString 获取随机字符串
@@ -406,8 +407,8 @@ func TrimHtml(src string) string {
 	return strings.TrimSpace(src)
 }
 
-//1556164246  ->  2019-04-25 03:50:46 +0000
-//timestamp
+// 1556164246  ->  2019-04-25 03:50:46 +0000
+// timestamp
 // TimeToTimestamp
 func TimeToTimestamp() {
 	fmt.Println(time.Unix(1556164246, 0).Format(FormatDateTime))
@@ -467,7 +468,7 @@ func GetWilsonScore(p, n float64) float64 {
 	return toFixed(((p+1.9208)/(p+n)-1.96*math.Sqrt(p*n/(p+n)+0.9604)/(p+n))/(1+3.8416/(p+n)), 2)
 }
 
-//将中文数字转化成数字,比如 第三百四十五章,返回第345章 不支持一亿及以上
+// 将中文数字转化成数字,比如 第三百四十五章,返回第345章 不支持一亿及以上
 func ChangeWordsToNum(str string) (numStr string) {
 	words := ([]rune)(str)
 	num := 0
@@ -643,7 +644,7 @@ func GetMonthStartAndEnd(myYear string, myMonth string) (startDate, endDate stri
 	return t1, t2
 }
 
-//TrimStr 移除字符串中的空格
+// TrimStr 移除字符串中的空格
 func TrimStr(str string) (str2 string) {
 	return strings.Replace(str, " ", "", -1)
 }
@@ -1072,4 +1073,29 @@ func InArrayByStr(idStrList []string, searchId string) (has bool) {
 		}
 	}
 	return
-}
+}
+
+// PublicGetRequest 公共Get请求方法
+func PublicGetRequest(url, authorization string) (body []byte, err error) {
+	method := "GET"
+	client := &http.Client{}
+	req, err := http.NewRequest(method, url, nil)
+	if err != nil {
+		return
+	}
+	if authorization != "" {
+		req.Header.Add("Authorization", authorization)
+	}
+	res, err := client.Do(req)
+	if err != nil {
+		return
+	}
+	defer func() {
+		_ = res.Body.Close()
+	}()
+	body, err = ioutil.ReadAll(res.Body)
+	if err != nil {
+		return
+	}
+	return
+}