259 lines
6.8 KiB
Go
259 lines
6.8 KiB
Go
package api
|
||
|
||
import (
|
||
"SciencesServer/app/manage/controller/user"
|
||
"SciencesServer/app/service"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
type User struct{}
|
||
|
||
type userForm struct {
|
||
Account string `json:"account" form:"account" binding:"required"`
|
||
Name string `json:"name" form:"name" binding:"required"`
|
||
Mobile string `json:"mobile" form:"mobile" binding:"required"`
|
||
Gender int `json:"gender" form:"gender" binding:"required"`
|
||
Departments []uint64 `json:"departments" form:"departments"`
|
||
Roles []uint64 `json:"roles" form:"roles"`
|
||
Remark string `json:"remark" form:"remark"`
|
||
}
|
||
|
||
/**
|
||
* @apiDefine User 用户管理
|
||
*/
|
||
|
||
func (a *User) Info(c *gin.Context) {
|
||
data, err := user.NewInstance()(getSession()(c).(*service.Session)).Info()
|
||
APIResponse(err, data)(c)
|
||
}
|
||
|
||
func (a *User) List(c *gin.Context) {
|
||
form := &struct {
|
||
Name string `json:"name" form:"name"`
|
||
Mobile string `json:"mobile" form:"mobile"`
|
||
Status int `json:"status" form:"status"`
|
||
pageForm
|
||
}{}
|
||
if err := bind(form)(c); err != nil {
|
||
APIFailure(err.(error))(c)
|
||
return
|
||
}
|
||
data, err := user.NewInstance()(getSession()(c).(*service.Session)).List(form.Name, form.Mobile, form.Status, form.Page, form.PageSize)
|
||
APIResponse(err, data)(c)
|
||
}
|
||
|
||
/**
|
||
* @api {get} /api/user/menu 菜单信息
|
||
* @apiVersion 1.0.0
|
||
* @apiName UserMenu
|
||
* @apiGroup User
|
||
*
|
||
* @apiHeader {string} x-token token
|
||
*
|
||
* @apiSuccess (200) {Number} code 成功响应状态码!
|
||
* @apiSuccess (200) {String} msg 成功提示
|
||
* @apiSuccess (200) {Array} data 具体信息
|
||
* @apiSuccess (200) {Number} data.id 菜单ID
|
||
* @apiSuccess (200) {Number} data.parent_id 父级ID
|
||
* @apiSuccess (200) {String} data.name 菜单名称
|
||
* @apiSuccess (200) {Number} data.kind 类型(1:目录,2:菜单)
|
||
* @apiSuccess (200) {String} data.link 访问地址
|
||
* @apiSuccess (200) {String} data.component 组件
|
||
* @apiSuccess (200) {Array} data.children="[]" 子集
|
||
*
|
||
* @apiSuccessExample {json} Success response:
|
||
* HTTPS 200 OK
|
||
* {
|
||
* "code": 200
|
||
* "msg": "ok"
|
||
* "data": [
|
||
* "id": 1,
|
||
* "parent_id": 0,
|
||
* "name": "系统管理",
|
||
* "kind": 1,
|
||
* "link": "1"
|
||
* "component": ""
|
||
* "children": [
|
||
* {
|
||
* "id": 2,
|
||
* "parent_id": 1,
|
||
* "name": "用户管理",
|
||
* "kind": 1,
|
||
* "link": "1"
|
||
* "component": ""
|
||
* "children": [],
|
||
* }
|
||
* ]
|
||
* ]
|
||
* }
|
||
*/
|
||
func (a *User) Menu(c *gin.Context) {
|
||
data, err := user.NewMenu()(getSession()(c).(*service.Session)).List()
|
||
APIResponse(err, data)(c)
|
||
}
|
||
|
||
func (a *User) Add(c *gin.Context) {
|
||
form := &struct {
|
||
userForm
|
||
Password string `json:"password" form:"password" binding:"required"`
|
||
}{}
|
||
if err := bind(form)(c); err != nil {
|
||
APIFailure(err.(error))(c)
|
||
return
|
||
}
|
||
err := user.NewInstance()(getSession()(c).(*service.Session)).Add(&user.InstanceForm{
|
||
Account: form.Account, Name: form.Name, Mobile: form.Mobile, Password: form.Password,
|
||
Remark: form.Remark, Gender: form.Gender, Departments: form.Departments, Roles: form.Roles,
|
||
})
|
||
APIResponse(err)(c)
|
||
}
|
||
|
||
func (a *User) Edit(c *gin.Context) {
|
||
form := &struct {
|
||
idForm
|
||
userForm
|
||
}{}
|
||
if err := bind(form)(c); err != nil {
|
||
APIFailure(err.(error))(c)
|
||
return
|
||
}
|
||
err := user.NewInstance()(getSession()(c).(*service.Session)).Edit(&user.InstanceForm{
|
||
ID: form.ID, Account: form.Account, Name: form.Name, Mobile: form.Mobile,
|
||
Remark: form.Remark, Gender: form.Gender, Departments: form.Departments, Roles: form.Roles,
|
||
})
|
||
APIResponse(err)(c)
|
||
}
|
||
|
||
/**
|
||
* @api {post} /api/user/password/quick 快速设置新密码
|
||
* @apiVersion 1.0.0
|
||
* @apiName UserPasswordQuick
|
||
* @apiGroup User
|
||
*
|
||
* @apiHeader {string} x-token token
|
||
*
|
||
* @apiParam {Number} id ID
|
||
* @apiParam {String} password 登录密码
|
||
* @apiParam {String} repeat_pwd 重复密码
|
||
*
|
||
* @apiSuccess (200) {Number} code 成功响应状态码!
|
||
* @apiSuccess (200) {String} msg 成功提示
|
||
*
|
||
* @apiSuccessExample {json} Success response:
|
||
* HTTPS 200 OK
|
||
* {
|
||
* "code": 200
|
||
* "msg": "ok"
|
||
* "data": null
|
||
* }
|
||
*/
|
||
func (a *User) QuickPassword(c *gin.Context) {
|
||
form := &struct {
|
||
idForm
|
||
Password string `json:"password" form:"password" binding:"required"`
|
||
RepeatPwd string `json:"repeat_pwd" form:"repeat_pwd" binding:"required"`
|
||
}{}
|
||
if err := bind(form)(c); err != nil {
|
||
APIFailure(err.(error))(c)
|
||
return
|
||
}
|
||
err := user.NewInstance()(getSession()(c).(*service.Session)).Password(form.ID, form.Password, form.RepeatPwd)
|
||
APIResponse(err)(c)
|
||
}
|
||
|
||
/**
|
||
* @api {post} /api/user/password/edit 修改新密码
|
||
* @apiVersion 1.0.0
|
||
* @apiName UserPasswordEdit
|
||
* @apiGroup User
|
||
*
|
||
* @apiHeader {string} x-token token
|
||
*
|
||
* @apiParam {Number} id ID
|
||
* @apiParam {String} original_pwd 原始密码
|
||
* @apiParam {String} password 登录密码
|
||
* @apiParam {String} repeat_pwd 重复密码
|
||
*
|
||
* @apiSuccess (200) {Number} code 成功响应状态码!
|
||
* @apiSuccess (200) {String} msg 成功提示
|
||
*
|
||
* @apiSuccessExample {json} Success response:
|
||
* HTTPS 200 OK
|
||
* {
|
||
* "code": 200
|
||
* "msg": "ok"
|
||
* "data": null
|
||
* }
|
||
*/
|
||
func (a *User) EditPassword(c *gin.Context) {
|
||
form := &struct {
|
||
OldPwd string `json:"original_pwd" form:"original_pwd" binding:"required"`
|
||
Password string `json:"password" form:"password" binding:"required"`
|
||
RepeatPwd string `json:"repeat_pwd" form:"repeat_pwd" binding:"required"`
|
||
}{}
|
||
if err := bind(form)(c); err != nil {
|
||
APIFailure(err.(error))(c)
|
||
return
|
||
}
|
||
err := user.NewPerson()(getSession()(c).(*service.Session)).EditPassword(form.OldPwd, form.Password, form.RepeatPwd)
|
||
APIResponse(err)(c)
|
||
}
|
||
|
||
/**
|
||
* @api {post} /api/user/delete 用户删除
|
||
* @apiVersion 1.0.0
|
||
* @apiName UserDelete
|
||
* @apiGroup User
|
||
*
|
||
* @apiHeader {string} x-token token
|
||
*
|
||
* @apiParam {Number} id ID
|
||
*
|
||
* @apiSuccess (200) {Number} code 成功响应状态码!
|
||
* @apiSuccess (200) {String} msg 成功提示
|
||
*
|
||
* @apiSuccessExample {json} Success response:
|
||
* HTTPS 200 OK
|
||
* {
|
||
* "code": 200
|
||
* "msg": "ok"
|
||
* "data": null
|
||
* }
|
||
*/
|
||
func (a *User) Delete(c *gin.Context) {
|
||
form := new(idForm)
|
||
|
||
if err := bind(form)(c); err != nil {
|
||
APIFailure(err.(error))(c)
|
||
return
|
||
}
|
||
err := user.NewInstance()(getSession()(c).(*service.Session)).Delete(form.ID)
|
||
APIResponse(err)(c)
|
||
}
|
||
|
||
func (a *User) Role(c *gin.Context) {
|
||
form := &struct {
|
||
uidForm
|
||
}{}
|
||
if err := bind(form)(c); err != nil {
|
||
APIFailure(err.(error))(c)
|
||
return
|
||
}
|
||
data, err := user.NewRole()(getSession()(c).(*service.Session)).List(form.Convert())
|
||
APIResponse(err, data)(c)
|
||
}
|
||
|
||
func (a *User) RoleBind(c *gin.Context) {
|
||
form := &struct {
|
||
uidForm
|
||
RoleIDs []uint64 `json:"role_ids" form:"role_ids" binding:"required"`
|
||
}{}
|
||
if err := bind(form)(c); err != nil {
|
||
APIFailure(err.(error))(c)
|
||
return
|
||
}
|
||
err := user.NewRole()(getSession()(c).(*service.Session)).Bind(form.Convert(), form.RoleIDs)
|
||
APIResponse(err)(c)
|
||
}
|