From 25c6fdeee19925ce944d8763133edbce89af5bd5 Mon Sep 17 00:00:00 2001 From: pixelqm <303176530@qq.com> Date: Thu, 5 Sep 2019 23:55:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=B1=BB=E5=9E=8Bswagger?= =?UTF-8?q?=E7=9A=84=E6=B3=A8=E8=A7=A3=E4=B9=A6=E5=86=99=20=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=A4=B4=E5=83=8F=E4=B8=8A=E4=BC=A0=20=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- QMPlusServer/controller/api/reportformat.go | 14 ++++ QMPlusServer/controller/api/user.go | 88 ++++++++++++-------- QMPlusServer/controller/support/upload.go | 24 ++++-- QMPlusServer/docs/docs.go | 40 ++++++++- QMPlusServer/docs/swagger.json | 38 +++++++++ QMPlusServer/docs/swagger.yaml | 25 ++++++ QMPlusServer/model/dbModel/user.go | 7 ++ QMPlusServer/router/user.go | 1 + README.md | Bin 1614 -> 2106 bytes 9 files changed, 192 insertions(+), 45 deletions(-) create mode 100644 QMPlusServer/controller/api/reportformat.go diff --git a/QMPlusServer/controller/api/reportformat.go b/QMPlusServer/controller/api/reportformat.go new file mode 100644 index 00000000..52746847 --- /dev/null +++ b/QMPlusServer/controller/api/reportformat.go @@ -0,0 +1,14 @@ +package api + +import ( + "github.com/gin-gonic/gin" + "net/http" +) + +func ReportFormat(c *gin.Context, success bool, msg string, json gin.H) { + c.JSON(http.StatusOK, gin.H{ + "success": success, + "msg": msg, + "data": json, + }) +} diff --git a/QMPlusServer/controller/api/user.go b/QMPlusServer/controller/api/user.go index e4b53f5f..66d2d88c 100644 --- a/QMPlusServer/controller/api/user.go +++ b/QMPlusServer/controller/api/user.go @@ -3,7 +3,14 @@ package api import ( "fmt" "github.com/gin-gonic/gin" + "main/controller/support" "main/model/dbModel" + "mime/multipart" +) + +var ( + USER_HEADER_IMG_PATH string = "http://qmplusimg.henrongyi.top" + USER_HEADER_BUCKET string = "qm-plus-img" ) type RegistAndLoginStuct struct { @@ -24,20 +31,12 @@ func Regist(c *gin.Context) { U := &dbModel.User{UserName: R.UserName, PassWord: R.PassWord} err, user := U.Regist() if err != nil { - c.JSON(200, gin.H{ - "success": false, - "msg": fmt.Sprintf("%v", err), - "data": gin.H{ - "user": user, - }, + ReportFormat(c, false, fmt.Sprintf("%v", err), gin.H{ + "user": user, }) } else { - c.JSON(200, gin.H{ - "success": true, - "msg": "创建成功", - "data": gin.H{ - "user": user, - }, + ReportFormat(c, false, "创建成功", gin.H{ + "user": user, }) } } @@ -53,21 +52,9 @@ func Login(c *gin.Context) { _ = c.BindJSON(&L) U := &dbModel.User{UserName: L.UserName, PassWord: L.PassWord} if err, user := U.Login(); err != nil { - c.JSON(200, gin.H{ - "success": false, - "msg": "用户名密码错误", - "data": gin.H{ - "user": user, - }, - }) + ReportFormat(c, false, "用户名密码错误", gin.H{"user": user}) } else { - c.JSON(200, gin.H{ - "success": true, - "msg": "登录成功", - "data": gin.H{ - "user": user, - }, - }) + ReportFormat(c, true, "登录成功", gin.H{"user": user}) } } @@ -88,16 +75,45 @@ func ChangePassWord(c *gin.Context) { _ = c.BindJSON(¶ms) U := &dbModel.User{UserName: params.UserName, PassWord: params.PassWord} if err, _ := U.ChangePassWord(params.NewPassWord); err != nil { - c.JSON(200, gin.H{ - "success": false, - "msg": "修改失败,请检查用户名密码", - "data": gin.H{}, - }) + ReportFormat(c, false, "修改失败,请检查用户名密码", gin.H{}) } else { - c.JSON(200, gin.H{ - "success": true, - "msg": "修改成功", - "data": gin.H{}, - }) + ReportFormat(c, true, "修改成功", gin.H{}) + } +} + +type UserHeaderImg struct { + HeaderImg multipart.File `json:"headerImg"` +} + +// @Tags User +// @Summary 用户上传头像 +// @accept multipart/form-data +// @Produce application/json +// @Param headerImg formData file true "用户上传头像" +// @Param userName formData string true "用户上传头像" +// @Success 200 {string} json "{"success":true,"data":{},"msg":"上传成功"}" +// @Router /user/uploadHeaderImg [post] +func UploadHeaderImg(c *gin.Context) { + //获取头像文件 + _, header, err := c.Request.FormFile("headerImg") + //便于找到用户 以后从jwt中取 + userName := c.PostForm("userName") + if err != nil { + ReportFormat(c, false, fmt.Sprintf("上传文件失败,%v", err), gin.H{}) + } else { + //文件上传后拿到文件路径 + err, filePath := support.Upload(header, USER_HEADER_BUCKET, USER_HEADER_IMG_PATH) + if err != nil { + ReportFormat(c, false, fmt.Sprintf("接收返回值失败,%v", err), gin.H{}) + } else { + //修改数据库后得到修改后的user并且返回供前端使用 + err, user := new(dbModel.User).UploadHeaderImg(userName, filePath) + + if err != nil { + ReportFormat(c, false, fmt.Sprintf("修改数据库链接失败,%v", err), gin.H{}) + } else { + ReportFormat(c, true, "上传成功", gin.H{"user": user}) + } + } } } diff --git a/QMPlusServer/controller/support/upload.go b/QMPlusServer/controller/support/upload.go index 7c7a76f1..5e2ce760 100644 --- a/QMPlusServer/controller/support/upload.go +++ b/QMPlusServer/controller/support/upload.go @@ -1,17 +1,19 @@ package support import ( - "bytes" + "context" + "fmt" "github.com/qiniu/api.v7/auth/qbox" "github.com/qiniu/api.v7/storage" "mime/multipart" + "time" ) var accessKey string = "25j8dYBZ2wuiy0yhwShytjZDTX662b8xiFguwxzZ" // 你在七牛云的accessKey var secretKey string = "pgdbqEsf7ooZh7W3xokP833h3dZ_VecFXPDeG5JY" // 你在七牛云的secretKey -var bucket string = "a303176530" // 你七牛云标准空间的名字 -func Upload(file multipart.File) (err error, path string) { +// 接收两个参数 一个文件流 一个 bucket 你的七牛云标准空间的名字 +func Upload(file *multipart.FileHeader, bucket string, urlPath string) (err error, path string) { putPolicy := storage.PutPolicy{ Scope: bucket, } @@ -31,12 +33,18 @@ func Upload(file multipart.File) (err error, path string) { "x:name": "github logo", }, } - data := []byte("hello, this is qiniu cloud") - dataLen := int64(len(data)) - err := formUploader.Put(context.Background(), &ret, upToken, key, bytes.NewReader(data), dataLen, &putExtra) + f, e := file.Open() + if e != nil { + fmt.Println(e) + return e, "" + } + dataLen := file.Size + fileKey := fmt.Sprintf("%d%s", time.Now().Unix(), file.Filename) // 文件名格式 自己可以改 建议保证唯一性 + err = formUploader.Put(context.Background(), &ret, upToken, fileKey, f, dataLen, &putExtra) if err != nil { fmt.Println(err) - return + //qmlog.QMLog.Info(err) + return err, "" } - fmt.Println(ret.Key, ret.Hash) + return err, urlPath + "/" + ret.Key } diff --git a/QMPlusServer/docs/docs.go b/QMPlusServer/docs/docs.go index da02ec6d..afcd9e37 100644 --- a/QMPlusServer/docs/docs.go +++ b/QMPlusServer/docs/docs.go @@ -1,6 +1,6 @@ // GENERATED BY THE COMMAND ABOVE; DO NOT EDIT // This file was generated by swaggo/swag at -// 2019-09-03 23:59:41.1295277 +0800 CST m=+0.038362901 +// 2019-09-05 23:15:39.7963441 +0800 CST m=+0.062832501 package docs @@ -118,6 +118,44 @@ var doc = `{ } } } + }, + "/user/uploadHeaderImg": { + "post": { + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "tags": [ + "User" + ], + "summary": "用户上传头像", + "parameters": [ + { + "type": "file", + "description": "用户上传头像", + "name": "headerImg", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "用户上传头像", + "name": "userName", + "in": "formData", + "required": true + } + ], + "responses": { + "200": { + "description": "{\"success\":true,\"data\":{},\"msg\":\"上传成功\"}", + "schema": { + "type": "string" + } + } + } + } } }, "definitions": { diff --git a/QMPlusServer/docs/swagger.json b/QMPlusServer/docs/swagger.json index 04324641..8a6ea823 100644 --- a/QMPlusServer/docs/swagger.json +++ b/QMPlusServer/docs/swagger.json @@ -97,6 +97,44 @@ } } } + }, + "/user/uploadHeaderImg": { + "post": { + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "tags": [ + "User" + ], + "summary": "用户上传头像", + "parameters": [ + { + "type": "file", + "description": "用户上传头像", + "name": "headerImg", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "用户上传头像", + "name": "userName", + "in": "formData", + "required": true + } + ], + "responses": { + "200": { + "description": "{\"success\":true,\"data\":{},\"msg\":\"上传成功\"}", + "schema": { + "type": "string" + } + } + } + } } }, "definitions": { diff --git a/QMPlusServer/docs/swagger.yaml b/QMPlusServer/docs/swagger.yaml index a42591d6..f180d470 100644 --- a/QMPlusServer/docs/swagger.yaml +++ b/QMPlusServer/docs/swagger.yaml @@ -79,4 +79,29 @@ paths: summary: 用户注册账号 tags: - User + /user/uploadHeaderImg: + post: + consumes: + - multipart/form-data + parameters: + - description: 用户上传头像 + in: formData + name: headerImg + required: true + type: file + - description: 用户上传头像 + in: formData + name: userName + required: true + type: string + produces: + - application/json + responses: + "200": + description: '{"success":true,"data":{},"msg":"上传成功"}' + schema: + type: string + summary: 用户上传头像 + tags: + - User swagger: "2.0" diff --git a/QMPlusServer/model/dbModel/user.go b/QMPlusServer/model/dbModel/user.go index ed7cb38f..dd1047e5 100644 --- a/QMPlusServer/model/dbModel/user.go +++ b/QMPlusServer/model/dbModel/user.go @@ -62,3 +62,10 @@ func (u *User) Login() (err error, userInter *User) { err = qmsql.DEFAULTDB.Where("user_name = ? AND pass_word = ?", u.UserName, u.PassWord).First(&user).Error return err, &user } + +// 用户头像上传更新地址 +func (u *User) UploadHeaderImg(userName string, filePath string) (err error, userInter *User) { + var user User + err = qmsql.DEFAULTDB.Where("user_name = ?", userName).First(&user).Update("header_img", filePath).First(&user).Error + return err, &user +} diff --git a/QMPlusServer/router/user.go b/QMPlusServer/router/user.go index 87f22ee3..3cda13b4 100644 --- a/QMPlusServer/router/user.go +++ b/QMPlusServer/router/user.go @@ -11,5 +11,6 @@ func InitUserRouter(Router *gin.Engine) { UserRouter.POST("regist", api.Regist) UserRouter.POST("login", api.Login) UserRouter.POST("changePassWord", api.ChangePassWord) + UserRouter.POST("uploadHeaderImg", api.UploadHeaderImg) } } diff --git a/README.md b/README.md index bf2fa3244e650aeb31479f7407e61e64aa415c1c..bc38e8a20c0b2fce2d05ac35390037538708c545 100644 GIT binary patch delta 558 zcmX@dvrAxtS-moYGJ^uc%s`EB-=L)FybN4G9t7&7SI&4>X6@J5sh`FlBOk93zBHL7 zUNx==5)1aM_N<;C z^tS=oIaYpcemhflrtbG=3GwyU^4sOd5Pvb4wc&NV`~=;UUvVJE{t9=fP|WHM=lS2~ zr=JFr*GX5*s!!+nAMXd!|J08!!_PC94$0Y48vgYccH4AUDsEmKzn$tNoHs7G##&SWTND0XH*w$Lj6 zL;+8nbh<`3$d4^$dT}d(uH^Zjk`1y9