提交 8eae7f18 编写于 作者: Mr.奇淼('s avatar Mr.奇淼(

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

上级 b70ab642
...@@ -4,25 +4,100 @@ import ( ...@@ -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 创建用户 // @Tags User
// @Produce application/x-www-form-urlencoded // @Summary 用户注册账号
// @Param data body api.RegistStuct true "用户注册接口" // @Produce application/json
// @Success 200 {string} json "{"code":200,"data":{},"msg":"ok"}" // @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 U := &dbModel.User{UserName: R.UserName, PassWord: R.PassWord}
curd = U err, user := U.Regist()
err, user := curd.Create() if err != nil {
fmt.Println(err, user) 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{},
})
}
} }
// 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 = `{ ...@@ -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"
], ],
"summary": "创建用户", "tags": [
"User"
],
"summary": "用户注册账号",
"parameters": [ "parameters": [
{ {
"description": "用户注册接口", "description": "用户注册接口",
...@@ -40,13 +105,13 @@ var doc = `{ ...@@ -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 = `{ ...@@ -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": {
......
...@@ -5,12 +5,77 @@ ...@@ -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"
], ],
"summary": "创建用户", "tags": [
"User"
],
"summary": "用户注册账号",
"parameters": [ "parameters": [
{ {
"description": "用户注册接口", "description": "用户注册接口",
...@@ -19,13 +84,13 @@ ...@@ -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 @@ ...@@ -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": {
......
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: ...@@ -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: ...@@ -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"
...@@ -13,6 +13,7 @@ require ( ...@@ -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
......
...@@ -2,9 +2,10 @@ package dbModel ...@@ -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 { ...@@ -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方法
err = mysql.DEFAULTDB.Create(u).Error 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 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 func (u *User) Login() (err error, userInter *User) {
return err, u 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
} }
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)
......
...@@ -9,5 +9,7 @@ func InitUserRouter(Router *gin.Engine) { ...@@ -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)
} }
} }
...@@ -10,18 +10,22 @@ import ( ...@@ -10,18 +10,22 @@ 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) {
if value == "" { case string:
return errors.New(fmt.Sprintf("%s为空", field.Name)) 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))
}
} }
} }
// 获取方法 // 获取方法
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册