Skip to content

Commit 52df092

Browse files
committed
feat: add system api
1 parent b27b302 commit 52df092

File tree

151 files changed

+17756
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

151 files changed

+17756
-0
lines changed

common/captcha/captcha.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package captcha
2+
3+
import (
4+
"image/color"
5+
6+
"github.com/google/uuid"
7+
"github.com/mojocn/base64Captcha"
8+
)
9+
10+
// SetStore 设置store
11+
func SetStore(s base64Captcha.Store) {
12+
base64Captcha.DefaultMemStore = s
13+
}
14+
15+
// configJsonBody json request body.
16+
type configJsonBody struct {
17+
Id string
18+
CaptchaType string
19+
VerifyValue string
20+
DriverAudio *base64Captcha.DriverAudio
21+
DriverString *base64Captcha.DriverString
22+
DriverChinese *base64Captcha.DriverChinese
23+
DriverMath *base64Captcha.DriverMath
24+
DriverDigit *base64Captcha.DriverDigit
25+
}
26+
27+
func DriverStringFunc() (id, b64s string, err error) {
28+
e := configJsonBody{}
29+
e.Id = uuid.New().String()
30+
e.DriverString = base64Captcha.NewDriverString(46, 140, 2, 2, 4, "234567890abcdefghjkmnpqrstuvwxyz", &color.RGBA{240, 240, 246, 246}, nil, []string{"wqy-microhei.ttc"})
31+
driver := e.DriverString.ConvertFonts()
32+
cap := base64Captcha.NewCaptcha(driver, base64Captcha.DefaultMemStore)
33+
return cap.Generate()
34+
}
35+
36+
func DriverDigitFunc() (id, b64s string, err error) {
37+
e := configJsonBody{}
38+
e.Id = uuid.New().String()
39+
e.DriverDigit = base64Captcha.NewDriverDigit(80, 240, 4, 0.7, 80)
40+
driver := e.DriverDigit
41+
cap := base64Captcha.NewCaptcha(driver, base64Captcha.DefaultMemStore)
42+
return cap.Generate()
43+
}
44+
45+
// Verify 校验验证码
46+
func Verify(id, code string, clear bool) bool {
47+
return base64Captcha.DefaultMemStore.Verify(id, code, clear)
48+
}

common/cryptx/crypt.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package cryptx
2+
3+
import (
4+
"fmt"
5+
6+
"golang.org/x/crypto/scrypt"
7+
)
8+
9+
func PasswordEncrypt(salt, password string) string {
10+
dk, _ := scrypt.Key([]byte(password), []byte(salt), 32768, 8, 1, 32)
11+
return fmt.Sprintf("%x", string(dk))
12+
}

common/errorx/baseerror.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package errorx
2+
3+
import "github.com/yongxin/zen/common/globalkey"
4+
5+
type CodeError struct {
6+
Code int `json:"code"`
7+
Msg string `json:"msg"`
8+
}
9+
10+
type CodeErrorResponse struct {
11+
Code int `json:"code"`
12+
Msg string `json:"msg"`
13+
}
14+
15+
func NewCodeError(code int, msg string) error {
16+
return &CodeError{Code: code, Msg: msg}
17+
}
18+
19+
func NewDefaultError(code int) error {
20+
return NewCodeError(code, MapErrMsg(code))
21+
}
22+
23+
func NewHandlerError(code int, msg string) error {
24+
return NewCodeError(code, msg)
25+
}
26+
27+
func NewSystemError(code int, msg string) error {
28+
if globalkey.SysShowSystemError {
29+
return NewCodeError(code, msg)
30+
} else {
31+
return NewCodeError(code, MapErrMsg(code))
32+
}
33+
}
34+
35+
func (e *CodeError) Error() string {
36+
return e.Msg
37+
}
38+
39+
func (e *CodeError) Data() *CodeErrorResponse {
40+
return &CodeErrorResponse{
41+
Code: e.Code,
42+
Msg: e.Msg,
43+
}
44+
}

