update
This commit is contained in:
1 parent
f25da5f1ee
commit
1df55e75e5
72 files changed
+1401
-1559
No files matched your search
@@ -0,0 +1,46 @@
|
||||
package app
|
||||
|
||||
type Response struct {
|
||||
// 代码
|
||||
Code int `json:"code" example:"200"`
|
||||
// 数据集
|
||||
Data interface{} `json:"data"`
|
||||
// 消息
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
type Page struct {
|
||||
List interface{} `json:"list"`
|
||||
Count int `json:"count"`
|
||||
PageIndex int `json:"pageIndex"`
|
||||
PageSize int `json:"pageSize"`
|
||||
}
|
||||
|
||||
type PageResponse struct {
|
||||
// 代码
|
||||
Code int `json:"code" example:"200"`
|
||||
// 数据集
|
||||
Data Page `json:"data"`
|
||||
// 消息
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
|
||||
func (res *Response) ReturnOK() *Response {
|
||||
res.Code = 200
|
||||
return res
|
||||
}
|
||||
|
||||
func (res *Response) ReturnError(code int) *Response {
|
||||
res.Code = code
|
||||
return res
|
||||
}
|
||||
|
||||
|
||||
func (res *PageResponse) ReturnOK() *PageResponse {
|
||||
res.Code = 200
|
||||
return res
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package msg
|
||||
|
||||
var (
|
||||
CreatedSuccess = "创建成功!"
|
||||
UpdatedSuccess = "更新成功!"
|
||||
DeletedSuccess = "删除成功!"
|
||||
GetSuccess = "查询成功!"
|
||||
NotFound = "未找到相关内容或者数据为空!"
|
||||
)
|
||||
@@ -0,0 +1,44 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// 失败数据处理
|
||||
func Error(c *gin.Context, code int, err error, msg string) {
|
||||
var res Response
|
||||
res.Msg = err.Error()
|
||||
if msg != "" {
|
||||
res.Msg = msg
|
||||
}
|
||||
c.JSON(http.StatusOK, res.ReturnError(code))
|
||||
}
|
||||
|
||||
// 通常成功数据处理
|
||||
func OK(c *gin.Context, data interface{}, msg string) {
|
||||
var res Response
|
||||
res.Data = data
|
||||
if msg != "" {
|
||||
res.Msg = msg
|
||||
}
|
||||
c.JSON(http.StatusOK, res.ReturnOK())
|
||||
}
|
||||
|
||||
// 分页数据处理
|
||||
func PageOK(c *gin.Context, result interface{},count int,pageIndex int,pageSize int, msg string) {
|
||||
var res PageResponse
|
||||
res.Data.List = result
|
||||
res.Data.Count = count
|
||||
res.Data.PageIndex = pageIndex
|
||||
res.Data.PageSize = pageSize
|
||||
if msg != "" {
|
||||
res.Msg = msg
|
||||
}
|
||||
c.JSON(http.StatusOK, res.ReturnOK())
|
||||
}
|
||||
|
||||
// 兼容函数
|
||||
func Custum(c *gin.Context, data gin.H) {
|
||||
c.JSON(http.StatusOK,data)
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package captcha
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"github.com/mojocn/base64Captcha"
|
||||
"image/color"
|
||||
)
|
||||
|
||||
var store = base64Captcha.DefaultMemStore
|
||||
|
||||
//configJsonBody json request body.
|
||||
type configJsonBody struct {
|
||||
Id string
|
||||
CaptchaType string
|
||||
VerifyValue string
|
||||
DriverAudio *base64Captcha.DriverAudio
|
||||
DriverString *base64Captcha.DriverString
|
||||
DriverChinese *base64Captcha.DriverChinese
|
||||
DriverMath *base64Captcha.DriverMath
|
||||
DriverDigit *base64Captcha.DriverDigit
|
||||
}
|
||||
|
||||
|
||||
func DriverStringFunc() (id, b64s string, err error) {
|
||||
e :=configJsonBody{}
|
||||
e.Id = uuid.New().String()
|
||||
e.DriverString = base64Captcha.NewDriverString(46, 140, 2, 2, 4, "234567890abcdefghjkmnpqrstuvwxyz", &color.RGBA{240, 240, 246, 246}, []string{"wqy-microhei.ttc"})
|
||||
driver := e.DriverString.ConvertFonts()
|
||||
cap := base64Captcha.NewCaptcha(driver, store)
|
||||
return cap.Generate()
|
||||
}
|
||||
|
||||
|
||||
func DriverDigitFunc() (id, b64s string, err error) {
|
||||
e := configJsonBody{}
|
||||
e.Id = uuid.New().String()
|
||||
e.DriverDigit = base64Captcha.DefaultDriverDigit
|
||||
driver := e.DriverDigit
|
||||
cap := base64Captcha.NewCaptcha(driver, store)
|
||||
return cap.Generate()
|
||||
}
|
||||
+9
-19
@@ -7,34 +7,24 @@ import (
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"go-admin/config"
|
||||
"go-admin/utils"
|
||||
"go-admin/pkg/utils"
|
||||
)
|
||||
|
||||
var Em endpoint.Middleware
|
||||
|
||||
func init() {
|
||||
//Apter ,_ := gormadapter.NewAdapter("mysql", "testdb:Qq123456@tcp(rm-uf60100537o86401eao.mysql.rds.aliyuncs.com:3306)/testdb",true) // Your driver and data source.
|
||||
//e,_ := casbin.NewEnforcer("config/rbac_model.conf", "config/policy.csv")
|
||||
//
|
||||
//err := Apter.SavePolicy(e.GetModel())
|
||||
//if err != nil {
|
||||
// panic(err)
|
||||
//}
|
||||
//
|
||||
//// Load the policy from DB.
|
||||
//e.LoadPolicy()
|
||||
|
||||
}
|
||||
|
||||
func Casbin() (*casbin.Enforcer, error) {
|
||||
conn := config.DatabaseConfig.Username + ":" + config.DatabaseConfig.Password + "@tcp(" + config.DatabaseConfig.Host + ":" + utils.IntToString(config.DatabaseConfig.Port) + ")/" + config.DatabaseConfig.Database
|
||||
if config.DatabaseConfig.Dbtype == "sqlite3" {
|
||||
conn = config.DatabaseConfig.Host
|
||||
}
|
||||
Apter, _ := gormadapter.NewAdapter(config.DatabaseConfig.Dbtype, conn, true)
|
||||
|
||||
e, _ := casbin.NewEnforcer("config/rbac_model.conf", Apter)
|
||||
|
||||
Apter, err := gormadapter.NewAdapter(config.DatabaseConfig.Dbtype, conn, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
e, err := casbin.NewEnforcer("config/rbac_model.conf", Apter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := e.LoadPolicy(); err == nil {
|
||||
return e, err
|
||||
} else {
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package cronjob
|
||||
|
||||
import (
|
||||
"github.com/robfig/cron/v3"
|
||||
"log"
|
||||
)
|
||||
|
||||
func TestJob(c *cron.Cron) {
|
||||
id, err := c.AddFunc("1 * * * *", func() {
|
||||
|
||||
log.Println("Every hour on the one hour")
|
||||
})
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
log.Println("start error")
|
||||
} else {
|
||||
log.Println("Start Success; ID: %v", id)
|
||||
}
|
||||
}
|
||||
@@ -5,14 +5,13 @@ import (
|
||||
"errors"
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go-admin/config"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// MapClaims type that uses the map[string]interface{} for JSON decoding
|
||||
// This is the default claims type if you don't supply one
|
||||
type MapClaims map[string]interface{}
|
||||
|
||||
// GinJWTMiddleware provides a Json-Web-Token authentication implementation. On failure, a 401 HTTP response
|
||||
@@ -81,10 +80,12 @@ type GinJWTMiddleware struct {
|
||||
// rolekey
|
||||
RKey string
|
||||
|
||||
// roleId
|
||||
RoleIdKey string
|
||||
|
||||
RoleKey string
|
||||
|
||||
// roleName
|
||||
RoleNameKey string
|
||||
|
||||
// TokenLookup is a string in the form of "<source>:<name>" that is used
|
||||
@@ -281,13 +282,12 @@ func (mw *GinJWTMiddleware) MiddlewareInit() error {
|
||||
mw.SigningAlgorithm = "HS256"
|
||||
}
|
||||
|
||||
if mw.Timeout == 0 {
|
||||
mw.Timeout = time.Hour
|
||||
if config.JwtConfig.Timeout != 0 {
|
||||
// TODO: token过期时长
|
||||
mw.Timeout = time.Hour
|
||||
mw.Timeout = time.Duration(config.JwtConfig.Timeout) * time.Second
|
||||
}
|
||||
|
||||
//mw.Timeout = time.Second*5
|
||||
|
||||
if mw.TimeFunc == nil {
|
||||
mw.TimeFunc = time.Now
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
Info *log.Logger
|
||||
Warning *log.Logger
|
||||
Error * log.Logger
|
||||
)
|
||||
|
||||
func init(){
|
||||
errFile,err:=os.OpenFile("errors.log",os.O_CREATE|os.O_WRONLY|os.O_APPEND,0666)
|
||||
if err!=nil{
|
||||
log.Fatalln("打开日志文件失败:",err)
|
||||
}
|
||||
|
||||
Info = log.New(os.Stdout,"Info:",log.Ldate | log.Ltime | log.Lshortfile)
|
||||
Warning = log.New(os.Stdout,"Warning:",log.Ldate | log.Ltime | log.Lshortfile)
|
||||
Error = log.New(io.MultiWriter(os.Stderr,errFile),"Error:",log.Ldate | log.Ltime | log.Lshortfile)
|
||||
|
||||
}
|
||||
+3
-12
@@ -9,20 +9,11 @@ import (
|
||||
func StrToInt(err error, index string) int {
|
||||
result, err := strconv.Atoi(index)
|
||||
if err != nil {
|
||||
AssertErr(err, "string to int error"+err.Error(), -1)
|
||||
HasError(err, "string to int error"+err.Error(), -1)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
//加密
|
||||
func Encrypt(e string) ([]byte, error) {
|
||||
//var s []byte
|
||||
s, err := bcrypt.GenerateFromPassword([]byte(e), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s, err
|
||||
}
|
||||
|
||||
func CompareHashAndPassword(e string, p string) (bool, error) {
|
||||
err := bcrypt.CompareHashAndPassword([]byte(e), []byte(p))
|
||||
@@ -46,11 +37,11 @@ func Assert(condition bool, msg string, code ...int) {
|
||||
}
|
||||
}
|
||||
|
||||
// AssertErr 错误断言
|
||||
// HasError 错误断言
|
||||
// 当 error 不为 nil 时触发 panic
|
||||
// 对于当前请求不会再执行接下来的代码,并且返回指定格式的错误信息和错误码
|
||||
// 若 msg 为空,则默认为 error 中的内容
|
||||
func AssertErr(err error, msg string, code ...int) {
|
||||
func HasError(err error, msg string, code ...int) {
|
||||
if err != nil {
|
||||
statusCode := 200
|
||||
if len(code) > 0 {
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package utils
|
||||
|
||||
import "strconv"
|
||||
|
||||
func Float64ToString(e float64) string {
|
||||
return strconv.FormatFloat(e, 'E', -1, 64)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package utils
|
||||
|
||||
import "strconv"
|
||||
|
||||
func IntToString(e int) string {
|
||||
return strconv.Itoa(e)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package utils
|
||||
|
||||
import "strconv"
|
||||
|
||||
func Int64ToString(e int64) string {
|
||||
return strconv.FormatInt(e, 10)
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func GetLocation(ip string) string {
|
||||
if ip == "127.0.0.1" || ip == "localhost" {
|
||||
return "内部IP"
|
||||
}
|
||||
resp, err := http.Get("https://restapi.amap.com/v3/ip?ip=" + ip + "&key=3fabc36c20379fbb9300c79b19d5d05e")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
s, err := ioutil.ReadAll(resp.Body)
|
||||
fmt.Printf(string(s))
|
||||
|
||||
m := make(map[string]string)
|
||||
|
||||
err = json.Unmarshal(s, &m)
|
||||
if err != nil {
|
||||
fmt.Println("Umarshal failed:", err)
|
||||
}
|
||||
if m["province"] == "" {
|
||||
return "未知位置"
|
||||
}
|
||||
return m["province"] + "-" + m["city"]
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func StringToInt64(e string) (int64, error) {
|
||||
return strconv.ParseInt(e, 10, 64)
|
||||
}
|
||||
|
||||
func StringToInt(e string) (int, error) {
|
||||
return strconv.Atoi(e)
|
||||
}
|
||||
|
||||
|
||||
func GetCurrntTimeStr() string {
|
||||
return time.Now().Format("2006/01/02 15:04:05")
|
||||
}
|
||||
|
||||
func GetCurrntTime() time.Time {
|
||||
return time.Now()
|
||||
}
|
||||
|
||||
|
||||
func StructToJsonStr(e interface{}) (string, error) {
|
||||
if b, err := json.Marshal(e); err == nil {
|
||||
return string(b), err
|
||||
} else {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
func GetBodyString(c *gin.Context) (string, error) {
|
||||
body, err := ioutil.ReadAll(c.Request.Body)
|
||||
if err != nil {
|
||||
fmt.Printf("read body err, %v\n", err)
|
||||
return string(body), nil
|
||||
} else {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
func JsonStrToMap(e string) (map[string]interface{}, error) {
|
||||
var dict map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(e), &dict); err == nil {
|
||||
return dict, err
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//获取URL中批量id并解析
|
||||
func IdsStrToIdsIntGroup(key string, c *gin.Context) []int {
|
||||
return idsStrToIdsIntGroup(c.Param(key))
|
||||
}
|
||||
|
||||
|
||||
func idsStrToIdsIntGroup(keys string) []int {
|
||||
IDS := make([]int, 0)
|
||||
ids := strings.Split(keys, ",")
|
||||
for i := 0; i < len(ids); i++ {
|
||||
ID, _ := StringToInt(ids[i])
|
||||
IDS = append(IDS, ID)
|
||||
}
|
||||
return IDS
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
jwt "go-admin/pkg/jwtauth"
|
||||
)
|
||||
|
||||
func ExtractClaims(c *gin.Context) jwt.MapClaims {
|
||||
claims, exists := c.Get("JWT_PAYLOAD")
|
||||
if !exists {
|
||||
return make(jwt.MapClaims)
|
||||
}
|
||||
|
||||
return claims.(jwt.MapClaims)
|
||||
}
|
||||
|
||||
func GetUserId(c *gin.Context) int {
|
||||
data := ExtractClaims(c)
|
||||
if data["identity"] != nil {
|
||||
return int((data["identity"]).(float64))
|
||||
}
|
||||
fmt.Println("****************************** 路径:" + c.Request.URL.Path + " 请求方法:" + c.Request.Method + " 说明:缺少identity")
|
||||
return 0
|
||||
}
|
||||
|
||||
func GetUserIdStr(c *gin.Context) string {
|
||||
data := ExtractClaims(c)
|
||||
if data["identity"] != nil {
|
||||
return Int64ToString(int64((data["identity"]).(float64)))
|
||||
}
|
||||
fmt.Println("****************************** 路径:" + c.Request.URL.Path + " 请求方法:" + c.Request.Method + " 缺少identity")
|
||||
return ""
|
||||
}
|
||||
|
||||
func GetUserName(c *gin.Context) string {
|
||||
data := ExtractClaims(c)
|
||||
if data["nice"] != nil {
|
||||
return (data["nice"]).(string)
|
||||
}
|
||||
fmt.Println("****************************** 路径:" + c.Request.URL.Path + " 请求方法:" + c.Request.Method + " 缺少nice")
|
||||
return ""
|
||||
}
|
||||
|
||||
func GetRoleName(c *gin.Context) string {
|
||||
data := ExtractClaims(c)
|
||||
if data["rolekey"] != nil {
|
||||
return (data["rolekey"]).(string)
|
||||
}
|
||||
fmt.Println("****************************** 路径:" + c.Request.URL.Path + " 请求方法:" + c.Request.Method + " 缺少rolekey")
|
||||
return ""
|
||||
}
|
||||
|
||||
func GetRoleId(c *gin.Context) int {
|
||||
data := ExtractClaims(c)
|
||||
if data["roleid"] != nil {
|
||||
i := int((data["roleid"]).(float64))
|
||||
return i
|
||||
}
|
||||
fmt.Println("****************************** 路径:" + c.Request.URL.Path + " 请求方法:" + c.Request.Method + " 缺少roleid")
|
||||
return 0
|
||||
}
|
||||
Reference in new issue
Block a user