package controller
import (
"regexp"
"strconv"
"strings"
"time"
)
//获取int型时间戳
func GetIntTimestamp() int {
strInt64 := time.Now().Unix()
id16,_ := strconv.Atoi(strconv.FormatInt(strInt64,10))
return id16
}
//获取string型时间戳
func GetSringTimestamp() string {
strInt64 := time.Now().Unix()
id16:= strconv.FormatInt(strInt64,10)
return id16
}
//获取Y-m-d H:i:s格式的日期
func GetCurrentDate() string {
currentTiem := time.Now()
currentData := currentTiem.Format("2006-01-02 15:04:05")
return currentData
}
//过滤敏感词
func IsWordShield(word string) bool {
wordList := []string{
"测试",
}
for i:=0;i<len(wordList);i++{
if(strings.Index(word,wordList[i]) !=-1){
return false
}
}
return true
}
//验证手机号
func VerfyPhone(phone string) bool {
regStr := `^1[3456789]\d{9}$`
if match, _ := regexp.MatchString(regStr, phone); match {
return true
}
return false
}
//存储错误日志
func LogEorror(e interface{}) {
file := "./" + time.Now().Format("2006-01-02") + "_log" + ".txt"
logFile, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
if err != nil {
log.Fatal(err)
}
defer logFile.Close()
loger = log.New(logFile, "ERROR:", log.Ltime|log.Lshortfile) // 将文件设置为loger作为输出
loger.Print(e)
}
//postJson请求
func httpPostJson(url string, json string, header map[string]string) {
jsonStr := []byte(json)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
if err != nil {
fmt.Println(err)
}
for k, v := range header {
req.Header.Set(k, v)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
// handle error
fmt.Println(err)
return
}
defer resp.Body.Close()
statuscode := resp.StatusCode
hea := resp.Header
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
fmt.Println(statuscode)
fmt.Println(hea)
}
//md5
func stringToMd5(aa string){
//将key的md5作为id
h := md5.New()
h.Write([]byte(aa))
md5String = hex.EncodeToString(h.Sum(nil))
fmt.Println(md5String )
}
//加载yaml配置文件
func loadYaml() (yamlConfig Config) {
config := Config{}
yamlS, _ := ioutil.ReadFile("./config.yaml")
err := yaml.Unmarshal(yamlS, &config)
if err != nil {
fmt.Println(err)
}
fmt.Println(config)
return config
}
自己写的golang常用函数,正则验证,过滤词,时间戳
最新推荐文章于 2023-07-08 23:51:48 发布