common/errorx/errormsg.go

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package errorx
2+
3+
var errorMsg map[int]string
4+
5+
const (
6+
ServerErrorCode = 1000
7+
ParamErrorCode = 1001
8+
CaptchaErrorCode = 1002
9+
AccountErrorCode = 1003
10+
PasswordErrorCode = 1004
11+
NotPermMenuErrorCode = 1005
12+
DeletePermMenuErrorCode = 1006
13+
ParentPermMenuErrorCode = 1007
14+
AddRoleErrorCode = 1008
15+
DeleteRoleErrorCode = 1009
16+
AddDeptErrorCode = 1010
17+
DeleteDeptErrorCode = 1011
18+
AddJobErrorCode = 1012
19+
DeleteJobErrorCode = 1013
20+
AddProfessionErrorCode = 1014
21+
DeleteProfessionErrorCode = 1015
22+
AddUserErrorCode = 1016
23+
DeptHasUserErrorCode = 1017
24+
RoleIsUsingErrorCode = 1018
25+
ParentRoleErrorCode = 1019
26+
ParentDeptErrorCode = 1020
27+
AccountDisableErrorCode = 1021
28+
SetParentIdErrorCode = 1022
29+
SetParentTypeErrorCode = 1023
30+
AddConfigErrorCode = 1024
31+
AddDictionaryErrorCode = 1025
32+
AuthErrorCode = 1026
33+
DeleteDictionaryErrorCode = 1027
34+
JobIsUsingErrorCode = 1028
35+
ProfessionIsUsingErrorCode = 1029
36+
ForbiddenErrorCode = 1030
37+
UpdateRoleUniqueKeyErrorCode = 1031
38+
UpdateDeptUniqueKeyErrorCode = 1032
39+
AssigningRolesErrorCode = 1033
40+
DeptIdErrorCode = 1034
41+
ProfessionIdErrorCode = 1035
42+
PostIdErrorCode = 1036
43+
ParentRoleIdErrorCode = 1037
44+
ParentDeptIdErrorCode = 1038
45+
ParentPermMenuIdErrorCode = 1039
46+
ParentDictionaryIdErrorCode = 1040
47+
DictionaryIdErrorCode = 1041
48+
PermMenuIdErrorCode = 1042
49+
RoleIdErrorCode = 1043
50+
UserIdErrorCode = 1044
51+
ConfigErrorCode = 1045
52+
)
53+
54+
func init() {
55+
errorMsg = make(map[int]string)
56+
errorMsg[ServerErrorCode] = "服务繁忙,请稍后重试"
57+
errorMsg[CaptchaErrorCode] = "验证码错误"
58+
errorMsg[AccountErrorCode] = "账号错误"
59+
errorMsg[PasswordErrorCode] = "密码错误"
60+
errorMsg[NotPermMenuErrorCode] = "权限不足"
61+
errorMsg[DeletePermMenuErrorCode] = "该权限菜单存在子级权限菜单"
62+
errorMsg[ParentPermMenuErrorCode] = "父级菜单不能为自己"
63+
errorMsg[AddRoleErrorCode] = "角色已存在"
64+
errorMsg[DeleteRoleErrorCode] = "该角色存在子角色"
65+
errorMsg[AddDeptErrorCode] = "部门已存在"
66+
errorMsg[DeleteDeptErrorCode] = "该部门存在子部门"
67+
errorMsg[AddJobErrorCode] = "岗位已存在"
68+
errorMsg[DeleteJobErrorCode] = "该岗位正在使用中"
69+
errorMsg[AddProfessionErrorCode] = "职称已存在"
70+
errorMsg[DeleteProfessionErrorCode] = "该职称正在使用中"
71+
errorMsg[AddUserErrorCode] = "账号已存在"
72+
errorMsg[DeptHasUserErrorCode] = "该部门正在使用中"
73+
errorMsg[RoleIsUsingErrorCode] = "该角色正在使用中"
74+
errorMsg[ParentRoleErrorCode] = "父级角色不能为自己"
75+
errorMsg[ParentDeptErrorCode] = "父级部门不能为自己"
76+
errorMsg[AccountDisableErrorCode] = "账号已禁用"
77+
errorMsg[SetParentIdErrorCode] = "不能设置子级为自己的父级"
78+
errorMsg[SetParentTypeErrorCode] = "权限类型不能作为父级菜单"
79+
errorMsg[AddConfigErrorCode] = "配置已存在"
80+
errorMsg[AddDictionaryErrorCode] = "字典已存在"
81+
errorMsg[AuthErrorCode] = "授权已失效,请重新登录"
82+
errorMsg[DeleteDictionaryErrorCode] = "该字典集存在配置项"
83+
errorMsg[JobIsUsingErrorCode] = "该岗位正在使用中"
84+
errorMsg[ProfessionIsUsingErrorCode] = "该职称正在使用中"
85+
errorMsg[ForbiddenErrorCode] = "禁止操作"
86+
errorMsg[UpdateRoleUniqueKeyErrorCode] = "角色标识已存在"
87+
errorMsg[UpdateDeptUniqueKeyErrorCode] = "部门标识已存在"
88+
errorMsg[AssigningRolesErrorCode] = "角色不在可控范围"
89+
errorMsg[DeptIdErrorCode] = "部门不存在"
90+
errorMsg[ProfessionIdErrorCode] = "职称不存在"
91+
errorMsg[PostIdErrorCode] = "岗位不存在"
92+
errorMsg[ParentRoleIdErrorCode] = "父级角色不存在"
93+
errorMsg[ParentDeptIdErrorCode] = "父级部门不存在"
94+
errorMsg[ParentPermMenuIdErrorCode] = "父级菜单不存在"
95+
errorMsg[ParentDictionaryIdErrorCode] = "字典集不存在"
96+
errorMsg[DictionaryIdErrorCode] = "字典不存在"
97+
errorMsg[PermMenuIdErrorCode] = "权限菜单不存在"
98+
errorMsg[RoleIdErrorCode] = "角色不存在"
99+
errorMsg[UserIdErrorCode] = "用户不存在"
100+
errorMsg[ConfigErrorCode] = "配置不存在"
101+
}
102+
103+
func MapErrMsg(errCode int) string {
104+
if msg, ok := errorMsg[errCode]; ok {
105+
return msg
106+
} else {
107+
return "服务繁忙,请稍后重试"
108+
}
109+
}

