diff --git a/app/controller/admin/article.go b/app/controller/admin/article.go index 3caa2b0796773863643ecafaeea47a3bce1d9a78..cf54b0a1e4c324d7496c5609d964657b58622f53 100644 --- a/app/controller/admin/article.go +++ b/app/controller/admin/article.go @@ -87,11 +87,11 @@ func (b *Article) EditMd(c *gin.Context) { response.HTML(c, "article/md", nil) } -// Insert 新增保存 func (b *Article) Insert(c *gin.Context) { Article := &model.Article{} Article.Username = c.GetString("current_user_name") + // Bind the incoming request data to the Article struct if err := c.ShouldBind(&Article); err != nil { c.JSON(400, gin.H{ "err": err.Error(), @@ -99,41 +99,37 @@ func (b *Article) Insert(c *gin.Context) { return } - // Article.Content = utils.RepImages(Article.Content) - err := service.Article.Create(Article) + // Extract the first image URL from the content and store it in smallimg + Article.Smallimg = "\\" + extractImageURL(Article.Content) + // Create the article in the database + err := service.Article.Create(Article) if err != nil { logrus.Error("新增失败", err) response.Fail(c, e.ErrorUpdate) return } + // Return the success response with the created article data response.Success(c, Article) } -// Update 修改 -func (b *Article) Update(c *gin.Context) { - Article := &model.Article{} - - if err := c.ShouldBind(&Article); err != nil { - c.JSON(400, gin.H{ - "err": err.Error(), - }) - return - } - - id := strconv.Itoa(Article.Id) - err := service.Article.Update(id, Article) +// extractImageURL 用于提取 content 中的图片路径 +func extractImageURL(content string) string { + // 正则表达式匹配 img 标签中的 src 属性 + re := regexp.MustCompile(`]*src=["']([^"']+)["'][^>]*>`) + matches := re.FindStringSubmatch(content) - if err != nil { - logrus.Error("修改失败", err) - response.Fail(c, e.ErrorUpdate) - return + if len(matches) > 1 { + // 返回第一个图片的 src 地址 + return matches[1] } - response.Success(c, Article) + // 如果没有找到图片路径,返回空字符串 + return "" } + // Destroy 删除 func (b *Article) Destroy(c *gin.Context) { id := c.PostForm("id") diff --git a/app/controller/admin/upload.go b/app/controller/admin/upload.go index 5120369612170022ed88a03a4aabe6b9a34e7aab..dffe7661d1ab842ded4acec6b1dd02605d490bb3 100644 --- a/app/controller/admin/upload.go +++ b/app/controller/admin/upload.go @@ -78,7 +78,7 @@ func uploadLocal(c *gin.Context, file *multipart.FileHeader) (string, error) { if err != nil { return "", err } - return filePath, nil + return "\\"+filePath, nil } func uploadOss(file *multipart.FileHeader) (bool, string) { diff --git a/views/admin/banner/index.html b/views/admin/banner/index.html index 3ee726e2991fd12162d28367c3e16ab29da1afb2..dc178268a752b280f8b3fe387328c48c43d824f1 100644 --- a/views/admin/banner/index.html +++ b/views/admin/banner/index.html @@ -230,8 +230,8 @@ handleRemove (file, fileList) { }, handleUploadSuccess (response, file, fileList) { - if (response && response.data) { - this.modelForm.smallimg = '/' + response.data.path + if (response && response.location) { + this.modelForm.smallimg = '/' + response.location } }, handleImgPreview (file) {