作者 tangxvhui

获取图片的尺寸

... ... @@ -75,9 +75,11 @@ service Core {
@doc "小程序首页数据展示"
@handler MiniShowHomePage
get /show/home_page (MiniHomePageRequest) returns (MiniHomePageRespose)
get /show/home_page (MiniHomePageRequest) returns (MiniHomePageResponse)
@doc "小程序首页搜索文章"
@handler MiniSearchArticlePage
post /show/search_article (MiniSearchArticleRequest) returns (MiniSearchArticleResponse)
}
// 管理后台接口
... ...
... ... @@ -53,9 +53,10 @@ type (
CountLove int `json:"countLove"` // 点赞数量
CountComment int `json:"countComment"` // 评论数量
CountRead int `json:"countRead"` // 浏览数量
Show int `json:"show"` // 评论的展示状态(0显示、1不显示)
Show int `json:"show"` // 评论的展示状态(1显示、2不显示)
Edit int `json:"edit"` // 文章是否存在变更记录 (0 不存在 1存在)
MeLoveFlag int `json:"meLoveFlag"` // 当前人员对文章的点赞标识 (0 没有点赞 1有点赞)
Tags []string `json:"tags"` //文章的标签
}
ArticleSection {
Id int64 `json:"id"` //段落id
... ... @@ -469,7 +470,7 @@ type (
CompanyId int64 `path:",optional"`
UserId int64 `path:",optional"`
}
MiniHomePageRespose {
MiniHomePageResponse {
TagCategory []string `json:"tagCategory"`
Tags []ArticleTagCount `json:"tags"`
}
... ... @@ -483,3 +484,32 @@ type (
ReadArticle int `json:"readArticle"` // 已读的标签数量
}
)
//小程序首页搜索文章
type (
MiniSearchArticleRequest {
Page int `json:"page"`
Size int `json:"size"`
CompanyId int64 `json:",optional"`
UserId int64 `json:",optional"`
TagGroup string `json:"tagGroup"`
TagId int64 `json:"tagId"`
BeginTime int64 `json:"beginTime"`
EndTime int `json:"endTime"`
SearchWord string `json:"searchWord"`
}
//
MiniSearchArticleResponse {
Total int `json:"total"`
List []MiniSearchArticleItem `json:"list"`
}
//
MiniSearchArticleItem{
ArticleId int64 `json:"articleId"`
Title string `json:"title"`
Author string `json:"author"` // 发布人
Images []string `json:"images"`
CreatedAt int64 `json:"createdAt"`
MeReadFlag int `json:"meReadFlag"` //已读标识 [0:未读] [1:已读]
}
)
\ No newline at end of file
... ...
package article
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/article"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
)
func MiniSearchArticlePageHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.MiniSearchArticleRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := article.NewMiniSearchArticlePageLogic(r.Context(), svcCtx)
resp, err := l.MiniSearchArticlePage(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
... ... @@ -369,6 +369,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/show/home_page",
Handler: article.MiniShowHomePageHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/show/search_article",
Handler: article.MiniSearchArticlePageHandler(serverCtx),
},
},
rest.WithJwt(serverCtx.Config.MiniAuth.AccessSecret),
rest.WithPrefix("/v1/mini"),
... ...
... ... @@ -2,6 +2,7 @@ package article
import (
"context"
"strconv"
"strings"
"text/template"
... ... @@ -9,6 +10,7 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/tool/oss"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"github.com/samber/lo"
... ... @@ -52,13 +54,16 @@ func (l *MiniCreateArticleLogic) MiniCreateArticle(req *types.MiniArticleCreateR
if len(req.Images) > 9 {
return nil, xerr.NewErrMsg("图片数量最多9张")
}
//TODO 获取图片的尺寸大小
//获取图片的尺寸大小
images := []domain.Image{}
for _, val := range req.Images {
fInfo, _ := oss.GetImageInfo(val)
w, _ := strconv.Atoi(fInfo.ImageWidth.Value)
h, _ := strconv.Atoi(fInfo.ImageHeight.Value)
images = append(images, domain.Image{
Url: val,
Width: 0,
Height: 0,
Width: w,
Height: h,
})
}
... ... @@ -157,7 +162,7 @@ func (l *MiniCreateArticleLogic) MiniCreateArticle(req *types.MiniArticleCreateR
CountLove: 0,
CountComment: 0,
CountRead: 0,
Show: 0,
Show: domain.ArticleShowEnable,
Tags: []int64{},
}
if len(whoRead) > 0 {
... ...
... ... @@ -38,9 +38,7 @@ func (l *MiniGetArticleLogic) MiniGetArticle(req *types.MiniArticleGetRequest) (
if articleInfo.CompanyId != req.CompanyId {
return nil, xerr.NewErrMsg("没有查看权限")
}
//TODO 检查可查看人
if articleInfo.Show == domain.ArticleShowDisable {
// 文章内容不显示
resp = &types.MiniArticleGetResponse{
... ... @@ -71,6 +69,14 @@ func (l *MiniGetArticleLogic) MiniGetArticle(req *types.MiniArticleGetRequest) (
meLoveFlag = 1
}
}
tags := []string{}
if len(articleInfo.Tags) > 0 {
queryOption := domain.NewQueryOptions().WithFindOnly().MustWithKV("ids", articleInfo.Tags)
_, tagList, _ := l.svcCtx.ArticleTagRepository.Find(l.ctx, conn, req.CompanyId, queryOption)
for _, val := range tagList {
tags = append(tags, val.Name)
}
}
sortBy := domain.SortArticleSection(sectionList)
sort.Sort(sortBy)
... ... @@ -111,6 +117,7 @@ func (l *MiniGetArticleLogic) MiniGetArticle(req *types.MiniArticleGetRequest) (
Show: int(articleInfo.Show),
Edit: 0,
MeLoveFlag: meLoveFlag,
Tags: tags,
}
if articleInfo.CreatedAt != articleInfo.UpdatedAt {
resp.Edit = 1
... ...
package article
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type MiniSearchArticlePageLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewMiniSearchArticlePageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniSearchArticlePageLogic {
return &MiniSearchArticlePageLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *MiniSearchArticlePageLogic) MiniSearchArticlePage(req *types.MiniSearchArticleRequest) (resp *types.MiniSearchArticleResponse, err error) {
// todo: add your logic here and delete this line
return
}
... ...
... ... @@ -26,7 +26,7 @@ func NewMiniShowHomePageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *
}
}
func (l *MiniShowHomePageLogic) MiniShowHomePage(req *types.MiniHomePageRequest) (resp *types.MiniHomePageRespose, err error) {
func (l *MiniShowHomePageLogic) MiniShowHomePage(req *types.MiniHomePageRequest) (resp *types.MiniHomePageResponse, err error) {
// 获取所有的标签
var conn = l.svcCtx.DefaultDBConn()
queryOption := domain.NewQueryOptions().WithFindOnly()
... ... @@ -64,7 +64,7 @@ func (l *MiniShowHomePageLogic) MiniShowHomePage(req *types.MiniHomePageRequest)
tagCount = append(tagCount, m)
}
tagCategory = lo.Uniq(tagCategory)
resp = &types.MiniHomePageRespose{
resp = &types.MiniHomePageResponse{
TagCategory: tagCategory,
Tags: tagCount,
}
... ...
... ... @@ -2,11 +2,14 @@ package article
import (
"context"
"strconv"
"github.com/samber/lo"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway/authlib"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/tool/oss"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
... ... @@ -36,13 +39,16 @@ func (l *SystemUpdateArticleLogic) SystemUpdateArticle(req *types.SystemArticleU
if err != nil {
return nil, xerr.NewErrMsgErr("帖子不存在", err)
}
//TODO 获取图片的尺寸大小
//获取图片的尺寸大小
images := []domain.Image{}
for _, val := range req.Images {
fInfo, _ := oss.GetImageInfo(val)
w, _ := strconv.Atoi(fInfo.ImageWidth.Value)
h, _ := strconv.Atoi(fInfo.ImageHeight.Value)
images = append(images, domain.Image{
Url: val,
Width: 0,
Height: 0,
Width: w,
Height: h,
})
}
article.Title = req.Title
... ...
... ... @@ -130,7 +130,7 @@ func (l *MiniCreateArticleCommentLogic) MiniCreateArticleComment(req *types.Mini
CountReply: 0,
CountUserLove: 0,
CountAdminLove: 0,
Show: 0,
Show: domain.CommentShowEnable,
AtWho: []domain.UserSimple{},
}
... ...
... ... @@ -3,10 +3,12 @@ package tags
import (
"context"
"fmt"
"strconv"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/tool/oss"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"github.com/zeromicro/go-zero/core/logx"
... ... @@ -41,8 +43,10 @@ func (l *CreateTagLogic) CreateTag(req *types.TagCreateRequest) (resp *types.Tag
if cnt > 0 {
return nil, xerr.NewErrMsg(fmt.Sprintf("已存在标签 分类[%s]名称[%s]", req.Category, req.Name))
}
//TODO 获取图片的尺寸大小
//获取图片的尺寸大小
fInfo, _ := oss.GetImageInfo(req.Image)
w, _ := strconv.Atoi(fInfo.ImageWidth.Value)
h, _ := strconv.Atoi(fInfo.ImageHeight.Value)
newTag := &domain.ArticleTag{
Id: 0,
CompanyId: req.CompanyId,
... ... @@ -52,8 +56,8 @@ func (l *CreateTagLogic) CreateTag(req *types.TagCreateRequest) (resp *types.Tag
Version: 0,
Image: domain.Image{
Url: req.Image,
Width: 0,
Height: 0,
Width: w,
Height: h,
},
Name: req.Name,
Category: req.Category,
... ...
... ... @@ -3,10 +3,12 @@ package tags
import (
"context"
"fmt"
"strconv"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/tool/oss"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"github.com/zeromicro/go-zero/core/logx"
... ... @@ -52,11 +54,16 @@ func (l *EditTagLogic) EditTag(req *types.TagEditRequest) (resp *types.TagEditRe
if oldTag.CompanyId != req.CompanyId {
return nil, xerr.NewErrMsg("修改标签失败")
}
//TODO 获取图片的尺寸大小
//获取图片的尺寸大小
fInfo, _ := oss.GetImageInfo(req.Image)
w, _ := strconv.Atoi(fInfo.ImageWidth.Value)
h, _ := strconv.Atoi(fInfo.ImageHeight.Value)
oldTag.Category = req.Category
oldTag.Image.Url = req.Image
oldTag.Image = domain.Image{
Url: req.Image,
Width: w,
Height: h,
}
oldTag.Name = req.Name
oldTag.Remark = req.Remark
oldTag.Other = req.Other
... ...
... ... @@ -615,9 +615,10 @@ type MiniArticleGetResponse struct {
CountLove int `json:"countLove"` // 点赞数量
CountComment int `json:"countComment"` // 评论数量
CountRead int `json:"countRead"` // 浏览数量
Show int `json:"show"` // 评论的展示状态(0显示、1不显示)
Show int `json:"show"` // 评论的展示状态(1显示、2不显示)
Edit int `json:"edit"` // 文章是否存在变更记录 (0 不存在 1存在)
MeLoveFlag int `json:"meLoveFlag"` // 当前人员对文章的点赞标识 (0 没有点赞 1有点赞)
Tags []string `json:"tags"` //文章的标签
}
type ArticleSection struct {
... ... @@ -1004,7 +1005,7 @@ type MiniHomePageRequest struct {
UserId int64 `path:",optional"`
}
type MiniHomePageRespose struct {
type MiniHomePageResponse struct {
TagCategory []string `json:"tagCategory"`
Tags []ArticleTagCount `json:"tags"`
}
... ... @@ -1019,6 +1020,32 @@ type ArticleTagCount struct {
ReadArticle int `json:"readArticle"` // 已读的标签数量
}
type MiniSearchArticleRequest struct {
Page int `json:"page"`
Size int `json:"size"`
CompanyId int64 `json:",optional"`
UserId int64 `json:",optional"`
TagGroup string `json:"tagGroup"`
TagId int64 `json:"tagId"`
BeginTime int64 `json:"beginTime"`
EndTime int `json:"endTime"`
SearchWord string `json:"searchWord"`
}
type MiniSearchArticleResponse struct {
Total int `json:"total"`
List []MiniSearchArticleItem `json:"list"`
}
type MiniSearchArticleItem struct {
ArticleId int64 `json:"articleId"`
Title string `json:"title"`
Author string `json:"author"` // 发布人
Images []string `json:"images"`
CreatedAt int64 `json:"createdAt"`
MeReadFlag int `json:"meReadFlag"` //已读标识 [0:未读] [1:已读]
}
type RoleGetRequest struct {
Id int64 `path:"id"`
}
... ...
... ... @@ -147,13 +147,13 @@ t_article_and_tag_2 as (
from article_and_tag
join article on article_and_tag.article_id = article.id
where article.deleted_at=0
and article.company_id =?
and article."show" =0
and article.company_id = ?
and article."show" = ?
and (article.target_user =0 or article.who_read @> ?)
),
-- 查询人员已查看的文章
t_user_read as(
select user_read_article.article_id from user_read_article where user_read_article.user_id =?
select user_read_article.article_id from user_read_article where user_read_article.user_id = ?
)
-- 汇总统计 total_article 符合条件的文章总数,read_article 已浏览的数量
select count(t_article_and_tag_2.article_id) as total_article ,count(t_user_read.article_id) as read_article, t_article_and_tag_2.tag_id
... ... @@ -163,6 +163,7 @@ group by t_article_and_tag_2.tag_id
`
condition := []interface{}{
companyId,
domain.ArticleShowEnable,
fmt.Sprintf("[%d]", userId),
userId,
}
... ...
... ... @@ -279,11 +279,11 @@ func (repository *ArticleCommentRepository) Top5Comment(ctx context.Context, con
article_comment.id ,
(article_comment.count_reply +article_comment.count_user_love +article_comment.count_admin_love ) cnt
from article_comment
where top_id =0 and article_id =? and deleted_at=0 and show=0 and company_id=?
where top_id =0 and article_id =? and deleted_at=0 and show= ? and company_id=?
order by cnt desc,article_comment.id desc
limit 5 `
db := conn.DB()
rows, err := db.Raw(sql1, articleId, companyId).Rows()
rows, err := db.Raw(sql1, articleId, domain.CommentShowEnable, companyId).Rows()
if err != nil {
return nil, err
}
... ...
... ... @@ -25,7 +25,7 @@ type Article struct {
CountLove int `json:"countLove"` // 点赞数量
CountComment int `json:"countComment"` // 评论数量
CountRead int `json:"countRead"` // 浏览数量
Show ArticleShow `json:"show"` // 评论的展示状态(0显示、1不显示
Show ArticleShow `json:"show"` // 评论的展示状态(1显示,2不显示、
Tags []int64 `json:"tags"` // 定性标签
Summary string `json:"summary"` // 内容概要
// ...more
... ... @@ -60,12 +60,12 @@ func (a ArticleTarget) Named() string {
return ""
}
// 文章的展示状态(0显示、1不显示)
// 文章的展示状态(1显示,2不显示)
type ArticleShow int
const (
ArticleShowEnable ArticleShow = 0
ArticleShowDisable ArticleShow = 1
ArticleShowEnable ArticleShow = 1
ArticleShowDisable ArticleShow = 2
)
func (a ArticleShow) Named() string {
... ...
... ... @@ -27,17 +27,17 @@ type ArticleComment struct {
CountReply int `json:"countReply"` // 回复数量
CountUserLove int `json:"countUserLove"` // 用户点赞数量
CountAdminLove int `json:"countAdminLove"` // 运营点赞数量
Show CommentShow `json:"showState"` // 评论的展示状态(0显示、1不显示)
Show CommentShow `json:"showState"` // 评论的展示状态(1显示、2不显示)
AtWho []UserSimple `json:"atWho"` // 填写评论时@的人
// ...more
}
// 评论的展示状态(0显示、1不显示)
// 评论的展示状态(1显示、2不显示)
type CommentShow int
const (
CommentShowEnable CommentShow = 0
CommentShowDisable CommentShow = 1
CommentShowEnable CommentShow = 1
CommentShowDisable CommentShow = 2
)
func (show CommentShow) Named() string {
... ...
package oss
import (
"encoding/json"
"fmt"
"image"
_ "image/jpeg"
"net/http"
"strings"
"time"
)
type FileInfo struct {
FileSize struct {
Value string
}
Format struct {
Value string
}
ImageHeight struct {
Value string
}
ImageWidth struct {
Value string
}
}
func GetImageInfo(url string) (info FileInfo, err error) {
//ok := strings.HasPrefix(url, "https://timeless-world.oss-cn-shenzhen.aliyuncs.com")
ok := strings.HasPrefix(url, "http")
if !ok {
return
}
ok = strings.Contains(url, "aliyuncs")
if !ok {
return
}
apiUrl := url + `?x-oss-process=image/info`
req, err := http.NewRequest(http.MethodGet, apiUrl, nil)
if err != nil {
return info, err
}
httpclient := http.Client{
Timeout: 30 * time.Second,
}
resp, err := httpclient.Do(req)
if err != nil {
return info, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return
}
jDecoder := json.NewDecoder(resp.Body)
err = jDecoder.Decode(&info)
if err != nil {
return info, err
}
return info, nil
}
// 获取视频封面图
func GetVideoCover(videoUrl string) (coverUrl string, w int, h int, err error) {
ok := strings.HasPrefix(videoUrl, "http")
if !ok {
return
}
ok = strings.Contains(videoUrl, "aliyuncs")
if !ok {
return
}
videoUrl = videoUrl + "?x-oss-process=video/snapshot,t_100,f_jpg,m_fast"
res, err := http.Get(videoUrl)
if err != nil || res.StatusCode != http.StatusOK {
return videoUrl, 600, 600, fmt.Errorf("获取图片失败:%s", err)
}
defer res.Body.Close()
m, _, err := image.Decode(res.Body)
if err != nil {
return videoUrl, 600, 600, fmt.Errorf("获取图片失败:%s", err)
}
return videoUrl, m.Bounds().Dx(), m.Bounds().Dy(), nil
}
... ...
package oss
import "testing"
func TestGetVideoCover(t *testing.T) {
cover, w, h, err := GetVideoCover("https://timeless-world.oss-cn-shenzhen.aliyuncs.com/open-api/dev_online/20230913/object/1694587897_yYfG6TYTsGMCKETxdnTEhAQjXpYGD3MB.mp4")
t.Logf("cover=%v, w=%v, h=%v, err=%v", cover, w, h, err)
}
... ...