utils.go 4.5 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7 8 9
package controller

import (
	"encoding/json"
	"errors"
	"net/http"
	"reflect"
	"strconv"

黄孟柱 已提交
10
	log "github.com/eolinker/goku-api-gateway/goku-log"
E
eoLinker API Management 已提交
11

黄孟柱 已提交
12
	"github.com/eolinker/goku-api-gateway/console/module/account"
E
eoLinker API Management 已提交
13 14 15
)

const (
Y
Your Name 已提交
16
	//OperationEDIT edit权限
E
eoLinker API Management 已提交
17
	OperationEDIT = "edit"
Y
Your Name 已提交
18
	//OperationREAD read权限
E
eoLinker API Management 已提交
19 20 21
	OperationREAD = "read"
)
const (
Y
Your Name 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
	//OperationNone none
	OperationNone = ""
	//OperationAPI api
	OperationAPI = "apiManagement"
	//OperationADMIN admin
	OperationADMIN = "adminManagement"
	//OperationLoadBalance balance
	OperationLoadBalance = "loadBalance"
	//OperationStrategy strategy
	OperationStrategy = "strategyManagement"
	//OperationNode node
	OperationNode = "nodeManagement"
	//OperationPlugin plugin
	OperationPlugin = "pluginManagement"
	//OperationGatewayConfig gatewayConfig
E
eoLinker API Management 已提交
37
	OperationGatewayConfig = "gatewayConfig"
Y
Your Name 已提交
38 39
	//OperationAlert alert
	OperationAlert = "alertManagement"
E
eoLinker API Management 已提交
40 41
)

Y
Your Name 已提交
42
//PageInfo pageInfo
E
eoLinker API Management 已提交
43 44 45 46 47 48 49
type PageInfo struct {
	ItemNum  int `json:"itemNum,"`
	Page     int `json:"page,omitempty"`
	PageSize int `json:"pageSize,omitempty"`
	TotalNum int `json:"totalNum,"`
}

Y
Your Name 已提交
50
//SetPage 设置页码信息
E
eoLinker API Management 已提交
51 52 53 54 55 56
func (p *PageInfo) SetPage(page, size, total int) *PageInfo {
	p.Page = page
	p.PageSize = size
	p.TotalNum = total
	return p
}
Y
Your Name 已提交
57 58

//NewItemNum 创建pageInfo对象
E
eoLinker API Management 已提交
59 60 61 62 63
func NewItemNum(num int) *PageInfo {
	return &PageInfo{
		ItemNum: num,
	}
}
Y
Your Name 已提交
64 65

//WriteResult 输出返回结果
E
eoLinker API Management 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
func WriteResult(w http.ResponseWriter, code string, resultType, resultKey string, result interface{}, pageInfo *PageInfo) {
	ret := map[string]interface{}{
		"statusCode": code,
	}
	if resultType != "" {
		ret["type"] = resultType
	}
	if result != nil {
		ret[resultKey] = result
	}
	if pageInfo != nil {
		ret["page"] = pageInfo
	}

	data, err := json.Marshal(ret)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		log.WithFields(ret).Debug(err)
		return
	}

	i, err := w.Write(data)
	if err != nil {
		log.WithFields(ret).Debug("write error:", err)
	} else {
		log.WithFields(ret).Debug("write :", i)
	}
}
Y
Your Name 已提交
94 95

//WriteResultInfoWithPage 输出带页码信息的返回
E
eoLinker API Management 已提交
96 97 98
func WriteResultInfoWithPage(w http.ResponseWriter, resultType, resultKey string, result interface{}, pageInfo *PageInfo) {
	WriteResult(w, "000000", resultType, resultKey, result, pageInfo)
}
Y
Your Name 已提交
99 100

//WriteResultInfo 输出结果信息
E
eoLinker API Management 已提交
101 102 103 104 105 106 107 108 109 110 111
func WriteResultInfo(w http.ResponseWriter, resultType string, resultKey string, result interface{}) {

	if result != nil {
		if t := reflect.TypeOf(result); t.Kind() == reflect.Slice {

			WriteResultInfoWithPage(w, resultType, resultKey, result, NewItemNum(reflect.ValueOf(result).Len()))
			return
		}
	}
	WriteResultInfoWithPage(w, resultType, resultKey, result, nil)
}
Y
Your Name 已提交
112 113

//WriteResultInfoWithCode 输出带状态码的信息
E
eoLinker API Management 已提交
114 115 116
func WriteResultInfoWithCode(w http.ResponseWriter, code string, resultType, resultKey string, result interface{}) {
	WriteResult(w, code, resultType, resultKey, result, nil)
}
Y
Your Name 已提交
117 118

//WriteError 输出错误
E
eoLinker API Management 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
func WriteError(w http.ResponseWriter, statusCode, resultType, resultDesc string, resuleErr error) {

	ret := map[string]interface{}{
		"type":       resultType,
		"statusCode": statusCode,
		"resultDesc": resultDesc,
	}

	if resuleErr != nil {
		log.Info(resuleErr)
	}

	data, err := json.Marshal(ret)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		log.WithFields(ret).Debug("write error:", err)
	}

	w.Write(data)
	log.WithFields(ret).Debug("write error:", err)

}

Y
Your Name 已提交
142
//CheckLogin 检查登录
E
eoLinker API Management 已提交
143 144 145 146 147
func CheckLogin(w http.ResponseWriter, r *http.Request, operationType, operation string) (int, error) {

	userIDCookie, idErr := r.Cookie("userID")
	userCookie, userErr := r.Cookie("userToken")
	if idErr != nil || userErr != nil {
Y
Your Name 已提交
148 149

		e := errors.New("user not logged in")
E
eoLinker API Management 已提交
150 151 152 153 154
		WriteError(w, "100001", "user", e.Error(), e)
		return 0, e
	}
	userID, err := strconv.Atoi(userIDCookie.Value)
	if err != nil {
Y
Your Name 已提交
155
		WriteError(w, "100001", "user", "Illegal user ID!", err)
E
eoLinker API Management 已提交
156 157 158 159
		return 0, err
	}
	flag := account.CheckLogin(userCookie.Value, userID)
	if !flag {
Y
Your Name 已提交
160
		e := errors.New("illegal users")
E
eoLinker API Management 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
		WriteError(w, "100001", "user", "Illegal users!", e)
		return userID, e
	}
	if operation == OperationEDIT && OperationNone != operationType {
		if operationType == OperationADMIN {

			flag, desc, err := account.CheckUserIsAdmin(userID)
			if !flag {
				WriteError(w, "100002", "user", desc, err)
				return userID, errors.New(desc)
			}
		} else {
			flag, desc, err := account.CheckUserPermission(operationType, "edit", userID)
			if !flag {

				WriteError(w, "100002", "user", desc, err)
				return userID, errors.New(desc)
			}
		}

	}

	return userID, nil
}