feat/modules: 更新模块引用并添加 Redis 配置
- 更新 go.work 文件,添加 modules 引用 - 修改 logic/main.go,增加 Redis 模式监听 - 更新 login/main.go,引入 modules 模块 - 修改 manifest/config/config.yaml,添加 Redis 配置信息
This commit is contained in:
@@ -1,240 +1,251 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"blazing/cool"
|
||||
|
||||
"blazing/modules/base/model"
|
||||
|
||||
"github.com/gogf/gf/v2/container/garray"
|
||||
"github.com/gogf/gf/v2/container/gset"
|
||||
"github.com/gogf/gf/v2/crypto/gmd5"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
type BaseSysUserService struct {
|
||||
*cool.Service
|
||||
}
|
||||
|
||||
// Person 方法 返回不带密码的用户信息
|
||||
func (s *BaseSysUserService) Person(userId uint) (res gdb.Record, err error) {
|
||||
m := cool.DBM(s.Model)
|
||||
res, err = m.Where("id = ?", userId).FieldsEx("password").One()
|
||||
return
|
||||
}
|
||||
|
||||
func (s *BaseSysUserService) ModifyBefore(ctx context.Context, method string, param g.MapStrAny) (err error) {
|
||||
if method == "Delete" {
|
||||
// 禁止删除超级管理员
|
||||
userIds := garray.NewIntArrayFrom(gconv.Ints(param["ids"]))
|
||||
currentId, found := userIds.Get(0)
|
||||
superAdminId := 1
|
||||
|
||||
if userIds.Len() == 1 && found && currentId == superAdminId {
|
||||
err = gerror.New("超级管理员不能删除")
|
||||
return
|
||||
}
|
||||
|
||||
// 删除超级管理员
|
||||
userIds.RemoveValue(1)
|
||||
g.RequestFromCtx(ctx).SetParam("ids", userIds.Slice())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *BaseSysUserService) ModifyAfter(ctx context.Context, method string, param g.MapStrAny) (err error) {
|
||||
if method == "Delete" {
|
||||
userIds := garray.NewIntArrayFrom(gconv.Ints(param["ids"]))
|
||||
userIds.RemoveValue(1)
|
||||
// 删除用户时删除相关数据
|
||||
cool.DBM(model.NewBaseSysUserRole()).WhereIn("userId", userIds.Slice()).Delete()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// ServiceAdd 方法 添加用户
|
||||
func (s *BaseSysUserService) ServiceAdd(ctx context.Context, req *cool.AddReq) (data interface{}, err error) {
|
||||
var (
|
||||
m = cool.DBM(s.Model)
|
||||
r = g.RequestFromCtx(ctx)
|
||||
reqmap = r.GetMap()
|
||||
)
|
||||
// 如果reqmap["password"]不为空,则对密码进行md5加密
|
||||
if !r.Get("password").IsNil() {
|
||||
reqmap["password"] = gmd5.MustEncryptString(r.Get("password").String())
|
||||
}
|
||||
lastInsertId, err := m.Data(reqmap).InsertAndGetId()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
data = g.Map{"id": lastInsertId}
|
||||
return
|
||||
}
|
||||
|
||||
// ServiceInfo 方法 返回服务信息
|
||||
func (s *BaseSysUserService) ServiceInfo(ctx g.Ctx, req *cool.InfoReq) (data interface{}, err error) {
|
||||
result, err := s.Service.ServiceInfo(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if result.(gdb.Record).IsEmpty() {
|
||||
return nil, nil
|
||||
}
|
||||
// g.DumpWithType(result)
|
||||
resultMap := result.(gdb.Record).Map()
|
||||
|
||||
// 获取角色
|
||||
roleIds, err := cool.DBM(model.NewBaseSysUserRole()).Where("userId = ?", resultMap["id"]).Fields("roleId").Array()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resultMap["roleIdList"] = roleIds
|
||||
data = resultMap
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// ServiceUpdate 方法 更新用户信息
|
||||
func (s *BaseSysUserService) ServiceUpdate(ctx context.Context, req *cool.UpdateReq) (data interface{}, err error) {
|
||||
var (
|
||||
admin = cool.GetAdmin(ctx)
|
||||
m = cool.DBM(s.Model)
|
||||
)
|
||||
|
||||
r := g.RequestFromCtx(ctx)
|
||||
rMap := r.GetMap()
|
||||
|
||||
// 如果不传入ID代表更新当前用户
|
||||
userId := r.Get("id", admin.UserId).Uint()
|
||||
userInfo, err := m.Where("id = ?", userId).One()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if userInfo.IsEmpty() {
|
||||
err = gerror.New("用户不存在")
|
||||
return
|
||||
}
|
||||
|
||||
// 禁止禁用超级管理员
|
||||
if userId == 1 && (!r.Get("status").IsNil() && r.Get("status").Int() == 0) {
|
||||
err = gerror.New("禁止禁用超级管理员")
|
||||
return
|
||||
}
|
||||
|
||||
// 如果请求的password不为空并且密码加密后的值有变动,说明要修改密码
|
||||
var rPassword = r.Get("password", "").String()
|
||||
if rPassword != "" && rPassword != userInfo["password"].String() {
|
||||
rMap["password"], _ = gmd5.Encrypt(rPassword)
|
||||
rMap["passwordV"] = userInfo["passwordV"].Int() + 1
|
||||
cool.CacheManager.Set(ctx, fmt.Sprintf("admin:passwordVersion:%d", userId), rMap["passwordV"], 0)
|
||||
} else {
|
||||
delete(rMap, "password")
|
||||
}
|
||||
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
||||
roleModel := cool.DBM(model.NewBaseSysUserRole()).TX(tx).Where("userId = ?", userId)
|
||||
roleIds, err := roleModel.Fields("roleId").Array()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 如果请求参数中不包含roleIdList说明不修改角色信息
|
||||
if !r.Get("roleIdList").IsNil() {
|
||||
inRoleIdSet := gset.NewFrom(r.Get("roleIdList").Ints())
|
||||
roleIdsSet := gset.NewFrom(gconv.Ints(roleIds))
|
||||
|
||||
// 如果请求的角色信息未发生变化则跳过更新逻辑
|
||||
if roleIdsSet.Diff(inRoleIdSet).Size() != 0 || inRoleIdSet.Diff(roleIdsSet).Size() != 0 {
|
||||
roleArray := garray.NewArray()
|
||||
inRoleIdSet.Iterator(func(v interface{}) bool {
|
||||
roleArray.PushRight(g.Map{
|
||||
"userId": gconv.Uint(userId),
|
||||
"roleId": gconv.Uint(v),
|
||||
})
|
||||
return true
|
||||
})
|
||||
|
||||
_, err = roleModel.Delete()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = roleModel.Fields("userId,roleId").Insert(roleArray)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_, err = m.TX(tx).Update(rMap)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Move 移动用户部门
|
||||
func (s *BaseSysUserService) Move(ctx g.Ctx) (err error) {
|
||||
request := g.RequestFromCtx(ctx)
|
||||
departmentId := request.Get("departmentId").Int()
|
||||
userIds := request.Get("userIds").Slice()
|
||||
|
||||
_, err = cool.DBM(s.Model).Where("`id` IN(?)", userIds).Data(g.Map{"departmentId": departmentId}).Update()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// NewBaseSysUserService 创建一个新的BaseSysUserService实例
|
||||
func NewBaseSysUserService() *BaseSysUserService {
|
||||
return &BaseSysUserService{
|
||||
Service: &cool.Service{
|
||||
Model: model.NewBaseSysUser(),
|
||||
InfoIgnoreProperty: "password",
|
||||
UniqueKey: map[string]string{
|
||||
"username": "用户名不能重复",
|
||||
},
|
||||
PageQueryOp: &cool.QueryOp{
|
||||
Select: "base_sys_user.*,dept.`name` as departmentName,GROUP_CONCAT( role.`name` ) AS `roleName`",
|
||||
Join: []*cool.JoinOp{
|
||||
{
|
||||
Model: model.NewBaseSysDepartment(),
|
||||
Alias: "dept",
|
||||
Type: "LeftJoin",
|
||||
Condition: "`base_sys_user`.`departmentId` = `dept`.`id`",
|
||||
},
|
||||
{
|
||||
Model: model.NewBaseSysUserRole(),
|
||||
Alias: "user_role",
|
||||
Type: "LeftJoin",
|
||||
Condition: "`base_sys_user`.`id` = `user_role`.`userId`",
|
||||
},
|
||||
{
|
||||
Model: model.NewBaseSysRole(),
|
||||
Alias: "`role`",
|
||||
Type: "LeftJoin",
|
||||
Condition: "`role`.`id` = `user_role`.`roleId`",
|
||||
},
|
||||
},
|
||||
Where: func(ctx context.Context) []g.Array {
|
||||
r := g.RequestFromCtx(ctx).GetMap()
|
||||
return []g.Array{
|
||||
{"(departmentId IN (?))", gconv.SliceStr(r["departmentIds"])},
|
||||
}
|
||||
},
|
||||
Extend: func(ctx g.Ctx, m *gdb.Model) *gdb.Model {
|
||||
return m.Group("`base_sys_user`.`id`")
|
||||
},
|
||||
KeyWordField: []string{"name", "username", "nickName"},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"blazing/cool"
|
||||
|
||||
"blazing/modules/base/model"
|
||||
|
||||
"github.com/gogf/gf/v2/container/garray"
|
||||
"github.com/gogf/gf/v2/container/gset"
|
||||
"github.com/gogf/gf/v2/crypto/gmd5"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
type BaseSysUserService struct {
|
||||
*cool.Service
|
||||
}
|
||||
|
||||
// Person 方法 返回不带密码的用户信息
|
||||
func (s *BaseSysUserService) Person(userId uint) (res gdb.Record, err error) {
|
||||
m := cool.DBM(s.Model)
|
||||
res, err = m.Where("id = ?", userId).FieldsEx("password").One()
|
||||
return
|
||||
}
|
||||
func (s *BaseSysUserService) GetSession(email string, password string) (res *model.BaseSysUser, err error) {
|
||||
m := cool.DBM(s.Model)
|
||||
|
||||
m.Where("email = ?", email).Where("password=?", password).Where("status=?", 1).Scan(&res)
|
||||
if res == nil {
|
||||
err = gerror.New("账户或密码不正确~")
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
func (s *BaseSysUserService) ModifyBefore(ctx context.Context, method string, param g.MapStrAny) (err error) {
|
||||
if method == "Delete" {
|
||||
// 禁止删除超级管理员
|
||||
userIds := garray.NewIntArrayFrom(gconv.Ints(param["ids"]))
|
||||
currentId, found := userIds.Get(0)
|
||||
superAdminId := 1
|
||||
|
||||
if userIds.Len() == 1 && found && currentId == superAdminId {
|
||||
err = gerror.New("超级管理员不能删除")
|
||||
return
|
||||
}
|
||||
|
||||
// 删除超级管理员
|
||||
userIds.RemoveValue(1)
|
||||
g.RequestFromCtx(ctx).SetParam("ids", userIds.Slice())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *BaseSysUserService) ModifyAfter(ctx context.Context, method string, param g.MapStrAny) (err error) {
|
||||
if method == "Delete" {
|
||||
userIds := garray.NewIntArrayFrom(gconv.Ints(param["ids"]))
|
||||
userIds.RemoveValue(1)
|
||||
// 删除用户时删除相关数据
|
||||
cool.DBM(model.NewBaseSysUserRole()).WhereIn("userId", userIds.Slice()).Delete()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// ServiceAdd 方法 添加用户
|
||||
func (s *BaseSysUserService) ServiceAdd(ctx context.Context, req *cool.AddReq) (data interface{}, err error) {
|
||||
var (
|
||||
m = cool.DBM(s.Model)
|
||||
r = g.RequestFromCtx(ctx)
|
||||
reqmap = r.GetMap()
|
||||
)
|
||||
// 如果reqmap["password"]不为空,则对密码进行md5加密
|
||||
if !r.Get("password").IsNil() {
|
||||
reqmap["password"] = gmd5.MustEncryptString(r.Get("password").String())
|
||||
}
|
||||
lastInsertId, err := m.Data(reqmap).InsertAndGetId()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
data = g.Map{"id": lastInsertId}
|
||||
return
|
||||
}
|
||||
|
||||
// ServiceInfo 方法 返回服务信息
|
||||
func (s *BaseSysUserService) ServiceInfo(ctx g.Ctx, req *cool.InfoReq) (data interface{}, err error) {
|
||||
result, err := s.Service.ServiceInfo(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if result.(gdb.Record).IsEmpty() {
|
||||
return nil, nil
|
||||
}
|
||||
// g.DumpWithType(result)
|
||||
resultMap := result.(gdb.Record).Map()
|
||||
|
||||
// 获取角色
|
||||
roleIds, err := cool.DBM(model.NewBaseSysUserRole()).Where("userId = ?", resultMap["id"]).Fields("roleId").Array()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resultMap["roleIdList"] = roleIds
|
||||
data = resultMap
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// ServiceUpdate 方法 更新用户信息
|
||||
func (s *BaseSysUserService) ServiceUpdate(ctx context.Context, req *cool.UpdateReq) (data interface{}, err error) {
|
||||
var (
|
||||
admin = cool.GetAdmin(ctx)
|
||||
m = cool.DBM(s.Model)
|
||||
)
|
||||
|
||||
r := g.RequestFromCtx(ctx)
|
||||
rMap := r.GetMap()
|
||||
|
||||
// 如果不传入ID代表更新当前用户
|
||||
userId := r.Get("id", admin.UserId).Uint()
|
||||
userInfo, err := m.Where("id = ?", userId).One()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if userInfo.IsEmpty() {
|
||||
err = gerror.New("用户不存在")
|
||||
return
|
||||
}
|
||||
|
||||
// 禁止禁用超级管理员
|
||||
if userId == 1 && (!r.Get("status").IsNil() && r.Get("status").Int() == 0) {
|
||||
err = gerror.New("禁止禁用超级管理员")
|
||||
return
|
||||
}
|
||||
|
||||
// 如果请求的password不为空并且密码加密后的值有变动,说明要修改密码
|
||||
var rPassword = r.Get("password", "").String()
|
||||
if rPassword != "" && rPassword != userInfo["password"].String() {
|
||||
rMap["password"], _ = gmd5.Encrypt(rPassword)
|
||||
rMap["passwordV"] = userInfo["passwordV"].Int() + 1
|
||||
cool.CacheManager.Set(ctx, fmt.Sprintf("admin:passwordVersion:%d", userId), rMap["passwordV"], 0)
|
||||
} else {
|
||||
delete(rMap, "password")
|
||||
}
|
||||
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
||||
roleModel := cool.DBM(model.NewBaseSysUserRole()).TX(tx).Where("userId = ?", userId)
|
||||
roleIds, err := roleModel.Fields("roleId").Array()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 如果请求参数中不包含roleIdList说明不修改角色信息
|
||||
if !r.Get("roleIdList").IsNil() {
|
||||
inRoleIdSet := gset.NewFrom(r.Get("roleIdList").Ints())
|
||||
roleIdsSet := gset.NewFrom(gconv.Ints(roleIds))
|
||||
|
||||
// 如果请求的角色信息未发生变化则跳过更新逻辑
|
||||
if roleIdsSet.Diff(inRoleIdSet).Size() != 0 || inRoleIdSet.Diff(roleIdsSet).Size() != 0 {
|
||||
roleArray := garray.NewArray()
|
||||
inRoleIdSet.Iterator(func(v interface{}) bool {
|
||||
roleArray.PushRight(g.Map{
|
||||
"userId": gconv.Uint(userId),
|
||||
"roleId": gconv.Uint(v),
|
||||
})
|
||||
return true
|
||||
})
|
||||
|
||||
_, err = roleModel.Delete()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = roleModel.Fields("userId,roleId").Insert(roleArray)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_, err = m.TX(tx).Update(rMap)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Move 移动用户部门
|
||||
func (s *BaseSysUserService) Move(ctx g.Ctx) (err error) {
|
||||
request := g.RequestFromCtx(ctx)
|
||||
departmentId := request.Get("departmentId").Int()
|
||||
userIds := request.Get("userIds").Slice()
|
||||
|
||||
_, err = cool.DBM(s.Model).Where("`id` IN(?)", userIds).Data(g.Map{"departmentId": departmentId}).Update()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// NewBaseSysUserService 创建一个新的BaseSysUserService实例
|
||||
func NewBaseSysUserService() *BaseSysUserService {
|
||||
return &BaseSysUserService{
|
||||
Service: &cool.Service{
|
||||
Model: model.NewBaseSysUser(),
|
||||
InfoIgnoreProperty: "password",
|
||||
UniqueKey: map[string]string{
|
||||
// "username": "用户名不能重复",
|
||||
"email": "邮箱不能重复",
|
||||
},
|
||||
PageQueryOp: &cool.QueryOp{
|
||||
Select: "base_sys_user.*,dept.`name` as departmentName,GROUP_CONCAT( role.`name` ) AS `roleName`",
|
||||
Join: []*cool.JoinOp{
|
||||
{
|
||||
Model: model.NewBaseSysDepartment(),
|
||||
Alias: "dept",
|
||||
Type: "LeftJoin",
|
||||
Condition: "`base_sys_user`.`departmentId` = `dept`.`id`",
|
||||
},
|
||||
{
|
||||
Model: model.NewBaseSysUserRole(),
|
||||
Alias: "user_role",
|
||||
Type: "LeftJoin",
|
||||
Condition: "`base_sys_user`.`id` = `user_role`.`userId`",
|
||||
},
|
||||
{
|
||||
Model: model.NewBaseSysRole(),
|
||||
Alias: "`role`",
|
||||
Type: "LeftJoin",
|
||||
Condition: "`role`.`id` = `user_role`.`roleId`",
|
||||
},
|
||||
},
|
||||
Where: func(ctx context.Context) []g.Array {
|
||||
r := g.RequestFromCtx(ctx).GetMap()
|
||||
return []g.Array{
|
||||
{"(departmentId IN (?))", gconv.SliceStr(r["departmentIds"])},
|
||||
}
|
||||
},
|
||||
Extend: func(ctx g.Ctx, m *gdb.Model) *gdb.Model {
|
||||
return m.Group("`base_sys_user`.`id`")
|
||||
},
|
||||
KeyWordField: []string{"name", "username", "nickName"},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
76
modules/blazing/controller/admin/login.go
Normal file
76
modules/blazing/controller/admin/login.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
baseservice "blazing/modules/base/service"
|
||||
"blazing/modules/blazing/service"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
type SessionReq struct {
|
||||
g.Meta `path:"/getSessionByAuth" method:"GET"`
|
||||
Email string `json:"email" v:"required|email"`
|
||||
Password string `json:"password" v:"required"`
|
||||
}
|
||||
type SessionRes struct {
|
||||
Msg string `json:"msg"`
|
||||
Code int `json:"code"`
|
||||
Session string `json:"session"`
|
||||
}
|
||||
|
||||
func (c *BlazingController) GetSession(ctx context.Context, req *SessionReq) (res *SessionRes, err error) {
|
||||
// res = &DemoSampleWelcomeRes{
|
||||
// BaseRes: cool.Ok("Welcome to Cool Admin Go"),
|
||||
// Data: gjson.New(`{"name": "Cool Admin Go", "age":0}`),
|
||||
// }
|
||||
res = &SessionRes{
|
||||
Msg: "success",
|
||||
Code: 200,
|
||||
Session: ""}
|
||||
if err := g.Validator().Data(req).Run(ctx); err != nil {
|
||||
fmt.Println(err)
|
||||
res.Code = 400
|
||||
res.Msg = err.Error()
|
||||
|
||||
}
|
||||
res1, err := baseservice.NewBaseSysUserService().GetSession(req.Email, req.Password)
|
||||
|
||||
if err != nil {
|
||||
res.Code = 400
|
||||
res.Msg = err.Error()
|
||||
}
|
||||
|
||||
accountID := res1.ID
|
||||
|
||||
// 生成SID
|
||||
sidInfo := fmt.Sprintf("%d%d", accountID, time.Now().UnixNano())
|
||||
hash := sha256.Sum256([]byte(sidInfo))
|
||||
sidByte := hex.EncodeToString(hash[:])
|
||||
|
||||
// 确保长度为48字符
|
||||
if len(sidByte) < 48 {
|
||||
sidByte = sidByte + strings.Repeat("0", 48-len(sidByte))
|
||||
} else {
|
||||
sidByte = sidByte[:48]
|
||||
}
|
||||
|
||||
// UID拼接SID
|
||||
hex8 := fmt.Sprintf("%08X", int(accountID&0xFFFFFFFF))
|
||||
res.Session = hex8 + sidByte[8:]
|
||||
|
||||
// 保存后端校验的SID
|
||||
backendSID := res.Session[8 : len(res.Session)-8]
|
||||
if err := service.NewLoginServiceService().SaveSessionId(backendSID, gconv.String(accountID)); err != nil {
|
||||
res.Code = 400
|
||||
res.Msg = err.Error()
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
55
modules/blazing/controller/admin/reg.go
Normal file
55
modules/blazing/controller/admin/reg.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"blazing/cool"
|
||||
|
||||
"blazing/modules/blazing/service"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
type BlazingController struct {
|
||||
*cool.Controller
|
||||
}
|
||||
|
||||
func init() {
|
||||
var demo_sample_controller = &BlazingController{
|
||||
&cool.Controller{
|
||||
Prefix: "/seer/game",
|
||||
Api: []string{},
|
||||
Service: service.NewDemoSampleService(),
|
||||
},
|
||||
}
|
||||
// 注册路由
|
||||
cool.RegisterController(demo_sample_controller)
|
||||
}
|
||||
|
||||
// 增加 Welcome 演示 方法
|
||||
type RegReq struct {
|
||||
g.Meta `path:"/reg" method:"POST"`
|
||||
Email string `json:"email" v:"required|email"`
|
||||
Password string `json:"password" v:"required"`
|
||||
}
|
||||
type RegRes struct {
|
||||
*cool.BaseRes
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
func (c *BlazingController) Reg(ctx context.Context, req *RegReq) (res *cool.BaseRes, err error) {
|
||||
// res = &DemoSampleWelcomeRes{
|
||||
// BaseRes: cool.Ok("Welcome to Cool Admin Go"),
|
||||
// Data: gjson.New(`{"name": "Cool Admin Go", "age":0}`),
|
||||
// }
|
||||
if err := g.Validator().Data(req).Run(ctx); err != nil {
|
||||
fmt.Println(err)
|
||||
res = cool.Ok(err)
|
||||
|
||||
} else {
|
||||
res = cool.Ok("注册成功")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
5
modules/blazing/controller/controller.go
Normal file
5
modules/blazing/controller/controller.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
_ "blazing/modules/blazing/controller/admin"
|
||||
)
|
||||
7
modules/blazing/demo.go
Normal file
7
modules/blazing/demo.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
_ "blazing/modules/blazing/controller"
|
||||
_ "blazing/modules/blazing/model"
|
||||
_ "blazing/modules/blazing/service"
|
||||
)
|
||||
@@ -1,38 +1,38 @@
|
||||
module blazing/modules/demo
|
||||
|
||||
go 1.18
|
||||
|
||||
require github.com/gogf/gf/v2 v2.6.3
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||
github.com/clbanning/mxj/v2 v2.7.0 // indirect
|
||||
github.com/fatih/color v1.16.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/go-logr/logr v1.4.1 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
|
||||
github.com/gorilla/websocket v1.5.1 // indirect
|
||||
github.com/grokify/html-strip-tags-go v0.1.0 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/kr/pretty v0.1.0 // indirect
|
||||
github.com/kr/text v0.2.0 // indirect
|
||||
github.com/magiconair/properties v1.8.7 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.15 // indirect
|
||||
github.com/olekukonko/tablewriter v0.0.5 // indirect
|
||||
github.com/rivo/uniseg v0.4.7 // indirect
|
||||
github.com/rogpeppe/go-internal v1.6.1 // indirect
|
||||
go.opentelemetry.io/otel v1.24.0 // indirect
|
||||
go.opentelemetry.io/otel/metric v1.24.0 // indirect
|
||||
go.opentelemetry.io/otel/sdk v1.24.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.24.0 // indirect
|
||||
golang.org/x/net v0.21.0 // indirect
|
||||
golang.org/x/sys v0.17.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
gorm.io/gorm v1.25.7 // indirect
|
||||
)
|
||||
module blazing/modules/blazing
|
||||
|
||||
go 1.18
|
||||
|
||||
require github.com/gogf/gf/v2 v2.6.3
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||
github.com/clbanning/mxj/v2 v2.7.0 // indirect
|
||||
github.com/fatih/color v1.16.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/go-logr/logr v1.4.1 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
|
||||
github.com/gorilla/websocket v1.5.1 // indirect
|
||||
github.com/grokify/html-strip-tags-go v0.1.0 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/kr/pretty v0.1.0 // indirect
|
||||
github.com/kr/text v0.2.0 // indirect
|
||||
github.com/magiconair/properties v1.8.7 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.15 // indirect
|
||||
github.com/olekukonko/tablewriter v0.0.5 // indirect
|
||||
github.com/rivo/uniseg v0.4.7 // indirect
|
||||
github.com/rogpeppe/go-internal v1.6.1 // indirect
|
||||
go.opentelemetry.io/otel v1.24.0 // indirect
|
||||
go.opentelemetry.io/otel/metric v1.24.0 // indirect
|
||||
go.opentelemetry.io/otel/sdk v1.24.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.24.0 // indirect
|
||||
golang.org/x/net v0.21.0 // indirect
|
||||
golang.org/x/sys v0.17.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
gorm.io/gorm v1.25.7 // indirect
|
||||
)
|
||||
@@ -1,35 +1,35 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"blazing/cool"
|
||||
)
|
||||
|
||||
const TableNameDemoGoods = "demo_goods"
|
||||
|
||||
// DemoGoods mapped from table <demo_goods>
|
||||
type DemoGoods struct {
|
||||
*cool.Model
|
||||
Name string `gorm:"not null" json:"name"`
|
||||
}
|
||||
|
||||
// TableName DemoGoods's table name
|
||||
func (*DemoGoods) TableName() string {
|
||||
return TableNameDemoGoods
|
||||
}
|
||||
|
||||
// GroupName DemoGoods's table group
|
||||
func (*DemoGoods) GroupName() string {
|
||||
return "default"
|
||||
}
|
||||
|
||||
// NewDemoGoods create a new DemoGoods
|
||||
func NewDemoGoods() *DemoGoods {
|
||||
return &DemoGoods{
|
||||
Model: cool.NewModel(),
|
||||
}
|
||||
}
|
||||
|
||||
// init 创建表
|
||||
func init() {
|
||||
cool.CreateTable(&DemoGoods{})
|
||||
}
|
||||
package model
|
||||
|
||||
import (
|
||||
"blazing/cool"
|
||||
)
|
||||
|
||||
const TableNameDemoGoods = "demo_goods"
|
||||
|
||||
// DemoGoods mapped from table <demo_goods>
|
||||
type DemoGoods struct {
|
||||
*cool.Model
|
||||
Name string `gorm:"not null" json:"name"`
|
||||
}
|
||||
|
||||
// TableName DemoGoods's table name
|
||||
func (*DemoGoods) TableName() string {
|
||||
return TableNameDemoGoods
|
||||
}
|
||||
|
||||
// GroupName DemoGoods's table group
|
||||
func (*DemoGoods) GroupName() string {
|
||||
return "default"
|
||||
}
|
||||
|
||||
// NewDemoGoods create a new DemoGoods
|
||||
func NewDemoGoods() *DemoGoods {
|
||||
return &DemoGoods{
|
||||
Model: cool.NewModel(),
|
||||
}
|
||||
}
|
||||
|
||||
// init 创建表
|
||||
func init() {
|
||||
cool.CreateTable(&DemoGoods{})
|
||||
}
|
||||
@@ -1,35 +1,35 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"blazing/cool"
|
||||
)
|
||||
|
||||
const TableNameDemoSample = "demo_sample"
|
||||
|
||||
// DemoSample mapped from table <demo_sample>
|
||||
type DemoSample struct {
|
||||
*cool.Model
|
||||
// Name string `gorm:"column:name;not null;comment:名称" json:"name"`
|
||||
}
|
||||
|
||||
// TableName DemoSample's table name
|
||||
func (*DemoSample) TableName() string {
|
||||
return TableNameDemoSample
|
||||
}
|
||||
|
||||
// GroupName DemoSample's table group
|
||||
func (*DemoSample) GroupName() string {
|
||||
return "default"
|
||||
}
|
||||
|
||||
// NewDemoSample create a new DemoSample
|
||||
func NewDemoSample() *DemoSample {
|
||||
return &DemoSample{
|
||||
Model: cool.NewModel(),
|
||||
}
|
||||
}
|
||||
|
||||
// init 创建表
|
||||
func init() {
|
||||
cool.CreateTable(&DemoSample{})
|
||||
}
|
||||
package model
|
||||
|
||||
import (
|
||||
"blazing/cool"
|
||||
)
|
||||
|
||||
const TableNameDemoSample = "demo_sample"
|
||||
|
||||
// DemoSample mapped from table <demo_sample>
|
||||
type DemoSample struct {
|
||||
*cool.Model
|
||||
// Name string `gorm:"column:name;not null;comment:名称" json:"name"`
|
||||
}
|
||||
|
||||
// TableName DemoSample's table name
|
||||
func (*DemoSample) TableName() string {
|
||||
return TableNameDemoSample
|
||||
}
|
||||
|
||||
// GroupName DemoSample's table group
|
||||
func (*DemoSample) GroupName() string {
|
||||
return "default"
|
||||
}
|
||||
|
||||
// NewDemoSample create a new DemoSample
|
||||
func NewDemoSample() *DemoSample {
|
||||
return &DemoSample{
|
||||
Model: cool.NewModel(),
|
||||
}
|
||||
}
|
||||
|
||||
// init 创建表
|
||||
func init() {
|
||||
cool.CreateTable(&DemoSample{})
|
||||
}
|
||||
@@ -1,23 +1,23 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"blazing/cool"
|
||||
|
||||
"blazing/modules/demo/model"
|
||||
)
|
||||
|
||||
type DemoGoodsService struct {
|
||||
*cool.Service
|
||||
}
|
||||
|
||||
func NewDemoGoodsService() *DemoGoodsService {
|
||||
return &DemoGoodsService{
|
||||
&cool.Service{
|
||||
Model: model.NewDemoGoods(),
|
||||
ListQueryOp: &cool.QueryOp{
|
||||
|
||||
Join: []*cool.JoinOp{},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
package service
|
||||
|
||||
import (
|
||||
"blazing/cool"
|
||||
|
||||
"blazing/modules/blazing/model"
|
||||
)
|
||||
|
||||
type DemoGoodsService struct {
|
||||
*cool.Service
|
||||
}
|
||||
|
||||
func NewDemoGoodsService() *DemoGoodsService {
|
||||
return &DemoGoodsService{
|
||||
&cool.Service{
|
||||
Model: model.NewDemoGoods(),
|
||||
ListQueryOp: &cool.QueryOp{
|
||||
|
||||
Join: []*cool.JoinOp{},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,19 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"blazing/cool"
|
||||
|
||||
"blazing/modules/demo/model"
|
||||
)
|
||||
|
||||
type DemoSampleService struct {
|
||||
*cool.Service
|
||||
}
|
||||
|
||||
func NewDemoSampleService() *DemoSampleService {
|
||||
return &DemoSampleService{
|
||||
&cool.Service{
|
||||
Model: model.NewDemoSample(),
|
||||
},
|
||||
}
|
||||
}
|
||||
package service
|
||||
|
||||
import (
|
||||
"blazing/cool"
|
||||
|
||||
"blazing/modules/blazing/model"
|
||||
)
|
||||
|
||||
type DemoSampleService struct {
|
||||
*cool.Service
|
||||
}
|
||||
|
||||
func NewDemoSampleService() *DemoSampleService {
|
||||
return &DemoSampleService{
|
||||
&cool.Service{
|
||||
Model: model.NewDemoSample(),
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,21 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"blazing/cool"
|
||||
)
|
||||
|
||||
type DemoTestService struct {
|
||||
*cool.Service
|
||||
}
|
||||
|
||||
func NewDemoTestService() *DemoTestService {
|
||||
return &DemoTestService{
|
||||
&cool.Service{},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *DemoTestService) GetDemoTestList() (interface{}, error) {
|
||||
// gsvc.SetRegistry(etcd.New(`127.0.0.1:2379`))
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
package service
|
||||
|
||||
import (
|
||||
"blazing/cool"
|
||||
)
|
||||
|
||||
type DemoTestService struct {
|
||||
*cool.Service
|
||||
}
|
||||
|
||||
func NewDemoTestService() *DemoTestService {
|
||||
return &DemoTestService{
|
||||
&cool.Service{},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *DemoTestService) GetDemoTestList() (interface{}, error) {
|
||||
// gsvc.SetRegistry(etcd.New(`127.0.0.1:2379`))
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
21
modules/blazing/service/login.go
Normal file
21
modules/blazing/service/login.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"blazing/cool"
|
||||
)
|
||||
|
||||
type LoginService struct {
|
||||
*cool.Service
|
||||
}
|
||||
|
||||
func NewLoginServiceService() *LoginService {
|
||||
return &LoginService{
|
||||
&cool.Service{},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *LoginService) SaveSessionId(session, userid string) error {
|
||||
// gsvc.SetRegistry(etcd.New(`127.0.0.1:2379`))
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"blazing/cool"
|
||||
|
||||
"blazing/modules/demo/service"
|
||||
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
type DemoSampleController struct {
|
||||
*cool.Controller
|
||||
}
|
||||
|
||||
func init() {
|
||||
var demo_sample_controller = &DemoSampleController{
|
||||
&cool.Controller{
|
||||
Prefix: "/admin/demo/demo_sample",
|
||||
Api: []string{"Add", "Delete", "Update", "Info", "List", "Page"},
|
||||
Service: service.NewDemoSampleService(),
|
||||
},
|
||||
}
|
||||
// 注册路由
|
||||
cool.RegisterController(demo_sample_controller)
|
||||
}
|
||||
|
||||
// 增加 Welcome 演示 方法
|
||||
type DemoSampleWelcomeReq struct {
|
||||
g.Meta `path:"/welcome" method:"GET"`
|
||||
}
|
||||
type DemoSampleWelcomeRes struct {
|
||||
*cool.BaseRes
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
func (c *DemoSampleController) Welcome(ctx context.Context, req *DemoSampleWelcomeReq) (res *DemoSampleWelcomeRes, err error) {
|
||||
res = &DemoSampleWelcomeRes{
|
||||
BaseRes: cool.Ok("Welcome to Cool Admin Go"),
|
||||
Data: gjson.New(`{"name": "Cool Admin Go", "age":0}`),
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
_ "blazing/modules/demo/controller/admin"
|
||||
)
|
||||
@@ -1,7 +0,0 @@
|
||||
package demo
|
||||
|
||||
import (
|
||||
_ "blazing/modules/demo/controller"
|
||||
_ "blazing/modules/demo/model"
|
||||
_ "blazing/modules/demo/service"
|
||||
)
|
||||
6
modules/go.mod
Normal file
6
modules/go.mod
Normal file
@@ -0,0 +1,6 @@
|
||||
module blazing/modules
|
||||
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
)
|
||||
@@ -1,9 +1,9 @@
|
||||
package blazing/modules
|
||||
|
||||
import (
|
||||
_ "blazing/modules/base"
|
||||
_ "blazing/modules/demo"
|
||||
_ "blazing/modules/dict"
|
||||
_ "blazing/modules/space"
|
||||
_ "blazing/modules/task"
|
||||
)
|
||||
package modules
|
||||
|
||||
import (
|
||||
_ "blazing/modules/base"
|
||||
_ "blazing/modules/blazing"
|
||||
_ "blazing/modules/dict"
|
||||
_ "blazing/modules/space"
|
||||
_ "blazing/modules/task"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user