package actionutils import ( "fmt" "math" "net/url" "strings" "github.com/iwind/TeaGo/actions" ) type Page struct { Offset int64 // 开始位置 Size int64 // 每页显示数量 Current int64 // 当前页码 Max int64 // 最大页码 Total int64 // 总数量 Path string Query url.Values } func NewActionPage(actionPtr actions.ActionWrapper, total int64, size int64) *Page { action := actionPtr.Object() currentPage := action.ParamInt64("page") paramSize := action.ParamInt64("pageSize") if paramSize > 0 { size = paramSize } if size <= 0 { size = 10 } page := &Page{ Current: currentPage, Total: total, Size: size, Path: action.Request.URL.Path, Query: action.Request.URL.Query(), } page.calculate() return page } func (this *Page) calculate() { if this.Current < 1 { this.Current = 1 } if this.Size <= 0 { this.Size = 10 } this.Offset = this.Size * (this.Current - 1) this.Max = int64(math.Ceil(float64(this.Total) / float64(this.Size))) } func (this *Page) AsHTML() string { if this.Total <= this.Size { return "" } result := []string{} // 首页 if this.Max > 0 { result = append(result, `首页`) } else { result = append(result, `首页`) } // 上一页 if this.Current <= 1 { result = append(result, `上一页`) } else { result = append(result, `上一页`) } // 中间页数 before5 := this.max(this.Current-5, 1) after5 := this.min(before5+9, this.Max) if before5 > 1 { result = append(result, `...`) } for i := before5; i <= after5; i++ { if i == this.Current { result = append(result, ``+fmt.Sprintf("%d", i)+``) } else { result = append(result, ``+fmt.Sprintf("%d", i)+``) } } if after5 < this.Max { result = append(result, `...`) } // 下一页 if this.Current >= this.Max { result = append(result, "下一页") } else { result = append(result, `下一页`) } // 尾页 if this.Max > 0 { result = append(result, `尾页`) } else { result = append(result, `尾页`) } // 每页数 result = append(result, ``) return `
` + strings.Join(result, "") + `
` } // IsLastPage 判断是否为最后一页 func (this *Page) IsLastPage() bool { return this.Current == this.Max } func (this *Page) composeURL(page int64) string { this.Query["page"] = []string{fmt.Sprintf("%d", page)} return this.Path + "?" + this.Query.Encode() } func (this *Page) min(i, j int64) int64 { if i < j { return i } return j } func (this *Page) max(i, j int64) int64 { if i < j { return j } return i } func (this *Page) renderSizeOption(size int64) string { o := `` return o }