结构调整添加cmd

This commit is contained in:
zhangwenjian committed 2020-04-18 10:06:44 +08:00
1 parent f2829bfd98
commit a2f4c00246
72 files changed
+838 -597

No files matched your search

-46
View File
@@ -1,46 +0,0 @@
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
}
-9
View File
@@ -1,9 +0,0 @@
package msg
var (
CreatedSuccess = "创建成功!"
UpdatedSuccess = "更新成功!"
DeletedSuccess = "删除成功!"
GetSuccess = "查询成功!"
NotFound = "未找到相关内容或者数据为空!"
)
-44
View File
@@ -1,44 +0,0 @@
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)
}
-41
View File
@@ -1,41 +0,0 @@
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()
}
+3 -3
View File
@@ -6,14 +6,14 @@ import (
gormadapter "github.com/casbin/gorm-adapter/v2"
"github.com/go-kit/kit/endpoint"
_ "github.com/go-sql-driver/mysql"
"go-admin/config"
"go-admin/pkg/utils"
"go-admin/database"
"go-admin/tools/config"
)
var Em endpoint.Middleware
func Casbin() (*casbin.Enforcer, error) {
conn := config.DatabaseConfig.Username + ":" + config.DatabaseConfig.Password + "@tcp(" + config.DatabaseConfig.Host + ":" + utils.IntToString(config.DatabaseConfig.Port) + ")/" + config.DatabaseConfig.Database
conn := database.GetMysqlConnect()
if config.DatabaseConfig.Dbtype == "sqlite3" {
conn = config.DatabaseConfig.Host
}
+3 -3
View File
@@ -5,7 +5,7 @@ import (
"errors"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
"go-admin/config"
config2 "go-admin/tools/config"
"io/ioutil"
"net/http"
"strings"
@@ -283,9 +283,9 @@ func (mw *GinJWTMiddleware) MiddlewareInit() error {
}
mw.Timeout = time.Hour
if config.JwtConfig.Timeout != 0 {
if config2.JwtConfig.Timeout != 0 {
// TODO: token过期时长
mw.Timeout = time.Duration(config.JwtConfig.Timeout) * time.Second
mw.Timeout = time.Duration(config2.JwtConfig.Timeout) * time.Second
}
if mw.TimeFunc == nil {
-55
View File
@@ -1,55 +0,0 @@
package pkg
import (
"golang.org/x/crypto/bcrypt"
"log"
"strconv"
)
func StrToInt(err error, index string) int {
result, err := strconv.Atoi(index)
if err != nil {
HasError(err, "string to int error"+err.Error(), -1)
}
return result
}
func CompareHashAndPassword(e string, p string) (bool, error) {
err := bcrypt.CompareHashAndPassword([]byte(e), []byte(p))
if err != nil {
log.Print(err.Error())
return false, err
}
return true, nil
}
// Assert 条件断言
// 当断言条件为 假 时触发 panic
// 对于当前请求不会再执行接下来的代码,并且返回指定格式的错误信息和错误码
func Assert(condition bool, msg string, code ...int) {
if !condition {
statusCode := 200
if len(code) > 0 {
statusCode = code[0]
}
panic("CustomErroe#" + strconv.Itoa(statusCode) + "#" + msg)
}
}
// HasError 错误断言
// 当 error 不为 nil 时触发 panic
// 对于当前请求不会再执行接下来的代码,并且返回指定格式的错误信息和错误码
// 若 msg 为空,则默认为 error 中的内容
func HasError(err error, msg string, code ...int) {
if err != nil {
statusCode := 200
if len(code) > 0 {
statusCode = code[0]
}
if msg == "" {
msg = err.Error()
}
panic("CustomError#" + strconv.Itoa(statusCode) + "#" + msg)
}
}
-7
View File
@@ -1,7 +0,0 @@
package utils
import "strconv"
func Float64ToString(e float64) string {
return strconv.FormatFloat(e, 'E', -1, 64)
}
-7
View File
@@ -1,7 +0,0 @@
package utils
import "strconv"
func IntToString(e int) string {
return strconv.Itoa(e)
}
-7
View File
@@ -1,7 +0,0 @@
package utils
import "strconv"
func Int64ToString(e int64) string {
return strconv.FormatInt(e, 10)
}
-33
View File
@@ -1,33 +0,0 @@
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"]
}
-55
View File
@@ -1,55 +0,0 @@
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
}
}
-22
View File
@@ -1,22 +0,0 @@
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
}
-62
View File
@@ -1,62 +0,0 @@
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
}