123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- package controllers
- import (
- "fmt"
- "net/http"
- "strconv"
- "xugu_license/internal/global"
- "xugu_license/internal/models"
- "xugu_license/internal/module/license"
- "xugu_license/internal/utils"
- "github.com/gin-gonic/gin"
- )
- var reqLicense struct {
- page int
- pageSize int
- }
- func GetLicenseInfo(c *gin.Context) {
- userAny, exists := c.Get("userInfo")
- if !exists {
- global.Logger.Errorln("GetLicenseInfo: 用户信息不存在")
- c.JSON(http.StatusUnauthorized, gin.H{"error": "用户信息不存在"})
- c.Abort()
- return
- }
-
- userInfo := userAny.(*models.UserInfo)
-
- page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
- pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
- if userInfo.Role == "admin" {
- applications, total, err := models.GetLicenseInfoAllOrSingle(page, pageSize, "", "")
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "data": applications,
- "page": page,
- "pageSize": pageSize,
- "total": total,
- })
- } else {
- type licInfo struct {
- UniqueID string
- Creator string
- ApplicationDate string
- AssociatedProject string
- License1 string
- License2 string
- SalesPerson string
- SalesEmail string
- SupportPerson string
- SupportEmail string
- TotalNodes string
- Company string
- ProductName string
- Version string
- NodeCount string
- Processor string
- OperatingSystem string
- MasterMacAddress string
- SecondaryMasterMacAddress string
- LicenseFlage string
- }
- applications, total, err := models.GetLicenseInfoAllOrSingle(page, pageSize, "", userInfo.UniqueID)
- if err != nil {
- global.Logger.Errorln("指定用户名查询", err.Error())
- c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
- var licInfoList []licInfo
- for _, v := range applications {
- licInfo := licInfo{
- UniqueID: v.UniqueID,
- Creator: v.Creator,
- ApplicationDate: v.ApplicationDate,
- AssociatedProject: v.AssociatedProject,
- License1: v.License1.String,
- License2: v.License2.String,
- SalesPerson: v.SalesPerson,
- SalesEmail: v.SalesEmail,
- SupportPerson: v.SupportPerson,
- SupportEmail: v.SupportEmail,
- TotalNodes: v.TotalNodes,
- Company: v.Company,
- ProductName: v.ProductName,
- Version: v.Version,
- NodeCount: v.NodeCount,
- Processor: v.Processor,
- OperatingSystem: v.OperatingSystem,
- MasterMacAddress: v.MasterMacAddress,
- SecondaryMasterMacAddress: v.SecondaryMasterMacAddress.String,
- LicenseFlage: v.LicenseFlage,
- }
- licInfoList = append(licInfoList, licInfo)
- }
-
- c.JSON(http.StatusOK, gin.H{
- "data": licInfoList,
- "page": page,
- "pageSize": pageSize,
- "total": total,
- })
- }
- }
- type UniqueIDRequest struct {
- UniqueID string `json:"uniqueID"`
- }
- func GenerateLicenseStr(c *gin.Context) {
- var request UniqueIDRequest
- if err := c.ShouldBindJSON(&request); err != nil {
- global.Logger.Errorln("解析request失败 : ", err.Error())
- c.JSON(400, gin.H{
- "error": fmt.Sprintln("解析请求失败: ", err.Error()),
- })
- return
- }
- applications, _, err := models.GetLicenseInfoAllOrSingle(1, 1, request.UniqueID, "")
- if err != nil {
- global.Logger.Errorln("LicenseInfo数据查询失败: ", err.Error())
- c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintln("数据查询失败: ", err.Error())})
- return
- }
- pj := license.ProjectInfo{
- ProjectName: applications[0].Version,
- UserName: applications[0].Company,
- UserAddr: "未填写",
- SerialNumber: "未填写",
- }
- ei := license.EnvironmentInfo{
- CpuSN: "未填写",
- BaseboardSN: "未填写",
- MacAddr: applications[0].MasterMacAddress,
- DiskID: "未填写",
- IPAddr: "未填写",
- }
- LicType := utils.SwitchLicenseType(applications[0].Version)
- lI := license.LicenseInfo{
- GenDate: "2024-07-15",
- ExpireDate: "9999-12-31",
- LicenseType: LicType,
- LicenseVersion: 1,
- HardType: 3,
- }
- res := true
- if res {
- if applications[0].SecondaryMasterMacAddress.String != "" {
-
-
-
- err = models.UpdateLicenseStr(applications[0].UniqueID, []byte("b4j6z4rE2IfG1av0wIPT7YnvyGZFHxwIBikMGjgCLQILR0xsT1NHiuzoi+Dqq+bmiNDEiuPyitDVgdvlRmYbFAk+MAAGASlPTkdMbE9"), []byte("b4j6z4rE2IfG1av0wIPT7YnvyGZFHxwIBikMGjgCLQILR0xsT1NHiuzoi+Dqq+bmiNDEiuPyitDVgdvlRmYbFAk+MAAGASlPTkdMbE9"))
- } else {
-
-
- err = models.UpdateLicenseStr(applications[0].UniqueID, []byte("b4j6z4rE2IfG1av0wIPT7YnvyGZFHxwIBikMGjgCLQILR0xsT1NHiuzoi+Dqq+bmiNDEiuPyitDVgdvlRmYbFAk+MAAGASlPTkdMbE9"), nil)
- }
- } else {
- licStr := license.GenerateLicense(pj, ei, lI)
-
- if applications[0].SecondaryMasterMacAddress.String != "" {
- var licStr2 []byte
- ei.MacAddr = applications[0].SecondaryMasterMacAddress.String
- licStr2 = license.GenerateLicense(pj, ei, lI)
- err = models.UpdateLicenseStr(applications[0].UniqueID, licStr, licStr2)
- } else {
-
- err = models.UpdateLicenseStr(applications[0].UniqueID, licStr, nil)
- }
- }
- if err != nil {
- fmt.Println(err)
- if err := c.ShouldBindJSON(&request); err != nil {
- c.JSON(400, gin.H{
- "error": err.Error(),
- })
- return
- }
- }
-
-
-
-
-
-
-
-
-
-
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "License生成成功!",
- })
-
- }
- func DistributeLicenseByUser(c *gin.Context) {
-
- u, err := models.GetAllUser()
- if err != nil {
- c.JSON(http.StatusBadRequest, gin.H{
- "error": err.Error(),
- })
- return
- }
- fmt.Println("sdsad u", u)
- type allUserInfo struct {
- }
- c.JSON(http.StatusOK, gin.H{
- "message": "success",
- "data": u,
- })
- }
- func GetlicenseRecordInfoByUser(c *gin.Context) {
- type licenseByUserRequest struct {
- Id int `json:"id"`
- UserName string `json:"UserName"`
- UniqueID string `json:"UniqueID"`
- Page int `json:"page"`
- PageSize int `json:"pageSize"`
- }
- var request licenseByUserRequest
- if err := c.ShouldBindJSON(&request); err != nil {
- global.Logger.Errorln("解析请求失败 ", err.Error())
- c.JSON(400, gin.H{
- "error": fmt.Sprintln("解析请求失败: ", err.Error()),
- })
- return
- }
- applications, total, err := models.GetLicenseInfoAllOrSingle(request.Page, request.PageSize, "", request.UniqueID)
- if err != nil {
- global.Logger.Errorln("数据插入失败 ", err.Error())
- c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintln("数据插入失败: ", err.Error())})
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "data": applications,
- "total": total,
- })
- }
|