From 3abc42ee2421ed50675f0df68a4146fbea97038e Mon Sep 17 00:00:00 2001 From: listen <494177603@qq.com> Date: Tue, 20 Jul 2021 10:41:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E8=BE=85=E5=8A=A9=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base.go | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ map.go | 30 ++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 base.go diff --git a/base.go b/base.go new file mode 100644 index 0000000..2031050 --- /dev/null +++ b/base.go @@ -0,0 +1,107 @@ +package helper + +import ( + "encoding/json" + "git.tetele.net/tgo/conf" + "git.tetele.net/tgo/helper" + "git.tetele.net/tgo/site" + "git.tetele.net/tgo/userrpc" + "io/ioutil" + "log" + "net/http" + "strings" +) + +type BaseController struct { + W http.ResponseWriter + Req *http.Request + SiteInfo map[string]string + UserInfo map[string]string + Token string + Domain string + JsonData map[string]interface{} + JsonDataArr []map[string]interface{} +} + +//前置操作 +func (controller *BaseController)Construct(w http.ResponseWriter, req *http.Request) { + log.Println("get from ", req.RemoteAddr, req.Method, req.Referer()) + + controller.W = w + controller.Req = req + + var replyJson []byte + + helper.SetHeader(w, req) + + if strings.ToUpper(req.Method) == "OPTIONS" { + replyJson, _ = json.Marshal(1) + w.Write(replyJson) + return + } + + data := make(map[string]interface{}) + + var err error + + if conf.IS_PRIVATE { + controller.SiteInfo = map[string]string{ + "database": conf.DBNAME, + "site_id": conf.SITE_ID, + } + } else { + controller.SiteInfo, err = site.GetSiteInfoFromReq(conf.MASTER_URL, req) + } + + if controller.Token != "" { + controller.UserInfo, err = userrpc.GetUserByToken(controller.SiteInfo["database"], controller.Token, conf.USER_RPC_URL) + } + + controller.Domain = helper.GetDomain(req) + + //获取json数据 + body, _ := ioutil.ReadAll(req.Body) + defer req.Body.Close() + + if len(body) > 0 { + if strings.Index(string(body),"[{") == 0 { + err = json.Unmarshal(body, &controller.JsonDataArr) + } else{ + err = json.Unmarshal(body, &controller.JsonData) + } + } + + if err != nil { + data["code"] = 0 + data["msg"] = err.Error() + replyJson, _ = json.Marshal(data) + w.Write(replyJson) + return + } + +} + +func (controller *BaseController) Success(result interface{}) { + data := make(map[string]interface{}) + + data["data"] = result + data["code"] = 1 + data["msg"] = "" + + returnData, _ := json.Marshal(data) + + controller.W.Write(returnData) +} + +func (controller *BaseController) Error(msg string) { + + data := make(map[string]interface{}) + + data["data"] = nil + data["code"] = 0 + data["msg"] = msg + + returnData, _ := json.Marshal(data) + + controller.W.Write(returnData) +} \ No newline at end of file diff --git a/map.go b/map.go index 42cfe4b..7c6d88f 100644 --- a/map.go +++ b/map.go @@ -61,3 +61,33 @@ func ToMapStringArray(x interface{}) ([]map[string]string, error) { return ret, err } + +/** + * 将map[string]string数组转成map[string]interface数组 + */ +func MapStringToInterfaceArr(src []map[string]string)([]map[string]interface{}){ + var dest []map[string]interface{} + + for i := range src { + m := src[i] + n := make(map[string]interface{}) + for k,v := range m { + n[k] = interface{}(v) + } + dest = append(dest, n) + } + + return dest +} + +/** + * 将map[string]string转成map[string]interface + */ +func MapStringToInterface(src map[string]string)(map[string]interface{}){ + target := make(map[string]interface{}) + for k,v := range src { + target[k] = interface{}(v) + } + + return target +} \ No newline at end of file