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.

260 lines
4.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package CommonUtil
import (
"bufio"
"bytes"
"dsSso/Utils/IdCardUtil"
"errors"
"log"
"net"
"net/http"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
)
/**
功能从一个数据库的查询返回结果中获取指定字段的IDS
作者:黄海
时间2020-02-14
*/
func Ids(list []map[string]interface{}, key string) []string {
var ids []string
for i := 0; i < len(list); i++ {
switch list[i][key].(type) {
case string:
ids = append(ids, list[i][key].(string))
break
case int64:
ids = append(ids, strconv.FormatInt(list[i][key].(int64), 10))
}
}
return ids
}
/**
功能:判断是不是合法的日期格式
作者:黄海
时间2020-03-23
*/
func IsDate(date string) bool {
_, err := time.Parse("2006-01-02 15:04:05", date)
if err != nil {
return false
} else {
return true
}
}
/**
功能:获取蛇形命名的字符串
作者:黄海
时间2020-01-22
*/
func GetSnakeCaseStr(str string) string {
array := strings.Split(str, "_")
var result string
for i := 0; i < len(array); i++ {
result = result + strings.ToUpper(array[i][0:1]) + array[i][1:]
}
return result
}
/**
功能:获取当前的时间戳,精确度纳秒
作者:黄海
时间2020-02-18
*/
func GetCurrentTimestamp() int64 {
return time.Now().UnixNano()
}
/**
功能:获取当前的时间字符串
作者:黄海
时间2020-02-21
*/
func GetCurrentTime() string {
timeStr := time.Now().Format("2006-01-02 15:04:05")
//当前时间的字符串2006-01-02 15:04:05据说是golang的诞生时间固定写法
return timeStr
}
/**
功能将整数64转为字符串
作者:黄海
时间2020-04-17
*/
func ConvertInt64ToString(i int64) string {
s := strconv.FormatInt(i, 10)
return s
}
/**
功能:判断所给路径文件/文件夹是否存在
作者: 黄海
时间2020-03-26
*/
func Exists(path string) bool {
_, err := os.Stat(path) //os.Stat获取文件信息
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}
/**
功能获取访问的客户机IP
作者:黄海
时间2020-03-26
*/
func RemoteIp(req *http.Request) string {
remoteAddr := req.RemoteAddr
if ip := req.Header.Get("Remote_addr"); ip != "" {
remoteAddr = ip
} else {
remoteAddr, _, _ = net.SplitHostPort(remoteAddr)
}
if remoteAddr == "::1" {
remoteAddr = "127.0.0.1"
}
return remoteAddr
}
/**
功能:判断某一个值是否含在切片之中
作者:黄海
时间2020-03-26
*/
func InArray(need interface{}, haystack interface{}) bool {
switch key := need.(type) {
case int:
for _, item := range haystack.([]int) {
if item == key {
return true
}
}
case string:
for _, item := range haystack.([]string) {
if item == key {
return true
}
}
case int64:
for _, item := range haystack.([]int64) {
if item == key {
return true
}
}
case float64:
for _, item := range haystack.([]float64) {
if item == key {
return true
}
}
default:
return false
}
return false
}
/**
功能转换MapString 到MapInterface
万恶的map[string]string-->map[string]interface{}
作者:黄海
时间2020-03-27
*/
func ConvertMapStringToMapInterface(_bean map[string]string) map[string]interface{} {
n := make(map[string]interface{})
for k, v := range _bean {
n[k] = v
}
return n
}
/**
功能带回显的执行DOS命令
作者:黄海
时间2020-05-18
*/
func Exec(name string, args ...string) error {
cmd := exec.Command(name, args...)
stderr, _ := cmd.StderrPipe()
stdout, _ := cmd.StdoutPipe()
if err := cmd.Start(); err != nil {
log.Println("exec the cmd ", name, " failed")
return err
}
// 正常日志
logScan := bufio.NewScanner(stdout)
go func() {
for logScan.Scan() {
log.Println(logScan.Text())
}
}()
// 错误日志
errBuf := bytes.NewBufferString("")
scan := bufio.NewScanner(stderr)
for scan.Scan() {
s := scan.Text()
log.Println("", s)
errBuf.WriteString(s)
errBuf.WriteString("\n")
}
// 等待命令执行完
cmd.Wait()
if !cmd.ProcessState.Success() {
// 执行失败,返回错误信息
return errors.New(errBuf.String())
}
return nil
}
/**
功能:将字符串转为整数
作者:黄海
时间2020-06-03
*/
func ConvertStringToInt(s string) int {
int, _ := strconv.Atoi(s)
return int
}
/**
功能:将字符串转为整数
作者:黄海
时间2020-06-03
*/
func ConvertStringToInt32(s string) int32 {
int, _ := strconv.Atoi(s)
return int32(int)
}
/**
功能判断是不是合法的Email
作者:黄海
时间2020-03-23
*/
func IsEmail(email string) bool {
pattern := `^[0-9a-z][_.0-9a-z-]{0,31}@([0-9a-z][0-9a-z-]{0,30}[0-9a-z]\.){1,4}[a-z]{2,4}$`
reg := regexp.MustCompile(pattern)
return reg.MatchString(email)
}
/**
功能:判断是不是合法的身份证号
作者:黄海
时间2020-06-16
*/
func IsIdCard(idCard string) bool {
IdCardNo := []byte(idCard)
return IdCardUtil.IsValidIdCardNo(&IdCardNo)
}