87 lines
1.8 KiB
Go
87 lines
1.8 KiB
Go
package controllers
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/NothAmor/SourceTraceFront/common"
|
|
"github.com/NothAmor/SourceTraceFront/models"
|
|
"github.com/NothAmor/SourceTraceFront/proto"
|
|
"github.com/gin-gonic/gin"
|
|
// 导入其他必要的包
|
|
)
|
|
|
|
// 用户列表
|
|
func ListUsers(c *gin.Context) {
|
|
var listUsers []models.Users
|
|
pageArgs := proto.PageArgs{Page: 1}
|
|
err := common.RPC.Call(context.Background(), "ListUsers", pageArgs, &listUsers)
|
|
if err != nil {
|
|
log.Printf("failed to call: %v", err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 200,
|
|
"msg": "查询成功",
|
|
"data": listUsers,
|
|
})
|
|
}
|
|
|
|
// 增加用户
|
|
func AddUser(c *gin.Context) {
|
|
// 实现增加用户功能的代码
|
|
//1.获取参数
|
|
var userArgs proto.UserArgs
|
|
c.ShouldBindJSON(&userArgs)
|
|
if userArgs.Username == "" || userArgs.Password == "" {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 200,
|
|
"msg": "用户名&&密码不能为空",
|
|
"data": nil,
|
|
})
|
|
return
|
|
}
|
|
|
|
var result proto.Common
|
|
err := common.RPC.Call(context.Background(), "AddUser", userArgs, &result)
|
|
if err != nil {
|
|
log.Printf("failed to call: %v", err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 200,
|
|
"msg": result.Msg,
|
|
"data": nil,
|
|
})
|
|
}
|
|
|
|
// 删除用户
|
|
func DeleteUser(c *gin.Context) {
|
|
//获取要删除的用户id
|
|
userIDStr := c.Param("id")
|
|
userID, err := strconv.Atoi(userIDStr)
|
|
var result proto.Common
|
|
err = common.RPC.Call(context.Background(), "DeleteUser", userID, &result)
|
|
if err != nil {
|
|
log.Printf("failed to call: %v", err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 200,
|
|
"msg": result.Msg,
|
|
"data": nil,
|
|
})
|
|
}
|
|
|
|
// // 修改用户
|
|
// func UpdateUser(c *gin.Context) {
|
|
// // 实现修改用户功能的代码
|
|
// }
|
|
|
|
// // 查询用户
|
|
// func GetUser(c *gin.Context) {
|
|
// // 实现查询用户功能的代码
|
|
// }
|