123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- package utils
- import (
- "crypto/md5"
- "encoding/hex"
- "regexp"
- "strconv"
- )
- func MD5(data string) string {
- m := md5.Sum([]byte(data))
- return hex.EncodeToString(m[:])
- }
- func CheckPwd(pwd string) bool {
- compile := `([0-9a-z]+){6,12}|(a-z0-9]+){6,12}`
- reg := regexp.MustCompile(compile)
- flag := reg.MatchString(pwd)
- return flag
- }
- // 校验邮箱格式
- func ValidateEmailFormatat(email string) bool {
- reg := regexp.MustCompile(RegularEmail)
- return reg.MatchString(email)
- }
- // 验证是否是手机号
- func ValidateMobileFormatat(mobileNum string) bool {
- reg := regexp.MustCompile(RegularMobile)
- return reg.MatchString(mobileNum)
- }
- // 计算分页起始页
- func StartIndex(page, pagesize int) int {
- if page > 1 {
- return (page - 1) * pagesize
- }
- return 0
- }
- // GetLikeKeywordPars 获取sql查询中的参数切片
- func GetLikeKeywordPars(pars []interface{}, keyword string, num int) (newPars []interface{}) {
- newPars = pars
- if newPars == nil {
- newPars = make([]interface{}, 0)
- }
- for i := 1; i <= num; i++ {
- newPars = append(newPars, `%`+keyword+`%`)
- }
- return
- }
- func StringsToJSON(str string) string {
- rs := []rune(str)
- jsons := ""
- for _, r := range rs {
- rint := int(r)
- if rint < 128 {
- jsons += string(r)
- } else {
- jsons += "\\u" + strconv.FormatInt(int64(rint), 16) // json
- }
- }
- return jsons
- }
|