当前位置: 首页> 文旅> 美景 > 网络规划设计师培训视频教程_邢台市教育局官网_网络营销常见的工具_如何网上免费打广告

网络规划设计师培训视频教程_邢台市教育局官网_网络营销常见的工具_如何网上免费打广告

时间:2025/7/11 17:36:36来源:https://blog.csdn.net/hzether/article/details/143464342 浏览次数:0次
网络规划设计师培训视频教程_邢台市教育局官网_网络营销常见的工具_如何网上免费打广告

GoFrame Web广告模块开发教程

1. API接口定义

首先在 api/front/webad.go 中定义请求和响应结构:

package fronttype WebAdReq struct {Id       uint   `json:"id"`Title    string `json:"title"`    // 广告标题Image    string `json:"image"`    // 广告图片Url      string `json:"url"`      // 广告链接Sort     int    `json:"sort"`     // 排序Status   int    `json:"status"`   // 状态:0=禁用,1=启用Position int    `json:"position"` // 位置:1=首页轮播Page     int    `json:"page"`     // 当前页码Size     int    `json:"size"`     // 每页数量
}type WebAdRes struct {Id        uint   `json:"id"`Title     string `json:"title"`    // 广告标题Image     string `json:"image"`    // 广告图片Url       string `json:"url"`      // 广告链接Sort      int    `json:"sort"`     // 排序Status    int    `json:"status"`   // 状态Position  int    `json:"position"` // 位置CreatedAt string `json:"created_at"`
}type WebAdListRes struct {Total int64      `json:"total"` // 总数List  []WebAdRes `json:"list"`  // 列表
}

2. 业务逻辑实现

internal/logic/webad/webad.go 中实现业务逻辑:

package webadimport ("context""web/api/front""web/internal/service""github.com/gogf/gf/v2/frame/g"
)type sWebAd struct{}func New() *sWebAd {return &sWebAd{}
}func init() {service.RegisterWebAd(New())
}// Create 创建广告
func (s *sWebAd) Create(ctx context.Context, in *front.WebAdReq) (err error) {_, err = g.Model("web_ad").Data(in).Insert()return
}// Delete 删除广告
func (s *sWebAd) Delete(ctx context.Context, id uint) error {_, err := g.Model("web_ad").Where("id", id).Delete()return err
}// Update 更新广告
func (s *sWebAd) Update(ctx context.Context, in *front.WebAdReq) error {_, err := g.Model("web_ad").Data(in).Where("id", in.Id).Update()return err
}// GetList 获取广告列表
func (s *sWebAd) GetList(ctx context.Context, in *front.WebAdReq) (out *front.WebAdListRes, err error) {out = &front.WebAdListRes{}m := g.Model("web_ad")// 条件查询if in.Title != "" {m = m.WhereLike("title", "%"+in.Title+"%")}if in.Status > 0 {m = m.Where("status", in.Status)}if in.Position > 0 {m = m.Where("position", in.Position)}// 获取总数out.Total, err = m.Count()if err != nil {return out, err}// 分页查询err = m.Page(in.Page, in.Size).OrderDesc("sort").OrderDesc("id").Scan(&out.List)return
}// GetById 根据ID获取广告信息
func (s *sWebAd) GetById(ctx context.Context, id uint) (out *front.WebAdRes, err error) {out = &front.WebAdRes{}err = g.Model("web_ad").Where("id", id).Scan(out)return
}

3. 生成Service接口

运行命令生成service接口:

gf gen service

这将在 internal/service/webad.go 中生成接口定义:

package serviceimport ("context""web/api/front"
)type IWebAd interface {Create(ctx context.Context, in *front.WebAdReq) (err error)Delete(ctx context.Context, id uint) errorUpdate(ctx context.Context, in *front.WebAdReq) errorGetList(ctx context.Context, in *front.WebAdReq) (out *front.WebAdListRes, err error)GetById(ctx context.Context, id uint) (out *front.WebAdRes, err error)
}var localWebAd IWebAdfunc WebAd() IWebAd {if localWebAd == nil {panic("implement not found for interface IWebAd, forgot register?")}return localWebAd
}func RegisterWebAd(i IWebAd) {localWebAd = i
}

4. 控制器实现

internal/controller/webad/webad.go 中编写控制器:

package webadimport ("context""web/api/front""web/internal/service"
)type Controller struct{}func New() *Controller {return &Controller{}
}// Create 创建广告
func (c *Controller) Create(ctx context.Context, req *front.WebAdReq) (res *front.WebAdRes, err error) {err = service.WebAd().Create(ctx, req)return
}// Delete 删除广告
func (c *Controller) Delete(ctx context.Context, req *front.WebAdReq) (res *front.WebAdRes, err error) {err = service.WebAd().Delete(ctx, req.Id)return
}// Update 更新广告
func (c *Controller) Update(ctx context.Context, req *front.WebAdReq) (res *front.WebAdRes, err error) {err = service.WebAd().Update(ctx, req)return
}// GetList 获取广告列表
func (c *Controller) GetList(ctx context.Context, req *front.WebAdReq) (res *front.WebAdListRes, err error) {return service.WebAd().GetList(ctx, req)
}// GetById 获取广告详情
func (c *Controller) GetById(ctx context.Context, req *front.WebAdReq) (res *front.WebAdRes, err error) {return service.WebAd().GetById(ctx, req.Id)
}

5. 路由注册

internal/cmd/apiserver/router.go 中注册路由:

package apiserverimport ("github.com/gogf/gf/v2/net/ghttp""web/internal/controller/webad"
)func BindRouter(s *ghttp.Server) {// 广告管理s.Group("/api/webad", func(group *ghttp.RouterGroup) {group.Middleware(ghttp.MiddlewareHandlerResponse)controller := webad.New()group.Bind(controller.Create,controller.Delete,controller.Update,controller.GetList,controller.GetById,)})
}

6. 数据库表结构

创建 web_ad 表:

CREATE TABLE `web_ad` (`id` int unsigned NOT NULL AUTO_INCREMENT,`title` varchar(100) NOT NULL COMMENT '广告标题',`image` varchar(255) NOT NULL COMMENT '广告图片',`url` varchar(255) NOT NULL COMMENT '广告链接',`sort` int NOT NULL DEFAULT '0' COMMENT '排序',`status` tinyint NOT NULL DEFAULT '1' COMMENT '状态:0=禁用,1=启用',`position` tinyint NOT NULL COMMENT '位置:1=首页轮播',`created_at` datetime DEFAULT CURRENT_TIMESTAMP,`updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='广告表';

7. API接口说明

模块提供以下接口:

接口路径请求方式说明
/api/webad/createPOST创建广告
/api/webad/deletePOST删除广告
/api/webad/updatePOST更新广告
/api/webad/get-listGET获取广告列表
/api/webad/get-by-idGET获取广告详情

至此,一个完整的广告管理模块就开发完成了。该模块实现了基本的CRUD功能,包括创建、删除、更新、列表查询和详情查询等功能。

关键字:网络规划设计师培训视频教程_邢台市教育局官网_网络营销常见的工具_如何网上免费打广告

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: