Browse Source

用户登录注册修改密码接口

main
pixelqm 5 years ago
parent
commit
8eae7f186f
  1. 99
      QMPlusServer/controller/api/user.go
  2. 91
      QMPlusServer/docs/docs.go
  3. 89
      QMPlusServer/docs/swagger.json
  4. 61
      QMPlusServer/docs/swagger.yaml
  5. 1
      QMPlusServer/go.mod
  6. 40
      QMPlusServer/model/dbModel/user.go
  7. 3
      QMPlusServer/model/modelInterface/interface.go
  8. 2
      QMPlusServer/router/user.go
  9. 14
      QMPlusServer/tools/hasGap.go

99
QMPlusServer/controller/api/user.go

@ -4,25 +4,100 @@ import (
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"main/model/dbModel" "main/model/dbModel"
"main/model/modelInterface"
) )
type RegistStuct struct {
type RegistAndLoginStuct struct {
UserName string `json:"userName"` UserName string `json:"userName"`
PassWord string `json:"passWord"` 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] // @Router /user/regist [post]
func Regist(c *gin.Context) { func Regist(c *gin.Context) {
var R RegistStuct
var R RegistAndLoginStuct
_ = c.BindJSON(&R) _ = 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(&params)
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{},
})
}
} }

91
QMPlusServer/docs/docs.go

@ -1,6 +1,6 @@
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT // GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag at // 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 package docs
@ -26,12 +26,77 @@ var doc = `{
"host": "{{.Host}}", "host": "{{.Host}}",
"basePath": "{{.BasePath}}", "basePath": "{{.BasePath}}",
"paths": { "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": { "/user/regist": {
"post": { "post": {
"produces": [ "produces": [
"application/x-www-form-urlencoded"
"application/json"
],
"tags": [
"User"
], ],
"summary": "创建用户",
"summary": "用户注册账号",
"parameters": [ "parameters": [
{ {
"description": "用户注册接口", "description": "用户注册接口",
@ -40,13 +105,13 @@ var doc = `{
"required": true, "required": true,
"schema": { "schema": {
"type": "object", "type": "object",
"$ref": "#/definitions/api.RegistStuct"
"$ref": "#/definitions/api.RegistAndLoginStuct"
} }
} }
], ],
"responses": { "responses": {
"200": { "200": {
"description": "{\"code\":200,\"data\":{},\"msg\":\"ok\"}",
"description": "{\"success\":true,\"data\":{},\"msg\":\"注册成功\"}",
"schema": { "schema": {
"type": "string" "type": "string"
} }
@ -56,7 +121,21 @@ var doc = `{
} }
}, },
"definitions": { "definitions": {
"api.RegistStuct": {
"api.ChangePassWordStutrc": {
"type": "object",
"properties": {
"newPassWord": {
"type": "string"
},
"passWord": {
"type": "string"
},
"userName": {
"type": "string"
}
}
},
"api.RegistAndLoginStuct": {
"type": "object", "type": "object",
"properties": { "properties": {
"passWord": { "passWord": {

89
QMPlusServer/docs/swagger.json

@ -5,12 +5,77 @@
"license": {} "license": {}
}, },
"paths": { "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": { "/user/regist": {
"post": { "post": {
"produces": [ "produces": [
"application/x-www-form-urlencoded"
"application/json"
],
"tags": [
"User"
], ],
"summary": "创建用户",
"summary": "用户注册账号",
"parameters": [ "parameters": [
{ {
"description": "用户注册接口", "description": "用户注册接口",
@ -19,13 +84,13 @@
"required": true, "required": true,
"schema": { "schema": {
"type": "object", "type": "object",
"$ref": "#/definitions/api.RegistStuct"
"$ref": "#/definitions/api.RegistAndLoginStuct"
} }
} }
], ],
"responses": { "responses": {
"200": { "200": {
"description": "{\"code\":200,\"data\":{},\"msg\":\"ok\"}",
"description": "{\"success\":true,\"data\":{},\"msg\":\"注册成功\"}",
"schema": { "schema": {
"type": "string" "type": "string"
} }
@ -35,7 +100,21 @@
} }
}, },
"definitions": { "definitions": {
"api.RegistStuct": {
"api.ChangePassWordStutrc": {
"type": "object",
"properties": {
"newPassWord": {
"type": "string"
},
"passWord": {
"type": "string"
},
"userName": {
"type": "string"
}
}
},
"api.RegistAndLoginStuct": {
"type": "object", "type": "object",
"properties": { "properties": {
"passWord": { "passWord": {

61
QMPlusServer/docs/swagger.yaml

@ -1,5 +1,14 @@
definitions: definitions:
api.RegistStuct:
api.ChangePassWordStutrc:
properties:
newPassWord:
type: string
passWord:
type: string
userName:
type: string
type: object
api.RegistAndLoginStuct:
properties: properties:
passWord: passWord:
type: string type: string
@ -10,6 +19,46 @@ info:
contact: {} contact: {}
license: {} license: {}
paths: 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: /user/regist:
post: post:
parameters: parameters:
@ -18,14 +67,16 @@ paths:
name: data name: data
required: true required: true
schema: schema:
$ref: '#/definitions/api.RegistStuct'
$ref: '#/definitions/api.RegistAndLoginStuct'
type: object type: object
produces: produces:
- application/x-www-form-urlencoded
- application/json
responses: responses:
"200": "200":
description: '{"code":200,"data":{},"msg":"ok"}'
description: '{"success":true,"data":{},"msg":"注册成功"}'
schema: schema:
type: string type: string
summary: 创建用户
summary: 用户注册账号
tags:
- User
swagger: "2.0" swagger: "2.0"

1
QMPlusServer/go.mod

@ -13,6 +13,7 @@ require (
github.com/lestrrat/go-envload v0.0.0-20180220120943-6ed08b54a570 // indirect 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-file-rotatelogs v0.0.0-20180223000712-d3151e2a480f
github.com/lestrrat/go-strftime v0.0.0-20180220042222-ba3bf9c1d042 // indirect 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/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5
github.com/satori/go.uuid v1.2.0 github.com/satori/go.uuid v1.2.0
github.com/sirupsen/logrus v1.2.0 github.com/sirupsen/logrus v1.2.0

40
QMPlusServer/model/dbModel/user.go

@ -2,9 +2,10 @@ package dbModel
import ( import (
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid" uuid "github.com/satori/go.uuid"
"main/init/mysql" "main/init/mysql"
"main/model/modelInterface"
"main/tools"
) )
type User struct { type User struct {
@ -21,26 +22,43 @@ type User struct {
//type Propertie struct { //type Propertie struct {
// gorm.Model // 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) {
//注册接口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 err = mysql.DEFAULTDB.Create(u).Error
}
return err, u 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 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 err = mysql.DEFAULTDB.Create(u).Error
return err, u 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
} }

3
QMPlusServer/model/modelInterface/interface.go

@ -1,6 +1,7 @@
package modelInterface package modelInterface
// 因为我也不确定项目要不要多人维护 所以定义了CURD接口 凡是对数据库进行简单CURD操作 请实现此接口 默认首位返回 error
// 因为我也不确定项目要不要多人维护 所以定义了CURD接口 作为接口参考
// 由于很多接口使用Restful模式 暂时不用泛型 有需要可以iss提供示例
type CURD interface { type CURD interface {
Create() (error, CURD) Create() (error, CURD)
Updata() (error, CURD) Updata() (error, CURD)

2
QMPlusServer/router/user.go

@ -9,5 +9,7 @@ func InitUserRouter(Router *gin.Engine) {
UserRouter := Router.Group("user") UserRouter := Router.Group("user")
{ {
UserRouter.POST("regist", api.Regist) UserRouter.POST("regist", api.Regist)
UserRouter.POST("login", api.Login)
UserRouter.POST("changePassWord", api.ChangePassWord)
} }
} }

14
QMPlusServer/tools/hasGap.go

@ -10,19 +10,23 @@ import (
func HasGap(input interface{}) error { func HasGap(input interface{}) error {
getType := reflect.TypeOf(input) getType := reflect.TypeOf(input)
fmt.Println("获取类型 :", getType.Name())
getValue := reflect.ValueOf(input) getValue := reflect.ValueOf(input)
fmt.Println("所有字段", getValue)
// 获取方法字段 // 获取方法字段
for i := 0; i < getType.NumField(); i++ { for i := 0; i < getType.NumField(); i++ {
field := getType.Field(i) field := getType.Field(i)
value := getValue.Field(i).Interface() value := getValue.Field(i).Interface()
fmt.Printf("%s: %v = %v\n", field.Name, field.Type, value)
switch value.(type) {
case string:
if value == "" { if value == "" {
fmt.Printf("%s为空", field.Name)
return errors.New(fmt.Sprintf("%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))
}
}
} }
// 获取方法 // 获取方法
// 1. 先获取interface的reflect.Type,然后通过.NumMethod进行遍历 // 1. 先获取interface的reflect.Type,然后通过.NumMethod进行遍历

Loading…
Cancel
Save