404 lines
11 KiB
Go
404 lines
11 KiB
Go
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)
|
||
}
|