Skip to content

Commit

Permalink
refactor(consts): Use repo mode and add method
Browse files Browse the repository at this point in the history
  • Loading branch information
Northes committed Dec 20, 2022
1 parent 9f9a969 commit 0141785
Show file tree
Hide file tree
Showing 16 changed files with 314 additions and 165 deletions.
4 changes: 2 additions & 2 deletions controller/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ func AvatarHandler(c *gin.Context) {

// 按格式返回
switch req.GetOutput() {
case consts.JSON:
case consts.RepoOutput.JSON:
b := FileToBase64(filePath)
response.Success(c).Data(gin.H{"avatar": b}).JSON()
return
case consts.Base64:
case consts.RepoOutput.Base64:
b := FileToBase64(filePath)
c.String(http.StatusOK, b)
return
Expand Down
6 changes: 3 additions & 3 deletions controller/ip_bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func IPHandler(c *gin.Context) {

strOutput := c.Query("output")
if len(strOutput) == 0 {
strOutput = c.DefaultQuery("o", consts.JSON.String())
strOutput = c.DefaultQuery("o", consts.RepoOutput.JSON.String())
}

logger.L().Debug("request", zap.String("ip", strIP), zap.String("output", strOutput))
Expand All @@ -41,8 +41,8 @@ func IPHandler(c *gin.Context) {
info.CacheTime = info.UpdatedAt

// 按格式返回
switch consts.ToOutputType(strOutput) {
case consts.Text:
switch true {
case consts.RepoOutput.Is(strOutput, consts.RepoOutput.Text):
response.Success(c).Data(info.String()).String()
default:
response.Success(c).Data(info).JSON()
Expand Down
4 changes: 2 additions & 2 deletions controller/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func GetHandler(c *gin.Context) {

output := c.Param("output")

switch output {
case consts.CaseOutputType(output, consts.Text):
switch true {
case consts.RepoOutput.Is(output, consts.RepoOutput.Text):
str := protocol.ParamsToString(c.Request)
response.Success(c).Data(str).String()
default:
Expand Down
16 changes: 8 additions & 8 deletions logic/avatar/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewAvatar(req *models.AvatarReq) (string, error) {
identicon.SetBackgroundColorFunction(func(cb []byte, fc color.Color) color.Color {
if len(req.GetBackgroundColor()) != 0 {
// 透明
if req.GetBackgroundColor() == consts.ColorTransparent.String() {
if consts.RepoColor.Is(req.GetBackgroundColor(), consts.RepoColor.Transparent) {
return color.Transparent
}
// 自定义背景颜色
Expand Down Expand Up @@ -71,9 +71,9 @@ func NewAvatar(req *models.AvatarReq) (string, error) {
}

switch req.GetOutput() {
case consts.JPG, consts.JPEG:
case consts.RepoOutput.JPG, consts.RepoOutput.JPEG:
err = ident.Jpeg(req.GetSize(), req.GetQuality(), f)
case consts.SVG:
case consts.RepoOutput.SVG:
err = ident.Svg(req.GetSize(), f)
default:
err = ident.Png(req.GetSize(), f)
Expand All @@ -88,12 +88,12 @@ func NewAvatar(req *models.AvatarReq) (string, error) {
func getFilePath(req *models.AvatarReq) string {
ext := req.GetOutput().String()
switch req.GetOutput() {
case consts.JPG, consts.JPEG:
ext = consts.JPEG.String()
case consts.SVG:
ext = consts.SVG.String()
case consts.RepoOutput.JPG, consts.RepoOutput.JPEG:
ext = consts.RepoOutput.JPEG.String()
case consts.RepoOutput.SVG:
ext = consts.RepoOutput.SVG.String()
default:
ext = consts.PNG.String()
ext = consts.RepoOutput.PNG.String()
}

return path.Join(config.Share.File.Avatar, fmt.Sprintf(
Expand Down
2 changes: 1 addition & 1 deletion logic/greet/greet.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func GetGreet(s string) (*models.Greet, error) {
query = s
if len(query) == 0 {
q := strings.Builder{}
q.WriteString(consts.GetTimeCode().String())
q.WriteString(consts.RepoTime.Now().String())
query = q.String()
}

Expand Down
2 changes: 1 addition & 1 deletion logic/ip_bank/gaode.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,5 @@ func (g *gaode) GetIP(ip net.IP) (*models.IPBank, error) {
}

func (g *gaode) Platform() consts.PlatformCode {
return consts.Gaode
return consts.RepoPlatform.Gaode
}
2 changes: 1 addition & 1 deletion logic/ip_bank/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ func (l *local) GetIP(ip net.IP) (*models.IPBank, error) {
}

func (l *local) Platform() consts.PlatformCode {
return consts.Local
return consts.RepoPlatform.Local
}
2 changes: 1 addition & 1 deletion logic/ip_bank/tencent.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,5 @@ func (t *tencent) GetIP(ip net.IP) (*models.IPBank, error) {
}

func (t *tencent) Platform() consts.PlatformCode {
return consts.Tencent
return consts.RepoPlatform.Tencent
}
32 changes: 16 additions & 16 deletions models/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import (
// Gravatar: s,d,f,r

type AvatarReq struct {
Hash string // 哈希(不受Query参数值控制)
Block int `form:"block"` // 块数量
Size int `form:"size"` // Gravatar,图像大小
S int `form:"s"` // Gravatar,图像大小-缩写
UDefault string `form:"default"` // Gravatar,默认图片
D string `form:"d"` // Gravatar,默认图片-缩写
Random bool // 随机(不受参数值控制,只要存在random参数,就视为随机模式)
Density int `form:"density"` // 密度
Namespace string `form:"namespace"` // 命名空间
N string `form:"namespace"` // 命名空间-缩写
Output consts.OutputType `form:"output"` // 输出格式
O consts.OutputType `form:"O"` // 输出格式-简写
Quality int `form:"quality"` // 图像质量(仅jpg,jpeg可用)
Hash string // 哈希(不受Query参数值控制)
Block int `form:"block"` // 块数量
Size int `form:"size"` // Gravatar,图像大小
S int `form:"s"` // Gravatar,图像大小-缩写
UDefault string `form:"default"` // Gravatar,默认图片
D string `form:"d"` // Gravatar,默认图片-缩写
Random bool // 随机(不受参数值控制,只要存在random参数,就视为随机模式)
Density int `form:"density"` // 密度
Namespace string `form:"namespace"` // 命名空间
N string `form:"namespace"` // 命名空间-缩写
Output string `form:"output"` // 输出格式
O string `form:"O"` // 输出格式-简写
Quality int `form:"quality"` // 图像质量(仅jpg,jpeg可用)
// Pixels int `form:"pixels"` // 图像像素(图片大小)
BackgroundColor string `form:"backgroundcolor"` // 背景颜色
BGColor string `form:"bgcolor"` // 背景颜色-缩写
Expand Down Expand Up @@ -80,12 +80,12 @@ func (a *AvatarReq) GetNamespace() string {

func (a *AvatarReq) GetOutput() consts.OutputType {
if len(a.Output) != 0 {
return a.Output
return consts.RepoOutput.ToOutputType(a.Output)
}
if len(a.O) != 0 {
return a.O
return consts.RepoOutput.ToOutputType(a.Output)
}
return "png"
return consts.RepoOutput.PNG
}

func (a *AvatarReq) GetQuality() int {
Expand Down
22 changes: 20 additions & 2 deletions utils/consts/color.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
package consts

import "strings"

type ColorCode string

var RepoColor *color

type color struct {
Transparent ColorCode
}

const (
ColorTransparent ColorCode = "transparent"
colorTransparent ColorCode = "transparent"
)

var colorStringMap = map[ColorCode]string{
ColorTransparent: "transparent",
colorTransparent: "transparent",
}

func init() {
RepoColor = &color{
Transparent: colorTransparent,
}
}

func (c ColorCode) String() string {
return colorStringMap[c]
}

func (c *color) Is(str string, colorCode ColorCode) bool {
return strings.ToLower(str) == colorCode.String()
}
78 changes: 52 additions & 26 deletions utils/consts/day.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,80 @@ import (
"time"
)

// Day:星期一 星期二 星期四 星期五 星期六 星期天

type DayCode int

// Day:星期一 星期二 星期四 星期五 星期六 星期天
var RepoDay *day

type day struct {
Default DayCode
Monday DayCode
Tuesday DayCode
Wednesday DayCode
Thursday DayCode
Friday DayCode
Saturday DayCode
Sunday DayCode
}

const (
DayDefault DayCode = iota
DayMonday
DayTuesday
DayWednesday
DayThursday
DayFriday
DaySaturday
DaySunday
dayDefault DayCode = iota
dayMonday
dayTuesday
dayWednesday
dayThursday
dayFriday
daySaturday
daySunday
)

var dayMap = map[DayCode]string{
DayDefault: DefaultCode.CN(),
DayMonday: "星期一",
DayTuesday: "星期二",
DayWednesday: "星期三",
DayThursday: "星期四",
DayFriday: "星期五",
DaySaturday: "星期六",
DaySunday: "星期天",
dayDefault: DefaultCode.CN(),
dayMonday: "星期一",
dayTuesday: "星期二",
dayWednesday: "星期三",
dayThursday: "星期四",
dayFriday: "星期五",
daySaturday: "星期六",
daySunday: "星期天",
}

func init() {
RepoDay = &day{
Default: dayDefault,
Monday: dayMonday,
Tuesday: dayTuesday,
Wednesday: dayWednesday,
Thursday: dayThursday,
Friday: dayFriday,
Saturday: daySaturday,
Sunday: daySunday,
}
}

// 换取星期几文字
func (d DayCode) String() string {
return dayMap[d]
}

func GetDayCode() DayCode {
func (*day) Today() DayCode {
switch time.Now().Weekday() {
case time.Sunday:
return DaySunday
return daySunday
case time.Monday:
return DayMonday
return dayMonday
case time.Tuesday:
return DayTuesday
return dayTuesday
case time.Wednesday:
return DayWednesday
return dayWednesday
case time.Thursday:
return DayThursday
return dayThursday
case time.Friday:
return DayFriday
return dayFriday
case time.Saturday:
return DaySaturday
return daySaturday
default:
return DayDefault
return dayDefault
}
}
Loading

0 comments on commit 0141785

Please sign in to comment.