tool

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2024 License: MIT Imports: 14 Imported by: 0

README

Go Reference MIT


beego-tool

beego框架适用工具

安装

go get -u gitee.com/adam-qiang/beego-tool

一、context

适用于beego框架的上下文工具

1、NewContext

创建一个新的上下文

2、PostForm

接收POST表单参数

3、Query

接收GET请求的查询参数

4、JsonParams

接收application/json请求头的请求参数

5、SetStatus

设置网络状态

6、SetHeader

设置响应状态

7、OtuPut

输出响应

8、OtuPutString

输出普通字符串响应

9、OtuPutJson

输出application/json响应

10、OtuPutHtml

输出HTML响应

二、data_tool

适用于beego框架的数据工具

1、ExportCsv

导出CSV

2、ExportExcel

数据导出excel

三、validate

适用于beego框架的参数校验工具

1、InitValidate

初始化校验(在main中进行初始化)

2、Valid

公共的表单校验方法

四、数据库

适用于beego框架的数据库工具

1、初始化数据库(在main中进行初始化)
import _ "gitee.com/adam-qiang/beego-tool/database"

2、配置
[mysql]
mysql_urls =
mysql_port =
mysql_user =
mysql_pass =
mysql_db =
mysql_charset =
max_life_time =
max_open_conns =
max_idle_time =
max_idle_conns =

[redis]
address =
port =
password =
database =
key =
cache_database =
cache_key =
  • 注:redis配置中key和cache_key分别为redis普通操作前缀key和为redis缓存前缀key(可以不配置)
3、数据库操作
1、MySQL

操作遵循beego官方操作具体见beego官方文档

2、高级查询构造器扩展
  • WhereSqlBuilder

    根据where条件生成sql语句,支持OR、AND子查询及其嵌套查询条件

      import (
          orm2 "gitee.com/adam-qiang/beego-tool/database/orm"
          "github.com/beego/beego/v2/client/orm"
      )
      var where []orm.Where
      var orWhere []orm.Where
      groupIds := []string{"1", "2"}
      userId := 1
      type items struct {
          Id int64 `json:"id"`
      }
    
      orWhere = append(orWhere, orm.Where{Field: "group_id", Condition: "IN", Value: "'" + strings.Join(groupIds "','") + "'"})
      orWhere = append(orWhere, orm.Where{Field: "user_id", Condition: "=", Value: userId})
      where = append(orWhere, orm.Where{Field: "", Condition: "AND", Value: orWhere})
      where = append(where, orm.Where{Field: "", Condition: "OR", Value: orWhere})
    
      qb, _ := orm2.NewQueryBuilder("mysql")
      qb.Select("*")
      qb.From(r.TableName())
      qb.WhereSqlBuilder(where)
    
      qb.Limit(10)
      qb.Offset(0)
      sql := qb.String()
      params := qb.GetWhereParams()
      o := orm.NewOrmUsingDB("default")
      total, err = o.Raw(sql, params).QueryRows(&items)
    
  • GetWhereParams

    与WhereSqlBuilder对应,获取where条件参数

3、Redis

使用github.com/redis/go-redis/v9作为redis操作库进行二次封装,同时只封装了经常用到的方法,如有其他需求可随时issue

支持以下操:

2.1、KEY
  • Del
  • Dump
  • Restore
  • Exists
  • Expire
  • ExpireAt
  • Keys
  • Move
  • Persist
  • PExpire
  • PExpireAt
  • TTL
  • PTTL
  • Rename
  • RenameNX
  • Type
2.2、STRING
  • Set
  • SetNX
  • Get
  • Incr
  • IncrBy
  • Decr
  • DecrBy
  • MSet
  • SetEx
  • MSetNX
  • MGetMap
  • StrLen
2.3、HASH
  • HSet
  • HGet
  • HGetAll
  • HMSet
  • HMGetMap
  • HExists
  • HDel
  • HIncrBy
  • HKeys
  • HLen
  • LPop
  • LPush
  • BLPop
  • LPushX
  • RPop
  • RPush
  • RPushX
  • BRPop
  • RPopLPush
  • BRPopLPush
  • LIndex
  • LInsert
  • LLen
  • LRange
  • LRem
  • LSet
2.4、SET
  • SAdd
  • SCard
  • SDiff
  • SDiffStore
  • SInter
  • SInterStore
  • SIsMember
  • SMembers
  • SMove
  • SRem
  • SUnion
  • SUnionStore
