项目07_封装上传图片方 法 以及轮播图的增删改查

base.go

func (c *BaseController) UploadImg(picName string) (string, error) {
	//1、获取上传的文件
	f, h, err := c.GetFile(picName)
	if err != nil {
		return "", err
	}
	//2、关闭文件流
	defer f.Close()
	//3、获取后缀名 判断类型是否正确  .jpg .png .gif .jpeg
	extName := path.Ext(h.Filename)

	allowExtMap := map[string]bool{
		".jpg":  true,
		".png":  true,
		".gif":  true,
		".jpeg": true,
	}

	if _, ok := allowExtMap[extName]; !ok {

		return "", errors.New("图片后缀名不合法")
	}
	//4、创建图片保存目录  static/upload/20200623
	//day := models.GetDay()
	day := models.GetDay()
	dir := "static/upload/" + day

	if err := os.MkdirAll(dir, 0666); err != nil {
		return "", err
	}
	//5、生成文件名称   144325235235.png
	fileUnixName := strconv.FormatInt(models.GetUnix(), 10)
	//static/upload/20200623/144325235235.png
	saveDir := path.Join(dir, fileUnixName+extName)
	beego.Info("执行上传图片",saveDir)
	//6、保存图片
	c.SaveToFile(picName, saveDir)

	return saveDir, nil
}

轮播图

focus.go

package models

import (
	_ "github.com/jinzhu/gorm"
)
/*
 轮播图结构体
*/
type Focus struct {
	Id          int
	Title       string  // 标题
	FocusType   int    //
	FocusImg    string
	Link        string  // 跳转地址
    Sort        int
	Status      int
	AddTime     int
}

func (Focus) TableName() string  {
	return "focus"

}

controller

package admin

import (
	"github.com/astaxie/beego"
	"strconv"
	"xiaomi/models"
)
/*轮播图控制器*/

type FocusController struct {
	BaseController

}
// 轮播图数据
func (c *FocusController) Get(){
	focus := []models.Focus{}
	models.DB.Find(&focus)
	//c.Data["json"] = focus
	//c.ServeJSON()
	c.Data["focusList"] = focus
	c.TplName = "admin/focus/index.html"
}
func (c *FocusController) Add() {
	c.TplName = "admin/focus/add.html"
}
// 增加
func (c *FocusController) DoAdd() {

	focusType, err1 := c.GetInt("focus_type")
	title := c.GetString("title")
	link := c.GetString("link")
	sort, err2 := c.GetInt("sort")
	status, err3 := c.GetInt("status")

	if err1 != nil || err3 != nil {
		c.Error("非法请求", "/focus/add")
	}
	if err2 != nil {
		c.Error("排序表单里面输入的数据不合法", "/focus/add")
	}
	//执行图片上传
	focusImgSrc, _ := c.UploadImg("focus_img")
	beego.Info("执行图片上传",focusImgSrc)

	focus := models.Focus{
		Title:     title,
		FocusType: focusType,
		FocusImg:  focusImgSrc,
		Link:      link,
		Sort:      sort,
		Status:    status,
		AddTime:   int(models.GetUnix()),
	}
	models.DB.Create(&focus)
	c.Success("增加轮播图成功", "/focus")
}

func (c *FocusController) Edit() {

	id, err := c.GetInt("id")
	if err != nil {
		c.Error("非法请求", "/focus")
		return
	}
	focus := models.Focus{Id: id}
	models.DB.Find(&focus)
	c.Data["focus"] = focus

	c.TplName = "admin/focus/edit.html"
}
// 修改
func (c *FocusController) DoEdit() {
	id, err1 := c.GetInt("id")
	focusType, err2 := c.GetInt("focus_type")
	title := c.GetString("title")
	link := c.GetString("link")
	sort, err3 := c.GetInt("sort")
	status, err4 := c.GetInt("status")

	if err1 != nil || err2 != nil || err4 != nil {
		c.Error("非法请求", "/focus")
	}
	if err3 != nil {
		c.Error("排序表单里面输入的数据不合法", "/focus/edit?id="+strconv.Itoa(id))
	}
	//执行图片上传
	focusImgSrc, _ := c.UploadImg("focus_img")

	focus := models.Focus{Id: id}
	models.DB.Find(&focus)
	focus.Title = title
	focus.FocusType = focusType
	focus.Link = link
	focus.Sort = sort
	focus.Status = status
	if focusImgSrc != "" {
		focus.FocusImg = focusImgSrc
	}
	err := models.DB.Save(&focus).Error
	if err != nil {
		c.Error("修改数据失败", "/focus/edit?id="+strconv.Itoa(id))
		return
	}
	c.Success("修改数据成功", "/focus")
}
// 删除
func (c *FocusController) Delete() {

	id, err1 := c.GetInt("id")
	if err1 != nil {
		c.Error("传入参数错误", "/focus")
		return
	}
	focus := models.Focus{Id: id}
	models.DB.Delete(&focus)
	c.Success("删除管理员成功", "/focus")
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值