Bladeren bron

设备信息更新

wangbin 2 jaren geleden
bovenliggende
commit
8a161f5f9d
4 gewijzigde bestanden met toevoegingen van 229 en 22 verwijderingen
  1. 1 1
      api/v1/log/loging.go
  2. 36 5
      model/log/device_log.go
  3. 112 0
      service/log/log_statistics.go
  4. 80 16
      service/log/loging/logical_log.go

+ 1 - 1
api/v1/log/loging.go

@@ -409,7 +409,7 @@ func (s *ApiLoging) ComputerHeartbeat(c *gin.Context) {
 func (s *ApiLoging) ComputerTest(c *gin.Context) {
 	var paramsInfo request.OnlineComputerRequest
 	_ = c.ShouldBindJSON(&paramsInfo)
-	ServiceStatisticsLog.RegularCheckPc()
+	ServiceStatisticsLog.ImageIdentify(paramsInfo.PcCode)
 	response.OkWithDetailed("b", "上报成功", c)
 }
 

+ 36 - 5
model/log/device_log.go

@@ -54,12 +54,43 @@ type DeviceHex struct {
 	GameId             int    `json:"game_id"`
 }
 
+type DeviceErr struct {
+	Id                 uint      `json:"id"`
+	GameId             int       `json:"game_id"`
+	Account            string    `json:"account"`
+	ScriptDeviceId     string    `json:"script_device_id"`
+	DeviceManufacturer string    `json:"device_manufacturer"`
+	DeviceModel        string    `json:"device_model"`
+	DeviceImei         string    `json:"device_imei"`
+	DeviceSdk          string    `json:"device_sdk"`
+	DeviceMac          string    `json:"device_mac"`
+	DeviceNumber       string    `json:"device_number"`
+	DeviceId           string    `json:"device_id"`
+	DeviceHex          string    `json:"device_hex"`
+	AccountHex         string    `json:"account_hex"`
+	CreateTime         time.Time `json:"create_time"` // 创建时间
+	CreateDate         time.Time `json:"create_date"`
+	DeviceLogId        uint      `json:"device_log_id"`
+}
+
 type GameAccount struct {
-	Id             uint      `json:"id"`
-	GameId         int       `json:"game_id"`
-	Account        string    `json:"account"`
-	ScriptDeviceId string    `json:"script_device_id"`
-	CreateTime     time.Time `json:"create_time"` // 创建时间
+	Id                 uint      `json:"id"`
+	GameId             int       `json:"game_id"`
+	Account            string    `json:"account"`
+	ScriptDeviceId     string    `json:"script_device_id"`
+	DeviceManufacturer string    `json:"device_manufacturer"`
+	DeviceModel        string    `json:"device_model"`
+	DeviceImei         string    `json:"device_imei"`
+	DeviceSdk          string    `json:"device_sdk"`
+	DeviceMac          string    `json:"device_mac"`
+	DeviceNumber       string    `json:"device_number"`
+	DeviceId           string    `json:"device_id"`
+	DeviceHex          string    `json:"device_hex"`
+	AccountHex         string    `json:"account_hex"`
+	CreateTime         time.Time `json:"create_time"` // 创建时间
+	UseNum             int       `json:"use_num"`     //账号登录成功次数
+	Identify           int       `json:"identify"`    //图片识别的内容
+	UpdateTime         time.Time `json:"update_time"`
 }
 
 func (GameAccount) TableName() string {

+ 112 - 0
service/log/log_statistics.go

@@ -9,6 +9,7 @@ import (
 	"github.com/xuri/excelize/v2"
 	"go.uber.org/zap"
 	"gorm.io/gorm"
+	"io/ioutil"
 	"log-server/global"
 	"log-server/model/log"
 	"log-server/model/log/request"
@@ -17,6 +18,8 @@ import (
 	"log-server/model/typeManage"
 	loging2 "log-server/service/log/loging"
 	"log-server/utils"
+	"net/http"
+	"net/url"
 	"strconv"
 	"strings"
 	"time"
@@ -1739,3 +1742,112 @@ func (s *ServiceStatisticsLog) SupConErr(pcCode, describe string) (err error) {
 	s.SendContent(ct, mpsPerson[name].Url)
 	return
 }
+
+const API_KEY = "z9GNcyrC7VeV3g1xXEj3YL1s"
+const SECRET_KEY = "VASRBsEzeVsyKduSkkflfL87r5yqoqvj"
+const BAIDU_IDENTIFY = "baiduIdentifyToken"
+
+func (s *ServiceStatisticsLog) ImageIdentify(content string) {
+	token, err := GetAccessToken()
+	if err != nil {
+		global.GVA_LOG.Error("get token fail", zap.Error(err))
+		return
+	}
+	reqUrl := "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + token
+	var (
+		params = url.Values{}
+	)
+	params.Set("image", content)
+	requestData := params.Encode()
+	payload := strings.NewReader(requestData)
+	client := &http.Client{}
+	req, err := http.NewRequest("POST", reqUrl, payload)
+
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
+	req.Header.Add("Accept", "application/json")
+
+	res, err := client.Do(req)
+	if err != nil {
+		global.GVA_LOG.Error("request Identify fail", zap.Error(err))
+		return
+	}
+	defer res.Body.Close()
+
+	body, err := ioutil.ReadAll(res.Body)
+	if err != nil {
+		global.GVA_LOG.Error("Read Body Fail", zap.Error(err))
+		return
+	}
+	fmt.Println(string(body))
+	type Data struct {
+		WordsResult []struct {
+			Words string `json:"words"`
+		} `json:"words_result"`
+		WordsResultNum int `json:"words_result_num"`
+		LogId          int `json:"log_id"`
+	}
+	var data Data
+	err = json.Unmarshal(body, &data)
+	if err != nil {
+		global.GVA_LOG.Error("Read Body Fail", zap.Error(err))
+		return
+	}
+	identify := 0
+	if len(data.WordsResult) > 1 {
+		for _, words := range data.WordsResult {
+			i, err := strconv.Atoi(words.Words)
+			if err == nil {
+				identify = i
+				break
+			}
+		}
+	} else {
+		i, _ := strconv.Atoi(data.WordsResult[0].Words)
+		identify = i
+	}
+	if identify == 0 {
+		return
+	}
+	fmt.Println("-----LogId-----", data.LogId)
+	fmt.Println("-----WordsResultNum-----", data.WordsResultNum)
+	fmt.Println("-----WordsResult-----", data.WordsResult[0].Words)
+	return
+}
+
+// 更新game_acount
+func UpdateGameAccountIdentify(account string, gameId int, identify int) {
+	global.GVA_DB.Table("game_account").Where("game_id", gameId).Where("account", account).Update("identify", identify)
+}
+
+/**
+ * 使用 AK,SK 生成鉴权签名(Access Token)
+ * @return string 鉴权签名信息(Access Token)
+ */
+func GetAccessToken() (string, error) {
+	ctx := context.Background()
+	token, err := global.GVA_REDIS.Get(ctx, BAIDU_IDENTIFY).Result()
+	if err != nil || token == "" {
+		reqUrl := "https://aip.baidubce.com/oauth/2.0/token"
+		postData := fmt.Sprintf("grant_type=client_credentials&client_id=%s&client_secret=%s", API_KEY, SECRET_KEY)
+		resp, err := http.Post(reqUrl, "application/x-www-form-urlencoded", strings.NewReader(postData))
+		if err != nil {
+			fmt.Println(err)
+			return "", err
+		}
+		defer resp.Body.Close()
+		body, err := ioutil.ReadAll(resp.Body)
+		if err != nil {
+			fmt.Println(err)
+			return "", err
+		}
+		accessTokenObj := map[string]string{}
+		json.Unmarshal(body, &accessTokenObj)
+		global.GVA_REDIS.Set(ctx, BAIDU_IDENTIFY, accessTokenObj["access_token"], time.Hour*24*29)
+		return accessTokenObj["access_token"], err
+	}
+	return token, err
+}

+ 80 - 16
service/log/loging/logical_log.go

@@ -941,10 +941,20 @@ func (s *LogicalLog) AddDeviceLog(request request.AddLogRequest) {
 	logSC.PcCode = request.PcCode
 	logSC.SimulatorIpCity = request.SimulatorIpCity
 	ctx := context.Background()
+	if s.CheckDeviceLogIsSave(logSC) {
+		logSC.IsErr = 1
+		logSC.ErrStatus = 4
+		logSC.CreateDate = time.Now().Format("2006-01-02")
+		err := global.GVA_DB.Omit("create_time").Create(&logSC).Error
+		if err != nil {
+			global.GVA_LOG.Error("create LogScanningCode fail", zap.Error(err))
+		}
+		return
+	}
+	var deviceLogs []log.DeviceErr
 	if request.TaskType == 1 {
-		var deviceLog = log.DeviceLog{}
-		if !errors.Is(global.GVA_DB.Where("account = ?", request.Account).Where("game_id = ?", request.GameId).Order("id desc").First(&deviceLog).Error, gorm.ErrRecordNotFound) {
-			if deviceLog.AccountHex != logSC.AccountHex {
+		if !errors.Is(global.GVA_DB.Where("account = ?", request.Account).Where("game_id = ?", request.GameId).Order("id desc").Limit(1).Find(&deviceLogs).Error, gorm.ErrRecordNotFound) {
+			if deviceLogs[0].AccountHex != "" && deviceLogs[0].AccountHex != logSC.AccountHex {
 				logSC.IsErr = 1
 				logSC.ErrStatus = 3
 				gameDeviceErrKey := fmt.Sprintf(GameDeviceErrKey, logSC.CreateDate, request.GameId)
@@ -952,13 +962,8 @@ func (s *LogicalLog) AddDeviceLog(request request.AddLogRequest) {
 			}
 		}
 	} else {
-		var deviceLogs []log.DeviceLog
-		if !errors.Is(global.GVA_DB.Where("create_date = ?", logSC.CreateDate).Where("game_id = ?", request.GameId).Where("device_hex = ?", logSC.DeviceHex).Order("id desc").Limit(10).Find(&deviceLogs).Error, gorm.ErrRecordNotFound) {
-			mps := make(map[string]int, 10)
-			for _, dl := range deviceLogs {
-				mps[dl.Account] = 1
-			}
-			if len(mps) > 2 {
+		if !errors.Is(global.GVA_DB.Table("game_account").Where("game_id = ?", request.GameId).Where("device_hex = ?", logSC.DeviceHex).Order("id desc").Limit(10).Find(&deviceLogs).Error, gorm.ErrRecordNotFound) {
+			if len(deviceLogs) > 2 {
 				logSC.IsErr = 1
 				logSC.ErrStatus = 2
 				gameDeviceAccountErrKey := fmt.Sprintf(GameDeviceAccountErrKey, logSC.CreateDate, request.GameId)
@@ -971,6 +976,38 @@ func (s *LogicalLog) AddDeviceLog(request request.AddLogRequest) {
 	if err != nil {
 		global.GVA_LOG.Error("create LogScanningCode fail", zap.Error(err))
 	}
+	if logSC.ErrStatus == 3 || logSC.ErrStatus == 2 {
+		s.AddDeviceErr(logSC, deviceLogs)
+	}
+}
+
+func (s *LogicalLog) AddDeviceErr(deviceLog *log.DeviceLog, deviceErrs []log.DeviceErr) {
+	var deviceErr = log.DeviceErr{}
+	deviceErr.GameId = deviceLog.GameId
+	deviceErr.ScriptDeviceId = deviceLog.ScriptDeviceId
+	deviceErr.Account = deviceLog.Account
+	deviceErr.DeviceId = deviceLog.DeviceId
+	deviceErr.DeviceImei = deviceLog.DeviceImei
+	deviceErr.DeviceMac = deviceLog.DeviceMac
+	deviceErr.DeviceManufacturer = deviceLog.DeviceManufacturer
+	deviceErr.DeviceModel = deviceLog.DeviceModel
+	deviceErr.DeviceSdk = deviceLog.DeviceSdk
+	deviceErr.DeviceNumber = deviceLog.DeviceNumber
+	deviceErr.AccountHex = deviceLog.AccountHex
+	deviceErr.DeviceHex = deviceLog.DeviceHex
+	deviceErr.DeviceLogId = deviceLog.Id
+	deviceErrs = append(deviceErrs, deviceErr)
+	for _, errData := range deviceErrs {
+		errData.CreateDate = time.Now()
+	}
+	err := global.GVA_DB.Omit("create_time").Create(&deviceErrs).Error
+	if err != nil {
+		global.GVA_LOG.Error("create AddAccount fail", zap.Error(err))
+	}
+}
+
+func (s *LogicalLog) CheckDeviceLogIsSave(deviceLog *log.DeviceLog) bool {
+	return deviceLog.DeviceManufacturer == "" || deviceLog.DeviceModel == "" || deviceLog.DeviceImei == "" || deviceLog.DeviceSdk == "" || deviceLog.DeviceMac == "" || deviceLog.DeviceNumber == "" || deviceLog.DeviceId == ""
 }
 
 func (s *LogicalLog) DeviceHexLog(request request.AddLogRequest) string {
@@ -1018,13 +1055,23 @@ func (s *LogicalLog) UpdateDeviceLogScriptId(logUuid string, scriptDeviceId stri
 	}
 }
 
-func (s *LogicalLog) AddAccount(request request.AddLogRequest) {
-
+func (s *LogicalLog) AddAccount(request request.AddLogRequest, deviceLog log.DeviceLog) {
 	gameAccount := new(log.GameAccount)
 	gameAccount.GameId = request.GameId
 	gameAccount.ScriptDeviceId = request.ScriptDeviceId
 	gameAccount.Account = request.Account
-	err := global.GVA_DB.Omit("create_time").Create(&gameAccount).Error
+	if deviceLog.IsErr == -1 {
+		gameAccount.DeviceId = deviceLog.DeviceId
+		gameAccount.DeviceImei = deviceLog.DeviceImei
+		gameAccount.DeviceMac = deviceLog.DeviceMac
+		gameAccount.DeviceManufacturer = deviceLog.DeviceManufacturer
+		gameAccount.DeviceModel = deviceLog.DeviceModel
+		gameAccount.DeviceSdk = deviceLog.DeviceSdk
+		gameAccount.DeviceNumber = deviceLog.DeviceNumber
+		gameAccount.AccountHex = deviceLog.AccountHex
+		gameAccount.DeviceHex = deviceLog.DeviceHex
+	}
+	err := global.GVA_DB.Omit("create_time", "update_time", "use_num").Create(&gameAccount).Error
 	if err != nil {
 		global.GVA_LOG.Error("create AddAccount fail", zap.Error(err))
 	}
@@ -1054,7 +1101,10 @@ func (s *LogicalLog) CheckDeviceId(request request.AddLogRequest) {
 		global.GVA_LOG.Error("get CheckDeviceId fail", zap.Error(errors.New("没有获取到设备id")))
 		return
 	}
-	s.UpdateDeviceLogScriptId(request.LogUuid, request.ScriptDeviceId, time.Now().Format("2006-01-02"))
+	date := time.Now().Format("2006-01-02")
+	s.UpdateDeviceLogScriptId(request.LogUuid, request.ScriptDeviceId, date)
+	var device log.DeviceLog
+	global.GVA_DB.Table("device_log").Where("create_date = ?", date).Where("log_uuid = ?", request.LogUuid).First(&device)
 	global.GVA_LOG.Warn("进入 CheckDeviceId")
 	if request.TaskType == 0 {
 		b, deviceLog := s.CheckDeviceIdErr(request.ScriptDeviceId, request.LogUuid, request.GameId)
@@ -1064,7 +1114,7 @@ func (s *LogicalLog) CheckDeviceId(request request.AddLogRequest) {
 			//ct := fmt.Sprintf("<font color=\"warning\">%s:%d, 相同设备id</font>", request.PcCode, request.GameId)
 			//s.SendDeviceMsg(ct, request.Operator)
 		}
-		s.AddAccount(request)
+		s.AddAccount(request, device)
 		return
 	}
 	var gameAccount log.GameAccount
@@ -1072,7 +1122,7 @@ func (s *LogicalLog) CheckDeviceId(request request.AddLogRequest) {
 	if result.Error != nil {
 		if result.Error == gorm.ErrRecordNotFound {
 			// 数据不存在,执行创建操作
-			s.AddAccount(request)
+			s.AddAccount(request, device)
 			return
 		} else {
 			// 其他错误
@@ -1085,6 +1135,20 @@ func (s *LogicalLog) CheckDeviceId(request request.AddLogRequest) {
 		//ct := fmt.Sprintf("<font color=\"warning\">%s:%d, 设备id出现不同</font>", request.PcCode, request.GameId)
 		//s.SendDeviceMsg(ct, request.Operator)
 		gameAccount.ScriptDeviceId = request.ScriptDeviceId
+		if device.IsErr == -1 {
+			if gameAccount.DeviceHex == "" || gameAccount.DeviceHex != device.DeviceHex {
+				gameAccount.DeviceId = device.DeviceId
+				gameAccount.DeviceImei = device.DeviceImei
+				gameAccount.DeviceMac = device.DeviceMac
+				gameAccount.DeviceManufacturer = device.DeviceManufacturer
+				gameAccount.DeviceModel = device.DeviceModel
+				gameAccount.DeviceSdk = device.DeviceSdk
+				gameAccount.DeviceNumber = device.DeviceNumber
+				gameAccount.AccountHex = device.AccountHex
+				gameAccount.DeviceHex = device.DeviceHex
+			}
+		}
+		gameAccount.UseNum += 1
 		global.GVA_DB.Save(&gameAccount)
 	}
 }