Commit 8b0ae028998419e53bc71b77080042ae5c0b9f2e

Authored by 陆恒
1 parent 7e41792b
Exists in master

提交

src/HttpServer/logic/datadef.go
1 package logic 1 package logic
2 2
  3 +type TestAddCatReq struct {
  4 + Position int `json:"position"`
  5 + CatId int `json:"catId"`
  6 +}
  7 +
  8 +type TestAddCatData struct {
  9 +}
  10 +
  11 +type TestAddCatResp struct {
  12 + Code int `json:"code"`
  13 + Data TestAddCatData `json:"data"`
  14 + Message string `json:"message"`
  15 +}
  16 +
3 type UserLoginReq struct { 17 type UserLoginReq struct {
4 Lype int `json:"type"` 18 Lype int `json:"type"`
5 UserId int `json:"userId"` 19 UserId int `json:"userId"`
src/HttpServer/logic/httpserver.go
@@ -101,6 +101,10 @@ func CheckErr(err error) { @@ -101,6 +101,10 @@ func CheckErr(err error) {
101 } 101 }
102 102
103 func startServerHttpServe() { 103 func startServerHttpServe() {
  104 + //test
  105 + http.HandleFunc("/api/test/addCat", TestaddCat) //增加猫
  106 +
  107 + //real
104 http.HandleFunc("/api/account/login", UserLogin) //登录 108 http.HandleFunc("/api/account/login", UserLogin) //登录
105 http.HandleFunc("/api/home/buy", DoBuyCat) //购买猫 109 http.HandleFunc("/api/home/buy", DoBuyCat) //购买猫
106 http.HandleFunc("/api/home/synResource", GetUserData) //同步资源 110 http.HandleFunc("/api/home/synResource", GetUserData) //同步资源
@@ -1087,6 +1091,24 @@ func GetUserData(w http.ResponseWriter, r *http.Request) { @@ -1087,6 +1091,24 @@ func GetUserData(w http.ResponseWriter, r *http.Request) {
1087 HandlerGetUserData(w, s, Uuid) 1091 HandlerGetUserData(w, s, Uuid)
1088 } 1092 }
1089 1093
  1094 +func TestaddCat(w http.ResponseWriter, r *http.Request) {
  1095 + Uuid := 0
  1096 + if len(r.Header) > 0 {
  1097 + Uuid, _ = strconv.Atoi(r.Header.Get("uid"))
  1098 + }
  1099 + if Uuid == 0 {
  1100 + SetHeader(w)
  1101 + return
  1102 + }
  1103 + result, _ := ioutil.ReadAll(r.Body)
  1104 + r.Body.Close()
  1105 +
  1106 + s := string(result)
  1107 + logger.Info("TestaddCat , body:%v,uuid=%v", s, Uuid)
  1108 +
  1109 + HandlerTestaddCat(w, s, Uuid)
  1110 +}
  1111 +
1090 func UserLogin(w http.ResponseWriter, r *http.Request) { 1112 func UserLogin(w http.ResponseWriter, r *http.Request) {
1091 //w.Header().Set("Access-Control-Allow-Origin", "*") //允许访问所有域 1113 //w.Header().Set("Access-Control-Allow-Origin", "*") //允许访问所有域
1092 //w.Header().Set("Content-Type", "application/json") 1114 //w.Header().Set("Content-Type", "application/json")
src/HttpServer/logic/logic.go
@@ -40,6 +40,51 @@ func SetHeader(w http.ResponseWriter) { @@ -40,6 +40,51 @@ func SetHeader(w http.ResponseWriter) {
40 40
41 } 41 }
42 42
  43 +func HandlerTestaddCat(w http.ResponseWriter, data string, uuid int) {
  44 + SetHeader(w)
  45 + var resp TestAddCatResp
  46 + resp.Code = 0
  47 + resp.Message = "success"
  48 + var rdata TestAddCatReq
  49 + err := json.Unmarshal([]byte(data), &rdata)
  50 + if err != nil {
  51 + logger.Info("json decode HandlerTestaddCat data failed:%v,for:%v", err, data)
  52 + resp.Message = "json unmarshal failed"
  53 + resp.Code = 1
  54 + respstr, _ := json.Marshal(&resp)
  55 + logger.Info("###HandlerLogin###rdata:%v", string(respstr))
  56 + fmt.Fprint(w, string(respstr))
  57 + return
  58 + }
  59 +
  60 + for {
  61 +
  62 + uinfo, err := GetUserInfo(strconv.Itoa(uuid))
  63 + if err != nil || uinfo == nil {
  64 + logger.Error("HandlerTestaddCat getuserinfo failed=%v", err)
  65 + resp.Code = 1
  66 + resp.Message = "get userinfo failed"
  67 + break
  68 + }
  69 +
  70 + if rdata.Position < 0 || rdata.Position > len(uinfo.PosInfo)-1 {
  71 + logger.Error("HandlerTestaddCat Position failed=%v", err)
  72 + resp.Code = 1
  73 + resp.Message = "get Position failed"
  74 + break
  75 + }
  76 +
  77 + uinfo.PosInfo[rdata.Position].Cat = rdata.CatId
  78 + break
  79 + }
  80 +
  81 + //回包
  82 + respstr, _ := json.Marshal(&resp)
  83 + fmt.Fprint(w, string(respstr))
  84 +
  85 + logger.Info("###HandlerLogin###rdata:%v", string(respstr))
  86 +}
  87 +
43 func HandlerLogin(w http.ResponseWriter, data string, uuid int, token string) { 88 func HandlerLogin(w http.ResponseWriter, data string, uuid int, token string) {
44 SetHeader(w) 89 SetHeader(w)
45 var resp UserLoginResp 90 var resp UserLoginResp