一.封装上传图片方法
在models/tools.go文件下封装一个上传图片的方法
//图片上传
func UploadImg(c *gin.Context, picName string) (string, error) {
//1.获取上传文件
file, err := c.FormFile(picName)
//判断上传文件上否存在
if err != nil { //说明上传文件不存在
return "", nil
}
//2.获取后缀名,判断后缀是否正确: .jpg,.png,.gif,.jpeg
extName := path.Ext(file.Filename)
//设置后缀map
allowExtMap := map[string]bool{
".jpg": true,
".png": true,
".gif": true,
".jpeg": true,
}
//判断后缀是否合法
if _, ok := allowExtMap[extName]; !ok {
return "", errors.New("文件后缀名不合法")
}
//3.创建图片保存目录 ./static/upload/20230203
//获取日期
day := GetDay()
//拼接目录
dir := "./static/upload/" + day
//创建目录:MkdirAll 目录不存在,会一次性创建多层
err = os.MkdirAll(dir, 0666)
if err != nil {
return "", err
}
//4.生成文件名称和文件保存目录: models.GetUnixNano() 获取时间戳(int64) 纳秒:防止速度过快而上传图片失败; strconv.FormatInt() 把时间戳(int64)转换成字符串
filename := strconv.FormatInt(GetUnixNano(), 10) + extName
//5.执行上传
dst := path.Join(dir, filename)
//上传文件到指定目录
c.SaveUploadedFile(file, dst)
return dst, nil
}
//获取年月日
func GetDay() string {
template := "20060102"
return time.Now().Format(template)
}
二.轮播图的增删改查
创建轮播图模型
在models目录下创建focus.go轮播图模型
package models
//轮播图
type Focus struct {
Id int
Title string
FocusType int // 1 网站, 2 app, 3 小程序
FocusImg string
Link string
Sort int
Status int
AddTime int
}
func (Focus) TableName() string {
return "focus"
}
2.创建轮播图控制器
在controllers/admin下创建FocusController.go轮播图控制器
package admin
import (
"fmt"
"github.com/gin-gonic/gin"
"goshop/models"
"net/http"
"os"
"strings"
)
type FocusController struct {
BaseController
}
func (con FocusController) Index(c *gin.Context) {
//获取轮播图列表
focusList := []models.Focus{}
models.DB.Find(&focusList)
c.HTML(http.StatusOK, "admin/focus/index.html", gin.H{
"focusList": focusList,
})
}
func (con FocusController) Add(c *gin.Context) {
c.HTML(http.StatusOK, "admin/focus/add.html", gin.H{})
}
func (con FocusController) DoAdd(c *gin.Context) {
//获取请求的表单数据
title := strings.Trim(c.PostForm("title"), " ")
focusType, err1 := models.Int(c.PostForm("focus_type"))
link := strings.Trim(c.PostForm("link"), " ")
sort, err2 := models.Int(c.PostForm("sort"))
status, err3 := models.Int(c.PostForm("status"))
if err1 != nil || err3 != nil {
con.Error(c, "非法请求", "/admin/focus/add")
return
}
if err2 != nil {
con.Error(c, "请输入正确的排序值", "/admin/focus/add")
return
}
//文件上传操作
focusImgSrc, err := models.UploadImg(c, "focus_img")
if err != nil {
con.Error(c, "图片上传失败", "/admin/focus/add")
return
}
//实例化focus模型
focus := models.Focus{
Title: title,
FocusType: focusType,
FocusImg: focusImgSrc,
Link: link,
Sort: sort,
Status: status,
AddTime: int(models.GetUnix()),
}
err = models.DB.Create(&focus).Error
if err != nil {
con.Error(c, "增加轮播图失败", "/admin/focus/add")
return
}
con.Success(c, "增加轮播图成功", "/admin/focus")
}
func (con FocusController) Edit(c *gin.Context) {
//获取要修改的轮播图id
id, err := models.Int(c.Query("id"))
if err != nil {
con.Error(c, "传入数据错误", "/admin/focus")
return
}
focus := models.Focus{Id: id}
models.DB.Find(&focus)
c.HTML(http.StatusOK, "admin/focus/edit.html", gin.H{
"focus": focus,
})
}
func (con FocusController) DoEdit(c *gin.Context) {
//获取请求的表单数据
id, err := models.Int(c.PostForm("id"))
title := strings.Trim(c.PostForm("title"), " ")
focusType, err1 := models.Int(c.PostForm("focus_type"))
link := strings.Trim(c.PostForm("link"), " ")
sort, err2 := models.Int(c.PostForm("sort"))
status, err3 := models.Int(c.PostForm("status"))
if err != nil || err1 != nil || err3 != nil {
con.Error(c, "非法请求", "/admin/focus")
return
}
if err2 != nil {
con.Error(c, "请输入正确的排序值", "/admin/focus/edit?id="+models.String(id))
return
}
//文件上传操作
focusImgSrc, err11 := models.UploadImg(c, "focus_img")
if err11 != nil {
con.Error(c, "图片上传失败", "/admin/focus/edit?id="+models.String(id))
return
}
focus := models.Focus{Id: id}
models.DB.Find(&focus)
focus.Title = title
focus.FocusType = focusType
focus.Status = status
focus.Sort = sort
focus.Link = link
if focusImgSrc != "" {
focus.FocusImg = focusImgSrc
}
err4 := models.DB.Save(&focus).Error
if err4 != nil {
con.Error(c, "修改轮播图失败", "/admin/focus/edit?id="+models.String(id))
return
}
con.Success(c, "修改轮播图成功", "/admin/focus")
}
//删除
func (con FocusController) Delete(c *gin.Context) {
//获取提交的表单数据
id, err := models.Int(c.Query("id"))
if err != nil {
con.Error(c, "传入数据错误", "/admin/focus")
return
}
//查询数据是否存在
focus := models.Focus{Id: id}
models.DB.Find(&focus)
//需要对文件进行删除吗?
if focus.FocusImg != "" {
fmt.Println(focus.FocusImg)
os.Remove(focus.FocusImg)
}
err = models.DB.Delete(&focus).Error
if err != nil {
con.Error(c, "删除数据失败", "/admin/focus")
return
}
con.Success(c, "删除数据成功", "/admin/focus")
}
3.创建轮播图html
在templates/admin/focus下创建轮播图相关html
index.html
{{ define "admin/focus/index.html" }}
{{ template "admin/public/page_header.html" .}}
轮播图名称 |
轮播图类型 |
轮播图图片 |
跳转地址 |
排序 |
状态 |
操作 |
{{range $key,$value := .focusList}}
{{$value.Title}} |
{{if eq $value.FocusType 1}}
网站
{{else if eq $value.FocusType 2}}
APP
{{else}}
小程序
{{end}}
|
{{if ne $value.FocusImg ""}}
{{end}}
|
{{$value.Link}} |
{{$value.Sort}}
|
{{if eq $value.Status 1}}
{{else}}
{{end}}
|
修改
删除
|
{{end}}