Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

杨树贤 / kefu_server

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Settings
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Find file
Normal viewHistoryPermalink
Switch branch/tag
  • kefu_server
  • controllers
  • base_controller.go
base_controller.go 1.23 KB
chenxianqi's avatar
Optimize the code
f68f5df4
 
chenxianqi committed 5 years ago
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
package controllers

import (
	"kefu_server/configs"
	"kefu_server/models"
	"kefu_server/services"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
)

// BaseControllerInterface interface
type BaseControllerInterface interface {
	JSON()
	Prepare()
	GetAuthInfo() *models.Auths
}

// BaseController Base class
type BaseController struct {
	beego.Controller
}

// JSON handle http Response
// Return json data, and stop moving on
func (c *BaseController) JSON(status configs.ResponseStatusType, message string, data interface{}) {
	c.Ctx.Output.Header("Access-Control-Max-Age", "2592000")
	msg := message
	if status != configs.ResponseSucess && status != configs.ResponseFail {
		msg = "sorry server error"
		data = nil
	}
chenxianqi's avatar
Project progress follow-up
e023aa90
 
chenxianqi committed 5 years ago
33
	c.Data["json"] = &models.ResponseDto{Code: status, Message: msg, Data: &data}
chenxianqi's avatar
Optimize the code
f68f5df4
 
chenxianqi committed 5 years ago
34 35 36 37 38 39 40
	c.ServeJSON()
	c.StopRun()
}

// GetAuthInfo get current anth user that AuthInfo
func (c *BaseController) GetAuthInfo() *models.Auths {
	token := c.Ctx.Input.Header("Authorization")
chenxianqi's avatar
Project progress follow-up
e023aa90
 
chenxianqi committed 5 years ago
41
	var authsRepository = services.GetAuthsRepositoryInstance()
chenxianqi's avatar
Optimize the code
f68f5df4
 
chenxianqi committed 5 years ago
42 43 44 45 46 47 48
	auth := authsRepository.GetAuthInfo(token)
	if auth == nil {
		logs.Warn("GetAuthInfo fun error------------登录已失效!")
		c.JSON(configs.ResponseFail, "登录已失效!", nil)
	}
	return auth
}