This commit is contained in:
makeyangcom
2024-03-06 10:47:22 +08:00
parent c7da010f01
commit 02dd25ec67
23 changed files with 252 additions and 238 deletions

View File

@@ -0,0 +1,11 @@
/**
#*****************************************************************************
# @file empty.go
# @author MakerYang(https://www.makeryang.com)
# @statement 免费课程配套开源项目,任何形式收费均为盗版
#*****************************************************************************
*/
package Utils
type Empty struct{}

View File

@@ -1,3 +0,0 @@
package Utils
type EmptyData struct{}

View File

@@ -1,3 +1,11 @@
/**
#*****************************************************************************
# @file hash.go
# @author MakerYang(https://www.makeryang.com)
# @statement 免费课程配套开源项目任何形式收费均为盗版
#*****************************************************************************
*/
package Utils
import (

View File

@@ -1,3 +1,11 @@
/**
#*****************************************************************************
# @file header.go
# @author MakerYang(https://www.makeryang.com)
# @statement 免费课程配套开源项目,任何形式收费均为盗版
#*****************************************************************************
*/
package Utils
import "strings"
@@ -11,39 +19,3 @@ func CheckUserAgent(userAgent string) bool {
return Status
}
func CheckGame(token string) (int, int, bool) {
Status := false
GameId := 0
GameAccountId := 0
if token != "" {
tokenMap, _ := DecodeId(128, token)
if len(tokenMap) == 2 {
GameId = tokenMap[0]
GameAccountId = tokenMap[1]
Status = true
}
}
if GameId == 0 || GameAccountId == 0 {
Status = false
}
return GameId, GameAccountId, Status
}
func CheckUser(token string) (int, bool) {
Status := false
Uid := 0
if token != "" {
tokenMap, _ := DecodeId(32, token)
if len(tokenMap) == 3 {
Uid = tokenMap[0]
Status = true
}
}
return Uid, Status
}

View File

@@ -0,0 +1,25 @@
/**
#*****************************************************************************
# @file mail.go
# @author MakerYang(https://www.makeryang.com)
# @statement 免费课程配套开源项目,任何形式收费均为盗版
#*****************************************************************************
*/
package Utils
import "gopkg.in/gomail.v2"
func SendMail(to string, subject string, content string) bool {
status := true
mail := gomail.NewMessage()
mail.SetHeader("From", mail.FormatAddress("", ""))
mail.SetHeader("To", to)
mail.SetHeader("Subject", subject)
mail.SetBody("text/html", content)
send := gomail.NewDialer("smtp.qq.com", 587, "", "")
if err := send.DialAndSend(mail); err != nil {
status = false
}
return status
}

View File

@@ -1,17 +0,0 @@
package Utils
import "gopkg.in/gomail.v2"
func SendMail(to string, subject string, content string) bool {
status := true
mail := gomail.NewMessage()
mail.SetHeader("From", mail.FormatAddress("open@wileho.com", "GEEKROS"))
mail.SetHeader("To", to)
mail.SetHeader("Subject", subject)
mail.SetBody("text/html", content)
send := gomail.NewDialer("smtp.qq.com", 587, "open@wileho.com", "")
if err := send.DialAndSend(mail); err != nil {
status = false
}
return status
}

View File

@@ -1,27 +0,0 @@
package Utils
import (
"regexp"
"strings"
)
func FilterMarkdown(input string) string {
quoteBlockRegex := regexp.MustCompile(`^\s*>[ \t]*(.*)$`)
lines := strings.Split(input, "\n")
var quoteLines []string
for _, line := range lines {
if quoteBlockRegex.MatchString(line) {
match := quoteBlockRegex.FindStringSubmatch(line)
quoteLines = append(quoteLines, match[1])
}
}
return strings.Join(quoteLines, "")
}
func FilterSummary(input string, maxLength int) string {
text := strings.TrimSpace(input)
if len(text) <= maxLength {
return text
}
return text[:maxLength]
}

View File

@@ -1,9 +1,10 @@
/**
******************************************************************************
* @file md5.go
* @author MakerYang
******************************************************************************
*/
#*****************************************************************************
# @file md5.go
# @author MakerYang(https://www.makeryang.com)
# @statement 免费课程配套开源项目,任何形式收费均为盗版
#*****************************************************************************
*/
package Utils

View File

@@ -1,3 +1,11 @@
/**
#*****************************************************************************
# @file order.go
# @author MakerYang(https://www.makeryang.com)
# @statement 免费课程配套开源项目,任何形式收费均为盗版
#*****************************************************************************
*/
package Utils
import (

View File

@@ -1,11 +1,14 @@
/**
#*****************************************************************************
# @file phone.go
# @author MakerYang(https://www.makeryang.com)
# @statement 免费课程配套开源项目,任何形式收费均为盗版
#*****************************************************************************
*/
package Utils
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"regexp"
)
@@ -13,57 +16,3 @@ func MobileFormat(str string) string {
re, _ := regexp.Compile("(\\d{3})(\\d{6})(\\d{2})")
return re.ReplaceAllString(str, "$1******$3")
}
func SendMessage(form string, phone string, info string) bool {
status := true
if form == "" || phone == "" || info == "" {
status = false
return status
}
desc := ""
if form == "express" {
desc = "【GEEKROS】Hi" + info + " 你在GEEKROS的订单已经发货请留意快递信息及时查收。"
}
if form == "account" {
desc = "【GEEKROS】你的验证码为" + info + " 有效期10分钟工作人员绝不会索取此验证码切勿告知他人。"
}
apiUrl := "https://smssh1.253.com/msg/v1/send/json"
params := make(map[string]interface{})
params["account"] = ""
params["password"] = ""
params["phone"] = phone
params["msg"] = desc
params["report"] = "false"
bytesData, err := json.Marshal(params)
if err != nil {
status = false
return status
}
reader := bytes.NewReader(bytesData)
request, err := http.NewRequest("POST", apiUrl, reader)
if err != nil {
status = false
return status
}
request.Header.Set("Content-Type", "application/json;charset=UTF-8")
client := http.Client{}
resp, err := client.Do(request)
if err != nil {
status = false
return status
}
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
status = false
return status
}
log.Println("[PhoneMessage]", string(respBytes))
return true
}

