You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
217 lines
4.5 KiB
217 lines
4.5 KiB
|
2 months ago
|
package common
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"io"
|
||
|
|
"mime"
|
||
|
|
"net/http"
|
||
|
|
"os"
|
||
|
|
"server/call"
|
||
|
|
"server/config"
|
||
|
|
"server/modules/customer/app"
|
||
|
|
"server/modules/customer/bdb"
|
||
|
|
"server/modules/customer/values"
|
||
|
|
"server/util"
|
||
|
|
"strings"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
"github.com/liangdas/mqant/log"
|
||
|
|
)
|
||
|
|
|
||
|
|
// 上传图片 并返回图片地址
|
||
|
|
func UploadImage(c *gin.Context) {
|
||
|
|
a := app.NewApp(c)
|
||
|
|
defer func() {
|
||
|
|
a.Response()
|
||
|
|
}()
|
||
|
|
|
||
|
|
// 解析请求中的 multipart/form-data
|
||
|
|
err := c.Request.ParseMultipartForm(10 << 20) // 限制上传文件的大小为 10MB
|
||
|
|
if err != nil {
|
||
|
|
log.Error("图片超过10Mb, err:%v", err.Error())
|
||
|
|
a.Code = values.CodeParam
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
// 打开上传的文件
|
||
|
|
file, header, err := c.Request.FormFile("file")
|
||
|
|
if err != nil {
|
||
|
|
log.Error("打开上传图片失败, err:%v", err.Error())
|
||
|
|
a.Code = values.CodeParam
|
||
|
|
return
|
||
|
|
}
|
||
|
|
defer file.Close()
|
||
|
|
|
||
|
|
// 获取上传文件的 MIME 类型
|
||
|
|
contentType := header.Header.Get("Content-Type")
|
||
|
|
|
||
|
|
// 根据 MIME 类型获取文件扩展名
|
||
|
|
ext, err := mime.ExtensionsByType(contentType)
|
||
|
|
if err != nil || len(ext) == 0 {
|
||
|
|
log.Error("获取上传图片格式失败, err:%v", err.Error())
|
||
|
|
a.Code = values.CodeParam
|
||
|
|
return
|
||
|
|
}
|
||
|
|
imageName := call.SnowNode().Generate().String() + ext[0]
|
||
|
|
|
||
|
|
// 读取上传的文件内容
|
||
|
|
data, err := io.ReadAll(file)
|
||
|
|
if err != nil {
|
||
|
|
log.Error("读取图片内容失败, err:%v", err.Error())
|
||
|
|
a.Code = values.CodeRetry
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
// 获取当前本地时间
|
||
|
|
now := time.Now()
|
||
|
|
zeroTime := now.Format("20060102")
|
||
|
|
rounded := now.Hour()
|
||
|
|
dir := fmt.Sprintf("%v/%v/%v", values.ImagePath, zeroTime, rounded)
|
||
|
|
|
||
|
|
// 创建保存文件的目标文件
|
||
|
|
err = createDirIfNotExist(dir)
|
||
|
|
if err != nil {
|
||
|
|
log.Error("创建图片保存目录, err:%v", err.Error())
|
||
|
|
a.Code = values.CodeParam
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
// 将上传的文件复制到目标文件
|
||
|
|
err = os.WriteFile(dir+"/"+imageName, data, 0644) // 写入文件
|
||
|
|
if err != nil {
|
||
|
|
a.Code = values.CodeRetry
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
name := strings.Replace(dir+"/"+imageName, "/", ",", -1)
|
||
|
|
a.Data = config.GetConfig().Customer.ImageURL + name
|
||
|
|
}
|
||
|
|
|
||
|
|
// 检测文件目录是否存在
|
||
|
|
func createDirIfNotExist(path string) error {
|
||
|
|
// 检查目录是否存在
|
||
|
|
_, err := os.Stat(path)
|
||
|
|
if err == nil {
|
||
|
|
// 目录已存在,直接返回
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// 目录不存在,创建目录
|
||
|
|
if os.IsNotExist(err) {
|
||
|
|
err = os.MkdirAll(path, 0755)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// 返回图片
|
||
|
|
func DownImage(c *gin.Context) {
|
||
|
|
a := app.NewApp(c)
|
||
|
|
defer func() {
|
||
|
|
a.Response()
|
||
|
|
}()
|
||
|
|
//req := &values.DownLoadImageReq{}
|
||
|
|
//if !a.S(req) {
|
||
|
|
// return
|
||
|
|
//}
|
||
|
|
|
||
|
|
name := c.Query("path")
|
||
|
|
path := strings.Replace(name, ",", "/", -1)
|
||
|
|
|
||
|
|
// 打开要返回的图片文件
|
||
|
|
file, err := os.Open(path)
|
||
|
|
if err != nil {
|
||
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
defer file.Close()
|
||
|
|
|
||
|
|
// 读取图片文件的内容
|
||
|
|
data, err := io.ReadAll(file)
|
||
|
|
if err != nil {
|
||
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
arr := strings.Split(path, ".")
|
||
|
|
|
||
|
|
// 返回图片
|
||
|
|
c.Data(http.StatusOK, "image/"+arr[len(arr)-1], data)
|
||
|
|
}
|
||
|
|
|
||
|
|
// OptList 操作日志
|
||
|
|
func OptList(c *gin.Context) {
|
||
|
|
a := app.NewApp(c)
|
||
|
|
defer func() {
|
||
|
|
a.Response()
|
||
|
|
}()
|
||
|
|
req := new(values.EditHistoryListReq)
|
||
|
|
if !a.S(req) {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
resp := values.EditHistoryListResp{}
|
||
|
|
if err := bdb.BackDB.C().Order("time Desc").Limit(int(req.Num)).Offset(int((req.Page - 1) * req.Num)).Find(&resp.List).Error; err != nil {
|
||
|
|
log.Error("err:%v", err)
|
||
|
|
}
|
||
|
|
resp.Count = bdb.BackDB.Count(&values.EditHistory{}, "")
|
||
|
|
a.Data = resp
|
||
|
|
}
|
||
|
|
|
||
|
|
func GoodList(c *gin.Context) {
|
||
|
|
a := app.NewApp(c)
|
||
|
|
defer func() {
|
||
|
|
a.Response()
|
||
|
|
}()
|
||
|
|
goods := call.GetGoodsConfig()
|
||
|
|
resp := &values.GoodListResp{List: goods}
|
||
|
|
a.Data = resp
|
||
|
|
}
|
||
|
|
|
||
|
|
func ProductList(c *gin.Context) {
|
||
|
|
a := app.NewApp(c)
|
||
|
|
defer func() {
|
||
|
|
a.Response()
|
||
|
|
}()
|
||
|
|
products := call.GetConfigPayProduct()
|
||
|
|
resp := &values.ProductListResp{List: products}
|
||
|
|
a.Data = resp
|
||
|
|
}
|
||
|
|
|
||
|
|
func ChannelList(c *gin.Context) {
|
||
|
|
a := app.NewApp(c)
|
||
|
|
defer func() {
|
||
|
|
a.Response()
|
||
|
|
}()
|
||
|
|
ret := call.GetChannelListReal()
|
||
|
|
if ret == nil {
|
||
|
|
a.Code = values.CodeRetry
|
||
|
|
return
|
||
|
|
}
|
||
|
|
resp := values.ChannelListResp{}
|
||
|
|
for _, v := range ret {
|
||
|
|
if v.Show != 2 {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
if len(a.User.SChannels) > 0 {
|
||
|
|
if !util.SliceContain(a.User.SChannels, v.ChannelID) {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
}
|
||
|
|
resp.List = append(resp.List, v)
|
||
|
|
}
|
||
|
|
a.Data = resp
|
||
|
|
}
|
||
|
|
|
||
|
|
func UserInfo(c *gin.Context) {
|
||
|
|
a := app.NewApp(c)
|
||
|
|
defer func() {
|
||
|
|
a.Response()
|
||
|
|
}()
|
||
|
|
resp := values.UserInfoResp{Name: a.User.Name, Role: a.User.Role, Power: a.User.Power}
|
||
|
|
a.Data = resp
|
||
|
|
}
|