137 lines
3.0 KiB
Go
137 lines
3.0 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 UserManage(c *gin.Context) {
|
|
c.HTML(http.StatusOK, "userManage.html", nil)
|
|
}
|
|
|
|
// 用户列表
|
|
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,
|
|
})
|
|
}
|
|
|
|
// 增加用户GET
|
|
func AddUserPage(c *gin.Context) {
|
|
c.HTML(http.StatusOK, "addUser.html", nil)
|
|
}
|
|
|
|
// 增加用户
|
|
func AddUser(c *gin.Context) {
|
|
// 实现增加用户功能的代码
|
|
//1.获取参数
|
|
var userArgs proto.UserArgs
|
|
c.ShouldBindJSON(&userArgs)
|
|
if userArgs.Username == nil || userArgs.Password == nil {
|
|
if *userArgs.Username == "" || *userArgs.Password == "" {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 100,
|
|
"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)
|
|
if err != nil {
|
|
log.Printf("failed to convert string to int: %v", err)
|
|
return
|
|
}
|
|
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,
|
|
})
|
|
}
|
|
|
|
// 修改用户GET
|
|
func UpdateUserPage(c *gin.Context) {
|
|
c.HTML(http.StatusOK, "update.html", nil)
|
|
}
|
|
|
|
// 修改用户
|
|
func UpdateUser(c *gin.Context) {
|
|
// 实现修改用户功能的代码
|
|
var newUser proto.UserArgs
|
|
err := c.ShouldBindJSON(&newUser)
|
|
if err != nil {
|
|
log.Printf("should bind json failed. err:[%+v]\n", err)
|
|
return
|
|
}
|
|
|
|
var result proto.Common
|
|
err = common.RPC.Call(context.Background(), "UpdateUser", newUser, &result)
|
|
if err != nil {
|
|
log.Printf("failed to call: %v", err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
// 查询用户
|
|
func ListUsers2(c *gin.Context) {
|
|
// 实现查询用户功能的代码
|
|
var userArgs proto.UserArgs
|
|
err := c.ShouldBindJSON(&userArgs)
|
|
if err != nil {
|
|
log.Printf("should bind json failed. err:[%+v]\n", err)
|
|
return
|
|
}
|
|
var resp proto.UsersResp
|
|
err = common.RPC.Call(context.Background(), "ListUsers2", userArgs, &resp)
|
|
if err != nil {
|
|
log.Printf("failed to call:%v", err)
|
|
return
|
|
}
|
|
for _, user := range resp.Data {
|
|
println(user.Username, user.Address)
|
|
}
|
|
c.JSON(http.StatusOK, resp)
|
|
}
|