feat:完善项目

This commit is contained in:
henry
2021-11-02 10:02:52 +08:00
parent 4734344985
commit 690cd96bed
72 changed files with 5516 additions and 8 deletions

82
app/api/account.go Normal file
View File

@ -0,0 +1,82 @@
package api
import (
"ArmedPolice/app/controller"
"ArmedPolice/app/service"
"github.com/gin-gonic/gin"
)
type Account struct{}
/**
* @apiDefine Account 账号管理
*/
/**
* @api {post} /api/account/login 账号登录
* @apiVersion 1.0.0
* @apiName AccountLogin
* @apiGroup Account
*
* @apiHeader {string} x-equipment 设备平台WebH5App
*
* @apiParam {String} account 登录账号
* @apiParam {String} password 登录密码
* @apiParam {Json} captcha 验证码信息
* @apiParam {String} captcha.key 验证key
* @apiParam {String} captcha.value 验证value
*
* @apiSuccess (200) {Number} code 成功响应状态码!
* @apiSuccess (200) {String} msg 成功提示
* @apiSuccess (200) {String} data token
*
* @apiSuccessExample {json} Success response:
* HTTPS 200 OK
* {
* "code": 200
* "msg": "ok"
* "data": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIxNjE3NDU2OTMwIiwiaWF0IjoiMTYxNjg1MjEzMCIsInVpZCI6IjIwMTMxMTI4MTMwMTg0MTkyMDAifQ.D7oSD4OGdz8rJt0rFNVEl5Ea47_vtuC51IDrY9mUTPo"
* }
*/
func (a *Account) Login(c *gin.Context) {
form := &struct {
Account string `json:"account" form:"account" binding:"required"`
Password string `json:"password" form:"password" binding:"required"`
Captcha struct {
Key string `json:"key" form:"key" binding:"required"`
Value string `json:"value" form:"value" binding:"required"`
} `json:"captcha" form:"captcha" binding:"required"`
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
data, err := controller.NewAccount()(nil).Login(form.Account, form.Password, form.Captcha.Key, form.Captcha.Value,
c.GetHeader("x-equipment"), c.ClientIP())
APIResponse(err, data)(c)
}
/**
* @api {post} /api/account/logout 账号退出
* @apiVersion 1.0.0
* @apiName AccountLogout
* @apiGroup Account
*
* @apiHeader {string} x-token token
*
* @apiSuccess (200) {Number} code 成功响应状态码!
* @apiSuccess (200) {String} msg 成功提示
*
* @apiSuccessExample {json} Success response:
* HTTPS 200 OK
* {
* "code": 200
* "msg": "ok"
* "data": null
* }
*/
func (a *Account) Logout(c *gin.Context) {
err := controller.NewAccount()(getSession()(c).(*service.Session)).Logout()
APIResponse(err)(c)
}

101
app/api/auth.go Normal file
View File

@ -0,0 +1,101 @@
package api
import (
"ArmedPolice/app/controller/auth"
"ArmedPolice/app/service"
"github.com/gin-gonic/gin"
)
type Auth struct{}
/**
* @apiDefine Auth 权限管理
*/
func (a *Auth) List(c *gin.Context) {
data, err := auth.NewInstance()(getSession()(c).(*service.Session)).List()
APIResponse(err, data)(c)
}
/**
* @api {post} /api/auth/tenant/bind 租户权限绑定
* @apiVersion 1.0.0
* @apiName AuthTenantBind
* @apiGroup Auth
*
* @apiHeader {string} x-token token
*
* @apiParam {Number} tenant_id 租户ID
* @apiParam {Array.Number} auth_ids 权限ID
*
* @apiSuccess (200) {Number} code 成功响应状态码!
* @apiSuccess (200) {String} msg 成功提示
*
* @apiSuccessExample {json} Success response:
* HTTPS 200 OK
* {
* "code": 200
* "msg": "ok"
* "data": null
* }
*/
func (a *Auth) TenantBind(c *gin.Context) {
form := &struct {
TenantID uint64 `json:"tenant_id" form:"tenant_id" binding:"required"`
AuthIDs []uint64 `json:"auth_ids" form:"auth_ids" binding:"required"`
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := auth.NewTenant()(getSession()(c).(*service.Session)).Bind(form.TenantID, form.AuthIDs)
APIResponse(err)(c)
}
func (a *Auth) Role(c *gin.Context) {
form := &struct {
RoleID uint64 `json:"role_id" form:"role_id" binding:"required"`
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
data, err := auth.NewRole()(getSession()(c).(*service.Session)).List(form.RoleID)
APIResponse(err, data)(c)
}
/**
* @api {post} /api/auth/role/bind 角色权限绑定
* @apiVersion 1.0.0
* @apiName AuthRoleBind
* @apiGroup Auth
*
* @apiHeader {string} x-token token
*
* @apiParam {Number} role_id 角色ID
* @apiParam {Array.Number} auth_ids 权限ID
*
* @apiSuccess (200) {Number} code 成功响应状态码!
* @apiSuccess (200) {String} msg 成功提示
*
* @apiSuccessExample {json} Success response:
* HTTPS 200 OK
* {
* "code": 200
* "msg": "ok"
* "data": null
* }
*/
func (a *Auth) RoleBind(c *gin.Context) {
form := &struct {
RoleID uint64 `json:"role_id" form:"role_id" binding:"required"`
AuthIDs []uint64 `json:"auth_ids" form:"auth_ids" binding:"required"`
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := auth.NewRole()(getSession()(c).(*service.Session)).Bind(form.RoleID, form.AuthIDs)
APIResponse(err)(c)
}

108
app/api/base.go Normal file
View File

@ -0,0 +1,108 @@
package api
import (
"ArmedPolice/config"
"ArmedPolice/serve/logger"
"ArmedPolice/utils"
"errors"
"fmt"
"net/http"
"github.com/gin-gonic/gin/binding"
"github.com/gin-gonic/gin"
)
const (
SuccessCode = 200 //成功的状态码
FailureCode = 999 //失败的状态码
)
type response struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
type ApiHandle func(c *gin.Context, syslog ...bool) interface{}
func (c ApiHandle) SysLog() {
fmt.Println(123)
//service.Publish(config.EventForSysLogProduce, _session.Community, _session.UID, _session.Name, mode, event, content,
// c.Get("params"), c.ClientIP())
}
func getSession() ApiHandle {
return func(c *gin.Context, log ...bool) interface{} {
value, _ := c.Get(config.TokenForSession)
return value
}
}
func bind(req interface{}) ApiHandle {
return func(c *gin.Context, log ...bool) interface{} {
var err error
if c.Request.Method == "GET" {
err = c.ShouldBindQuery(req)
} else {
err = c.ShouldBindBodyWith(req, binding.JSON)
}
if err != nil {
logger.ErrorF("Request URL【%s】Params Bind Error%v", c.Request.URL, err)
return errors.New("参数异常/缺失")
}
c.Set("params", utils.AnyToJSON(req))
return nil
}
}
func APISuccess(data ...interface{}) ApiHandle {
return func(c *gin.Context, log ...bool) interface{} {
resp := &response{
Code: SuccessCode,
Message: "ok",
}
if len(data) > 0 {
resp.Data = data[0]
}
c.JSON(http.StatusOK, resp)
return nil
}
}
func APIFailure(err error) ApiHandle {
return func(c *gin.Context, log ...bool) interface{} {
resp := &response{
Code: FailureCode,
Message: "failure",
}
if err != nil {
resp.Message = err.Error()
}
c.JSON(http.StatusOK, resp)
c.Abort()
return nil
}
}
func APIResponse(err error, data ...interface{}) ApiHandle {
return func(c *gin.Context, log ...bool) interface{} {
resp := &response{
Code: SuccessCode,
Message: "ok",
}
if err != nil {
resp.Code = FailureCode
resp.Message = err.Error()
c.JSON(http.StatusOK, resp)
c.Abort()
return nil
}
if len(data) > 0 {
resp.Data = data[0]
}
c.JSON(http.StatusOK, resp)
return nil
}
}

42
app/api/captcha.go Normal file

File diff suppressed because one or more lines are too long

34
app/api/config.go Normal file
View File

@ -0,0 +1,34 @@
package api
import (
"ArmedPolice/app/controller"
"github.com/gin-gonic/gin"
)
type Config struct{}
func (a *Config) List(c *gin.Context) {
form := &struct {
Kind int `json:"kind" form:"kind"`
pageForm
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
data, err := controller.NewConfig()().Config(form.Kind, form.Page, form.PageSize)
APIResponse(err, data)
}
func (a *Config) Edit(c *gin.Context) {
form := &struct {
Params map[string]interface{} `json:"params" form:"params" binding:"required"`
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := controller.NewConfig()().Form(form.Params)
APIResponse(err)
}

23
app/api/logs.go Normal file
View File

@ -0,0 +1,23 @@
package api
import (
"ArmedPolice/app/controller"
"ArmedPolice/app/service"
"github.com/gin-gonic/gin"
)
type Log struct{}
func (a *Log) Login(c *gin.Context) {
form := &struct {
Name string `json:"name" form:"name"`
pageForm
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
data, err := controller.NewLog()(getSession()(c).(*service.Session)).Login(form.Name, form.Page, form.PageSize)
APIResponse(err, data)(c)
}

425
app/api/menu.go Normal file
View File

@ -0,0 +1,425 @@
package api
import (
"ArmedPolice/app/controller/menu"
"ArmedPolice/app/service"
"github.com/gin-gonic/gin"
)
type Menu struct{}
type (
// menuForm 菜单信息
menuForm struct {
ParentID uint64 `json:"parent_id" form:"parent_id"`
Name string `json:"name" form:"name" binding:"required"`
Kind int `json:"kind" form:"kind" binding:"required"`
Link string `json:"link" form:"link"`
Component string `json:"component" form:"component"`
Icon string `json:"icon" form:"icon"`
Auth int `json:"auth" form:"auth"`
Sort int `json:"sort" form:"sort"`
Status int `json:"status" form:"status"`
Remark string `json:"remark" form:"remark"`
}
)
/**
* @apiDefine Menu 菜单管理
*/
/**
* @api {get} /api/menu/list 菜单列表
* @apiVersion 1.0.0
* @apiName MenuList
* @apiGroup Menu
*
* @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 *Menu) List(c *gin.Context) {
data, err := menu.NewInstance()(getSession()(c).(*service.Session)).List()
APIResponse(err, data)(c)
}
/**
* @api {post} /api/menu/add 菜单添加
* @apiVersion 1.0.0
* @apiName MenuAdd
* @apiGroup Menu
*
* @apiHeader {string} x-token token
*
* @apiParam {Number} parent_id 父级ID
* @apiParam {String} name 菜单名
* @apiParam {Number} kind 菜单类型1目录2菜单
* @apiParam {String} link 访问地址
* @apiParam {String} component 页面组件
* @apiParam {String} icon 页面icon
* @apiParam {Number} auth 菜单权限0普通权限1超管权限
* @apiParam {Number} sort 排序,从大到小
* @apiParam {Number} status 忘了干嘛,没用
* @apiParam {String} remark 备注信息
*
* @apiSuccess (200) {Number} code 成功响应状态码!
* @apiSuccess (200) {String} msg 成功提示
*
* @apiSuccessExample {json} Success response:
* HTTPS 200 OK
* {
* "code": 200
* "msg": "ok"
* }
*/
func (a *Menu) Add(c *gin.Context) {
form := new(menuForm)
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := menu.NewInstance()(getSession()(c).(*service.Session)).Data(&menu.InstanceParams{
ParentID: form.ParentID, Name: form.Name, Kind: form.Kind, Link: form.Link, Component: form.Component,
Icon: form.Icon, Auth: form.Auth, Sort: form.Sort, Status: form.Status, Remark: form.Remark,
})
APIResponse(err)(c)
}
/**
* @api {post} /api/menu/edit 菜单修改
* @apiVersion 1.0.0
* @apiName MenuEdit
* @apiGroup Menu
*
* @apiHeader {string} x-token token
*
* @apiParam {Number} id ID
* @apiParam {Number} parent_id 父级ID
* @apiParam {String} name 菜单名
* @apiParam {Number} kind 菜单类型1目录2菜单
* @apiParam {String} link 访问地址
* @apiParam {String} component 页面组件
* @apiParam {String} icon 页面icon
* @apiParam {Number} auth 菜单权限0普通权限1超管权限
* @apiParam {Number} sort 排序,从大到小
* @apiParam {Number} status 禁用状态1启用2禁用
* @apiParam {String} remark 备注信息
*
* @apiSuccess (200) {Number} code 成功响应状态码!
* @apiSuccess (200) {String} msg 成功提示
*
* @apiSuccessExample {json} Success response:
* HTTPS 200 OK
* {
* "code": 200
* "msg": "ok"
* }
*/
func (a *Menu) Edit(c *gin.Context) {
form := &struct {
idForm
menuForm
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := menu.NewInstance()(getSession()(c).(*service.Session)).Data(&menu.InstanceParams{
ID: form.ID, ParentID: form.ParentID, Name: form.Name, Kind: form.Kind, Link: form.Link, Component: form.Component,
Icon: form.Icon, Auth: form.Auth, Sort: form.Sort, Status: form.Status, Remark: form.Remark,
})
APIResponse(err)(c)
}
/**
* @api {post} /api/menu/status 菜单状态
* @apiVersion 1.0.0
* @apiName MenuStatus
* @apiGroup Menu
*
* @apiHeader {string} x-token token
*
* @apiParam {Number} id ID
* @apiParam {Number} status 状态1启动2禁用
*
* @apiSuccess (200) {Number} code=200 成功响应状态码!
* @apiSuccess (200) {String} msg="ok" 成功提示
*
* @apiSuccessExample {json} Success response:
* HTTPS 200 OK
* {
* "code": 200
* "msg": "ok"
* "data": null
* }
*/
func (a *Menu) Status(c *gin.Context) {
form := &struct {
idForm
Status int `json:"status" form:"status" binding:"required"`
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := menu.NewInstance()(getSession()(c).(*service.Session)).Status(form.ID, form.Status)
APIResponse(err)(c)
}
/**
* @api {post} /api/menu/delete 删除菜单
* @apiVersion 1.0.0
* @apiName MenuDelete
* @apiGroup Menu
*
* @apiHeader {string} x-token token
*
* @apiParam {Number} id ID
*
* @apiSuccess (200) {Number} code=200 成功响应状态码!
* @apiSuccess (200) {String} msg="ok" 成功提示
*
* @apiSuccessExample {json} Success response:
* HTTPS 200 OK
* {
* "code": 200
* "msg": "ok"
* "data": null
* }
*/
func (a *Menu) Delete(c *gin.Context) {
form := new(idForm)
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := menu.NewInstance()(getSession()(c).(*service.Session)).Delete(form.ID)
APIResponse(err)(c)
}
/**
* @api {get} /api/menu/user 用户菜单列表
* @apiVersion 1.0.0
* @apiName MenuUser
* @apiGroup Menu
*
* @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 *Menu) User(c *gin.Context) {
data, err := menu.NewUser()(getSession()(c).(*service.Session)).List()
APIResponse(err, data)(c)
}
func (a *Menu) Tenant(c *gin.Context) {
form := &struct {
TenantID uint64 `json:"tenant_id" form:"tenant_id" binding:"required"`
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
data, err := menu.NewTenant()(getSession()(c).(*service.Session)).List(form.TenantID)
APIResponse(err, data)(c)
}
/**
* @api {post} /api/tenant/bind 租户菜单绑定
* @apiVersion 1.0.0
* @apiName MenuTenantBind
* @apiGroup Menu
*
* @apiHeader {string} x-token token
*
* @apiParam {Number} tenant_id 租户ID
* @apiParam {Array.Number} menu_ids 菜单ID
*
* @apiSuccess (200) {Number} code=200 成功响应状态码!
* @apiSuccess (200) {String} msg="ok" 成功提示
*
* @apiSuccessExample {json} Success response:
* HTTPS 200 OK
* {
* "code": 200
* "msg": "ok"
* "data": null
* }
*/
func (a *Menu) TenantBind(c *gin.Context) {
form := &struct {
TenantID uint64 `json:"tenant_id" form:"tenant_id" binding:"required"`
MenuIDs []uint64 `json:"menu_ids" form:"menu_ids" binding:"required"`
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := menu.NewTenant()(getSession()(c).(*service.Session)).Bind(form.TenantID, form.MenuIDs)
APIResponse(err)(c)
}
/**
* @api {post} /api/menu/role 角色菜单列表
* @apiVersion 1.0.0
* @apiName MenuRole
* @apiGroup Menu
*
* @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 *Menu) Role(c *gin.Context) {
form := &struct {
RoleID uint64 `json:"role_id" form:"role_id" binding:"required"`
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
data, err := menu.NewRole()(getSession()(c).(*service.Session)).List(form.RoleID)
APIResponse(err, data)(c)
}
/**
* @api {post} /api/role/bind 角色菜单绑定
* @apiVersion 1.0.0
* @apiName MenuRoleBind
* @apiGroup Menu
*
* @apiHeader {string} x-token token
*
* @apiParam {Number} role_id 角色ID
* @apiParam {Array.Number} menu_ids 菜单ID
*
* @apiSuccess (200) {Number} code=200 成功响应状态码!
* @apiSuccess (200) {String} msg="ok" 成功提示
*
* @apiSuccessExample {json} Success response:
* HTTPS 200 OK
* {
* "code": 200
* "msg": "ok"
* "data": null
* }
*/
func (a *Menu) RoleBind(c *gin.Context) {
form := &struct {
RoleID uint64 `json:"role_id" form:"role_id" binding:"required"`
MenuIDs []uint64 `json:"menu_ids" form:"menu_ids" binding:"required"`
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := menu.NewRole()(getSession()(c).(*service.Session)).Bind(form.RoleID, form.MenuIDs)
APIResponse(err)(c)
}

107
app/api/role.go Normal file
View File

@ -0,0 +1,107 @@
package api
import (
"ArmedPolice/app/controller/role"
"ArmedPolice/app/service"
"github.com/gin-gonic/gin"
)
type Role struct{}
func (a *Role) List(c *gin.Context) {
form := &struct {
Name string `json:"name" form:"name"`
Status int `json:"status" form:"status"`
pageForm
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
data, err := role.NewInstance()(getSession()(c).(*service.Session)).List(form.Name, form.Status, form.Page, form.PageSize)
APIResponse(err, data)(c)
}
func (a *Role) Select(c *gin.Context) {
data, err := role.NewInstance()(getSession()(c).(*service.Session)).Select()
APIResponse(err, data)(c)
}
func (a *Role) Add(c *gin.Context) {
form := &struct {
Name string `json:"name" form:"name" binding:"required"`
Remark string `json:"remark" form:"remark" binding:"required"`
Sort int `json:"sort" form:"sort"`
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := role.NewInstance()(getSession()(c).(*service.Session)).Data(0, form.Name, form.Remark, form.Sort)
APIResponse(err)(c)
}
func (a *Role) Edit(c *gin.Context) {
form := &struct {
idForm
Name string `json:"name" form:"name" binding:"required"`
Remark string `json:"remark" form:"remark" binding:"required"`
Sort int `json:"sort" form:"sort"`
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := role.NewInstance()(getSession()(c).(*service.Session)).Data(form.ID, form.Name, form.Remark, form.Sort)
APIResponse(err)(c)
}
func (a *Role) Status(c *gin.Context) {
form := &struct {
idForm
Status int `json:"status" form:"status" binding:"required"`
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := role.NewInstance()(getSession()(c).(*service.Session)).Status(form.ID, form.Status)
APIResponse(err)(c)
}
func (a *Role) Delete(c *gin.Context) {
form := new(idForm)
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := role.NewInstance()(getSession()(c).(*service.Session)).Delete(form.ID)
APIResponse(err)(c)
}
func (a *Role) User(c *gin.Context) {
form := &struct {
uidForm
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
data, err := role.NewUser()(getSession()(c).(*service.Session)).List(form.Convert())
APIResponse(err, data)(c)
}
func (a *Role) UserBind(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 := role.NewUser()(getSession()(c).(*service.Session)).Bind(form.Convert(), form.RoleIDs)
APIResponse(err)(c)
}

37
app/api/struct.go Normal file
View File

@ -0,0 +1,37 @@
package api
import (
"ArmedPolice/config"
"ArmedPolice/utils"
"strings"
)
type idForm struct {
ID uint64 `json:"id" form:"id" binding:"required"`
}
type uidForm struct {
UID string `json:"uid" form:"uid" binding:"required"`
}
func (this *uidForm) Convert() uint64 {
return utils.StringToUnit64(this.UID)
}
type imageForm struct {
Image string `json:"image" form:"image"`
}
func (this *imageForm) FilterImageURL() string {
return strings.Replace(this.Image, config.SettingInfo.Domain, "", -1)
}
type positionForm struct {
Longitude float64 `json:"longitude" form:"longitude" binding:"required"`
Latitude float64 `json:"latitude" form:"latitude" binding:"required"`
}
type pageForm struct {
Page int `json:"current" form:"current" binding:"required"`
PageSize int `json:"pageSize" form:"pageSize" binding:"required"`
}

403
app/api/tenant.go Normal file
View File

@ -0,0 +1,403 @@
package api
import (
"ArmedPolice/app/controller/tenant"
"ArmedPolice/app/service"
"github.com/gin-gonic/gin"
)
type Tenant struct{}
type (
// tenantForm 基本信息
tenantForm struct {
Name string `json:"name" form:"name" binding:"required"` // 名称
imageForm // 图片
Account string `json:"account" form:"account" binding:"required"` // 登录帐号
Password string `json:"password" form:"password" binding:"required"` // 登录密码
RepeatPwd string `json:"repeat_pwd" form:"repeat_pwd" binding:"required"` // 重复登录密码
Deadline string `json:"deadline" form:"deadline" binding:"required"` // 协约终止时间
Remark string `json:"remark" form:"remark"` // 备注
}
// tenantSettingForm 配置信息
tenantSettingForm struct {
Protocol []uint `json:"protocol" form:"protocol" binding:"required"` // 消息协议
MaxDevices int `json:"max_devices" form:"max_devices" binding:"required"` // 允许最多的设备数
MaxCustomer int `json:"max_customer" form:"max_customer"` // 允许最多的客户数
}
)
/**
* @apiDefine Tenant 租户管理
*/
/**
* @api {post} /api/tenant/list 租户列表
* @apiVersion 1.0.0
* @apiName TenantList
* @apiGroup Tenant
*
* @apiHeader {string} x-token token
*
* @apiParam {String} [name='""'] 租户名称
* @apiParam {Number} [status=0] 租户状态
* @apiParam {Number} current 当前页
* @apiParam {Number} page_size 页展示数
*
* @apiSuccess (200) {Number} code 成功响应状态码!
* @apiSuccess (200) {String} msg 成功提示
* @apiSuccess (200) {Array} data 具体信息
* @apiSuccess (200) {Number} data.id ID
* @apiSuccess (200) {String} data.key 标识key
* @apiSuccess (200) {String} data.name 公司名称
* @apiSuccess (200) {Time} data.deadline 协议到期时间
* @apiSuccess (200) {Number} data.device_count 设备数量
* @apiSuccess (200) {Json} data.config 配置信息
* @apiSuccess (200) {Number} data.config.max_devices 最大设备数
* @apiSuccess (200) {Number} data.config.max_customer 最大客户数
* @apiSuccess (200) {Number} data.config.protocol 支持协议(&
*
* @apiSuccessExample {json} Success response:
* HTTPS 200 OK
* {
* "code": 200
* "msg": "ok"
* "data": [
* {
* "id": 1,
* "key": "3xPdWH",
* "image": "",
* "name": "商挈智能",
* "deadline": "2021-12-31T23:59:59+08:00",
* "remark": "测试",
* "created_at": "2021-07-27T10:45:18+08:00",
* "updated_at": "2021-07-27T10:45:18+08:00",
* "device_count": 0,
* "config": {
* "max_devices": 1,
* "max_customer": 1,
* "protocol": 3
* }
* }
* ]
* }
*/
func (a *Tenant) List(c *gin.Context) {
form := &struct {
Name string `json:"name" form:"name"`
Status int `json:"status" form:"status"`
pageForm
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
data, err := tenant.NewInstance()(getSession()(c).(*service.Session)).List(form.Name, form.Status, form.Page, form.PageSize)
APIResponse(err, data)(c)
}
/**
* @api {post} /api/tenant/add 租户添加
* @apiVersion 1.0.0
* @apiName TenantAdd
* @apiGroup Tenant
*
* @apiHeader {string} x-token token
*
* @apiParam {String} name 租户名称
* @apiParam {String} image 租户图片
* @apiParam {String} account 租户管理员登录帐号
* @apiParam {String} password 租户管理员登录密码
* @apiParam {String} repeat_pwd 重复密码
* @apiParam {Number} max_devices 最大设备数
* @apiParam {Number} [max_customer=0] 租户可拥有的最大客户数
* @apiParam {Array.Number} protocol 消息协议
* @apiParam {Time} deadline 协议有效期2021-12-31
* @apiParam {String} [remark="''"] 备注信息
* @apiParam {Number} current 当前页
* @apiParam {Number} page_size 页展示数
*
* @apiSuccess (200) {Number} code 成功响应状态码!
* @apiSuccess (200) {String} msg 成功提示
*
* @apiSuccessExample {json} Success response:
* HTTPS 200 OK
* {
* "code": 200
* "msg": "ok"
* "data": null
* }
*/
func (a *Tenant) Add(c *gin.Context) {
form := new(tenantForm)
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := tenant.NewInstance()(getSession()(c).(*service.Session)).Add(&tenant.InstanceParams{Name: form.Name,
Image: form.FilterImageURL(), Account: form.Account, Password: form.Password, RepeatPwd: form.RepeatPwd,
Deadline: form.Deadline, Remark: form.Remark,
})
APIResponse(err)(c)
}
/**
* @api {post} /api/tenant/edit 租户修改
* @apiVersion 1.0.0
* @apiName TenantEdit
* @apiGroup Tenant
*
* @apiHeader {string} x-token token
*
* @apiParam {Number} id ID
* @apiParam {String} name 租户名称
* @apiParam {String} image 租户图片
* @apiParam {String} [remark="''"] 备注信息
*
* @apiSuccess (200) {Number} code 成功响应状态码!
* @apiSuccess (200) {String} msg 成功提示
*
* @apiSuccessExample {json} Success response:
* HTTPS 200 OK
* {
* "code": 200
* "msg": "ok"
* "data": null
* }
*/
func (a *Tenant) Edit(c *gin.Context) {
form := &struct {
idForm
Name string `json:"name" form:"name" binding:"required"`
imageForm
Remark string `json:"remark" form:"remark"`
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := tenant.NewInstance()(getSession()(c).(*service.Session)).Edit(&tenant.InstanceParams{ID: form.ID, Name: form.Name,
Image: form.FilterImageURL(), Remark: form.Remark,
})
APIResponse(err)(c)
}
/**
* @api {post} /api/tenant/edit/password 租户修改密码
* @apiVersion 1.0.0
* @apiName TenantEditPassword
* @apiGroup Tenant
*
* @apiHeader {string} x-token token
*
* @apiParam {Number} id ID
* @apiParam {String} name 租户名称
* @apiParam {String} image 租户图片
* @apiParam {String} [remark="''"] 备注信息
*
* @apiSuccess (200) {Number} code 成功响应状态码!
* @apiSuccess (200) {String} msg 成功提示
*
* @apiSuccessExample {json} Success response:
* HTTPS 200 OK
* {
* "code": 200
* "msg": "ok"
* "data": null
* }
*/
func (a *Tenant) EditPassword(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 := tenant.NewInstance()(getSession()(c).(*service.Session)).Edit(&tenant.InstanceParams{ID: form.ID, Password: form.Password,
RepeatPwd: form.RepeatPwd,
})
APIResponse(err)(c)
}
/**
* @api {post} /api/tenant/detail 租户详细信息
* @apiVersion 1.0.0
* @apiName TenantDetail
* @apiGroup Tenant
*
* @apiHeader {string} x-token token
*
* @apiParam {Number} id ID
* @apiParam {Number} type 详细信息1基本信息2资产设备信息3成员信息4权限信息
* @apiParam {Number} [current=0] 当前页
* @apiParam {Number} [page_size=0] 页展示数
* @apiParam {String} [name="''"] 成员名称
* @apiParam {Number} [status=0] 成员状态
*
* @apiSuccess (200) {Number} code 成功响应状态码!
* @apiSuccess (200) {String} msg 成功提示
*
* @apiSuccessExample {json} Success response:
* HTTPS 200 OK
* {
* "code": 200
* "msg": "ok"
* "data": Any
* }
*/
func (a *Tenant) Detail(c *gin.Context) {
form := &struct {
idForm
Type tenant.InstanceDetailType `json:"type" form:"type" binding:"required"`
Page int `json:"current" form:"current"`
PageSize int `json:"pageSize" form:"pageSize"`
Name string `json:"name" form:"name"`
Status int `json:"status" form:"status"`
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
data, err := tenant.NewInstance()(getSession()(c).(*service.Session)).Detail(form.ID, form.Type, form.Page,
form.PageSize, form.Name, form.Status)
APIResponse(err, data)(c)
}
/**
* @api {post} /api/tenant/renewal 租户续期
* @apiVersion 1.0.0
* @apiName TenantRenewal
* @apiGroup Tenant
*
* @apiHeader {string} x-token token
*
* @apiParam {Number} id ID
* @apiParam {Time} deadline 协议有效期2021-12-31
*
* @apiSuccess (200) {Number} code 成功响应状态码!
* @apiSuccess (200) {String} msg 成功提示
*
* @apiSuccessExample {json} Success response:
* HTTPS 200 OK
* {
* "code": 200
* "msg": "ok"
* "data": null
* }
*/
func (a *Tenant) Renewal(c *gin.Context) {
form := &struct {
idForm
Deadline string `json:"deadline" form:"deadline" binding:"required"`
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := tenant.NewInstance()(getSession()(c).(*service.Session)).Renewal(form.ID, form.Deadline)
APIResponse(err)(c)
}
/**
* @api {post} /api/tenant/start_up 租户启用
* @apiVersion 1.0.0
* @apiName TenantStartUp
* @apiGroup Tenant
*
* @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 *Tenant) StartUp(c *gin.Context) {
form := new(idForm)
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := tenant.NewInstance()(getSession()(c).(*service.Session)).StartUp(form.ID)
APIResponse(err)(c)
}
/**
* @api {post} /api/tenant/disable 租户禁用
* @apiVersion 1.0.0
* @apiName TenantDisable
* @apiGroup Tenant
*
* @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 *Tenant) Disable(c *gin.Context) {
form := new(idForm)
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := tenant.NewInstance()(getSession()(c).(*service.Session)).Disable(form.ID)
APIResponse(err)(c)
}
/**
* @api {post} /api/tenant/member/bind 租户用户绑定状态
* @apiVersion 1.0.0
* @apiName TenantMemberBind
* @apiGroup Tenant
*
* @apiHeader {string} x-token token
*
* @apiParam {Number} id ID
* @apiParam {Number} status 状态1启用2禁用
*
* @apiSuccess (200) {Number} code 成功响应状态码!
* @apiSuccess (200) {String} msg 成功提示
*
* @apiSuccessExample {json} Success response:
* HTTPS 200 OK
* {
* "code": 200
* "msg": "ok"
* "data": null
* }
*/
func (a *Tenant) MemberBind(c *gin.Context) {
form := &struct {
uidForm
Status int `json:"status" form:"status" binding:"required"`
}{}
if err := bind(form)(c); err != nil {
APIFailure(err.(error))(c)
return
}
err := tenant.NewInstance()(getSession()(c).(*service.Session)).MemberBind(form.Convert(), form.Status)
APIResponse(err)(c)
}

64
app/api/upload.go Normal file
View File

@ -0,0 +1,64 @@
package api
import (
"ArmedPolice/config"
"ArmedPolice/lib"
"github.com/gin-gonic/gin"
)
type Upload struct{}
/**
* @apiDefine Upload 上传管理
*/
/**
* @api {post} /api/upload 上传接口
* @apiVersion 1.0.0
* @apiName Upload
* @apiGroup Upload
*
* @apiHeader {string} x-token token
*
* @apiParam {File} file 文件信息
*
* @apiSuccess (200) {Number} code 成功响应状态码!
* @apiSuccess (200) {String} msg 成功提示
* @apiSuccess (200) {Object} data 具体信息
* @apiSuccess (200) {Number} data.url 文件访问地址
* @apiSuccess (200) {String} data.filepath 文件地址
* @apiSuccess (200) {String} data.filename 文件名称
*
* @apiSuccessExample {json} Success response:
* HTTPS 200 OK
* {
* "code": 200
* "msg": "ok"
* "data":{
* "url": "http://192.168.99.185:8010/upload/20210401/ad228811386cb8cd089a9d668d2885cd.png",
* "filepath": "/upload/20210401/ad228811386cb8cd089a9d668d2885cd.png",
* "filename": "8251863448d7ed13393bf0aae2211272.jpg"
* }
* }
*/
func (a *Upload) Upload(c *gin.Context) {
file, err := c.FormFile("file")
if err != nil {
APIFailure(err)(c)
return
}
resp := new(lib.UploadHandle)
if resp, err = lib.Upload(config.SettingInfo.Upload.Path, config.SettingInfo.Upload.Exts, config.SettingInfo.Upload.Size,
config.SettingInfo.Upload.Rename).Handle()(file, config.SettingInfo.Domain); err != nil {
APIFailure(err)(c)
return
}
if err = c.SaveUploadedFile(file, resp.RelativePath); err != nil {
APIFailure(err)(c)
return
}
APISuccess(resp)(c)
}

115
app/api/user.go Normal file
View File

@ -0,0 +1,115 @@
package api
import (
"ArmedPolice/app/controller/user"
"ArmedPolice/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)
}
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)
}
func (a *User) Password(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)
}
func (a *User) EditPassword(c *gin.Context) {
form := &struct {
OldPwd string `json:"old_pwd" form:"old_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)
}
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)
}