200 lines
4.8 KiB
Go
200 lines
4.8 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
"xiawan/wx/api/req"
|
|
"xiawan/wx/api/service"
|
|
"xiawan/wx/api/vo"
|
|
"xiawan/wx/srv/srvconfig"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func IsValidAdminKey(key string) (bool, vo.DTO) {
|
|
if key == srvconfig.GlobalSetting.AdminKey {
|
|
return true, vo.NewSuccess(gin.H{}, "")
|
|
}
|
|
return false, vo.NewFail("软件授权 ADMIN_KEY 错误")
|
|
}
|
|
|
|
// GenAuthKey1 生成授权码(新设备)
|
|
func GenAuthKey1(ctx *gin.Context) {
|
|
reqModel := new(req.GenAuthKeyModel)
|
|
key, _ := ctx.GetQuery("key")
|
|
flag, errRes := IsValidAdminKey(key)
|
|
if !flag {
|
|
ctx.JSON(http.StatusOK, errRes)
|
|
return
|
|
}
|
|
|
|
_ = ctx.ShouldBindJSON(&reqModel)
|
|
result := service.GenAuthKeyService(reqModel.Count, reqModel.Days)
|
|
ctx.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
// GenAuthKey2 生成授权码(新设备)
|
|
func GenAuthKey2(ctx *gin.Context) {
|
|
key, _ := ctx.GetQuery("key")
|
|
flag, errRes := IsValidAdminKey(key)
|
|
if !flag {
|
|
ctx.JSON(http.StatusOK, errRes)
|
|
return
|
|
}
|
|
|
|
reqModel := new(req.GenAuthKeyModel)
|
|
_ = ctx.BindQuery(&reqModel)
|
|
|
|
result := service.GenAuthKeyService(reqModel.Count, reqModel.Days)
|
|
ctx.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
// GenAuthKey3 生成授权码(1-日 7-周 30-月 90-季 180-半年 365-年 30000-永久)此key不使用无过期时间
|
|
func GenAuthKey3(ctx *gin.Context) {
|
|
reqModel := new(req.GenAuthKeyTypeModel)
|
|
|
|
key, _ := ctx.GetQuery("key")
|
|
flag, errRes := IsValidAdminKey(key)
|
|
if !flag {
|
|
ctx.JSON(http.StatusOK, errRes)
|
|
return
|
|
}
|
|
|
|
_ = ctx.ShouldBindJSON(&reqModel)
|
|
|
|
result := service.GenAuthKeyService3(reqModel.Count, reqModel.Type)
|
|
ctx.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
// 西柚云授权码延期
|
|
func DelayAuthKeyNew(ctx *gin.Context) {
|
|
reqModel := new(req.DelayAuthKeyModelNew)
|
|
_ = ctx.ShouldBindJSON(&reqModel)
|
|
|
|
key, _ := ctx.GetQuery("key")
|
|
_, err := service.CheckKey(key)
|
|
if err != nil {
|
|
ctx.JSON(http.StatusOK, vo.NewFail(err.Error()))
|
|
return
|
|
}
|
|
_, err = service.CheckKey(reqModel.KeyUse)
|
|
if err != nil {
|
|
ctx.JSON(http.StatusOK, vo.NewFail(err.Error()))
|
|
return
|
|
}
|
|
|
|
result := service.DelayAuthKeyServiceNew(key, *reqModel)
|
|
ctx.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
// 西柚云解绑授权码
|
|
func UnbindAuthKey(ctx *gin.Context) {
|
|
key, _ := ctx.GetQuery("key")
|
|
_, err := service.CheckKey(key)
|
|
if err != nil {
|
|
ctx.JSON(http.StatusOK, vo.NewFail(err.Error()))
|
|
return
|
|
}
|
|
result := service.UnbindAuthKeyService(key)
|
|
ctx.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
// 西柚云查看配置
|
|
func GetConfig(ctx *gin.Context) {
|
|
key, _ := ctx.GetQuery("key")
|
|
_, err := service.CheckKey(key)
|
|
if err != nil {
|
|
ctx.JSON(http.StatusOK, vo.NewFail(err.Error()))
|
|
return
|
|
}
|
|
result := service.GetConfigService(key)
|
|
ctx.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
// DelayAuthKey 授权码延期
|
|
func DelayAuthKey(ctx *gin.Context) {
|
|
reqModel := new(req.DelayAuthKeyModel)
|
|
key, _ := ctx.GetQuery("key")
|
|
flag, errRes := IsValidAdminKey(key)
|
|
if !flag {
|
|
ctx.JSON(http.StatusOK, errRes)
|
|
return
|
|
}
|
|
_ = ctx.ShouldBindJSON(&reqModel)
|
|
|
|
// 延期授权码 AuthKey
|
|
result := service.DelayAuthKeyService(*reqModel)
|
|
ctx.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
// DeleteAuthKey 删除授权码
|
|
func DeleteAuthKey(ctx *gin.Context) {
|
|
reqModel := new(req.DeleteAuthKeyModel)
|
|
key, _ := ctx.GetQuery("key")
|
|
flag, errRes := IsValidAdminKey(key)
|
|
if !flag {
|
|
ctx.JSON(http.StatusOK, errRes)
|
|
return
|
|
}
|
|
_ = ctx.ShouldBindJSON(&reqModel)
|
|
|
|
// 删除授权码 AuthKey
|
|
result := service.DeleteAuthKeyService(*reqModel)
|
|
ctx.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
// 禁用授权码
|
|
func DisableAuthKey(ctx *gin.Context) {
|
|
reqModel := new(req.BannedAuthKeyModel)
|
|
key, _ := ctx.GetQuery("key")
|
|
flag, errRes := IsValidAdminKey(key)
|
|
if !flag {
|
|
ctx.JSON(http.StatusOK, errRes)
|
|
return
|
|
}
|
|
_ = ctx.ShouldBindJSON(&reqModel)
|
|
|
|
// 禁用授权码 AuthKey
|
|
result := service.BannedAuthKeyService(*reqModel)
|
|
ctx.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
// HttpSyncLicenseKey 同步卡密激活状态, HTTP-轮询方式
|
|
func HttpSyncLicenseKey(ctx *gin.Context) {
|
|
key, _ := ctx.GetQuery("key")
|
|
flag, errRes := IsValidAdminKey(key)
|
|
if !flag {
|
|
ctx.JSON(http.StatusOK, errRes)
|
|
return
|
|
}
|
|
|
|
result := service.HttpSyncLicenseKeyService()
|
|
ctx.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
// GetLicenseKey 查询卡密有效期
|
|
func GetLicenseKey(ctx *gin.Context) {
|
|
queryKey, isExist := ctx.GetQuery("key")
|
|
if !isExist || strings.Trim(queryKey, "") == "" || strings.Trim(queryKey, "") == "null" {
|
|
//确保每次都有Key
|
|
ctx.JSON(http.StatusOK, vo.NewFailUUId(""))
|
|
return
|
|
}
|
|
|
|
result := service.GetLicenseKeyService(queryKey)
|
|
ctx.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
// GetActiveLicenseKeys 查询所有激活状态的卡密
|
|
func GetActiveLicenseKeys(ctx *gin.Context) {
|
|
adminKey, _ := ctx.GetQuery("key")
|
|
flag, errRes := IsValidAdminKey(adminKey)
|
|
if !flag {
|
|
ctx.JSON(http.StatusOK, errRes)
|
|
return
|
|
}
|
|
|
|
result := service.GetActiveLicenseKeysService()
|
|
ctx.JSON(http.StatusOK, result)
|
|
}
|