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
- }
- // 获取用户已被分配的license信息
- 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)
- }
- //fmt.Printf("licInfoList %#v", licInfoList)
- c.JSON(http.StatusOK, gin.H{
- "data": licInfoList,
- "page": page,
- "pageSize": pageSize,
- "total": total,
- })
- }
- }
- // 定义接收 JSON 数据的结构体
- type UniqueIDRequest struct {
- UniqueID string `json:"uniqueID"`
- }
- // func DelLicenseInfoRow(c *gin.Context) {
- // // 解析 JSON 请求体
- // var request UniqueIDRequest
- // if err := c.ShouldBindJSON(&request); err != nil {
- // c.JSON(400, gin.H{
- // "error": err.Error(),
- // })
- // return
- // }
- // err := models.DelLicenseApplicationAndInfoRow(request.UniqueID)
- // if err != nil {
- // global.Logger.Errorln("删除一行License信息失败 ", err.Error())
- // c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintln("删除失败: ", err.Error())})
- // return
- // }
- // c.JSON(http.StatusOK, gin.H{
- // "success": true,
- // "data": "删除成功",
- // })
- // }
- 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 != "" {
- // var licStr2 []byte
- // ei.MacAddr = applications[0].SecondaryMasterMacAddress
- // licStr2 = license.GenerateLicense(pj, ei, lI)
- err = models.UpdateLicenseStr(applications[0].UniqueID, []byte("b4j6z4rE2IfG1av0wIPT7YnvyGZFHxwIBikMGjgCLQILR0xsT1NHiuzoi+Dqq+bmiNDEiuPyitDVgdvlRmYbFAk+MAAGASlPTkdMbE9"), []byte("b4j6z4rE2IfG1av0wIPT7YnvyGZFHxwIBikMGjgCLQILR0xsT1NHiuzoi+Dqq+bmiNDEiuPyitDVgdvlRmYbFAk+MAAGASlPTkdMbE9"))
- } else {
- //fmt.Println("licStr licStr2", licStr, licStr2)
- //插入到数据库
- err = models.UpdateLicenseStr(applications[0].UniqueID, []byte("b4j6z4rE2IfG1av0wIPT7YnvyGZFHxwIBikMGjgCLQILR0xsT1NHiuzoi+Dqq+bmiNDEiuPyitDVgdvlRmYbFAk+MAAGASlPTkdMbE9"), nil)
- }
- } else {
- licStr := license.GenerateLicense(pj, ei, lI)
- //生成副主节点license
- 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
- }
- }
- // if err != nil {
- // fmt.Println(err)
- // if err := c.ShouldBindJSON(&request); err != nil {
- // global.Logger.Errorln("LicenseInfo数据查询失败: ", err.Error())
- // c.JSON(400, gin.H{
- // "error": fmt.Sprintln("数据查询失败: ", err.Error()),
- // })
- // return
- // }
- // }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "License生成成功!",
- })
- //xlsx.ExcelToMail(lic, licStr, licStr2)
- }
- func DistributeLicenseByUser(c *gin.Context) {
- // 从token中解析出user_id
- 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 DistributeLicense(c *gin.Context) {
- // type DistributeLicenseRequest struct {
- // LicenseUniqueID string `json:"LicenseUniqueID"`
- // OperatorUniqueID string `json:"OperatorUniqueID"`
- // UserUniqueIDs []string `json:"UserUniqueIDs"`
- // UserAccounts []string `json:"UserAccounts"`
- // UserNames []string `json:"UserNames"`
- // Emails string `json:"emails"`
- // }
- // //判断是否发邮件
- // //数据库查询license信息
- // var request DistributeLicenseRequest
- // if err := c.ShouldBindJSON(&request); err != nil {
- // global.Logger.Errorln("邮件解析请求解析失败 %s", err.Error())
- // c.JSON(400, gin.H{
- // "error": fmt.Sprintln("邮件解析请求解析失败 %s", err.Error()),
- // })
- // return
- // }
- // fmt.Println("DistributeLicenseRequest", request)
- // //查询该license是否已经分发给了该用户
- // var licToUser []string
- // for i, v := range request.UserUniqueIDs {
- // fmt.Println("request.UserUniqueIDs", request.LicenseUniqueID, v)
- // if isTurn, err := models.CheckLicenseToUser(request.LicenseUniqueID, v); err != nil {
- // global.Logger.Errorln("该license查询是否分发给用户失败 ", v, err.Error())
- // c.JSON(400, gin.H{
- // "success": false,
- // "error": fmt.Sprintln("该license查询是否分发给用户失败: %s", v, err.Error()),
- // })
- // return
- // } else if isTurn {
- // global.Logger.Info("该license已经分发给了该用户 ", v)
- // licToUser = append(licToUser, request.UserNames[i])
- // }
- // }
- // if len(licToUser) != 0 {
- // c.JSON(400, gin.H{
- // "success": false,
- // "error": fmt.Sprintf("该license已经分发给了该用户: %s\n", licToUser),
- // })
- // return
- // }
- // applications, _, err := models.GetLicenseInfoAllOrSingle(1, 1, request.LicenseUniqueID, "")
- // if err != nil {
- // c.JSON(http.StatusBadRequest, gin.H{"success": false,
- // "error": err.Error()})
- // return
- // }
- // if applications[0].License1.String == "" {
- // global.Logger.Errorln("license未生成 ")
- // c.JSON(http.StatusBadRequest, gin.H{"success": false,
- // "error": "license未生成"})
- // return
- // }
- // //邮箱不空则发送
- // var EmailArray []string
- // if request.Emails != "" {
- // fmt.Println("request.Emails", request.Emails)
- // EmailArray = strings.Split(request.Emails, ",")
- // fmt.Println("EmailArray", EmailArray)
- // em, err := email.BuildEmail(applications[0].LicenseApplication, EmailArray, applications[0].License1.String, applications[0].License2.String)
- // if err != nil {
- // global.Logger.Errorln("邮件生成失败", err.Error())
- // c.JSON(400, gin.H{
- // "success": false,
- // "error": fmt.Sprintln("邮件生成失败: ", err.Error()),
- // })
- // return
- // }
- // err = email.SendEmail(em)
- // if err != nil {
- // global.Logger.Errorln("邮件发送失败", err.Error())
- // c.JSON(400, gin.H{
- // "success": false,
- // "error": fmt.Sprintln("邮件发送失败: ", err.Error()),
- // })
- // return
- // }
- // }
- // for i, v := range request.UserUniqueIDs {
- // err = models.InsertlicenseRecordByUserRow(applications[0].UniqueID, v, request.UserAccounts[i], request.OperatorUniqueID)
- // if err != nil {
- // global.Logger.Errorln("数据库插入失败: ", err.Error())
- // c.JSON(400, gin.H{
- // "success": false,
- // "error": fmt.Sprintln("插入失败: ", err.Error()),
- // })
- // return
- // }
- // }
- // if len(EmailArray) != 0 {
- // for _, v := range EmailArray {
- // err = models.InsertlicenseRecordByEmailRow(applications[0].UniqueID, v, request.OperatorUniqueID)
- // if err != nil {
- // global.Logger.Errorln("数据库插入失败: ", err.Error())
- // c.JSON(400, gin.H{
- // "error": fmt.Sprintln("插入失败: ", err.Error()),
- // })
- // return
- // }
- // }
- // }
- // c.JSON(http.StatusOK, gin.H{
- // "success": true,
- // "data": "分发成功!",
- // })
- // }
- // func GetlicenseRecordInfo(c *gin.Context) {
- // type licenseRecordInfoRequest struct {
- // UniqueID string `json:"uniqueID"`
- // Id int `json:"id"`
- // }
- // var request UniqueIDRequest
- // if err := c.ShouldBindJSON(&request); err != nil {
- // global.Logger.Errorln("解析请求失败 ", err.Error())
- // c.JSON(400, gin.H{
- // "error": fmt.Sprintln("解析请求失败: ", err.Error()),
- // })
- // return
- // }
- // LicUers, err := models.GetlicenseRecordByUser(request.UniqueID)
- // if err != nil {
- // global.Logger.Errorln("数据查询失败 ", err.Error())
- // c.JSON(400, gin.H{
- // "error": fmt.Sprintln("数据查询失败: ", err.Error()),
- // })
- // return
- // }
- // LicEmails, err := models.GetlicenseRecordByEmail(request.UniqueID)
- // if err != nil {
- // global.Logger.Errorln("数据查询失败 ", err.Error())
- // c.JSON(400, gin.H{
- // "error": fmt.Sprintln("数据查询失败: ", err.Error()),
- // })
- // return
- // }
- // licR := models.LicenseRecord{
- // LicenseRecordToUser: LicUers,
- // LicenseRecordToEmails: LicEmails,
- // }
- // fmt.Println("分发历史applications", licR)
- // c.JSON(http.StatusOK, gin.H{
- // "success": true,
- // "data": licR,
- // })
- // }
- // 获取用户所拥有的license信息
- 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,
- })
- }
- // func UpdateLicense(c *gin.Context) {
- // type RecvLicense struct {
- // UniqueID string `json:"UniqueID" binding:"required"`
- // Creator string `json:"Creator" binding:"required"`
- // ApplicationDate string `json:"ApplicationDate" binding:"required"`
- // AssociatedProject string `json:"AssociatedProject" binding:"required"`
- // SalesPerson string `json:"SalesPerson" binding:"required"`
- // SalesEmail string `json:"SalesEmail" binding:"required,email"`
- // SupportPerson string `json:"SupportPerson" binding:"required"`
- // SupportEmail string `json:"SupportEmail" binding:"required,email"`
- // TotalNodes string `json:"TotalNodes" binding:"required"`
- // Company string `json:"Company" binding:"required"`
- // ProductName string `json:"ProductName" binding:"required"`
- // Version string `json:"Version" binding:"required"`
- // NodeCount string `json:"NodeCount" binding:"required"`
- // }
- // var license RecvLicense
- // // 绑定JSON数据到License结构体
- // if err := c.ShouldBindJSON(&license); err != nil {
- // c.JSON(http.StatusBadRequest, gin.H{
- // "success": false,
- // "error": err.Error(),
- // })
- // return
- // }
- // fmt.Printf(" license.ApplicationDate: %#v ", license)
- // err := models.UpdatelicenseInfoRow(models.LicenseInfo{
- // UniqueID: license.UniqueID,
- // LicenseApplication: models.LicenseApplication{
- // Creator: license.Creator,
- // ApplicationDate: license.ApplicationDate,
- // AssociatedProject: license.AssociatedProject,
- // SalesPerson: license.SalesPerson,
- // SalesEmail: license.SalesEmail,
- // SupportPerson: license.SupportPerson,
- // SupportEmail: license.SupportEmail,
- // TotalNodes: license.TotalNodes,
- // Company: license.Company,
- // ProductName: license.ProductName,
- // Version: license.Version,
- // NodeCount: license.NodeCount,
- // },
- // })
- // if err != nil {
- // global.Logger.Errorln("数据插入失败 ", err.Error())
- // c.JSON(http.StatusBadRequest, gin.H{"success": false, "error": fmt.Sprintln("数据插入失败: ", err.Error())})
- // return
- // }
- // c.JSON(http.StatusOK, gin.H{
- // "success": true,
- // })
- // }
|