2.5、SORTED SET
  • ZAdd
  • ZCard
  • ZCount
  • ZIncrBy
  • ZRange
  • ZRangeByScore
  • ZRank
  • ZRem
  • ZRemRangeByRank
  • ZRemRangeByScore
  • ZRevRange
  • ZRevRangeByLex
  • ZRevRangeByScore
  • ZRevRangeByScoreWithScores
  • ZRevRangeWithScores
  • ZRevRank
  • ZScore
3、Redis Cache

操作遵循beego官方操作具体见beego官方文档

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MessageTemples = map[string]string{
	"Required":     "不能为空",
	"Min":          "最小为%d",
	"Max":          "最大为%d",
	"Range":        "范围在%d至%d",
	"MinSize":      "最小长度为%d",
	"MaxSize":      "最大长度为%d",
	"Length":       "长度必须是%d",
	"Alpha":        "必须是有效的字母字符",
	"Numeric":      "必须是有效的数字字符",
	"AlphaNumeric": "必须是有效的字母或数字字符",
	"Match":        "必须匹配格式%s",
	"NoMatch":      "必须不匹配格式%s",
	"AlphaDash":    "必须是有效的字母或数字或破折号(-_)字符",
	"Email":        "必须是有效的邮件地址",
	"IP":           "必须是有效的IP地址",
	"Base64":       "必须是有效的base64字符",
	"Mobile":       "必须是有效手机号码",
	"Tel":          "必须是有效电话号码",
	"Phone":        "必须是有效的电话号码或者手机号码",
	"ZipCode":      "必须是有效的邮政编码",
}

MessageTemples 错误提示模板

Functions

func ExportCsv

func ExportCsv(ctx *beegoContext.Context, title []string, dataList [][]string, fileName string) error

ExportCsv 数据导出CSV @param ctx *beegoContext.Context @param title []string @param dataList [][]string @param fileName string @return error

func ExportExcel

func ExportExcel(w http.ResponseWriter, r *http.Request, titleList []string, dataList [][]interface{}, fileName string)

ExportExcel 数据导出excel @param w http.ResponseWriter @param r *http.Request @param titleList []string @param dataList [][]interface{} @param fileName string

func InitValidate

func InitValidate()

InitValidate 初始化校验(在main中进行初始化)

func Valid

func Valid(obj interface{}, validate interface{}) (error string)

Valid 公共的表单校验方法 @param obj interface{} @return error string

Types

type Content

type Content map[string]interface{}

type Context

type Context struct {
	Writer     *beegoContext.Response
	Req        *beegoContext.Context
	Path       string
	Method     string
	StatusCode int
}

Context 上下文结构

func NewContext

func NewContext(ctx *beegoContext.Context) *Context

NewContext 创建新的上下文 @param ctx *beegoContext.Context @return *Context

func (*Context) JsonParams

func (ctx *Context) JsonParams(params map[string]interface{})

JsonParams 接收application/json请求头的请求参数 @receiver ctx *Context @param params map[string]interface{}

func (*Context) OtuPut

func (ctx *Context) OtuPut(code int, data []byte)

OtuPut 响应输出 @receiver ctx *Context @param code int @param data []byte

func (*Context) OtuPutHtml

func (ctx *Context) OtuPutHtml(code int, html string)

OtuPutHtml 输出HTML响应 @receiver ctx *Context @param code int @param html string

func (*Context) OtuPutJson

func (ctx *Context) OtuPutJson(code int, obj interface{})

OtuPutJson 输出json @receiver ctx *Context @param code int @param obj interface{}

func (*Context) OtuPutString

func (ctx *Context) OtuPutString(code int, format string, values ...interface{})

OtuPutString 输出字符串 @receiver ctx *Context @param code int @param format string @param values ...interface{}

func (*Context) PostForm

func (ctx *Context) PostForm(key string) string

PostForm 获取post表单参数 @receiver ctx *Context @param key string @return string

func (*Context) Query

func (ctx *Context) Query(key string) string

Query 查询get请求参数 @receiver ctx *Context @param key string @return string

func (*Context) SetHeader

func (ctx *Context) SetHeader(key string, value string)

SetHeader 设置响应header @receiver ctx *Context @param key string @param value string

func (*Context) SetStatus

func (ctx *Context) SetStatus(code int)

SetStatus 设置网络状态 @receiver ctx *Context @param code int

type ReturnMsg

type ReturnMsg struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

ReturnMsg

@Description: 响应结构体

Directories

Path Synopsis
orm

Jump to

Keyboard shortcuts

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