kobe6258 8 сар өмнө
parent
commit
dc6af1ab25

+ 14 - 0
common/component/config/ht_biz_config.go

@@ -6,6 +6,8 @@ import "eta/eta_mini_ht_api/common/contants"
 type HTOpts struct {
 	ReportIndex string
 	MediaIndex  string
+	Encode      string
+	DesCode     string
 }
 type HTBizConfig struct {
 	BaseConfig
@@ -20,10 +22,22 @@ func (e *HTBizConfig) GetMediaIndex() string {
 	return e.opts.MediaIndex
 }
 
+func (e *HTBizConfig) NeedEncode() bool {
+	if e.opts.Encode == "true" {
+		return true
+	}
+	return false
+}
+
+func (e *HTBizConfig) GetDesCode() string {
+	return e.opts.DesCode
+}
 func (e *HTBizConfig) InitConfig() {
 	opts := HTOpts{
 		ReportIndex: e.GetString("es_report_index"),
 		MediaIndex:  e.GetString("es_media_index"),
+		Encode:      e.GetString("response.encode"),
+		DesCode:     e.GetString("response.des_code"),
 	}
 	e.opts = opts
 }

+ 31 - 0
common/utils/auth/auth_utils.go

@@ -1,7 +1,11 @@
 package auth
 
 import (
+	"bytes"
+	"crypto/cipher"
+	"crypto/des"
 	"crypto/rand"
+	"encoding/base64"
 	"eta/eta_mini_ht_api/common/exception"
 	stringUtils "eta/eta_mini_ht_api/common/utils/string"
 	"math/big"
@@ -35,3 +39,30 @@ func IsValidMobile(mobile string) bool {
 	matched, _ := regexp.MatchString(regex, mobile)
 	return matched
 }
+
+func DesBase64Encrypt(origData []byte, desKey string) []byte {
+	result, err := tripleDesEncrypt(origData, []byte(desKey))
+	if err != nil {
+		panic(any(err))
+	}
+	return []byte(base64.StdEncoding.EncodeToString(result))
+}
+
+// 3DES加密
+func tripleDesEncrypt(origData, key []byte) ([]byte, error) {
+	block, err := des.NewTripleDESCipher(key)
+	if err != nil {
+		return nil, err
+	}
+	origData = pKCS5Padding(origData, block.BlockSize())
+	// origData = ZeroPadding(origData, block.BlockSize())
+	blockMode := cipher.NewCBCEncrypter(block, key[:8])
+	crypted := make([]byte, len(origData))
+	blockMode.CryptBlocks(crypted, origData)
+	return crypted, nil
+}
+func pKCS5Padding(ciphertext []byte, blockSize int) []byte {
+	padding := blockSize - len(ciphertext)%blockSize
+	padtext := bytes.Repeat([]byte{byte(padding)}, padding)
+	return append(ciphertext, padtext...)
+}

+ 25 - 2
controllers/base_controller.go

@@ -3,13 +3,20 @@ package controllers
 import (
 	"encoding/json"
 	"errors"
+	"eta/eta_mini_ht_api/common/component/config"
 	logger "eta/eta_mini_ht_api/common/component/log"
+	"eta/eta_mini_ht_api/common/contants"
 	"eta/eta_mini_ht_api/common/exception"
 	"eta/eta_mini_ht_api/common/http"
+	"eta/eta_mini_ht_api/common/utils/auth"
 	"github.com/beego/beego/v2/server/web"
 	"net/url"
 )
 
+var (
+	htConfig = config.GetConfig(contants.HT).(*config.HTBizConfig)
+)
+
 type WrapData struct {
 	Msg  string
 	Data interface{}
@@ -40,7 +47,15 @@ func (b *BaseController) FailResponse(errInfo error, msg string) {
 		ErrMsg:  etaError.ErrorMsg,
 		ErrCode: etaError.ErrorCode,
 		Data:    nil}
-	b.Data["json"] = retData
+	content, err := json.Marshal(retData)
+	if err != nil {
+		logger.Error("加密失败")
+	} else {
+		if htConfig.NeedEncode() {
+			content = auth.DesBase64Encrypt(content, htConfig.GetDesCode())
+		}
+	}
+	b.Data["json"] = content
 	b.ServeJSON()
 }
 
@@ -55,7 +70,15 @@ func (b *BaseController) JsonResult(status int, errCode int, errMsg string, msg
 		Success: success}
 
 	b.Ctx.Output.SetStatus(status)
-	b.Data["json"] = retData
+	content, err := json.Marshal(retData)
+	if err != nil {
+		logger.Error("加密失败")
+	} else {
+		if htConfig.NeedEncode() {
+			content = auth.DesBase64Encrypt(content, htConfig.GetDesCode())
+		}
+	}
+	b.Data["json"] = content
 	b.ServeJSON()
 }