View File

@@ -1,3 +1,11 @@
/**
#*****************************************************************************
# @file price.go
# @author MakerYang(https://www.makeryang.com)
# @statement 免费课程配套开源项目,任何形式收费均为盗版
#*****************************************************************************
*/
package Utils
import "fmt"

View File

@@ -1,3 +1,11 @@
/**
#*****************************************************************************
# @file rand.go
# @author MakerYang(https://www.makeryang.com)
# @statement 免费课程配套开源项目,任何形式收费均为盗版
#*****************************************************************************
*/
package Utils
import (

View File

@@ -1,9 +1,19 @@
/**
#*****************************************************************************
# @file return.go
# @author MakerYang(https://www.makeryang.com)
# @statement 免费课程配套开源项目,任何形式收费均为盗版
#*****************************************************************************
*/
package Utils
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"log"
"math"
"net/http"
"os"
"strconv"
@@ -11,95 +21,112 @@ import (
)
type logData struct {
Timestamp int64 `json:"timestamp"`
TimestampFormat string `json:"timestamp_format"`
ClientMethod string `json:"client_method"`
ClientIp string `json:"client_ip"`
ClientParameter string `json:"client_parameter"`
ServerParameter string `json:"server_parameter"`
ServerUrl string `json:"server_url"`
ServerName string `json:"server_name"`
ServerYear string `json:"server_year"`
ServerMonth string `json:"server_month"`
ServerDay string `json:"server_day"`
ServerTime string `json:"server_time"`
TimeLength string `json:"time_length"`
ClientIp string `json:"client_ip"`
ClientMethod string `json:"client_method"`
ClientUrl string `json:"client_url"`
ClientSign string `json:"client_sign"`
ClientToken string `json:"client_token"`
ClientReferer string `json:"client_referer"`
ClientParameter clientParameter `json:"client_parameter"`
ServerParameter string `json:"server_parameter"`
Date string `json:"date"`
Time int64 `json:"time"`
Server string `json:"server"`
Length string `json:"length"`
}
func recordLog(c *gin.Context, serverParameter string) {
type clientParameter struct {
Get interface{} `json:"get"`
Post interface{} `json:"post"`
}
func requestLog(c *gin.Context, serverParameter string) {
data := &logData{}
data.Timestamp = time.Now().Unix()
data.TimestampFormat = time.Now().Format("2006-01-02 15:04:05")
data.Time = time.Now().Unix()
data.Date = time.Now().Format("2006-01-02 15:04:05")
data.ClientMethod = c.Request.Method
data.ClientIp = c.ClientIP()
data.ClientSign = c.Request.Header.Get("Client-Sign")
data.ClientToken = c.Request.Header.Get("Client-Token")
data.ClientReferer = c.Request.Header.Get("Client-Referer")
if data.ClientMethod == "GET" {
data.ClientParameter = c.Request.RequestURI
data.ClientParameter.Get = c.Request.URL.Query()
}
if data.ClientMethod == "POST" {
clientParam, err := json.Marshal(c.Request.PostForm)
if err != nil {
data.ClientParameter = ""
}
if err == nil {
data.ClientParameter = string(clientParam)
}
data.ClientParameter.Post = c.Request.PostForm
}
scheme := "http://"
if c.Request.TLS != nil {
scheme = "https://"
}
serverUrl := scheme + c.Request.Host + c.Request.URL.Path
data.ClientUrl = serverUrl
serverName, _ := os.Hostname()
data.ServerUrl = serverUrl
data.ServerName = serverName
data.ClientParameter = c.GetString("client_parameter")
data.Server = serverName
data.ServerParameter = serverParameter
data.ServerYear = time.Now().Format("2006")
data.ServerMonth = time.Now().Format("01")
data.ServerDay = time.Now().Format("02")
data.ServerTime = time.Now().Format("15:04:05")
data.TimeLength = strconv.FormatFloat(float64(time.Now().UnixNano())/1000000-c.GetFloat64("start_time"), 'f', 2, 64)
clientTimeStr := c.Request.Header.Get("Client-Time")
if clientTimeStr != "" {
clientTimeMs, _ := strconv.ParseInt(clientTimeStr, 10, 64)
serverTimeMs := time.Now().UnixNano() / 1e6
processingTimeMs := serverTimeMs - clientTimeMs
data.Length = fmt.Sprintf("%.3f", math.Abs(float64(processingTimeMs)/1000.0))
}
dataString, _ := json.Marshal(data)
log.Println("[Log]", string(dataString))
log.Println("[request]", string(dataString))
}
func Success(c *gin.Context, data interface{}) {
c.JSON(http.StatusOK, gin.H{
"code": 0,
"msg": "success",
"data": data,
})
logJson, _ := json.Marshal(gin.H{"code": 0, "msg": "success", "data": data})
recordLog(c, string(logJson))
requestLog(c, string(logJson))
}
func Error(c *gin.Context, data interface{}) {
c.JSON(http.StatusOK, gin.H{
"code": 10000,
"msg": "error",
"data": data,
})
logJson, _ := json.Marshal(gin.H{"code": 10000, "msg": "error", "data": data})
recordLog(c, string(logJson))
requestLog(c, string(logJson))
}
func Warning(c *gin.Context, code int, msg string, data interface{}) {
c.JSON(http.StatusOK, gin.H{
"code": code,
"msg": msg,
"data": data,
})
logJson, _ := json.Marshal(gin.H{"code": code, "msg": msg, "data": data})
recordLog(c, string(logJson))
requestLog(c, string(logJson))
}
func AuthError(c *gin.Context, code int, msg string, data interface{}) {
c.JSON(http.StatusUnauthorized, gin.H{
"code": code,
"msg": msg,
"data": data,
})
logJson, _ := json.Marshal(gin.H{"code": code, "msg": msg, "data": data})
recordLog(c, string(logJson))
requestLog(c, string(logJson))
}

View File

@@ -1,3 +1,11 @@
/**
#*****************************************************************************
# @file time.go
# @author MakerYang(https://www.makeryang.com)
# @statement 免费课程配套开源项目,任何形式收费均为盗版
#*****************************************************************************
*/
package Utils
import (