友链查询与添加管理
This commit is contained in:
parent
f2a1bb5c37
commit
9cc5036bc8
@ -9,10 +9,6 @@ import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type IHelloV1 interface {
|
||||
Hello(ctx context.Context, req *request.HelloReq) (res *request.HelloRes, err error)
|
||||
}
|
||||
|
||||
type IAuthV1 interface {
|
||||
AuthRegister(ctx context.Context, req *request.RegisterReq) (res *request.RegisterRes, err error)
|
||||
AuthLogin(ctx context.Context, req *request.LoginReq) (res *request.LoginRes, err error)
|
||||
@ -24,3 +20,13 @@ type ITokenV1 interface {
|
||||
TokenCreate(ctx context.Context, req *request.TokenCreateReq) (res *request.TokenCreateRes, err error)
|
||||
TokenVerify(ctx context.Context, req *request.TokenVerifyReq) (res *request.TokenVerifyRes, err error)
|
||||
}
|
||||
|
||||
type ILinkV1 interface {
|
||||
GetLinkFriend(ctx context.Context, req *request.GetLinkFriendReq) (res *request.GetLinkFriendRes, err error)
|
||||
AddLinkFriend(ctx context.Context, req *request.AddLinkFriendReq) (res *request.AddLinkFriendRes, err error)
|
||||
GetSortAndLink(ctx context.Context, req *request.GetSortAndLinkReq) (res *request.GetSortAndLinkRes, err error)
|
||||
}
|
||||
|
||||
type IUserV1 interface {
|
||||
GetUserCurrent(ctx context.Context, req *request.GetUserReq) (res *request.GetUserRes, err error)
|
||||
}
|
||||
|
@ -1,12 +0,0 @@
|
||||
package request
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
type HelloReq struct {
|
||||
g.Meta `path:"/hello" tags:"Hello" method:"get" summary:"You first hello api"`
|
||||
}
|
||||
type HelloRes struct {
|
||||
g.Meta `mime:"text/html" example:"string"`
|
||||
}
|
17
api/request/linkApi.go
Normal file
17
api/request/linkApi.go
Normal file
@ -0,0 +1,17 @@
|
||||
package request
|
||||
|
||||
import "github.com/gogf/gf/v2/frame/g"
|
||||
|
||||
type GetLinkFriendReq struct {
|
||||
g.Meta `path:"/list" tags:"获取友链" method:"get" summary:"获取友链"`
|
||||
}
|
||||
type AddLinkFriendReq struct {
|
||||
g.Meta `path:"/add" tags:"添加友链" method:"post" summary:"添加友链"`
|
||||
}
|
||||
type GetSortAndLinkReq struct {
|
||||
g.Meta `path:"/sort" tags:"获取分类和友链" method:"get" summary:"获取分类和友链"`
|
||||
}
|
||||
|
||||
type GetLinkFriendRes struct{}
|
||||
type AddLinkFriendRes struct{}
|
||||
type GetSortAndLinkRes struct{}
|
9
api/request/userApi.go
Normal file
9
api/request/userApi.go
Normal file
@ -0,0 +1,9 @@
|
||||
package request
|
||||
|
||||
import "github.com/gogf/gf/v2/frame/g"
|
||||
|
||||
type GetUserReq struct {
|
||||
g.Meta `path:"/current" tags:"获取用户信息" method:"get" summary:"获取用户信息"`
|
||||
}
|
||||
|
||||
type GetUserRes struct{}
|
@ -1,16 +1,16 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"PersonalMain/internal/controller/auth/link/custom/friend"
|
||||
"PersonalMain/internal/controller/auth/token"
|
||||
"PersonalMain/internal/controller/auth/user"
|
||||
authUser "PersonalMain/internal/controller/auth/user"
|
||||
"PersonalMain/internal/controller/user"
|
||||
"PersonalMain/internal/middleware"
|
||||
"context"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gcmd"
|
||||
|
||||
"PersonalMain/internal/controller/hello"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -24,16 +24,14 @@ var (
|
||||
group.Middleware(middleware.JsonResponseMiddleware)
|
||||
group.Middleware(middleware.TimestampMiddleware)
|
||||
|
||||
group.Group("/", func(group *ghttp.RouterGroup) {
|
||||
group.Bind(
|
||||
hello.NewV1(),
|
||||
)
|
||||
})
|
||||
/*
|
||||
* 用户管理
|
||||
*/
|
||||
group.Group("/auth", func(group *ghttp.RouterGroup) {
|
||||
group.Group("/user", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(middleware.VerifyTokenMiddleware)
|
||||
group.Bind(
|
||||
user.NewAuthV1(),
|
||||
authUser.NewAuthV1(),
|
||||
)
|
||||
})
|
||||
group.Group("/token", func(group *ghttp.RouterGroup) {
|
||||
@ -42,6 +40,38 @@ var (
|
||||
)
|
||||
})
|
||||
})
|
||||
/*
|
||||
* 博客连接管理
|
||||
*/
|
||||
group.Group("/link", func(group *ghttp.RouterGroup) {
|
||||
// 公共访问
|
||||
group.Group("/custom", func(group *ghttp.RouterGroup) {
|
||||
group.Group("/friend", func(group *ghttp.RouterGroup) {
|
||||
group.Bind(
|
||||
friend.NewLinkCustomFriendV1(),
|
||||
)
|
||||
})
|
||||
group.Group("/location", func(group *ghttp.RouterGroup) {
|
||||
//location.NewLinkCustomLocationV1()
|
||||
})
|
||||
group.Group("/color", func(group *ghttp.RouterGroup) {
|
||||
//color.NewLinkCustomColorV1()
|
||||
})
|
||||
})
|
||||
// 管理员访问
|
||||
//group.Group("/admin", func(group *ghttp.RouterGroup) {
|
||||
//
|
||||
//})
|
||||
})
|
||||
/*
|
||||
* 用户管理
|
||||
*/
|
||||
group.Group("/user", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(middleware.VerifyTokenMiddleware)
|
||||
group.Bind(
|
||||
user.NewUserV1(),
|
||||
)
|
||||
})
|
||||
})
|
||||
s.SetServerRoot("resource/public")
|
||||
s.Run()
|
||||
|
@ -0,0 +1,87 @@
|
||||
package friend
|
||||
|
||||
import (
|
||||
"PersonalMain/api"
|
||||
"PersonalMain/api/request"
|
||||
"PersonalMain/internal/model/entity"
|
||||
"PersonalMain/internal/service/LinkService"
|
||||
"PersonalMain/utility/ErrorCode"
|
||||
"PersonalMain/utility/ResultUtil"
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
)
|
||||
|
||||
type ControllerV1 struct{}
|
||||
|
||||
func NewLinkCustomFriendV1() api.ILinkV1 {
|
||||
return &ControllerV1{}
|
||||
}
|
||||
|
||||
func linkService() LinkService.LinkService {
|
||||
return LinkService.NewLinkService()
|
||||
}
|
||||
|
||||
// GetLinkFriend
|
||||
//
|
||||
// 获取友链
|
||||
func (*ControllerV1) GetLinkFriend(ctx context.Context, _ *request.GetLinkFriendReq) (res *request.GetLinkFriendRes, err error) {
|
||||
req := ghttp.RequestFromCtx(ctx)
|
||||
// 获取业务
|
||||
linkService().GetLinkFriend(req)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// GetSortAndLink
|
||||
//
|
||||
// 获取分类和友链
|
||||
func (*ControllerV1) GetSortAndLink(ctx context.Context, _ *request.GetSortAndLinkReq) (res *request.GetSortAndLinkRes, err error) {
|
||||
req := ghttp.RequestFromCtx(ctx)
|
||||
// 获取业务
|
||||
linkService().GetSortAndLink(req)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// AddLinkFriend
|
||||
//
|
||||
// 添加友链
|
||||
func (*ControllerV1) AddLinkFriend(ctx context.Context, _ *request.AddLinkFriendReq) (res *request.AddLinkFriendRes, err error) {
|
||||
req := ghttp.RequestFromCtx(ctx)
|
||||
// 获取业务
|
||||
addFriendVO := entity.LinkAddFriendVO{}
|
||||
err = req.GetRequestStruct(&addFriendVO)
|
||||
if err == nil {
|
||||
// 检查对象
|
||||
errStruct := g.Validator().Data(addFriendVO).Run(ctx)
|
||||
if errStruct == nil {
|
||||
if addFriendVO.LinkAccept {
|
||||
hasAdd, blogDO, info := linkService().AddLinkFriendCustom(req, addFriendVO)
|
||||
if hasAdd {
|
||||
ResultUtil.Success(req, "添加成功", blogDO)
|
||||
} else {
|
||||
switch info {
|
||||
case "FriendLinkAlreadyExists":
|
||||
ResultUtil.ErrorNoData(req, ErrorCode.FriendLinkAlreadyExists)
|
||||
case "LinkAddressError":
|
||||
ResultUtil.ErrorNoData(req, ErrorCode.LinkAddressError)
|
||||
case "LikeLinkAlreadyExists":
|
||||
ResultUtil.ErrorNoData(req, ErrorCode.LikeLinkAlreadyExists)
|
||||
case "AddLinkFriendError":
|
||||
ResultUtil.ErrorNoData(req, ErrorCode.ServerDatabaseInteriorError)
|
||||
default:
|
||||
ResultUtil.ErrorNoData(req, ErrorCode.ServerUnknownError)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ResultUtil.ErrorNoData(req, ErrorCode.LinkAcceptDenied)
|
||||
}
|
||||
} else {
|
||||
g.Log().Cat("Struct").Cat("Link").Notice(ctx, errStruct.Map())
|
||||
ResultUtil.Error(req, ErrorCode.RequestBodyError, errStruct.Error())
|
||||
}
|
||||
} else {
|
||||
g.Log().Cat("Struct").Cat("Link").Error(ctx, err.Error())
|
||||
ResultUtil.Error(req, ErrorCode.RequestBodyError, err.Error())
|
||||
}
|
||||
return res, err
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package token
|
||||
|
||||
import (
|
||||
"PersonalMain/api"
|
||||
)
|
||||
|
||||
type ControllerV1 struct{}
|
||||
|
||||
func NewTokenV1() api.ITokenV1 {
|
||||
return &ControllerV1{}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package token
|
||||
|
||||
import (
|
||||
"PersonalMain/api"
|
||||
"PersonalMain/api/request"
|
||||
"PersonalMain/internal/model/do"
|
||||
"PersonalMain/internal/service/TokenService"
|
||||
@ -11,6 +12,12 @@ import (
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
)
|
||||
|
||||
type ControllerV1 struct{}
|
||||
|
||||
func NewTokenV1() api.ITokenV1 {
|
||||
return &ControllerV1{}
|
||||
}
|
||||
|
||||
// TokenCreate
|
||||
//
|
||||
// 生成 TokenServiceImpl
|
||||
|
@ -1,11 +0,0 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"PersonalMain/api"
|
||||
)
|
||||
|
||||
type ControllerV1 struct{}
|
||||
|
||||
func NewAuthV1() api.IAuthV1 {
|
||||
return &ControllerV1{}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"PersonalMain/api"
|
||||
"PersonalMain/api/request"
|
||||
"PersonalMain/internal/model/entity"
|
||||
"PersonalMain/internal/service/UserService"
|
||||
@ -15,6 +16,12 @@ func userService() UserService.UserService {
|
||||
return UserService.NewUserService()
|
||||
}
|
||||
|
||||
type ControllerV1 struct{}
|
||||
|
||||
func NewAuthV1() api.IAuthV1 {
|
||||
return &ControllerV1{}
|
||||
}
|
||||
|
||||
// AuthRegister
|
||||
//
|
||||
// 用户注册
|
||||
@ -29,9 +36,11 @@ func (*ControllerV1) AuthRegister(ctx context.Context, _ *request.RegisterReq) (
|
||||
// 进行用户注册
|
||||
userService().UserRegister(req, &userRegister)
|
||||
} else {
|
||||
g.Log().Cat("Struct").Cat("Auth").Error(ctx, errStruct.Error())
|
||||
ResultUtil.Error(req, ErrorCode.RequestBodyMismatching, errStruct.Map())
|
||||
}
|
||||
} else {
|
||||
g.Log().Cat("Struct").Cat("Auth").Error(ctx, errStruct.Error())
|
||||
ResultUtil.Error(req, ErrorCode.RequestBodyError, errStruct.Error())
|
||||
}
|
||||
return res, err
|
||||
@ -48,6 +57,7 @@ func (*ControllerV1) AuthLogin(ctx context.Context, _ *request.LoginReq) (res *r
|
||||
// 进行用户注册
|
||||
userService().UserLogin(req, &userLogin)
|
||||
} else {
|
||||
g.Log().Cat("Struct").Cat("Auth").Error(ctx, errStruct.Error())
|
||||
ResultUtil.Error(req, ErrorCode.RequestBodyMismatching, errStruct.Map())
|
||||
}
|
||||
} else {
|
||||
|
@ -1 +1,35 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"PersonalMain/api"
|
||||
"PersonalMain/api/request"
|
||||
"PersonalMain/internal/service/UserService"
|
||||
"PersonalMain/utility/ErrorCode"
|
||||
"PersonalMain/utility/ResultUtil"
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
)
|
||||
|
||||
func userService() UserService.UserService {
|
||||
return UserService.NewUserService()
|
||||
}
|
||||
|
||||
type ControllerV1 struct{}
|
||||
|
||||
func NewUserV1() api.IUserV1 {
|
||||
return &ControllerV1{}
|
||||
}
|
||||
|
||||
// GetUserCurrent
|
||||
//
|
||||
// 获取当前用户
|
||||
func (*ControllerV1) GetUserCurrent(ctx context.Context, _ *request.GetUserReq) (res *request.GetUserRes, err error) {
|
||||
req := ghttp.RequestFromCtx(ctx)
|
||||
userDO := userService().UserCurrent(req)
|
||||
if userDO != nil {
|
||||
ResultUtil.Success(req, "获取成功", userDO)
|
||||
} else {
|
||||
ResultUtil.ErrorNoData(req, ErrorCode.UserNotExist)
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
146
internal/dao/linkDAO/linkFriendDAO.go
Normal file
146
internal/dao/linkDAO/linkFriendDAO.go
Normal file
@ -0,0 +1,146 @@
|
||||
package linkDAO
|
||||
|
||||
import (
|
||||
"PersonalMain/internal/model/do"
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
// GetBlogInformation
|
||||
//
|
||||
// 获取友链
|
||||
func GetBlogInformation() *[]do.BlogDO {
|
||||
// 数据库读取信息
|
||||
var userDO []do.BlogDO
|
||||
result, err := g.Model("xf_blog").All()
|
||||
if err == nil {
|
||||
if !result.IsEmpty() {
|
||||
_ = result.Structs(&userDO)
|
||||
g.Log().Cat("Database").Cat("Blog").Notice(context.Background(), "xf_blog 数据表数据提取成功")
|
||||
return &userDO
|
||||
} else {
|
||||
g.Log().Cat("Database").Cat("Blog").Notice(context.Background(), "xf_blog 数据表中没有博客友链相关信息")
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
g.Log().Cat("Database").Cat("Blog").Error(context.Background(), err.Error())
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// GetSort
|
||||
//
|
||||
// 获取分类
|
||||
func GetSort() *[]do.SortDO {
|
||||
// 数据库读取信息
|
||||
var sortDO []do.SortDO
|
||||
result, err := g.Model("xf_blog_sort").OrderAsc("sort").All()
|
||||
if err == nil {
|
||||
if !result.IsEmpty() {
|
||||
_ = result.Structs(&sortDO)
|
||||
g.Log().Cat("Database").Cat("Sort").Notice(context.Background(), "xf_sort 数据表数据提取成功")
|
||||
return &sortDO
|
||||
} else {
|
||||
g.Log().Cat("Database").Cat("Sort").Notice(context.Background(), "xf_sort 数据表中没有分类相关信息")
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
g.Log().Cat("Database").Cat("Sort").Error(context.Background(), err.Error())
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// GetBlogForSort
|
||||
//
|
||||
// 博客内容归类
|
||||
func GetBlogForSort(sortDO *[]do.SortDO) *[]do.SortDO {
|
||||
// 数据库读取信息
|
||||
result, err := g.Model("xf_blog").All()
|
||||
if err == nil {
|
||||
// 检查数据是否为空
|
||||
if result.IsEmpty() {
|
||||
g.Log().Cat("Database").Cat("Blog").Notice(context.Background(), "xf_blog 数据表中没有博客相关信息")
|
||||
return nil
|
||||
}
|
||||
// 获取数据
|
||||
var blogDO []do.BlogDO
|
||||
_ = result.Structs(&blogDO)
|
||||
for _, sort := range *sortDO {
|
||||
var newBlogsDO *[]do.BlogDO
|
||||
for _, blog := range blogDO {
|
||||
if *sort.Id == blog.BlogLocation {
|
||||
if newBlogsDO == nil {
|
||||
newBlogsDO = &[]do.BlogDO{blog}
|
||||
} else {
|
||||
*newBlogsDO = append(*newBlogsDO, blog)
|
||||
}
|
||||
}
|
||||
}
|
||||
(*sortDO)[*sort.Id-1].Blogs = newBlogsDO
|
||||
}
|
||||
g.Log().Cat("Database").Cat("Blog").Notice(context.Background(), "xf_blog 数据表数据提取成功")
|
||||
return sortDO
|
||||
} else {
|
||||
g.Log().Cat("Database").Cat("Blog").Error(context.Background(), err.Error())
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// GetBlogForName
|
||||
//
|
||||
// 检查是否已存在此博客
|
||||
func GetBlogForName(linkName string) *do.BlogDO {
|
||||
// 数据库读取信息
|
||||
var blogDO do.BlogDO
|
||||
result, err := g.Model("xf_blog").Where("blog_title = ?", linkName).One()
|
||||
if err == nil {
|
||||
if !result.IsEmpty() {
|
||||
_ = result.Struct(&blogDO)
|
||||
g.Log().Cat("Database").Cat("Blog").Notice(context.Background(), "xf_blog 数据表成功提取", linkName, "博客信息")
|
||||
return &blogDO
|
||||
} else {
|
||||
g.Log().Cat("Database").Cat("Blog").Notice(context.Background(), "xf_blog 数据表中没有", linkName, "博客友链相关信息")
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
g.Log().Cat("Database").Cat("Blog").Error(context.Background(), err.Error())
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// GetBlogForDomain
|
||||
//
|
||||
// 检查是否存在相似链接
|
||||
func GetBlogForDomain(domain string) *do.BlogDO {
|
||||
// 数据库读取信息
|
||||
var blogDO do.BlogDO
|
||||
result, err := g.Model("xf_blog").Where("blog_url like ?", "%"+domain+"%").One()
|
||||
if err == nil {
|
||||
if !result.IsEmpty() {
|
||||
_ = result.Struct(&blogDO)
|
||||
g.Log().Cat("Database").Cat("Blog").Notice(context.Background(), "xf_blog 数据表成功提取", blogDO.BlogTitle, "博客信息")
|
||||
return &blogDO
|
||||
} else {
|
||||
g.Log().Cat("Database").Cat("Blog").Notice(context.Background(), "xf_blog 数据表中没有查找到", domain, "相似的信息")
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
g.Log().Cat("Database").Cat("Blog").Error(context.Background(), err.Error())
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// CreateBlog
|
||||
//
|
||||
// 创建博客
|
||||
func CreateBlog(newBlogDO do.BlogDO) bool {
|
||||
// 数据库读取信息
|
||||
_, err := g.Model("xf_blog").Data(newBlogDO).Insert()
|
||||
if err == nil {
|
||||
g.Log().Cat("Database").Cat("Blog").Notice(context.Background(), "xf_blog 数据表成功创建", newBlogDO.BlogTitle, "博客信息")
|
||||
return true
|
||||
} else {
|
||||
g.Log().Cat("Database").Cat("Blog").Error(context.Background(), err.Error())
|
||||
return false
|
||||
}
|
||||
}
|
@ -74,7 +74,7 @@ func DeleteToken(token string) bool {
|
||||
// UpdateToken
|
||||
//
|
||||
// 更新Token业务
|
||||
func UpdateToken(token string, userId *int64) (*do.TokenDO, error) {
|
||||
func UpdateToken(token string, userId *uint64) (*do.TokenDO, error) {
|
||||
// 查找 token
|
||||
getTokenDO := GetToken(token)
|
||||
if getTokenDO != nil {
|
||||
|
103
internal/logic/LinkServiceImpl/FriendServiceImpl.go
Normal file
103
internal/logic/LinkServiceImpl/FriendServiceImpl.go
Normal file
@ -0,0 +1,103 @@
|
||||
package LinkServiceImpl
|
||||
|
||||
import (
|
||||
"PersonalMain/internal/dao/linkDAO"
|
||||
"PersonalMain/internal/model/do"
|
||||
"PersonalMain/internal/model/entity"
|
||||
"PersonalMain/internal/service/UserService"
|
||||
"PersonalMain/utility/ErrorCode"
|
||||
"PersonalMain/utility/ResultUtil"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
regexp "regexp"
|
||||
)
|
||||
|
||||
// GetLinkFriend
|
||||
//
|
||||
// 获取友链
|
||||
func (*DefaultLinkImpl) GetLinkFriend(req *ghttp.Request) {
|
||||
// 数据库读取信息
|
||||
blogDO := linkDAO.GetBlogInformation()
|
||||
if blogDO != nil {
|
||||
ResultUtil.Success(req, "查询成功", blogDO)
|
||||
} else {
|
||||
ResultUtil.ErrorNoData(req, ErrorCode.ServerDatabaseInteriorError)
|
||||
}
|
||||
}
|
||||
|
||||
// GetSortAndLink
|
||||
//
|
||||
// 获取分类和友链
|
||||
func (*DefaultLinkImpl) GetSortAndLink(req *ghttp.Request) {
|
||||
// 获取Sort
|
||||
sortDO := linkDAO.GetSort()
|
||||
if sortDO != nil {
|
||||
// 博客内容归类
|
||||
sortDO = linkDAO.GetBlogForSort(sortDO)
|
||||
ResultUtil.Success(req, "查询成功", sortDO)
|
||||
} else {
|
||||
ResultUtil.ErrorNoData(req, ErrorCode.ServerDatabaseInteriorError)
|
||||
}
|
||||
}
|
||||
|
||||
// AddLinkFriendCustom
|
||||
//
|
||||
// 添加友链
|
||||
func (*DefaultLinkImpl) AddLinkFriendCustom(req *ghttp.Request, addFriendVO entity.LinkAddFriendVO) (bool, *do.BlogDO, string) {
|
||||
// 检查是否已存在此博客
|
||||
blogDO := linkDAO.GetBlogForName(addFriendVO.LinkName)
|
||||
if blogDO == nil {
|
||||
// 获取链接域名
|
||||
regex, _ := regexp.Compile("^(https?://)?([^:/\\s]+)")
|
||||
matches := regex.FindStringSubmatch(addFriendVO.LinkAddress)
|
||||
if len(matches) >= 3 {
|
||||
// 检查是否存在相似链接
|
||||
likeBlogDO := linkDAO.GetBlogForDomain(matches[2])
|
||||
if likeBlogDO == nil {
|
||||
// 数据处理
|
||||
var email *string
|
||||
var userId *uint64
|
||||
if !req.Cookie.Get("Token").IsEmpty() {
|
||||
email = &UserService.NewUserService().UserCurrent(req).Email
|
||||
userId = UserService.NewUserService().UserCurrent(req).Id
|
||||
} else {
|
||||
email = addFriendVO.LinkEmail
|
||||
userId = nil
|
||||
}
|
||||
var getRss *string
|
||||
if addFriendVO.LinkRssJudge {
|
||||
getRss = addFriendVO.LinkRss
|
||||
} else {
|
||||
getRss = nil
|
||||
}
|
||||
// 创建博客
|
||||
newBlogDO := do.BlogDO{
|
||||
BlogTitle: addFriendVO.LinkName,
|
||||
BlogUrl: addFriendVO.LinkAddress,
|
||||
BlogDesc: addFriendVO.LinkDesc,
|
||||
BlogEmail: email,
|
||||
BlogAvatar: addFriendVO.LinkAvatar,
|
||||
BlogRssJudge: addFriendVO.LinkRssJudge,
|
||||
BlogRss: getRss,
|
||||
BlogHost: &addFriendVO.LinkHost,
|
||||
BlogLocation: addFriendVO.LinkUserLocation,
|
||||
BlogAddType: 0,
|
||||
BlogColor: addFriendVO.LinkUserColor,
|
||||
BlogUserId: userId,
|
||||
BlogRemark: addFriendVO.LinkRemark,
|
||||
}
|
||||
if linkDAO.CreateBlog(newBlogDO) {
|
||||
return true, &newBlogDO, ""
|
||||
} else {
|
||||
return false, nil, "AddLinkFriendError"
|
||||
}
|
||||
} else {
|
||||
return false, nil, "LikeLinkAlreadyExists"
|
||||
|
||||
}
|
||||
} else {
|
||||
return false, nil, "LinkAddressError"
|
||||
}
|
||||
} else {
|
||||
return false, nil, "FriendLinkAlreadyExists"
|
||||
}
|
||||
}
|
3
internal/logic/LinkServiceImpl/defaultLinkImpl.go
Normal file
3
internal/logic/LinkServiceImpl/defaultLinkImpl.go
Normal file
@ -0,0 +1,3 @@
|
||||
package LinkServiceImpl
|
||||
|
||||
type DefaultLinkImpl struct{}
|
@ -109,3 +109,18 @@ func (*DefaultUserImpl) UserLogout(req *ghttp.Request) {
|
||||
ResultUtil.ErrorNoData(req, ErrorCode.TokenNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
// UserCurrent
|
||||
//
|
||||
// 获取当前用户信息
|
||||
func (*DefaultUserImpl) UserCurrent(req *ghttp.Request) *do.UserDO {
|
||||
// 获取 Token
|
||||
userDO := userDAO.GetUserByToken(req.Cookie.Get("token").String())
|
||||
if userDO != nil {
|
||||
userDO.Password = ""
|
||||
userDO.OldPassword = nil
|
||||
return userDO
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
22
internal/model/do/blogDO.go
Normal file
22
internal/model/do/blogDO.go
Normal file
@ -0,0 +1,22 @@
|
||||
package do
|
||||
|
||||
import "time"
|
||||
|
||||
type BlogDO struct {
|
||||
Id *uint64 `json:"id"`
|
||||
BlogTitle string `json:"blog_title"`
|
||||
BlogUrl string `json:"blog_url"`
|
||||
BlogDesc string `json:"blog_desc"`
|
||||
BlogEmail *string `json:"blog_email"`
|
||||
BlogAvatar string `json:"blog_avatar"`
|
||||
BlogRssJudge bool `json:"blog_rss_judge"`
|
||||
BlogRss *string `json:"blog_rss"`
|
||||
BlogHost *string `json:"blog_host"`
|
||||
BlogLocation uint `json:"blog_location"`
|
||||
BlogAddType uint8 `json:"blog_add_type"`
|
||||
BlogColor uint `json:"blog_color"`
|
||||
BlogUserId *uint64 `json:"blog_user_id"`
|
||||
BlogRemark *string `json:"blog_remark"`
|
||||
CreatedAt *time.Time `json:"created_at"`
|
||||
UpdatedAt *time.Time `json:"updated_at"`
|
||||
}
|
10
internal/model/do/sortDO.go
Normal file
10
internal/model/do/sortDO.go
Normal file
@ -0,0 +1,10 @@
|
||||
package do
|
||||
|
||||
type SortDO struct {
|
||||
Id *uint `json:"id"`
|
||||
Sort *uint `json:"sort"`
|
||||
UserAble bool `json:"user_able"`
|
||||
Title string `json:"title"`
|
||||
Desc *string `json:"desc"`
|
||||
Blogs *[]BlogDO `json:"blogs"`
|
||||
}
|
@ -5,8 +5,8 @@ import (
|
||||
)
|
||||
|
||||
type TokenDO struct {
|
||||
Id *int64 `json:"id"`
|
||||
UserId *int64 `json:"user_id"`
|
||||
Id *uint64 `json:"id"`
|
||||
UserId *uint64 `json:"user_id"`
|
||||
Token string `json:"token"`
|
||||
ExpiredAt time.Time `json:"expired_at"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
|
@ -3,7 +3,7 @@ package do
|
||||
import "time"
|
||||
|
||||
type UserDO struct {
|
||||
Id *int64 `json:"id"`
|
||||
Id *uint64 `json:"id"`
|
||||
UserName string `json:"username"`
|
||||
DisplayName *string `json:"display_name"`
|
||||
Email string `json:"email"`
|
||||
|
16
internal/model/entity/linkAddFriendVO.go
Normal file
16
internal/model/entity/linkAddFriendVO.go
Normal file
@ -0,0 +1,16 @@
|
||||
package entity
|
||||
|
||||
type LinkAddFriendVO struct {
|
||||
LinkEmail *string `json:"link_email" v:"required|email#请输入邮箱|邮箱格式错误"`
|
||||
LinkHost string `json:"link_host" v:"required#请输入所用(主机)服务商"`
|
||||
LinkName string `json:"link_name" v:"required#请输入博客名称"`
|
||||
LinkAddress string `json:"link_address" v:"required|regex:[a-zA-z]+://[^\\s]*#请输入博客地址|网址不正确(例:https://blog.x-lf.com/)"`
|
||||
LinkDesc string `json:"link_desc" v:"required#请输入博客描述"`
|
||||
LinkAvatar string `json:"link_avatar" v:"required#请输入博客头像"`
|
||||
LinkRssJudge bool `json:"link_rss_judge" v:"required#请输入是否开启Rss"`
|
||||
LinkRss *string `json:"link_rss" v:"required-if:link_rss_judge,true|regex:[a-zA-z]+://[^\\s]*#请输入Rss地址|Rss地址不正确(例:https://blog.x-lf.com/atom.xml)"`
|
||||
LinkUserLocation uint `json:"link_user_location" v:"required#请输入期望板块"`
|
||||
LinkUserColor uint `json:"link_user_color" v:"required#请输入期望颜色"`
|
||||
LinkRemark *string `json:"link_remark"`
|
||||
LinkAccept bool `json:"link_accept" v:"required#请同意协议"`
|
||||
}
|
18
internal/service/LinkService/linkService.go
Normal file
18
internal/service/LinkService/linkService.go
Normal file
@ -0,0 +1,18 @@
|
||||
package LinkService
|
||||
|
||||
import (
|
||||
"PersonalMain/internal/logic/LinkServiceImpl"
|
||||
"PersonalMain/internal/model/do"
|
||||
"PersonalMain/internal/model/entity"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
)
|
||||
|
||||
func NewLinkService() LinkService {
|
||||
return &LinkServiceImpl.DefaultLinkImpl{}
|
||||
}
|
||||
|
||||
type LinkService interface {
|
||||
GetLinkFriend(*ghttp.Request)
|
||||
GetSortAndLink(*ghttp.Request)
|
||||
AddLinkFriendCustom(*ghttp.Request, entity.LinkAddFriendVO) (bool, *do.BlogDO, string)
|
||||
}
|
@ -2,6 +2,7 @@ package UserService
|
||||
|
||||
import (
|
||||
"PersonalMain/internal/logic/UserServiceImpl"
|
||||
"PersonalMain/internal/model/do"
|
||||
"PersonalMain/internal/model/entity"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
)
|
||||
@ -15,4 +16,5 @@ type UserService interface {
|
||||
UserLogin(*ghttp.Request, *entity.UserLoginVO)
|
||||
CheckLogin(*ghttp.Request)
|
||||
UserLogout(*ghttp.Request)
|
||||
UserCurrent(*ghttp.Request) *do.UserDO
|
||||
}
|
||||
|
@ -25,6 +25,10 @@ var (
|
||||
RequestBodyError = ErrorCode{output: "RequestBodyError", code: 40201, message: "请求体错误"}
|
||||
UserExist = ErrorCode{output: "UserExists", code: 40300, message: "用户已存在"}
|
||||
UserNotExist = ErrorCode{output: "UserNotExist", code: 40301, message: "用户不存在"}
|
||||
FriendLinkAlreadyExists = ErrorCode{output: "FriendLinkAlreadyExists", code: 40302, message: "友链已存在"}
|
||||
LinkAddressError = ErrorCode{output: "LinkAddressError", code: 40303, message: "链接地址错误"}
|
||||
LikeLinkAlreadyExists = ErrorCode{output: "LikeLinkAlreadyExists", code: 40304, message: "链接已存在"}
|
||||
LinkAcceptDenied = ErrorCode{output: "LinkAcceptDenied", code: 40305, message: "链接接受协议拒绝"}
|
||||
ServerUnknownError = ErrorCode{output: "ServerUnknownError", code: 50000, message: "服务器未知错误"}
|
||||
ServerDatabaseInteriorError = ErrorCode{output: "ServerDatabaseInteriorError", code: 50001, message: "服务器数据库内部错误"}
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user