Files
easygoadmin/app/controller/index.go
yaoyilin 1b36bd8fbe feat: 初始化项目
初始化项目
2022-10-31 22:29:16 +08:00

222 lines
4.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 系统主页-控制器
* @author
* @since 2021/9/7
* @File : index
*/
package controller
import (
"easygoadmin/app/dto"
"easygoadmin/app/model"
"easygoadmin/app/service"
"easygoadmin/app/vo"
"easygoadmin/utils"
"easygoadmin/utils/common"
"easygoadmin/utils/gconv"
"easygoadmin/utils/response"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"net/http"
)
// 用户API管理对象
var Index = new(indexCtl)
type indexCtl struct{}
func (c *indexCtl) None(ctx *gin.Context) {
// 记录ID
id := ctx.Param("id")
if id == "" {
ctx.String(http.StatusOK, "")
return
}
res := service.PbShortLink.GetInfoById(ctx, gconv.Int64(id))
if res.Id == 0 || res.Url == "" {
ctx.String(http.StatusOK, "")
return
}
ctx.Redirect(http.StatusTemporaryRedirect, res.Url)
return
}
func (c *indexCtl) Index(ctx *gin.Context) {
// 已登录
// 获取用户信息
userInfo := service.Login.GetProfile(ctx)
// 获取菜单列表
menuList := service.Menu.GetPermissionMenuList(userInfo.Id)
// 渲染模板并绑定数据
response.BuildTpl(ctx, "index.html").WriteTpl(gin.H{
"userInfo": userInfo,
"menuList": menuList,
})
}
func (c *indexCtl) Main(ctx *gin.Context) {
response.BuildTpl(ctx, "welcome.html").WriteTpl()
}
// 个人中心
func (c *indexCtl) UserInfo(ctx *gin.Context) {
if ctx.Request.Method == "POST" {
// 参数验证
var req *dto.UserInfoReq
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 更新信息
_, err := service.User.UpdateUserInfo(req, utils.Uid(ctx))
if err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "更新成功",
})
return
}
// 获取用户信息
userInfo := service.Login.GetProfile(ctx)
// 渲染模板
response.BuildTpl(ctx, "user_info.html").WriteTpl(gin.H{
"userInfo": userInfo,
})
}
// 获取系统菜单
func (c *indexCtl) Menu(ctx *gin.Context) {
// 获取菜单列表
menuList := service.Menu.GetPermissionList(utils.Uid(ctx))
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "操作成功",
Data: menuList,
})
}
// 获取用户信息
func (c *indexCtl) User(ctx *gin.Context) {
// 获取用户信息
user := model.User{}
has, err := utils.XormDb.Id(utils.Uid(ctx)).Get(&user)
if err != nil && !has {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 用户信息
var profile vo.ProfileInfoVo
profile.Realname = user.Realname
profile.Nickname = user.Nickname
profile.Avatar = utils.GetImageUrl(user.Avatar)
profile.Gender = user.Gender
profile.Mobile = user.Mobile
profile.Email = user.Email
profile.Intro = user.Intro
profile.Address = user.Address
// 角色
profile.Roles = make([]interface{}, 0)
// 权限
profile.Authorities = make([]interface{}, 0)
// 获取权限节点
permissionList := service.Menu.GetPermissionsList(utils.Uid(ctx))
profile.PermissionList = permissionList
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "查询成功",
Data: profile,
})
}
//// 个人中心
//func (c *indexCtl) UpdateUserInfo(ctx *gin.Context) {
// // 参数验证
// var req *dto.UserInfoReq
// if err := ctx.ShouldBind(&req); err != nil {
// ctx.JSON(http.StatusOK, common.JsonResult{
// Code: -1,
// Msg: err.Error(),
// })
// return
// }
// // 更新信息
// _, err := service.User.UpdateUserInfo(req, utils.Uid(ctx))
// if err != nil {
// ctx.JSON(http.StatusOK, common.JsonResult{
// Code: -1,
// Msg: err.Error(),
// })
// return
// }
//
// // 返回结果
// ctx.JSON(http.StatusOK, common.JsonResult{
// Code: 0,
// Msg: "更新成功",
// })
//}
// 更新密码
func (c *indexCtl) UpdatePwd(ctx *gin.Context) {
// 参数验证
var req *dto.UpdatePwd
if err := ctx.ShouldBind(&req); err != nil {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 调用更新密码方法
rows, err := service.User.UpdatePwd(req, utils.Uid(ctx))
if err != nil || rows == 0 {
ctx.JSON(http.StatusOK, common.JsonResult{
Code: -1,
Msg: err.Error(),
})
return
}
// 返回结果
ctx.JSON(http.StatusOK, common.JsonResult{
Code: 0,
Msg: "更新密码成功",
})
}
// 注销系统
func (c *indexCtl) Logout(ctx *gin.Context) {
// 初始化session对象
session := sessions.Default(ctx)
//session.Set("userId", "")
//session.Save()
// 删除session数据
//session.Delete("userId")
// 清空session
session.Clear()
// 保存session数据
session.Save()
// 跳转登录页,方式301(永久移动),308(永久重定向),307(临时重定向)
ctx.Redirect(http.StatusTemporaryRedirect, "/login")
}