Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions api/v1/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ type (
type (
SiteUpdateReq struct {
Id int `json:"id" uri:"id"` // ID
Icon *string `json:"thumb" form:"thumb"` // 网站 logo
Title *string `json:"title" form:"title"` // 名称简介
Url *string `json:"url" form:"url"` // 链接
CategoryId *int `json:"category_id" form:"category_id"` // 分类id
Description *string `json:"description" form:"description"` // 描述
Icon string `json:"thumb" form:"thumb"` // 网站 logo
Title string `json:"title" form:"title"` // 名称简介
Url string `json:"url" form:"url"` // 链接
CategoryId int `json:"category_id" form:"category_id"` // 分类id
Description string `json:"description" form:"description"` // 描述
IsUsed *bool `json:"is_used" form:"is_used"` // 是否启用
File *multipart.FileHeader `json:"file"` // 上传 logo 图片
}
Expand Down
2 changes: 1 addition & 1 deletion internal/service/site/batchcreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (s *service) BatchCreate(ctx context.Context, req *v1.SiteCreateReq) (*v1.S
return nil
})
g.Go(func() error {
icon = getWebLogoIcon(url)
icon = getWebLogoIconBase64(url)
return nil
})
g.Go(func() error {
Expand Down
2 changes: 1 addition & 1 deletion internal/service/site/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (s *service) i() {}
// default icon base64
const defaultIcon = "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAA+VBMVEUAAAC6sWUzNjZCQkIsLi8sLjAsLi//tSs6Oj8tLzAsLi/9sycsLi/9sycsLi/9tCctLi/8tSj/tSguMTEuMzQzMzMwMzb/ujP9tCcsLi8uLzEtLy8sLi8sLi8tLi8sLi8tLi8sLi8tLjAsLi8sLy8sLi8sLjAsLjAtLy8tLzAtLzAuMDAuMDL/tyn9tCf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4Mb1AWIsirMv9HYA+Or4Q68XQu+eBL0wqunllSEKN2GF4iyHrkFuN6FfwxdCHnXeKDXmqvKn+Q+oeE3vIQF62QAAAABJRU5ErkJggg=="

func getWebLogoIcon(url string) string {
func getWebLogoIconBase64(url string) string {
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Expand Down
2 changes: 1 addition & 1 deletion internal/service/site/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (s *service) Sync(ctx *gin.Context, req *v1.SiteSyncReq) (resp *v1.SiteSync

_, err = s.siteRepository.WithContext(ctx).Update(&model.StSite{
Title: getWebTitle(site.URL),
Icon: getWebLogoIcon(site.URL),
Icon: getWebLogoIconBase64(site.URL),
Description: getWebDescription(site.URL),
IsUsed: false,
},
Expand Down
60 changes: 40 additions & 20 deletions internal/service/site/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,71 @@
package site

import (
"path/filepath"
"crypto/tls"
"encoding/base64"
"io"
"net/http"

"github.com/gin-gonic/gin"

v1 "github.com/ch3nnn/webstack-go/api/v1"
)

func IconPath(ctx *gin.Context, req *v1.SiteUpdateReq) string {
// 修改操作是否手动上传 logo 图片
if req.File != nil {
return getWebLogoIconUrlByUploadImg(ctx)
func getIconBase64ByFormFile(req *v1.SiteUpdateReq) string {
file, err := req.File.Open()
if err != nil {
return defaultIcon
}
defer file.Close()

if req.Icon == nil {
return ""
imgData, err := io.ReadAll(file)
if err != nil {
return defaultIcon
}

return *req.Icon
return base64.StdEncoding.EncodeToString(imgData)
}

func getWebLogoIconUrlByUploadImg(ctx *gin.Context) string {
file, _ := ctx.FormFile("file")
dst := filepath.Join("upload", file.Filename) // 上传静态文件 url
if err := ctx.SaveUploadedFile(file, filepath.Join("web", dst)); err != nil {
return ""
func getIconBase64ByURL(req *v1.SiteUpdateReq) string {
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}

resp, err := client.Get(req.Icon)
if err != nil {
return defaultIcon
}
defer resp.Body.Close()

imgData, err := io.ReadAll(resp.Body)
if err != nil {
return defaultIcon
}
return filepath.Join("/", dst)

return base64.StdEncoding.EncodeToString(imgData)
}

func (s *service) Update(ctx *gin.Context, req *v1.SiteUpdateReq) (resp *v1.SiteUpdateResp, err error) {
update := make(map[string]any)

if req.CategoryId != nil {
if req.CategoryId != 0 {
update["CategoryID"] = req.CategoryId
}
if req.Title != nil {
if req.Title != "" {
update["Title"] = req.Title
}
if req.Icon != nil {
update["Icon"] = IconPath(ctx, req)
if req.Icon != "" {
update["Icon"] = getIconBase64ByURL(req)
}
if req.File != nil {
update["Icon"] = getIconBase64ByFormFile(req)
}
if req.Description != nil {
if req.Description != "" {
update["Description"] = req.Description
}
if req.Url != nil {
if req.Url != "" {
update["Url"] = req.Url
}
if req.IsUsed != nil {
Expand Down
19 changes: 4 additions & 15 deletions web/templates/site/site_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,8 @@
'<input style="display:none" autofocus="" class="form-control" type="text" maxLength="200" id="id" value="' + id + '">' +
'</div>' +
'<div>' +
'<label class="control-label">Logo 链接</label>' +
'<input autofocus="" class="form-control" type="text" maxLength="200" id="thumb" placeholder="请输入网站 LOGO" value="' + thumb + '">' +
'<label class="control-label">Logo</label>' +
'<input autofocus="" class="form-control" type="text" maxLength="200" id="thumb" placeholder="请输入Logo链接" value="">' +
'</div>' +
'<input id="file" name="file" type="file" class="file">' +
'<div>' +
Expand All @@ -422,7 +422,7 @@
showPreview: false, // 显示预览
maxFileSize: 2500, // 最大文件大小
initialPreviewAsData: true,
initialPreview: [thumb], // 初始预览数据
initialPreview: [], // 初始预览数据
allowedFileExtensions: ["jpg", "png"], // 允许的文件扩展名
browseLabel: '选择文件', // 自定义浏览按钮标签
removeLabel: '删除', // 自定义删除按钮标签
Expand Down Expand Up @@ -461,20 +461,9 @@
btnClass: 'btn-orange',

action: function () {
const file = $("#file").val();
const thumb = $("#thumb").val();

if (thumb === "" && file === "") {
$.alert({
title: '温馨提示',
icon: 'mdi mdi-alert',
type: 'orange',
content: '请输入网站 logo 或者上传 logo 图片',
});
return false;
}

const title = $("#title").val();

if (title === "") {
$.alert({
title: '温馨提示',
Expand Down
Loading