347 lines
8.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package panel
import (
"encoding/json"
"fmt"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"sun-panel/api/api_v1/common/apiData/commonApiStructs"
"sun-panel/api/api_v1/common/apiData/panelApiStructs"
"sun-panel/api/api_v1/common/apiReturn"
"sun-panel/api/api_v1/common/base"
"sun-panel/global"
"sun-panel/lib/cmn"
"sun-panel/lib/siteFavicon"
"sun-panel/models"
"time"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"gorm.io/gorm"
)
type ItemIcon struct {
}
func (a *ItemIcon) Edit(c *gin.Context) {
userInfo, _ := base.GetCurrentUserInfo(c)
req := models.ItemIcon{}
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
apiReturn.ErrorParamFomat(c, err.Error())
return
}
if req.ItemIconGroupId == 0 {
// apiReturn.Error(c, "Group is mandatory")
apiReturn.ErrorParamFomat(c, "Group is mandatory")
return
}
req.UserId = userInfo.ID
// json转字符串
if j, err := json.Marshal(req.Icon); err == nil {
req.IconJson = string(j)
}
if req.ID != 0 {
if userInfo.Role != 1 {
apiReturn.ErrorNoAccess(c)
c.Abort()
return
}
// 修改
updateField := []string{"IconJson", "Icon", "Title", "Url", "LanUrl", "Description", "OpenMethod", "GroupId", "UserId", "ItemIconGroupId"}
if req.Sort != 0 {
updateField = append(updateField, "Sort")
}
global.Db.Model(&models.ItemIcon{}).
Select(updateField).
Where("id=?", req.ID).Updates(&req)
} else {
req.Sort = 9999
// 创建
global.Db.Create(&req)
}
apiReturn.SuccessData(c, req)
}
// 添加多个图标
func (a *ItemIcon) AddMultiple(c *gin.Context) {
userInfo, _ := base.GetCurrentUserInfo(c)
// type Request
req := []models.ItemIcon{}
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
apiReturn.ErrorParamFomat(c, err.Error())
return
}
for i := 0; i < len(req); i++ {
if req[i].ItemIconGroupId == 0 {
apiReturn.ErrorParamFomat(c, "Group is mandatory")
return
}
req[i].UserId = userInfo.ID
// json转字符串
if j, err := json.Marshal(req[i].Icon); err == nil {
req[i].IconJson = string(j)
}
}
global.Db.Create(&req)
apiReturn.SuccessData(c, req)
}
// // 获取详情
// func (a *ItemIcon) GetInfo(c *gin.Context) {
// req := systemApiStructs.AiDrawGetInfoReq{}
// if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
// apiReturn.ErrorParamFomat(c, err.Error())
// return
// }
// userInfo, _ := base.GetCurrentUserInfo(c)
// aiDraw := models.AiDraw{}
// aiDraw.ID = req.ID
// if err := aiDraw.GetInfo(global.Db); err != nil {
// if err == gorm.ErrRecordNotFound {
// apiReturn.Error(c, "不存在记录")
// return
// }
// apiReturn.ErrorDatabase(c, err.Error())
// return
// }
// if userInfo.ID != aiDraw.UserID {
// apiReturn.ErrorNoAccess(c)
// return
// }
// apiReturn.SuccessData(c, aiDraw)
// }
func (a *ItemIcon) GetListByGroupId(c *gin.Context) {
req := models.ItemIcon{}
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
apiReturn.ErrorParamFomat(c, err.Error())
return
}
itemIcons := []models.ItemIcon{}
if err := global.Db.Order("sort ,created_at").Find(&itemIcons, "item_icon_group_id = ? ", req.ItemIconGroupId).Error; err != nil {
apiReturn.ErrorDatabase(c, err.Error())
return
}
for k, v := range itemIcons {
json.Unmarshal([]byte(v.IconJson), &itemIcons[k].Icon)
}
apiReturn.SuccessListData(c, itemIcons, 0)
}
func (a *ItemIcon) Deletes(c *gin.Context) {
req := commonApiStructs.RequestDeleteIds[uint]{}
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
apiReturn.ErrorParamFomat(c, err.Error())
return
}
userInfo, exist := base.GetCurrentUserInfo(c)
if exist != true {
// 处理获取用户信息失败的情况
// 这里可以根据实际情况返回错误或进行其他处理
// 例如,返回一个 HTTP 错误响应或记录错误日志
apiReturn.ErrorNoAccess(c)
return
}
if userInfo.Role != 1 {
// 如果 userInfo 为空或者 userInfo 的 Role 不等于 1
// 可以根据实际情况返回错误或进行其他处理
// 例如,返回一个 HTTP 错误响应或记录错误日志
apiReturn.ErrorNoAccess(c)
return
}
if err := global.Db.Delete(&models.ItemIcon{}, "id in ? ", req.Ids).Error; err != nil {
apiReturn.ErrorDatabase(c, err.Error())
return
}
apiReturn.Success(c)
}
// 保存排序
func (a *ItemIcon) SaveSort(c *gin.Context) {
req := panelApiStructs.ItemIconSaveSortRequest{}
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
apiReturn.ErrorParamFomat(c, err.Error())
return
}
userInfo, exist := base.GetCurrentUserInfo(c)
if exist != true {
// 处理获取用户信息失败的情况
// 这里可以根据实际情况返回错误或进行其他处理
// 例如,返回一个 HTTP 错误响应或记录错误日志
apiReturn.ErrorNoAccess(c)
return
}
if userInfo.Role != 1 {
// 如果 userInfo 为空或者 userInfo 的 Role 不等于 1
// 可以根据实际情况返回错误或进行其他处理
// 例如,返回一个 HTTP 错误响应或记录错误日志
apiReturn.ErrorNoAccess(c)
return
}
transactionErr := global.Db.Transaction(func(tx *gorm.DB) error {
// 在事务中执行一些 db 操作(从这里开始,您应该使用 'tx' 而不是 'db'
for _, v := range req.SortItems {
if err := tx.Model(&models.ItemIcon{}).Where("id=? AND item_icon_group_id=?", v.Id, req.ItemIconGroupId).Update("sort", v.Sort).Error; err != nil {
// 返回任何错误都会回滚事务
return err
}
}
// 返回 nil 提交事务
return nil
})
if transactionErr != nil {
apiReturn.ErrorDatabase(c, transactionErr.Error())
return
}
apiReturn.Success(c)
}
// 支持获取并直接下载对方网站图标到服务器
func (a *ItemIcon) GetSiteFavicon(c *gin.Context) {
userInfo, _ := base.GetCurrentUserInfo(c)
req := panelApiStructs.ItemIconGetSiteFaviconReq{}
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
apiReturn.ErrorParamFomat(c, err.Error())
return
}
resp := panelApiStructs.ItemIconGetSiteFaviconResp{}
fullUrl := ""
fullUrl = siteFavicon.GetOneFaviconURL(req.Url)
parsedURL, err := url.Parse(req.Url)
if err != nil {
apiReturn.Error(c, "acquisition failed:"+err.Error())
return
}
protocol := parsedURL.Scheme
global.Logger.Debug("protocol:", protocol)
global.Logger.Debug("fullUrl:", fullUrl)
// 如果URL以双斜杠//)开头,则使用当前页面协议
if strings.HasPrefix(fullUrl, "//") {
fullUrl = protocol + "://" + fullUrl[2:]
} else if !strings.HasPrefix(fullUrl, "http://") && !strings.HasPrefix(fullUrl, "https://") {
// 如果URL既不以http://开头也不以https://开头则默认为http协议
fullUrl = "http://" + fullUrl
}
global.Logger.Debug("fullUrl111:", fullUrl)
// 生成保存目录
configUpload := global.Config.GetValueString("base", "source_path")
savePath := fmt.Sprintf("%s/%d/%d/%d/", configUpload, time.Now().Year(), time.Now().Month(), time.Now().Day())
isExist, _ := cmn.PathExists(savePath)
if !isExist {
os.MkdirAll(savePath, os.ModePerm)
}
// 下载
var imgInfo *os.File
{
{
imgInfo, err = siteFavicon.DownloadImage(fullUrl, savePath, 1024*1024)
if err != nil {
u, err := url.Parse(fullUrl)
if err != nil {
// 处理 URL 解析错误
return
}
// 获取主机名
hostname := u.Hostname()
// 去掉子域名前缀
parts := strings.Split(hostname, ".")
if len(parts) > 2 {
hostname = strings.Join(parts[1:], ".")
}
// 构建新的 URL
newUrl := u.Scheme + "://" + hostname + u.RequestURI()
imgInfo, err = siteFavicon.DownloadImage(newUrl, savePath, 1024*1024)
// 重新尝试下载
if err != nil {
if iconUrl, err := siteFavicon.GetOneFaviconURLByhead(req.Url); err != nil {
apiReturn.Error(c, "acquisition failed: get ico error:"+err.Error())
return
} else {
fullUrl = iconUrl
u, err := url.Parse(fullUrl)
if err != nil {
// 处理 URL 解析错误
apiReturn.Error(c, "acquisition failed: get ico error:"+err.Error())
return
}
// 获取主机名
hostname := u.Hostname()
absolutePath := filepath.Clean(u.RequestURI())
// 构建新的 URL
newUrl := u.Scheme + "://" + hostname + absolutePath
newUrl = strings.ReplaceAll(newUrl, "\\", "/")
fmt.Println(newUrl)
imgInfo, err = siteFavicon.DownloadImage(newUrl, savePath, 1024*1024)
if err != nil {
fmt.Println(err)
apiReturn.Error(c, "acquisition failed: get ico error:"+err.Error())
return
}
}
}
}
}
}
// 保存到数据库
ext := path.Ext(fullUrl)
mFile := models.File{}
if _, err := mFile.AddFile(userInfo.ID, parsedURL.Host, ext, imgInfo.Name()); err != nil {
apiReturn.ErrorDatabase(c, err.Error())
return
}
resp.IconUrl = imgInfo.Name()[1:]
apiReturn.SuccessData(c, resp)
}