Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hduhelp task #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions hxywd666/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 说明

杭电助手后端部的大佬你好,我是计算机学院软件工程专业的`23050724黄效瑜`同学

这个代码按照后端任务做了基本的问答功能和登录鉴权功能

代码文件也是进行了模块化管理,因为我本身是有少量工程化开发经验的

问答功能就是类似于知乎那样的发帖的模式,一个人发布帖子,其它用户可以在下面评论

除了最基本的问答功能,我还增加了一些用户账户管理功能,比如修改用户个人信息

还有接入了星火大模型,但只有最基本的聊天功能,勉强……算是接入了一个AI助手

具体功能不在这里细说了,请看代码

在这里先自我检讨一下:

时间有限,我没有给代码写注释,但是代码量又比较多……

而且有些功能开发得比较粗糙,在一些细节处理上做的不是很好,比如没有弄配置文件,没有做事务一致性处理……

还请见谅



96 changes: 96 additions & 0 deletions hxywd666/controller/bot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package controller

import (
"QASystem/dao"
"QASystem/models"
"QASystem/utils"
"encoding/base64"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"strconv"
"strings"
)

type BotController struct{}

type GetBotProfileRequest struct {
Name string `json:"name"`
Avatar string `json:"avatar"`
}

type UpdateBotProfileRequest struct {
Name string `json:"name"`
Avatar string `json:"avatar"`
UserId string `json:"userId"`
}

func (b BotController) GetBotProfile(c *gin.Context) {
paramId := c.Param("id")
botId, err := strconv.ParseInt(paramId, 10, 64)
if err != nil {
ReturnError(c, 0, "服务端异常")
}
bot, err := dao.GetBotByID(botId)
if err != nil {
ReturnError(c, 0, "获取大模型信息失败")
return
}
if bot == nil {
ReturnError(c, 0, "获取大模型信息失败")
return
}
res := &GetBotProfileRequest{
Name: bot.Name,
Avatar: bot.Avatar,
}
ReturnSuccess(c, 1, "获取大模型信息成功", res)
}

func (b BotController) UpdateBotProfile(c *gin.Context) {
var req UpdateBotProfileRequest
err := c.ShouldBindJSON(&req)
if err != nil {
ReturnError(c, 0, "参数错误")
}

avatar := req.Avatar
if !utils.ValidatorURL(avatar) {
fileExtension := utils.GetImageExtensionFromBase64(avatar)
if fileExtension == "" {
ReturnError(c, 0, "头像格式错误")
return
}
fileName, err := uuid.NewRandom()
if err != nil {
ReturnError(c, 0, "图片上传失败")
return
}
file := fileName.String() + fileExtension
base64Avatar := avatar[strings.Index(avatar, ",")+1:]
imgBytes, err := base64.StdEncoding.DecodeString(base64Avatar)
if err != nil {
ReturnError(c, 0, "图片上传失败")
return
}
url := utils.UploadFile(imgBytes, file)
if url == "" {
ReturnError(c, 0, "图片上传失败")
return
}
userId, err := strconv.ParseInt(req.UserId, 10, 64)
if err != nil {
ReturnError(c, 0, "服务端异常")
}
bot := &models.Bot{
Name: req.Name,
Avatar: url,
UserId: userId,
}
isUpdate := dao.UpdateBotProfile(bot)
if isUpdate != nil {
ReturnError(c, 0, "更新大模型信息失败")
return
}
ReturnSuccess(c, 1, "更新大模型信息成功", nil)
}
}
28 changes: 28 additions & 0 deletions hxywd666/controller/chat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package controller

import (
"QASystem/utils"
"github.com/gin-gonic/gin"
)

type ChatController struct{}

type ChatRequest struct {
Message string `json:"message"`
Model string `json:"model"`
}

func (chat ChatController) ChatWithSpark(c *gin.Context) {
var chatRequest ChatRequest
err := c.ShouldBindJSON(&chatRequest)
if err != nil {
ReturnError(c, 0, "参数错误")
}
res, err := utils.SendRequest(chatRequest.Message, chatRequest.Model)
if err != nil {
ReturnError(c, 0, "请求失败")
panic(err)
return
}
ReturnSuccess(c, 1, "请求成功", res)
}
97 changes: 97 additions & 0 deletions hxywd666/controller/comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package controller

import (
"QASystem/dao"
"QASystem/models"
"github.com/gin-gonic/gin"
"strconv"
"time"
)

type CommentController struct{}

type CreateCommentRequest struct {
Content string `json:"content"`
PostID int64 `json:"post_id"`
}

type GetCommentRequest struct {
Page int `json:"page"`
PageSize int `json:"pageSize"`
PostID int64 `json:"post_id"`
OrderBy int `json:"order_by"`
}

type GetCommentResponse struct {
Content string `json:"content"`
CreatTime string `json:"create_time"`
Avatar string `json:"avatar"`
Name string `json:"name"`
Likes int64 `json:"likes"`
}

func (cc CommentController) CreateComment(c *gin.Context) {
var req CreateCommentRequest
err := c.ShouldBind(&req)
if err != nil {
ReturnError(c, 0, "参数错误")
return
}
comment := &models.Comment{
Content: req.Content,
PostID: req.PostID,
UserID: c.GetInt64("user_id"),
Likes: 0,
CreateTime: time.Now(),
}
err = dao.CreateComment(comment)
if err != nil {
ReturnError(c, 0, "评论失败")
return
}
ReturnSuccess(c, 1, "评论成功", nil)
}

func (cc CommentController) DeleteComment(c *gin.Context) {
commentID, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
ReturnError(c, 0, "参数错误")
return
}
err = dao.DeleteComment(commentID)
if err != nil {
ReturnError(c, 0, "删除失败")
return
}
ReturnSuccess(c, 1, "删除成功", nil)
}

func (cc CommentController) GetComment(c *gin.Context) {
var req GetCommentRequest
err := c.ShouldBind(&req)
if err != nil {
ReturnError(c, 0, "参数错误")
return
}
comments, err := dao.GetComment(req.PostID, req.Page, req.PageSize, req.OrderBy)
if err != nil {
ReturnError(c, 0, "获取失败")
return
}
var res []GetCommentResponse
for _, comment := range comments {
user, err := dao.GetUserByID(comment.UserID)
if err != nil {
ReturnError(c, 0, "获取失败")
return
}
res = append(res, GetCommentResponse{
Content: comment.Content,
CreatTime: comment.CreateTime.Format("2006-01-02 15:04:05"),
Avatar: user.Avatar,
Name: user.Name,
Likes: comment.Likes,
})
}
ReturnSuccess(c, 1, "获取成功", res)
}
33 changes: 33 additions & 0 deletions hxywd666/controller/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package controller

import (
"github.com/gin-gonic/gin"
)

type JsonStruct struct {
Code int `json:"code"`
Msg interface{} `json:"msg"`
Data interface{} `json:"data"`
}

type JsonErrorStruct struct {
Code int `json:"code"`
Msg interface{} `json:"msg"`
}

func ReturnSuccess(c *gin.Context, code int, msg interface{}, data interface{}) {
json := &JsonStruct{
Code: code,
Msg: msg,
Data: data,
}
c.JSON(200, json)
}

func ReturnError(c *gin.Context, code int, msg interface{}) {
json := &JsonErrorStruct{
Code: code,
Msg: msg,
}
c.JSON(200, json)
}
Loading