/**
* 文件上传组件
* @author
* @since 2021/7/23
* @File : upload
*/
package widget
import (
"fmt"
"gitlab.52pay.top/go/easygoadmin/utils/gconv"
"html/template"
"strings"
)
// 上传图片
func UploadImage(param string, url interface{}, exts string, size int) template.HTML {
// 参数拆解
item := strings.Split(param, "|")
// 组件名称
name := item[0]
// 组件标题
title := "图片"
if item[1] != "" {
title = item[1]
}
// 组件显示尺寸
sizeStr := "90x90"
if item[2] != "" {
sizeStr = item[2]
}
sizeArr := strings.Split(sizeStr, "x")
// 组件提示语
showTips := item[3]
// 裁剪尺寸
cropSize := ""
isCrop := 2
if len(item) >= 5 && item[4] != "" {
cropSize = item[4]
isCrop = 1
} else {
cropSize = "300x300"
isCrop = 2
}
// 计算长宽比例
cropArr := strings.Split(cropSize, "x")
// 裁剪比例
cropRate := float64(gconv.Int(cropArr[0])) / float64(gconv.Int(cropArr[1]))
cropRateStr := fmt.Sprintf("%2f", cropRate)
// 上传后缀
if exts == "" {
exts = "jpg|png|gif|bmp|jpeg"
}
// 上传大小
if size <= 0 {
size = 10 * 1024
}
html := ``
html += `
`
// 组件JS代码
html += ``
return template.HTML(html)
}
func Album(param string, data interface{}, exts string, size int) template.HTML {
// 参数拆解
item := strings.Split(param, "|")
// 组件名称
albumName := item[0]
//// 组件标题
//albumTitle := "图片"
//if item[1] != "" {
// albumTitle = item[1]
//}
// 组件显示尺寸
sizeStr := "90x90"
if item[2] != "" {
sizeStr = item[2]
}
sizeArr := strings.Split(sizeStr, "x")
// 允许最大上传数
albumNum := item[3]
// 组件提示语
albumTips := item[4]
// 裁剪尺寸
cropSize := ""
isCrop := 2
if len(item) >= 6 && item[5] != "" {
cropSize = item[5]
isCrop = 1
} else {
cropSize = "300x300"
isCrop = 2
}
// 计算长宽比例
cropArr := strings.Split(cropSize, "x")
// 裁剪比例
cropRate := gconv.Int(cropArr[0]) / gconv.Int(cropArr[1])
// 上传后缀
if exts == "" {
exts = "jpg|png|gif|bmp|jpeg"
}
// 上传大小
if size <= 0 {
size = 10 * 1024
}
// HTML拼接
html := ``
// CSS拼接
html += ``
var list []string
switch data.(type) {
case []string:
list = data.([]string)
case string:
if data.(string) != "" {
list = strings.Split(data.(string), ",")
}
}
// 上传控件拼接
for _, v := range list {
html += ``
}
html += ``
// JS拼接
html += ``
return template.HTML(html)
}