utils

package
v0.0.0-...-783c0ba Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 19, 2020 License: Apache-2.0 Imports: 48 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KC_RAND_KIND_NUM   = 0 // 纯数字
	KC_RAND_KIND_LOWER = 1 // 小写字母
	KC_RAND_KIND_UPPER = 2 // 大写字母
	KC_RAND_KIND_ALL   = 3 // 数字、大小写字母
)
View Source
const (
	StoreLocal  = "local"
	StoreOss    = "oss"
	VirtualRoot = "virtualroot"
)

更多存储类型有待扩展

Variables

View Source
var (
	BooksRelease  = BooksLock{Books: make(map[int]bool)}
	BooksGenerate = BooksLock{Books: make(map[int]bool)}
)
View Source
var (
	Version     string = "unknown"
	GitHash     string = "unknown"
	BuildAt     string = "unknown"
	Segmenter   sego.Segmenter
	BasePath, _ = filepath.Abs(filepath.Dir(os.Args[0]))
	StoreType   = beego.AppConfig.String("store_type") //存储类型

)

分词器

View Source
var ScreenShotProjects sync.Map // map[bookIdentify]selector,书籍标识和书籍元素选择器

Functions

func AbsolutePath

func AbsolutePath(p string) (string, error)

func Asset

func Asset(p string, cdn string) string

func CopyFile

func CopyFile(dstName, srcName string) (written int64, err error)

func CopyObject

func CopyObject(src, dst interface{})

func CrawlByChrome

func CrawlByChrome(urlStr string, bookIdentify string) (cont string, err error)

使用chrome采集网页HTML

func CrawlHtml2Markdown

func CrawlHtml2Markdown(urlstr string, contType int, force bool, intelligence int, diySelector string, excludeSelector []string, links map[string]string, headers ...map[string]string) (cont string, err error)

采集HTML并把相对链接和相对图片 内容类型,contType:0表示markdown,1表示html,2表示文本 force:是否是强力采集 intelligence:是否是智能提取,智能提取,使用html2article,否则提取body diySelecter:自定义选择器 注意:由于参数问题,采集并下载图片的话,在headers中加上key为"project"的字段,值为文档项目的标识

func CropImage

func CropImage(file string, width, height int) (err error)

图片缩放居中裁剪 图片缩放居中裁剪 @param file 图片文件 @param width 图片宽度 @param height 图片高度 @return err 错误

func DealUri

func DealUri(po *PageOptions, requestURI string)

*

  • 处理url,目的是保存参数

func Decode

func Decode(value string, r interface{}) error

解码

func DeleteFile

func DeleteFile(file string)

func DeleteScreenShot

func DeleteScreenShot(bookIdentify string)

func DownImage

func DownImage(src string, headers ...map[string]string) (filename string, err error)

下载图片

func Encode

func Encode(value interface{}) (string, error)

编码

func ExecuteViewPathTemplate

func ExecuteViewPathTemplate(tplName string, data interface{}) (string, error)

ExecuteViewPathTemplate 执行指定的模板并返回执行结果. @param tplName 模板文件路径

func FileExists

func FileExists(name string) bool

FileExists reports whether the named file or directory exists.

func FormatBytes

func FormatBytes(size int64) string

func FormatReadingTime

func FormatReadingTime(seconds int, withoutTag ...bool) string

func GetBookWXACode

func GetBookWXACode(accessToken string, bookId int) (tmpFile string, err error)

获取书籍页面小程序码

func GetIP

func GetIP(ctx *context.Context, field string) (ip string)

获取ip地址

func GetLang

func GetLang(lang string) string

func GetPagerHtml

func GetPagerHtml(requestURI string, pageIndex, pageSize, totalCount int) html.HTML
func GetPagerLinks(po *PageOptions, requestURI string) (int, int, orm.RawSeter, html.HTML)

*

  • 返回总记录条数,总页数,当前页面数据,分页html
  • 根据分页选项,生成分页连接 下面是一个实例: func (this *MainController) Test() { var po util.PageOptions po.EnablePreNexLink = true po.EnableFirstLastLink = true po.LinkItemCount = 7 po.TableName = "help_topic" cp, _ := this.GetInt("pno") po.CurrentPage = int(cp) _,_,_ pager := util.GetPagerLinks(&po, this.Ctx) this.Data["Email"] = html.HTML(pager) this.TplName = "test.html" }

func GetPagesInfo

func GetPagesInfo(tableName string, currentpage int, pagesize int, conditions string) (int, int, orm.RawSeter)

*

  • 分页函数,适用任何表
  • 返回 总记录条数,总页数,以及当前请求的数据RawSeter,调用中需要"rs.QueryRows(&tblog)"就行了 --tblog是一个Tb_log对象
  • 参数:表名,当前页数,页面大小,条件(查询条件,格式为 " and name='zhifeiya' and age=12 ")

