12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package proxy
- import (
- "encoding/json"
- "errors"
- "github.com/astaxie/beego/logs"
- "nginx-ui/server/base"
- "nginx-ui/server/models"
- )
- type Controller struct {
- base.Controller
- service *Service
- }
- var Proxy = NewReverseProxy()
- var Instance = &Controller{
- service: NewProxyService(),
- }
- func (c *Controller) RefreshProxies() {
- list := c.service.GetAll(&GetProxyListReq{Enable: true})
- Proxy.LoadProxies(list)
- }
- // List 登录
- func (c *Controller) List() {
- user := c.RequiredUser()
- if user == nil {
- return
- }
- var req GetProxyListReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- logs.Error(err, string(c.Ctx.Input.RequestBody))
- c.ErrorJson(err)
- return
- }
- req.Uid = user.Id
- resp := c.service.GetAll(&req)
- c.SetData(resp).Json()
- }
- // Save 获取全部用户信息
- func (c *Controller) Save() {
- user := c.RequiredUser()
- if user == nil {
- return
- }
- req := models.ProxyEntity{}
- if !c.ReadBody(&req) {
- return
- }
- if req.Id == 0 {
- req.UserId = user.Id
- }
- resp, err := c.service.Update(&req)
- if err != nil {
- logs.Warn("Users get fail: %v", err)
- c.ErrorJson(err)
- return
- }
- c.RefreshProxies()
- c.SetData(resp).Json()
- }
- // Remove 删除指定的代理
- func (c *Controller) Remove() {
- user := c.RequiredUser()
- if user == nil {
- return
- }
- req := models.ProxyEntity{}
- if !c.ReadBody(&req) {
- return
- }
- if req.UserId != user.Id {
- c.ErrorJson(errors.New("更新失败,无操作权限!"))
- return
- }
- resp, err := c.service.Delete(&req)
- if err != nil {
- logs.Warn("Users get fail: %v", err)
- c.ErrorJson(err)
- return
- }
- c.SetSession("user", *resp)
- c.RefreshProxies()
- c.SetData(resp).Json()
- }
|