first commit
This commit is contained in:
@@ -0,0 +1,241 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
"xiawan/wx/api/req"
|
||||
"xiawan/wx/api/vo"
|
||||
"xiawan/wx/db"
|
||||
"xiawan/wx/srv/wxface"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/lunny/log"
|
||||
)
|
||||
|
||||
// 检查请求参数 key 对应的 license 是否可用
|
||||
func CheckKey(queryKey string) (string, error) {
|
||||
errMsg := fmt.Sprintf("%s 该 key 无效! 请 检查正确性 或 联系管理员生成", queryKey)
|
||||
if len(strings.TrimSpace(queryKey)) == 0 {
|
||||
// 请求参数中没有携带 license 或者 license 为空
|
||||
return "", errors.New(errMsg)
|
||||
}
|
||||
has, err := db.HasLicense(queryKey)
|
||||
if has == nil || err != nil {
|
||||
// MySQL 数据库没有该 license
|
||||
return "", errors.New(errMsg)
|
||||
}
|
||||
if db.CheckExpiry(has.ExpiryDate, has.Type) {
|
||||
// MySQL 数据库中的该 license 已过期
|
||||
return "", fmt.Errorf("%s 该 key 已过期!", queryKey)
|
||||
}
|
||||
|
||||
// license 可用
|
||||
return queryKey, nil
|
||||
}
|
||||
|
||||
// GenAuthKeyService 生成授权码(新设备)
|
||||
func GenAuthKeyService(count, days int) vo.DTO {
|
||||
if count < 1 {
|
||||
count = 1
|
||||
}
|
||||
if days < 1 {
|
||||
days = 30
|
||||
}
|
||||
|
||||
res := db.CreateLicense(count, days)
|
||||
authKeys := strings.Split(res, "\n\t")
|
||||
if len(res) == 0 {
|
||||
authKeys = []string{}
|
||||
}
|
||||
return vo.NewSuccessObj(authKeys, "AuthKey生成成功")
|
||||
}
|
||||
|
||||
// 按类型生成(不使用不计算时间)
|
||||
func GenAuthKeyService3(count int, itype int) vo.DTO {
|
||||
//1日 7 周 30月 90季 180 半年 365年 30000
|
||||
types := []int{1, 7, 30, 90, 180, 365, 30000}
|
||||
// 判断 itype 在 types 中
|
||||
isType := false
|
||||
for _, t := range types {
|
||||
if t == itype {
|
||||
isType = true
|
||||
break
|
||||
}
|
||||
}
|
||||
log.Println("isType", isType)
|
||||
log.Println("Type", itype)
|
||||
log.Println("types", types)
|
||||
if !isType {
|
||||
return vo.NewFail("type 参数错误")
|
||||
}
|
||||
|
||||
res := db.CreateLicense3(count, itype)
|
||||
authKeys := strings.Split(res, "\n\t")
|
||||
if len(res) == 0 {
|
||||
authKeys = []string{}
|
||||
}
|
||||
return vo.NewSuccessObj(authKeys, "AuthKey生成成功")
|
||||
}
|
||||
|
||||
// 授权码延期 西柚云
|
||||
func DelayAuthKeyServiceNew(key string, m req.DelayAuthKeyModelNew) vo.DTO {
|
||||
if m.KeyUse == "" || key == "" {
|
||||
return vo.NewFail("keyuse 参数错误")
|
||||
}
|
||||
|
||||
// 计算被使用key的剩余天数
|
||||
keyuse_expirydata, err := db.HasLicense(m.KeyUse)
|
||||
if err != nil || keyuse_expirydata == nil {
|
||||
return vo.NewFail("查询失败")
|
||||
}
|
||||
key_use_days := keyuse_expirydata.Type
|
||||
|
||||
ms := req.DelayAuthKeyModel{
|
||||
Key: key,
|
||||
Days: key_use_days,
|
||||
ExpiryDate: "",
|
||||
}
|
||||
|
||||
db.DeleteLicense(m.KeyUse, 2)
|
||||
|
||||
return DelayAuthKeyService(ms)
|
||||
}
|
||||
|
||||
// 授权码解绑 西柚云
|
||||
func UnbindAuthKeyService(key string) vo.DTO {
|
||||
if key == "" {
|
||||
return vo.NewFail("key 参数错误")
|
||||
}
|
||||
ms := req.DeleteAuthKeyModel{
|
||||
Key: key,
|
||||
Opt: 3,
|
||||
}
|
||||
return checkExIdPerformNoCreateConnect(key, func(connect wxface.IWXConnect, newIWXConnect bool) vo.DTO {
|
||||
wxAccount := connect.GetWXAccount()
|
||||
wxAccount.GetUserInfo().SetLoginState(0)
|
||||
if wxAccount.GetUserInfo().GetLoginState() == 0 {
|
||||
return DeleteAuthKeyService(ms)
|
||||
} else {
|
||||
return vo.NewFail("解绑失败")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 查看配置
|
||||
func GetConfigService(key string) vo.DTO {
|
||||
if key == "" {
|
||||
return vo.NewFail("key 参数错误")
|
||||
}
|
||||
query, err := db.QueryCommand(key)
|
||||
if err != nil {
|
||||
return vo.NewFail("操作失败!")
|
||||
}
|
||||
return vo.NewSuccessObj(query, "操作成功!")
|
||||
}
|
||||
|
||||
// DelayAuthKeyService 授权码延期
|
||||
func DelayAuthKeyService(m req.DelayAuthKeyModel) vo.DTO {
|
||||
if m.Days < 1 {
|
||||
m.Days = 30
|
||||
}
|
||||
|
||||
if len(strings.TrimSpace(m.ExpiryDate)) > 0 {
|
||||
// 获取当前的过期日期
|
||||
expiryDate, err := time.Parse("2006-01-02", m.ExpiryDate)
|
||||
if err != nil {
|
||||
return vo.NewFail("ExpiryDate 参数解析错误")
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
if expiryDate.Before(now) {
|
||||
return vo.NewFail("ExpiryDate 参数设置错误")
|
||||
}
|
||||
|
||||
// ExpiryDate 参数可用
|
||||
m.ExpiryDate = strings.TrimSpace(m.ExpiryDate)
|
||||
m.Days = 0
|
||||
}
|
||||
|
||||
expiryDateStr, err := db.DelayLicense(m)
|
||||
if err != nil {
|
||||
return vo.NewFail("延期失败: " + err.Error())
|
||||
}
|
||||
|
||||
// 延期成功; 更新已建立链接的授权码的过期时间
|
||||
return getExistWxConnect(m.Key, func(connect wxface.IWXConnect, newIWXConnect bool) vo.DTO {
|
||||
if connect != nil {
|
||||
connect.GetWXServer().UpdateExpiryDate(m.Key, expiryDateStr)
|
||||
}
|
||||
|
||||
return vo.NewSuccess(gin.H{
|
||||
"expiryDate": expiryDateStr,
|
||||
}, "延期成功")
|
||||
})
|
||||
}
|
||||
|
||||
// 更新禁用状态
|
||||
func BannedAuthKeyService(m req.BannedAuthKeyModel) vo.DTO {
|
||||
bannedStr := "启用:0"
|
||||
|
||||
if m.IsBanned != 0 {
|
||||
m.IsBanned = 1
|
||||
bannedStr = "禁用:1"
|
||||
}
|
||||
|
||||
err := db.DisableLicense(m.Key, m.IsBanned)
|
||||
if err != nil {
|
||||
return vo.NewFail("设置失败: " + err.Error())
|
||||
}
|
||||
|
||||
// 延期成功; 更新已建立链接的授权码的过期时间
|
||||
return getExistWxConnect(m.Key, func(connect wxface.IWXConnect, newIWXConnect bool) vo.DTO {
|
||||
if connect != nil {
|
||||
connect.GetWXServer().UpdateDisable(m.Key, m.IsBanned)
|
||||
}
|
||||
|
||||
return vo.NewSuccess(gin.H{
|
||||
"isBanned": bannedStr,
|
||||
}, "设置成功")
|
||||
})
|
||||
}
|
||||
|
||||
// HttpSyncLicenseKeyService HTTP-激活状态
|
||||
func HttpSyncLicenseKeyService() vo.DTO {
|
||||
results, err := db.HttpSyncLicenseKey()
|
||||
if err != nil {
|
||||
return vo.NewFail("发生错误: " + err.Error())
|
||||
}
|
||||
return vo.NewSuccessObj(results, "")
|
||||
}
|
||||
|
||||
// DeleteAuthKeyService 删除授权码
|
||||
func DeleteAuthKeyService(m req.DeleteAuthKeyModel) vo.DTO {
|
||||
err := db.DeleteLicense(m.Key, m.Opt)
|
||||
if err != nil {
|
||||
return vo.NewFail("删除授权码失败: " + err.Error())
|
||||
}
|
||||
return vo.NewSuccess(gin.H{}, "删除成功")
|
||||
}
|
||||
|
||||
// GetLicenseKeyService 查询卡密有效期
|
||||
func GetLicenseKeyService(queryKey string) vo.DTO {
|
||||
has, err := db.HasLicense(queryKey)
|
||||
if err != nil {
|
||||
return vo.NewFail("查询失败")
|
||||
}
|
||||
if has == nil {
|
||||
return vo.NewFail("卡密不存在")
|
||||
}
|
||||
return vo.NewSuccessObj(has, "查询成功")
|
||||
}
|
||||
|
||||
// GetActiveLicenseKeysService 获取所有激活状态的卡密
|
||||
func GetActiveLicenseKeysService() vo.DTO {
|
||||
results, err := db.GetActiveLicenseKeys()
|
||||
if err != nil {
|
||||
return vo.NewFail("查询失败: " + err.Error())
|
||||
}
|
||||
return vo.NewSuccessObj(results, "查询成功")
|
||||
}
|
||||
Reference in New Issue
Block a user