Ai
198 Star 566 Fork 254

lock-li/opms

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
string.go 2.48 KB
一键复制 编辑 原始数据 按行查看 历史
lock 提交于 2017-03-30 17:44 +08:00 . opms v1.3
package utils
import (
"crypto/rand"
"math/big"
"net/smtp"
"regexp"
"strconv"
"strings"
"github.com/astaxie/beego"
"github.com/zheng-ji/goSnowFlake"
)
/*
func AjaxMsg(code int, message string) {
this.Data["json"] = map[string]interface{}{"code": code, "message": message}
this.ServeJSON()
return
}
*/
//字串截取
func SubString(s string, pos, length int) string {
runes := []rune(s)
l := pos + length
if l > len(runes) {
l = len(runes)
}
return string(runes[pos:l])
}
func GetFileSuffix(s string) string {
re, _ := regexp.Compile(".(jpg|jpeg|png|gif|exe|doc|docx|ppt|pptx|xls|xlsx)")
suffix := re.ReplaceAllString(s, "")
return suffix
}
func RandInt64(min, max int64) int64 {
maxBigInt := big.NewInt(max)
i, _ := rand.Int(rand.Reader, maxBigInt)
if i.Int64() < min {
RandInt64(min, max)
}
return i.Int64()
}
func Strim(str string) string {
str = strings.Replace(str, "\t", "", -1)
str = strings.Replace(str, " ", "", -1)
str = strings.Replace(str, "\n", "", -1)
str = strings.Replace(str, "\r", "", -1)
return str
}
func Unicode(rs string) string {
json := ""
for _, r := range rs {
rint := int(r)
if rint < 128 {
json += string(r)
} else {
json += "\\u" + strconv.FormatInt(int64(rint), 16)
}
}
return json
}
func HTMLEncode(rs string) string {
html := ""
for _, r := range rs {
html += "&#" + strconv.Itoa(int(r)) + ";"
}
return html
}
/**
* to: example@example.com;example1@163.com;example2@sina.com.cn;...
* subject:The subject of mail
* body: The content of mail
*/
func SendMail(to string, subject string, body string) error {
user := beego.AppConfig.String("mailfrom")
password := beego.AppConfig.String("mailpassword")
host := beego.AppConfig.String("mailhost")
hp := strings.Split(host, ":")
auth := smtp.PlainAuth("", user, password, hp[0])
var content_type string
content_type = "Content-type:text/html;charset=utf-8"
msg := []byte("To: " + to + "\r\nFrom: " + user + "<" + user + ">\r\nSubject: " + subject + "\r\n" + content_type + "\r\n\r\n" + body)
send_to := strings.Split(to, ";")
err := smtp.SendMail(host, auth, user, send_to, msg)
return err
}
func SnowFlakeId() int64 {
iw, _ := goSnowFlake.NewIdWorker(1)
if id, err := iw.NextId(); err != nil {
return 0
} else {
return id
}
}
//数组去重来源网络
func RemoveDuplicatesAndEmpty(a []string) (ret []string) {
a_len := len(a)
for i := 0; i < a_len; i++ {
if (i > 1 && a[i-1] == a[i]) || len(a[i]) == 0 {
continue
}
ret = append(ret, a[i])
}
return
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/lock-upme/opms.git
git@gitee.com:lock-upme/opms.git
lock-upme
opms
opms
558463a10cc0

搜索帮助