From 8eae7f186fc20f1db183c251ad1fdfebbbecac15 Mon Sep 17 00:00:00 2001 From: pixelqm <303176530@qq.com> Date: Wed, 4 Sep 2019 00:22:14 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=99=BB=E5=BD=95=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E4=BF=AE=E6=94=B9=E5=AF=86=E7=A0=81=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- QMPlusServer/controller/api/user.go | 99 ++++++++++++++++--- QMPlusServer/docs/docs.go | 91 +++++++++++++++-- QMPlusServer/docs/swagger.json | 89 ++++++++++++++++- QMPlusServer/docs/swagger.yaml | 61 +++++++++++- QMPlusServer/go.mod | 1 + QMPlusServer/model/dbModel/user.go | 42 +++++--- .../model/modelInterface/interface.go | 3 +- QMPlusServer/router/user.go | 2 + QMPlusServer/tools/hasGap.go | 18 ++-- 9 files changed, 358 insertions(+), 48 deletions(-) diff --git a/QMPlusServer/controller/api/user.go b/QMPlusServer/controller/api/user.go index d6ac6f52..e4b53f5f 100644 --- a/QMPlusServer/controller/api/user.go +++ b/QMPlusServer/controller/api/user.go @@ -4,25 +4,100 @@ import ( "fmt" "github.com/gin-gonic/gin" "main/model/dbModel" - "main/model/modelInterface" ) -type RegistStuct struct { +type RegistAndLoginStuct struct { UserName string `json:"userName"` PassWord string `json:"passWord"` } -// @Summary 创建用户 -// @Produce application/x-www-form-urlencoded -// @Param data body api.RegistStuct true "用户注册接口" -// @Success 200 {string} json "{"code":200,"data":{},"msg":"ok"}" +// @Tags User +// @Summary 用户注册账号 +// @Produce application/json +// @Param data body api.RegistAndLoginStuct true "用户注册接口" +// @Success 200 {string} json "{"success":true,"data":{},"msg":"注册成功"}" // @Router /user/regist [post] func Regist(c *gin.Context) { - var R RegistStuct + var R RegistAndLoginStuct _ = c.BindJSON(&R) - U := dbModel.NewUser(dbModel.User{UserName: R.UserName, PassWord: R.PassWord}) - var curd modelInterface.CURD - curd = U - err, user := curd.Create() - fmt.Println(err, user) + + 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, + }, + }) + } else { + c.JSON(200, gin.H{ + "success": true, + "msg": "创建成功", + "data": gin.H{ + "user": user, + }, + }) + } +} + +// @Tags User +// @Summary 用户登录 +// @Produce application/json +// @Param data body api.RegistAndLoginStuct true "用户登录接口" +// @Success 200 {string} json "{"success":true,"data":{},"msg":"登陆成功"}" +// @Router /user/login [post] +func Login(c *gin.Context) { + var L RegistAndLoginStuct + _ = 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, + }, + }) + } else { + c.JSON(200, gin.H{ + "success": true, + "msg": "登录成功", + "data": gin.H{ + "user": user, + }, + }) + } +} + +type ChangePassWordStutrc struct { + UserName string `json:"userName"` + PassWord string `json:"passWord"` + NewPassWord string `json:"newPassWord"` +} + +// @Tags User +// @Summary 用户修改密码 +// @Produce application/json +// @Param data body api.ChangePassWordStutrc true "用户修改密码" +// @Success 200 {string} json "{"success":true,"data":{},"msg":"修改成功"}" +// @Router /user/changePassWord [post] +func ChangePassWord(c *gin.Context) { + var params ChangePassWordStutrc + _ = 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{}, + }) + } else { + c.JSON(200, gin.H{ + "success": true, + "msg": "修改成功", + "data": gin.H{}, + }) + } } diff --git a/QMPlusServer/docs/docs.go b/QMPlusServer/docs/docs.go index 6f04a584..da02ec6d 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 14:28:38.1089338 +0800 CST m=+0.037899201 +// 2019-09-03 23:59:41.1295277 +0800 CST m=+0.038362901 package docs @@ -26,12 +26,77 @@ var doc = `{ "host": "{{.Host}}", "basePath": "{{.BasePath}}", "paths": { + "/user/changePassWord": { + "post": { + "produces": [ + "application/json" + ], + "tags": [ + "User" + ], + "summary": "用户修改密码", + "parameters": [ + { + "description": "用户修改密码", + "name": "data", + "in": "body", + "required": true, + "schema": { + "type": "object", + "$ref": "#/definitions/api.ChangePassWordStutrc" + } + } + ], + "responses": { + "200": { + "description": "{\"success\":true,\"data\":{},\"msg\":\"修改成功\"}", + "schema": { + "type": "string" + } + } + } + } + }, + "/user/login": { + "post": { + "produces": [ + "application/json" + ], + "tags": [ + "User" + ], + "summary": "用户登录", + "parameters": [ + { + "description": "用户登录接口", + "name": "data", + "in": "body", + "required": true, + "schema": { + "type": "object", + "$ref": "#/definitions/api.RegistAndLoginStuct" + } + } + ], + "responses": { + "200": { + "description": "{\"success\":true,\"data\":{},\"msg\":\"登陆成功\"}", + "schema": { + "type": "string" + } + } + } + } + }, "/user/regist": { "post": { "produces": [ - "application/x-www-form-urlencoded" + "application/json" ], - "summary": "创建用户", + "tags": [ + "User" + ], + "summary": "用户注册账号", "parameters": [ { "description": "用户注册接口", @@ -40,13 +105,13 @@ var doc = `{ "required": true, "schema": { "type": "object", - "$ref": "#/definitions/api.RegistStuct" + "$ref": "#/definitions/api.RegistAndLoginStuct" } } ], "responses": { "200": { - "description": "{\"code\":200,\"data\":{},\"msg\":\"ok\"}", + "description": "{\"success\":true,\"data\":{},\"msg\":\"注册成功\"}", "schema": { "type": "string" } @@ -56,7 +121,21 @@ var doc = `{ } }, "definitions": { - "api.RegistStuct": { + "api.ChangePassWordStutrc": { + "type": "object", + "properties": { + "newPassWord": { + "type": "string" + }, + "passWord": { + "type": "string" + }, + "userName": { + "type": "string" + } + } + }, + "api.RegistAndLoginStuct": { "type": "object", "properties": { "passWord": { diff --git a/QMPlusServer/docs/swagger.json b/QMPlusServer/docs/swagger.json index 95db4723..04324641 100644 --- a/QMPlusServer/docs/swagger.json +++ b/QMPlusServer/docs/swagger.json @@ -5,12 +5,77 @@ "license": {} }, "paths": { + "/user/changePassWord": { + "post": { + "produces": [ + "application/json" + ], + "tags": [ + "User" + ], + "summary": "用户修改密码", + "parameters": [ + { + "description": "用户修改密码", + "name": "data", + "in": "body", + "required": true, + "schema": { + "type": "object", + "$ref": "#/definitions/api.ChangePassWordStutrc" + } + } + ], + "responses": { + "200": { + "description": "{\"success\":true,\"data\":{},\"msg\":\"修改成功\"}", + "schema": { + "type": "string" + } + } + } + } + }, + "/user/login": { + "post": { + "produces": [ + "application/json" + ], + "tags": [ + "User" + ], + "summary": "用户登录", + "parameters": [ + { + "description": "用户登录接口", + "name": "data", + "in": "body", + "required": true, + "schema": { + "type": "object", + "$ref": "#/definitions/api.RegistAndLoginStuct" + } + } + ], + "responses": { + "200": { + "description": "{\"success\":true,\"data\":{},\"msg\":\"登陆成功\"}", + "schema": { + "type": "string" + } + } + } + } + }, "/user/regist": { "post": { "produces": [ - "application/x-www-form-urlencoded" + "application/json" ], - "summary": "创建用户", + "tags": [ + "User" + ], + "summary": "用户注册账号", "parameters": [ { "description": "用户注册接口", @@ -19,13 +84,13 @@ "required": true, "schema": { "type": "object", - "$ref": "#/definitions/api.RegistStuct" + "$ref": "#/definitions/api.RegistAndLoginStuct" } } ], "responses": { "200": { - "description": "{\"code\":200,\"data\":{},\"msg\":\"ok\"}", + "description": "{\"success\":true,\"data\":{},\"msg\":\"注册成功\"}", "schema": { "type": "string" } @@ -35,7 +100,21 @@ } }, "definitions": { - "api.RegistStuct": { + "api.ChangePassWordStutrc": { + "type": "object", + "properties": { + "newPassWord": { + "type": "string" + }, + "passWord": { + "type": "string" + }, + "userName": { + "type": "string" + } + } + }, + "api.RegistAndLoginStuct": { "type": "object", "properties": { "passWord": { diff --git a/QMPlusServer/docs/swagger.yaml b/QMPlusServer/docs/swagger.yaml index e6505b60..a42591d6 100644 --- a/QMPlusServer/docs/swagger.yaml +++ b/QMPlusServer/docs/swagger.yaml @@ -1,5 +1,14 @@ definitions: - api.RegistStuct: + api.ChangePassWordStutrc: + properties: + newPassWord: + type: string + passWord: + type: string + userName: + type: string + type: object + api.RegistAndLoginStuct: properties: passWord: type: string @@ -10,6 +19,46 @@ info: contact: {} license: {} paths: + /user/changePassWord: + post: + parameters: + - description: 用户修改密码 + in: body + name: data + required: true + schema: + $ref: '#/definitions/api.ChangePassWordStutrc' + type: object + produces: + - application/json + responses: + "200": + description: '{"success":true,"data":{},"msg":"修改成功"}' + schema: + type: string + summary: 用户修改密码 + tags: + - User + /user/login: + post: + parameters: + - description: 用户登录接口 + in: body + name: data + required: true + schema: + $ref: '#/definitions/api.RegistAndLoginStuct' + type: object + produces: + - application/json + responses: + "200": + description: '{"success":true,"data":{},"msg":"登陆成功"}' + schema: + type: string + summary: 用户登录 + tags: + - User /user/regist: post: parameters: @@ -18,14 +67,16 @@ paths: name: data required: true schema: - $ref: '#/definitions/api.RegistStuct' + $ref: '#/definitions/api.RegistAndLoginStuct' type: object produces: - - application/x-www-form-urlencoded + - application/json responses: "200": - description: '{"code":200,"data":{},"msg":"ok"}' + description: '{"success":true,"data":{},"msg":"注册成功"}' schema: type: string - summary: 创建用户 + summary: 用户注册账号 + tags: + - User swagger: "2.0" diff --git a/QMPlusServer/go.mod b/QMPlusServer/go.mod index e040b123..d543a550 100644 --- a/QMPlusServer/go.mod +++ b/QMPlusServer/go.mod @@ -13,6 +13,7 @@ require ( github.com/lestrrat/go-envload v0.0.0-20180220120943-6ed08b54a570 // indirect github.com/lestrrat/go-file-rotatelogs v0.0.0-20180223000712-d3151e2a480f github.com/lestrrat/go-strftime v0.0.0-20180220042222-ba3bf9c1d042 // indirect + github.com/pkg/errors v0.8.1 github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 github.com/satori/go.uuid v1.2.0 github.com/sirupsen/logrus v1.2.0 diff --git a/QMPlusServer/model/dbModel/user.go b/QMPlusServer/model/dbModel/user.go index d827f752..d94111a8 100644 --- a/QMPlusServer/model/dbModel/user.go +++ b/QMPlusServer/model/dbModel/user.go @@ -2,9 +2,10 @@ package dbModel import ( "github.com/jinzhu/gorm" + "github.com/pkg/errors" uuid "github.com/satori/go.uuid" "main/init/mysql" - "main/model/modelInterface" + "main/tools" ) type User struct { @@ -21,26 +22,43 @@ type User struct { //type Propertie struct { // gorm.Model //} -func NewUser(user User) *User { - return &User{UserName: user.UserName, PassWord: user.PassWord, NickName: user.NickName, HeaderImg: user.HeaderImg} -} -func (u *User) Create() (err error, user modelInterface.CURD) { - err = mysql.DEFAULTDB.Create(u).Error +//注册接口model方法 +func (u *User) Regist() (err error, userInter *User) { + var user User + //判断用户名是否注册 + findErr := mysql.DEFAULTDB.Where("user_name = ?", u.UserName).First(&user).Error + //err为nil表明读取到了 不能注册 + if findErr == nil { + return errors.New("用户名已注册"), nil + } else { + // 否则 附加uuid 密码md5简单加密 注册 + u.PassWord = tools.MD5V(u.PassWord) + u.UUID = uuid.NewV4() + err = mysql.DEFAULTDB.Create(u).Error + } return err, u } -func (u *User) Delete() (err error, user modelInterface.CURD) { - err = mysql.DEFAULTDB.Create(u).Error +//修改用户密码 +func (u *User) ChangePassWord(newPassWord string) (err error, userInter *User) { + var user User + //后期修改jwt+password模式 + u.PassWord = tools.MD5V(u.PassWord) + err = mysql.DEFAULTDB.Where("user_name = ? AND pass_word = ?", u.UserName, u.PassWord).First(&user).Update("pass_word", tools.MD5V(newPassWord)).Error return err, u } -func (u *User) Updata() (err error, user modelInterface.CURD) { +//用户更新接口 +func (u *User) UpdataUser() (err error, userInter *User) { err = mysql.DEFAULTDB.Create(u).Error return err, u } -func (u *User) Read() (err error, user modelInterface.CURD) { - err = mysql.DEFAULTDB.Create(u).Error - return err, u +//用户登录 +func (u *User) Login() (err error, userInter *User) { + var user User + u.PassWord = tools.MD5V(u.PassWord) + err = mysql.DEFAULTDB.Where("user_name = ? AND pass_word = ?", u.UserName, u.PassWord).First(&user).Error + return err, &user } diff --git a/QMPlusServer/model/modelInterface/interface.go b/QMPlusServer/model/modelInterface/interface.go index d4f8ccc4..11c1cbe4 100644 --- a/QMPlusServer/model/modelInterface/interface.go +++ b/QMPlusServer/model/modelInterface/interface.go @@ -1,6 +1,7 @@ package modelInterface -// 因为我也不确定项目要不要多人维护 所以定义了CURD接口 凡是对数据库进行简单CURD操作 请实现此接口 默认首位返回 error +// 因为我也不确定项目要不要多人维护 所以定义了CURD接口 作为接口参考 +// 由于很多接口使用Restful模式 暂时不用泛型 有需要可以iss提供示例 type CURD interface { Create() (error, CURD) Updata() (error, CURD) diff --git a/QMPlusServer/router/user.go b/QMPlusServer/router/user.go index 91784c17..87f22ee3 100644 --- a/QMPlusServer/router/user.go +++ b/QMPlusServer/router/user.go @@ -9,5 +9,7 @@ func InitUserRouter(Router *gin.Engine) { UserRouter := Router.Group("user") { UserRouter.POST("regist", api.Regist) + UserRouter.POST("login", api.Login) + UserRouter.POST("changePassWord", api.ChangePassWord) } } diff --git a/QMPlusServer/tools/hasGap.go b/QMPlusServer/tools/hasGap.go index c765daf8..66a65e99 100644 --- a/QMPlusServer/tools/hasGap.go +++ b/QMPlusServer/tools/hasGap.go @@ -10,18 +10,22 @@ import ( func HasGap(input interface{}) error { getType := reflect.TypeOf(input) - fmt.Println("获取类型 :", getType.Name()) - getValue := reflect.ValueOf(input) - fmt.Println("所有字段", getValue) - // 获取方法字段 for i := 0; i < getType.NumField(); i++ { field := getType.Field(i) value := getValue.Field(i).Interface() - fmt.Printf("%s: %v = %v\n", field.Name, field.Type, value) - if value == "" { - return errors.New(fmt.Sprintf("%s为空", field.Name)) + switch value.(type) { + case string: + if value == "" { + fmt.Printf("%s为空", field.Name) + return errors.New(fmt.Sprintf("%s为空", field.Name)) + } + default: + if value == nil { + fmt.Printf("%s为空", field.Name) + return errors.New(fmt.Sprintf("%s为空", field.Name)) + } } } // 获取方法