v2版本正式上线测试版
This commit is contained in:
120
libs/pager.go
120
libs/pager.go
@@ -1,120 +0,0 @@
|
||||
/*
|
||||
* @Author: haodaquan
|
||||
* @Date: 2017-06-21 13:10:05
|
||||
* @Last Modified by: haodaquan
|
||||
* @Last Modified time: 2017-06-21 13:10:15
|
||||
*/
|
||||
|
||||
package libs
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Pager struct {
|
||||
Page int
|
||||
Totalnum int
|
||||
Pagesize int
|
||||
urlpath string
|
||||
urlquery string
|
||||
nopath bool
|
||||
}
|
||||
|
||||
func NewPager(page, totalnum, pagesize int, url string, nopath ...bool) *Pager {
|
||||
p := new(Pager)
|
||||
p.Page = page
|
||||
p.Totalnum = totalnum
|
||||
p.Pagesize = pagesize
|
||||
|
||||
arr := strings.Split(url, "?")
|
||||
p.urlpath = arr[0]
|
||||
if len(arr) > 1 {
|
||||
p.urlquery = "?" + arr[1]
|
||||
} else {
|
||||
p.urlquery = ""
|
||||
}
|
||||
|
||||
if len(nopath) > 0 {
|
||||
p.nopath = nopath[0]
|
||||
} else {
|
||||
p.nopath = false
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
func (this *Pager) url(page int) string {
|
||||
if this.nopath { //不使用目录形式
|
||||
if this.urlquery != "" {
|
||||
return fmt.Sprintf("%s%s&page=%d", this.urlpath, this.urlquery, page)
|
||||
} else {
|
||||
return fmt.Sprintf("%s?page=%d", this.urlpath, page)
|
||||
}
|
||||
} else {
|
||||
return fmt.Sprintf("%s/page/%d%s", this.urlpath, page, this.urlquery)
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Pager) ToString() string {
|
||||
if this.Totalnum <= this.Pagesize {
|
||||
return ""
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
var from, to, linknum, offset, totalpage int
|
||||
|
||||
offset = 5
|
||||
linknum = 10
|
||||
|
||||
totalpage = int(math.Ceil(float64(this.Totalnum) / float64(this.Pagesize)))
|
||||
|
||||
if totalpage < linknum {
|
||||
from = 1
|
||||
to = totalpage
|
||||
} else {
|
||||
from = this.Page - offset
|
||||
to = from + linknum
|
||||
if from < 1 {
|
||||
from = 1
|
||||
to = from + linknum - 1
|
||||
} else if to > totalpage {
|
||||
to = totalpage
|
||||
from = totalpage - linknum + 1
|
||||
}
|
||||
}
|
||||
|
||||
buf.WriteString("<ul class=\"pagination\">")
|
||||
if this.Page > 1 {
|
||||
buf.WriteString(fmt.Sprintf("<li><a href=\"%s\">«</a></li>", this.url(this.Page-1)))
|
||||
} else {
|
||||
buf.WriteString("<li class=\"disabled\"><a href=\"#\" aria-label=\"Previous\"><span aria-hidden=\"true\">«</span></a></li>")
|
||||
}
|
||||
|
||||
if this.Page > linknum {
|
||||
buf.WriteString(fmt.Sprintf("<li><a href=\"%s\">1...</a></li>", this.url(1)))
|
||||
}
|
||||
|
||||
for i := from; i <= to; i++ {
|
||||
if i == this.Page {
|
||||
buf.WriteString(fmt.Sprintf("<li class=\"active\"><a href=\"#\">%d</a></li>", i))
|
||||
} else {
|
||||
buf.WriteString(fmt.Sprintf("<li><a href=\"%s\">%d</a></li>", this.url(i), i))
|
||||
}
|
||||
}
|
||||
|
||||
if totalpage > to {
|
||||
buf.WriteString(fmt.Sprintf("<li><a href=\"%s\">...%d</a></li>", this.url(totalpage), totalpage))
|
||||
}
|
||||
|
||||
if this.Page < totalpage {
|
||||
buf.WriteString(fmt.Sprintf("<li><a href=\"%s\">»</a></li>", this.url(this.Page+1)))
|
||||
} else {
|
||||
buf.WriteString(fmt.Sprintf("<li class=\"disabled\"><a href=\"#\" aria-label=\"Next\"><span aria-hidden=\"true\">»</span></a></li>"))
|
||||
}
|
||||
buf.WriteString("</ul>")
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
@@ -1,16 +1,19 @@
|
||||
/*
|
||||
* @Author: haodaquan
|
||||
* @Date: 2017-06-20 10:01:39
|
||||
* @Last Modified by: haodaquan
|
||||
* @Last Modified time: 2017-06-20 10:02:07
|
||||
*/
|
||||
/**********************************************
|
||||
** @Des: This file ...
|
||||
** @Author: haodaquan
|
||||
** @Date: 2017-09-08 00:24:25
|
||||
** @Last Modified by: haodaquan
|
||||
** @Last Modified time: 2017-09-17 10:12:06
|
||||
***********************************************/
|
||||
|
||||
package libs
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"regexp"
|
||||
"time"
|
||||
)
|
||||
|
||||
var emailPattern = regexp.MustCompile("[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[a-zA-Z0-9](?:[\\w-]*[\\w])?")
|
||||
@@ -35,3 +38,32 @@ func SizeFormat(size float64) string {
|
||||
func IsEmail(b []byte) bool {
|
||||
return emailPattern.Match(b)
|
||||
}
|
||||
|
||||
func Password(len int, pwdO string) (pwd string, salt string) {
|
||||
salt = GetRandomString(4)
|
||||
defaultPwd := "george518"
|
||||
if pwdO != "" {
|
||||
defaultPwd = pwdO
|
||||
}
|
||||
pwd = Md5([]byte(defaultPwd + salt))
|
||||
return pwd, salt
|
||||
}
|
||||
|
||||
// 生成32位MD5
|
||||
// func MD5(text string) string{
|
||||
// ctx := md5.New()
|
||||
// ctx.Write([]byte(text))
|
||||
// return hex.EncodeToString(ctx.Sum(nil))
|
||||
// }
|
||||
|
||||
//生成随机字符串
|
||||
func GetRandomString(lens int) string {
|
||||
str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
bytes := []byte(str)
|
||||
result := []byte{}
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
for i := 0; i < lens; i++ {
|
||||
result = append(result, bytes[r.Intn(len(bytes))])
|
||||
}
|
||||
return string(result)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user