Bläddra i källkod

“获取用户信息”接口增加解密和脱敏功能

zhou.xiaoning 4 år sedan
förälder
incheckning
c0c99b047b
5 ändrade filer med 66 tillägg och 8 borttagningar
  1. 6 6
      config/config.xml
  2. 44 2
      controllers/user/user.go
  3. 6 0
      docs/docs.go
  4. 6 0
      docs/swagger.json
  5. 4 0
      docs/swagger.yaml

+ 6 - 6
config/config.xml

@@ -13,11 +13,11 @@
     <DbAddress value="192.168.31.117"/>
     <DbName value="orcl"/>
     <DbPort value="1521"/>
-    <DbUser value="mtp2_test211"/>
+    <DbUser value="mtp2_test171"/>
     <DbPwd value="muchinfo"/>
   </DbSetting>
   <RedisSetting>
-    <Address value="192.168.30.211"/>
+    <Address value="192.168.31.171"/>
     <Port value="5007"/>
     <Timeout value="3"/>
     <ConnNum value="1"/>
@@ -25,11 +25,11 @@
     <Pwd value=""/>
   </RedisSetting>
   <MqSetting>
-    <Url value="amqp://guest:guest@192.168.30.211:5020/test"/>
+    <Url value="amqp://guest:guest@192.168.31.171:5020/test"/>
     <Exchange value="entry"/>
   </MqSetting>
   <MongoDBSetting>
-    <HostName value="192.168.30.211"/>
+    <HostName value="192.168.31.171"/>
     <Port value="5025"/>
     <DBName value="HistoryQuote"/>
     <Username value="quote_test01"/>
@@ -38,8 +38,8 @@
   <MySQLSetting>
     <Host value="192.168.30.140"/>
     <Port value="3306"/>
-    <DBName value="historyquote_test211"/>
-    <Username value="quote_test211"/>
+    <DBName value="historyquote_test171"/>
+    <Username value="root"/>
     <Password value="123456"/>
   </MySQLSetting>
 </Configuration>

+ 44 - 2
controllers/user/user.go

@@ -1,10 +1,13 @@
 package user
 
 import (
+	"encoding/hex"
+	"fmt"
 	"mtp2_if/global/app"
 	"mtp2_if/global/e"
 	"mtp2_if/logger"
 	"mtp2_if/models"
+	"mtp2_if/utils"
 	"net/http"
 
 	"github.com/gin-gonic/gin"
@@ -52,7 +55,8 @@ func QueryUserReferNum(c *gin.Context) {
 
 // QueryUserInfoReq 获取用户信息请求参数
 type QueryUserInfoReq struct {
-	UserID int `form:"userID" binding:"required"`
+	UserID    int  `form:"userID" binding:"required"`
+	IsDecrypt bool `form:"isDecrypt"`
 }
 
 // QueryUserInfo 获取用户信息
@@ -60,6 +64,7 @@ type QueryUserInfoReq struct {
 // @Produce json
 // @Security ApiKeyAuth
 // @Param userID query int true "用户ID"
+// @Param isDecrypt query bool false "是否解密"
 // @Success 200 {object} models.Userinfo
 // @Failure 500 {object} app.Response
 // @Router /User/QueryUserInfo [get]
@@ -68,7 +73,7 @@ func QueryUserInfo(c *gin.Context) {
 	appG := app.Gin{C: c}
 
 	// 获取请求参数
-	var req QueryUserReferNumReq
+	var req QueryUserInfoReq
 	if err := appG.C.ShouldBindQuery(&req); err != nil {
 		logger.GetLogger().Errorf("QueryUserInfo failed: %s", err.Error())
 		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
@@ -87,6 +92,43 @@ func QueryUserInfo(c *gin.Context) {
 		return
 	}
 
+	// 解密
+	if req.IsDecrypt {
+		key, _ := hex.DecodeString(utils.AESSecretKey)
+		// 手机号码解密
+		if len(data.Mobile) > 0 {
+			if phonenum, err := hex.DecodeString(data.Mobile); err == nil { // hex -> []byte
+				if mobile, err := utils.AESDecrypt(phonenum, key); err == nil {
+					// 脱敏
+					tmp := string(mobile)
+					l := len(tmp)
+					if l > 7 {
+						tmp = fmt.Sprintf("%s****%s", tmp[:3], tmp[l-4:])
+					} else {
+						tmp = fmt.Sprintf("%s****", tmp[:3])
+					}
+					data.Mobile = tmp
+				}
+			}
+		}
+		// 证件号码解密
+		if len(data.Cardnum) > 0 {
+			if cardnum, err := hex.DecodeString(data.Cardnum); err == nil { // hex -> []byte
+				if c, err := utils.AESDecrypt(cardnum, key); err == nil {
+					// 脱敏
+					tmp := string(c)
+					l := len(tmp)
+					if l > 7 {
+						tmp = fmt.Sprintf("%s****%s", tmp[:3], tmp[l-4:])
+					} else {
+						tmp = fmt.Sprintf("%s****", tmp[:3])
+					}
+					data.Cardnum = tmp
+				}
+			}
+		}
+	}
+
 	// 查询成功
 	logger.GetLogger().Debugln("QueryUserInfo successed: %v", data)
 	appG.Response(http.StatusOK, e.SUCCESS, data)

+ 6 - 0
docs/docs.go

@@ -6769,6 +6769,12 @@ var doc = `{
                         "name": "userID",
                         "in": "query",
                         "required": true
+                    },
+                    {
+                        "type": "boolean",
+                        "description": "是否解密",
+                        "name": "isDecrypt",
+                        "in": "query"
                     }
                 ],
                 "responses": {

+ 6 - 0
docs/swagger.json

@@ -6753,6 +6753,12 @@
                         "name": "userID",
                         "in": "query",
                         "required": true
+                    },
+                    {
+                        "type": "boolean",
+                        "description": "是否解密",
+                        "name": "isDecrypt",
+                        "in": "query"
                     }
                 ],
                 "responses": {

+ 4 - 0
docs/swagger.yaml

@@ -14201,6 +14201,10 @@ paths:
         name: userID
         required: true
         type: integer
+      - description: 是否解密
+        in: query
+        name: isDecrypt
+        type: boolean
       produces:
       - application/json
       responses: