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. 42
      QMPlusServer/model/dbModel/user.go
  7. 3
      QMPlusServer/model/modelInterface/interface.go
  8. 2
      QMPlusServer/router/user.go
  9. 18
      QMPlusServer/tools/hasGap.go

99
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(&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
// 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": {

89
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": {

61
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"

1
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

42
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
}

3
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)

2
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)
}
}

18
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))
}
}
}
// 获取方法

Loading…
Cancel
Save