common/globalkey/core.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package globalkey
2+
3+
const (
4+
SysPermMenuPrefix = "/admin/"
5+
SysJwtUserId = "userId"
6+
SysPermMenuCachePrefix = "cache:rms:permmenu:"
7+
SysOnlineUserCachePrefix = "cache:rms:online:"
8+
SysLoginCaptchaCachePrefix = "cache:rms:captcha:"
9+
SysUserIdCachePrefix = "cache:rms:sysuser:id:"
10+
SysDateFormat = "2006-01-02 15:04:05"
11+
SysNewUserDefaultPassword = "123456"
12+
SysShowSystemError = true
13+
SysProtectPermMenuMaxId = 44
14+
SysProtectDictionaryMaxId = 4
15+
SysSuperUserId = 1
16+
SysSuperRoleId = 1
17+
SysDefaultPermType = 2
18+
SysEnable = 1
19+
SysDisable = 0
20+
SysLoginLogType = 1
21+
SysTopParentId = 0
22+
)

common/jwtx/jwt.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package jwtx
2+
3+
import "github.com/golang-jwt/jwt"
4+
5+
func GetToken(secretKey string, iat, seconds, userId int64) (string, error) {
6+
claims := make(jwt.MapClaims)
7+
claims["exp"] = iat + seconds
8+
claims["iat"] = iat
9+
claims["userId"] = userId
10+
token := jwt.New(jwt.SigningMethodHS256)
11+
token.Claims = claims
12+
return token.SignedString([]byte(secretKey))
13+
}

common/response/response.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package response
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/zeromicro/go-zero/rest/httpx"
7+
)
8+
9+
type Body struct {
10+
Code int `json:"code"`
11+
Msg string `json:"msg"`
12+
Data interface{} `json:"data,omitempty"`
13+
}
14+
15+
func Response(w http.ResponseWriter, resp interface{}, err error) {
16+
var body Body
17+
if err != nil {
18+
body.Code = 0
19+
body.Msg = err.Error()
20+
} else {
21+
body.Code = 200
22+
body.Msg = "success"
23+
body.Data = resp
24+
}
25+
httpx.OkJson(w, body)
26+
}

0 commit comments

Comments
 (0)