func GetTextFromHtml

func GetTextFromHtml(htmlStr string) (txt string)

从HTML中获取text

func GitClone

func GitClone(url, folder string) error

Git Clone

func HandleResponse

func HandleResponse(resp *http.Response, err error) error

处理http响应成败

func HandleSVG

func HandleSVG(doc *goquery.Document, project string) *goquery.Document

func InMap

func InMap(maps map[int]bool, key int) (ret bool)

判断数据是否在map中

func InitVirtualRoot

func InitVirtualRoot()

func IsMobile

func IsMobile(userAgent string) bool

func JoinURI

func JoinURI(elem ...string) string

func JoinURL

func JoinURL(rawURL string, urlPath string) string

func Krand

func Krand(size int, kind int) []byte

随机字符串

func MD5Sub16

func MD5Sub16(str string) string

截取md5前16个字符

func NewPaginations

func NewPaginations(rollPage, totalRows, listRows, currentPage int, urlPrefix string, urlSuffix string, urlParams ...interface{}) html1.HTML

分页函数(这个分页函数不具有通用性) rollPage:展示分页的个数 totalRows:总记录 currentPage:每页显示记录数 urlPrefix:url链接前缀 urlParams:url键值对参数

func ParseTitleFromMdHtml

func ParseTitleFromMdHtml(html string) (title string)

从md的html文件中提取文章标题(从h1-h6)

func PasswordHash

func PasswordHash(pass string) (string, error)

加密密码

func PasswordVerify

func PasswordVerify(hashing string, pass string) (bool, error)

校验密码是否有效

func PrintInfo

func PrintInfo()

func RangeNumber

func RangeNumber(val, min, max int) int

func RenderCoverByBookIdentify

func RenderCoverByBookIdentify(identify string) (err error)

生成

func RenderDocumentById

func RenderDocumentById(id int)

渲染markdown为html并录入数据库

func Round

func Round(val float64, places int) float64

func ScoreFloat

func ScoreFloat(score int) string

评分处理

func SegWord

func SegWord(str interface{}) (wds string)

分词 @param str 需要分词的文字

func SendMail

func SendMail(conf *conf.SmtpConf, subject, email string, body string) error

@param conf 邮箱配置 @param subject 邮件主题 @param email 收件人 @param body 邮件内容

func ShowImg

func ShowImg(img string, style ...string) (url string)

操作图片显示 如果用的是oss存储,这style是avatar、cover可选项

func Substr

func Substr(s string, length int) string

Substr returns the substr from start to length.

func UploadFile

func UploadFile(src, dest string) (err error)

Types

type BooksLock

type BooksLock struct {
	Books map[int]bool
	Lock  sync.RWMutex
}

书籍发布锁和书籍离线文档生成锁

func (BooksLock) Delete

func (this BooksLock) Delete(bookId int)

删除

func (BooksLock) Exist

func (this BooksLock) Exist(bookId int) (exist bool)

查询是否存在

func (BooksLock) Set

func (this BooksLock) Set(bookId int)

设置

type PageOptions

type PageOptions struct {
	TableName           string //表名  -----------------[必填]
	Conditions          string //条件
	CurrentPage         int    //当前页 ,默认1 每次分页,必须在前台设置新的页数,不设置始终默认1.在控制器中使用方式:cp, _ := this.GetInt("pno")   po.CurrentPage = int(cp)
	PageSize            int    //页面大小,默认20
	LinkItemCount       int    //生成A标签的个数 默认10个
	Href                string //A标签的链接地址  ---------[不需要设置]
	ParamName           string //参数名称  默认是pno
	FirstPageText       string //首页文字  默认"首页"
	LastPageText        string //尾页文字  默认"尾页"
	PrePageText         string //上一页文字 默认"上一页"
	NextPageText        string //下一页文字 默认"下一页"
	EnableFirstLastLink bool   //是否启用首尾连接 默认false 建议开启
	EnablePreNexLink    bool   //是否启用上一页,下一页连接 默认false 建议开启
	TotalPages          int
}

type ReflectVal

type ReflectVal struct {
	T reflect.Type
	V reflect.Value
}

type ScreenShotInfo

type ScreenShotInfo struct {
	Data [][]struct {
		Height float64 `json:"height"`
		Width  float64 `json:"width"`
		X      float64 `json:"x"`
		Y      float64 `json:"y"`
	} `json:"data"`
	Height float64 `json:"height"`
}

截图信息

type SplitMD

type SplitMD struct {
	Identify string
	Cont     string
}

type WechatToken

type WechatToken struct {
	AccessToken string `json:"access_token"`
	ExpiresIn   int64  `json:"expires_in"`
	ErrCode     int    `json:"errcode"`
	ErrMsg      string `json:"errmsg"`
}

func GetAccessToken

func GetAccessToken() (token *WechatToken)

获取access_token

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL