Commit d13a1335 authored by chenhan wang's avatar chenhan wang
Browse files

修改response和login

parent d1ae0183
......@@ -36,17 +36,15 @@ func Ping(c echo.Context) error {
// 成功则返回上传成功,否则报错
// sessionId不直接绑定,通过cookie传
func RecvFile(c echo.Context) error {
return c.HTML(http.StatusOK, "<p>文件上传失败: 文件大小超过8MB</p>")
}
func RecvFile1(c echo.Context) error {
info := new(File)
src, err := srcRead(c, info)
if err != nil {
return err // log在srcRead里进行
return response.SendResponse4(c, err) // log在srcRead里进行
}
// 创建目标文件,就是我们打算把用户上传的文件保存到什么地方
// info.Filename 参数指的是我们以用户上传的文件名,作为目标文件名,也就是服务端保存的文件名跟用户上传的文件名一样
dst, filePath := dstCreate(info)
//dst, filePath := dstCreate(info)
dst, _ := dstCreate(info)
defer dst.Close()
if !overflow(dst, 8*1024*1024) {
......@@ -60,10 +58,12 @@ func RecvFile1(c echo.Context) error {
}
// 更新数据库
sid, url := DBupdate(c, filePath, info)
//sid, url := DBupdate(c, filePath, info)
sid := "fakesid" //
url := "fakeurl" //
cookie := new(http.Cookie)
SetCookie(c, cookie, sid, info.Expiration, info.Time)
return response.SendResponse2(c, http.StatusOK, *cookie, GetFileContentType(info.FileType), fmt.Sprintf("文件上传成功: %s", info.FileName), url)
return response.SendResponse2(c, http.StatusOK, GetFileContentType(info.FileType), fmt.Sprintf("文件上传成功: %s", info.FileName), url)
}
/*
......@@ -92,5 +92,5 @@ func SendFile(c echo.Context) error {
filePath := "./files/" + info.FileType + "/" + info.FileName + info.FileType
data := readFile(filePath)
c.JSON(http.StatusOK, info)
return response.SendResponse3(c, http.StatusOK, *cookie, GetFileContentType(info.FileType), data)
return response.SendResponse3(c, http.StatusOK, GetFileContentType(info.FileType), data) // 返回数据
}
......@@ -55,11 +55,11 @@ func isUidValid(uid string) bool {
return true
}
// 从报文中获得uid
// 从报文中获得uid,给新用户分配uid
func getUid(c echo.Context) interface{} {
cookie, err := c.Cookie("User")
if err != nil || !isUidValid(cookie.Value) {
logrus.Panicln("uid invalid,使用默认uuid")
logrus.Println("uid invalid,分配新uid")
return 114514
} else {
return cookie.Value
......
......@@ -9,20 +9,18 @@ import (
type Response struct {
Code int `json:"code"`
Msg string `json:"msg"`
Type string `json:"type"`
Type string `json:"type"`
Data interface{} `json:"data"`
}
type Response2 struct {
Code int `json:"code"`
Cookie http.Cookie `json:"cookie"`
Type string `json:"type"`
Url string `json:"url"`
Type string `json:"type"`
Url string `json:"url"`
Data interface{} `json:"data"`
}
func SendResponse(c echo.Context, code int, msg string, data ...interface{}) error{
func SendResponse(c echo.Context, code int, msg string, data ...interface{}) error {
return c.JSON(http.StatusOK, Response{
Code: code,
Msg: msg,
......@@ -30,22 +28,24 @@ func SendResponse(c echo.Context, code int, msg string, data ...interface{}) err
})
}
func SendResponse2(c echo.Context, code int,cookie http.Cookie, typ string,url string, data ...interface{}) error{
func SendResponse2(c echo.Context, code int, typ string, url string, data ...interface{}) error {
return c.JSON(http.StatusOK, Response2{
Code: code,
Cookie:cookie,
Type: typ,
Url: url,
Url: url,
Data: data,
})
}
func SendResponse3(c echo.Context, code int,cookie http.Cookie, typ string, data ...interface{}) error{
func SendResponse3(c echo.Context, code int, typ string, data ...interface{}) error {
return c.JSON(http.StatusOK, Response2{
Code: code,
Cookie:cookie,
Type: typ,
Data: data,
})
}
\ No newline at end of file
}
// 返回err
func SendResponse4(c echo.Context, err error) error {
return c.JSON(http.StatusBadRequest, err.Error())
}
......@@ -5,13 +5,14 @@
package main
import "backend/app"
//"backend/model"
import (
"backend/app"
"backend/model"
)
func main() {
app.InitLogger() // 初始化logger设置
//model.Init()
model.Init()
app.InitWebFramework()
app.StartServer()
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment