Skip to content

Commit

Permalink
feat: 增加用户基本信息接口,管理后台头部调用用户信息
Browse files Browse the repository at this point in the history
前端
- 管理后台头部通过接口加载用户信息,并添加骨架占位

后端
- 增加获取当前用户基本信息接口
- 用户模型增加头像、状态字段
  • Loading branch information
jorben committed Jul 6, 2024
1 parent c3050a7 commit 5597de8
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 66 deletions.
3 changes: 3 additions & 0 deletions common/errs/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const (
ErrAuthUnauthorized = -10006 // 未授权
ErrAuthUnexpired = -10007 // 未过期
ErrMenu = -10030 // 获取菜单失败

ErrNoRecord = -10900 // 没有匹配到预期的记录,无数据
)

// 定义错误码对应的错误描述
Expand All @@ -25,6 +27,7 @@ var errorMsg = map[int]string{
ErrAuthUnauthorized: "未授权或权限不足",
ErrAuthUnexpired: "刷新登录态失败,当前登录态还有足够长的有效期",
ErrMenu: "获取菜单失败,请稍后重试",
ErrNoRecord: "未查询到相关数据",
}

// GetErrorMsg 获取错误码对应的错误描述
Expand Down
20 changes: 20 additions & 0 deletions dal/userdal.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ func NewUserDal(ctx *gin.Context) *UserDal {
}
}

// GetUserinfo 获取用户基本信息
func (u *UserDal) GetUserinfo(where *model.User, offset int, limit int) (*model.UserList, error) {
var count int64
var list []*model.User
// 计算总条数
if err := u.db.Model(where).Where(where).Count(&count).Error; err != nil {
return nil, err
}
// 获取数据
if err := u.db.Model(where).Where(where).Offset(offset).Limit(limit).Find(&list).Error; err != nil {
return nil, err
}

return &model.UserList{
Count: count,
List: list,
}, nil
}

