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