// UpdateBySource 更新第三方渠道来源用户信息
func (u *UserDal) UpdateBySource(info *model.UserSocialInfo) (bool, error) {
// 查询id
Expand Down Expand Up @@ -50,6 +69,7 @@ func (u *UserDal) SignUpBySource(info *model.UserSocialInfo) (uint, error) {
user := &model.User{
Nickname: info.Nickname,
Email: info.Email,
Avatar: info.Avatar,
Phone: info.Phone,
Gender: info.Gender,
Bio: info.Bio,
Expand Down
7 changes: 7 additions & 0 deletions model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ type User struct {
gorm.Model
Nickname string `gorm:"size:255;not null;default:''" json:"nickname"`
Email string `gorm:"size:255;not null;default:''" json:"email"` // 邮件
Avatar string `gorm:"size:255;not null;default:''" json:"avatar"` // 头像
Phone string `gorm:"size:64;not null;default:''" json:"phone"` // 手机
Bio string `gorm:"size:255;not null;default:''" json:"bio"` // 简介
Gender uint8 `gorm:"default:0;not null;comment:0-未知,1-男,2-女" json:"gender"` //性别
Status uint8 `gorm:"default:0;not null;comment:0-正常,1-禁用" json:"status"` // 用户状态
}

type UserList struct {
Count int64 //总记录数
List []*User // 用户信息
}
4 changes: 2 additions & 2 deletions router/api/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// GetIndexMenus 获取前台权限范围内的菜单项
func GetIndexMenus(ctx *gin.Context) {
c := context.CustomContext{Context: ctx}
if menus, err := getMenus(ctx, model.MenuTypeIndex); err != nil {
if menus, err := getMenus(ctx, model.MenuTypeIndex); err == nil {
c.CJSON(errs.Success, menus)
} else {
c.CJSON(errs.ErrMenu)
Expand All @@ -21,7 +21,7 @@ func GetIndexMenus(ctx *gin.Context) {
// GetAdminMenus 获取管理后台权限范围内的菜单项
func GetAdminMenus(ctx *gin.Context) {
c := context.CustomContext{Context: ctx}
if menus, err := getMenus(ctx, model.MenuTypeAdmin); err != nil {
if menus, err := getMenus(ctx, model.MenuTypeAdmin); err == nil {
c.CJSON(errs.Success, menus)
} else {
c.CJSON(errs.ErrMenu)
Expand Down
27 changes: 27 additions & 0 deletions router/api/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package api

import (
"github.com/gin-gonic/gin"
"league/common/context"
"league/common/errs"
"league/service"
"strconv"
)

// GetUserinfo 获取当前用户信息
func GetUserinfo(ctx *gin.Context) {
c := context.CustomContext{Context: ctx}
strId := ctx.Value("UserId").(string)
userId, err := strconv.ParseUint(strId, 10, 64) // 10进制
if err != nil {
c.CJSON(errs.ErrAuthNoLogin)
return
}
userService := service.NewUserService(ctx)
user := userService.GetUserInfo(uint(userId))
if user == nil {
c.CJSON(errs.ErrNoRecord, "没有用户信息")
return
}
c.CJSON(errs.Success, user)
}
6 changes: 6 additions & 0 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@ func SetupRouter(s *gin.Engine, feEmbed embed.FS) {
})

backend.Use(middleware.RequestId(), middleware.Logger(), middleware.Auth(), gin.Recovery())
// 权限相关接口
backend.GET("/auth/login", api.AuthLogin)
backend.GET("/auth/callback", api.AuthCallback)
backend.GET("/auth/renew", api.AuthRenew)
backend.GET("/auth/logout", api.AuthLogout)

// 菜单相关接口
backend.GET("/menu", api.GetIndexMenus)

// 用户相关接口
backend.GET("/user/current", api.GetUserinfo)

backendAdmin := backend.Group("/admin")
backendAdmin.GET("/menu", api.GetAdminMenus)

Expand Down
38 changes: 38 additions & 0 deletions service/userservice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package service

import (
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"league/dal"
"league/log"
"league/model"
)

type UserService struct {
Ctx *gin.Context
UserDal *dal.UserDal
}

// NewUserService 新建AuthService实例
func NewUserService(ctx *gin.Context) *UserService {
return &UserService{
Ctx: ctx,
UserDal: dal.NewUserDal(ctx),
}
}

// GetUserInfo 根据用户ID获取用户基本信息
func (u *UserService) GetUserInfo(id uint) *model.User {
user := &model.User{
Model: gorm.Model{ID: id},
}
result, err := u.UserDal.GetUserinfo(user, 0, 1)
if err != nil {
log.Errorf(u.Ctx, "Get userinfo failed, err: %s", err.Error())
return nil
}
if result == nil || result.Count == 0 || len(result.List) == 0 {
return nil
}
return result.List[0]
}
68 changes: 34 additions & 34 deletions web/src/pages/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useState } from "react";
import {
AppstoreOutlined,
ContainerOutlined,
Expand All @@ -7,71 +7,71 @@ import {
MenuFoldOutlined,
MenuUnfoldOutlined,
PieChartOutlined,
} from '@ant-design/icons';
import { Button, Menu } from 'antd';
} from "@ant-design/icons";
import { Button, Menu } from "antd";
const items = [
{
key: '1',
key: "1",
icon: <PieChartOutlined />,
label: 'Option 1',
label: "Option 1",
},
{
key: '2',
key: "2",
icon: <DesktopOutlined />,
label: 'Option 2',
label: "Option 2",
},
{
key: '3',
key: "3",
icon: <ContainerOutlined />,
label: 'Option 3',
label: "Option 3",
},
{
key: 'sub1',
label: 'Navigation One',
key: "sub1",
label: "Navigation One",
icon: <MailOutlined />,
children: [
{
key: '5',
label: 'Option 5',
key: "5",
label: "Option 5",
},
{
key: '6',
label: 'Option 6',
key: "6",
label: "Option 6",
},
{
key: '7',
label: 'Option 7',
key: "7",
label: "Option 7",
},
{
key: '8',
label: 'Option 8',
key: "8",
label: "Option 8",
},
],
},
{
key: 'sub2',
label: 'Navigation Two',
key: "sub2",
label: "Navigation Two",
icon: <AppstoreOutlined />,
children: [
{
key: '9',
label: 'Option 9',
key: "9",
label: "Option 9",
},
{
key: '10',
label: 'Option 10',
key: "10",
label: "Option 10",
},
{
key: 'sub3',
label: 'Submenu',
key: "sub3",
label: "Submenu",
children: [
{
key: '11',
label: 'Option 11',
key: "11",
label: "Option 11",
},
{
key: '12',
label: 'Option 12',
key: "12",
label: "Option 12",
},
],
},
Expand Down Expand Up @@ -99,8 +99,8 @@ const App = () => {
{collapsed ? <MenuUnfoldOutlined /> : <MenuFoldOutlined />}
</Button>
<Menu
defaultSelectedKeys={['1']}
defaultOpenKeys={['sub1']}
defaultSelectedKeys={["1"]}
defaultOpenKeys={["sub1"]}
mode="inline"
theme="dark"
inlineCollapsed={collapsed}
Expand All @@ -109,4 +109,4 @@ const App = () => {
</div>
);
};
export default App;
export default App;
Loading

0 comments on commit 5597de8

Please sign in to comment.