github.com/lianghucheng/zrddz@v0.0.0-20200923083010-c71f680932e2/src/game/internal/http.go (about) 1 package internal 2 3 import ( 4 "conf" 5 "encoding/json" 6 "fmt" 7 "msg" 8 "net/http" 9 "strconv" 10 11 "github.com/name5566/leaf/log" 12 "gopkg.in/mgo.v2/bson" 13 14 //_ "net/http/pprof" 15 "reflect" 16 ) 17 18 func init() { 19 go startHTTPServer() 20 /*go func() { 21 err := http.ListenAndServe(":8888", nil) 22 if err != nil { 23 log.Fatal("%v", err) 24 } 25 }()*/ 26 } 27 28 func startHTTPServer() { 29 mux := http.NewServeMux() 30 mux.Handle("/czddz/android", http.HandlerFunc(handleCZDDZAndroid)) 31 mux.Handle("/czddz/ios", http.HandlerFunc(handleCZDDZIOS)) 32 mux.Handle("/czddz/sougou", http.HandlerFunc(handleCZDDZSougou)) 33 mux.Handle("/czddz/google", http.HandlerFunc(handleCZDDZGoogle)) 34 mux.Handle("/alipay", http.HandlerFunc(handleAliPay)) 35 mux.Handle("/wxpay", http.HandlerFunc(handleWXPay)) 36 mux.Handle("/edywxpay", http.HandlerFunc(handleEdyWXPay)) 37 mux.Handle("/edyalipay", http.HandlerFunc(handleEdyAliPay)) 38 mux.Handle("/invite", http.HandlerFunc(handleInvite)) 39 mux.HandleFunc("/exit", handleDeal) 40 mux.HandleFunc("/set/system", handleSystem) 41 mux.HandleFunc("/unionid", handleUnionid) 42 mux.HandleFunc("/collect", handleCollect) 43 44 mux.HandleFunc("/fakeralipay", handleFakerAliPay) 45 mux.HandleFunc("/fakerwxpay", handleFakerWXPay) 46 mux.HandleFunc("/edyfakerwxpay", handleEdyWXPay) 47 mux.HandleFunc("/fakerrprecord", handleFakerRedPacketRecord) 48 err := http.ListenAndServe(conf.Server.HTTPAddr, mux) 49 if err != nil { 50 log.Fatal("%v", err) 51 } 52 } 53 54 func handleCZDDZAndroid(w http.ResponseWriter, req *http.Request) { 55 m := map[string]interface{}{} 56 m["version"] = landlordConfigData.AndroidVersion 57 m["downloadurl"] = landlordConfigData.AndroidDownloadUrl 58 m["guestlogin"] = landlordConfigData.AndroidGuestLogin 59 m["enteraddress"] = landlordConfigData.EnterAddress 60 m["online"] = len(userIDUsers) 61 data, err := json.Marshal(m) 62 if err != nil { 63 log.Error("marshal message %v error: %v", reflect.TypeOf(m), err) 64 return 65 } 66 w.Header().Set("Access-Control-Allow-Origin", "*") // 解决跨域问题 67 fmt.Fprintf(w, "%s", data) 68 } 69 70 func handleCZDDZIOS(w http.ResponseWriter, req *http.Request) { 71 m := map[string]interface{}{} 72 m["version"] = landlordConfigData.IOSVersion 73 m["downloadurl"] = landlordConfigData.IOSDownloadUrl 74 m["guestlogin"] = landlordConfigData.IOSGuestLogin 75 m["enteraddress"] = landlordConfigData.EnterAddress 76 m["online"] = len(userIDUsers) 77 data, err := json.Marshal(m) 78 if err != nil { 79 log.Error("marshal message %v error: %v", reflect.TypeOf(m), err) 80 return 81 } 82 w.Header().Set("Access-Control-Allow-Origin", "*") 83 fmt.Fprintf(w, "%s", data) 84 } 85 86 func handleCZDDZSougou(w http.ResponseWriter, req *http.Request) { 87 m := map[string]interface{}{} 88 m["version"] = landlordConfigData.SougouVersion 89 m["downloadurl"] = landlordConfigData.SougouDownloadUrl 90 m["guestlogin"] = landlordConfigData.SougouGuestLogin 91 m["enteraddress"] = landlordConfigData.EnterAddress 92 m["online"] = len(userIDUsers) 93 data, err := json.Marshal(m) 94 if err != nil { 95 log.Error("marshal message %v error: %v", reflect.TypeOf(m), err) 96 return 97 } 98 w.Header().Set("Access-Control-Allow-Origin", "*") 99 fmt.Fprintf(w, "%s", data) 100 } 101 102 func handleCZDDZGoogle(w http.ResponseWriter, req *http.Request) { 103 m := map[string]interface{}{} 104 m["version"] = landlordConfigData.GoogleVersion 105 m["downloadurl"] = landlordConfigData.GoogleDownloadUrl 106 m["guestlogin"] = landlordConfigData.GoogleGuestLogin 107 m["enteraddress"] = landlordConfigData.EnterAddress 108 m["online"] = len(userIDUsers) 109 data, err := json.Marshal(m) 110 if err != nil { 111 log.Error("marshal message %v error: %v", reflect.TypeOf(m), err) 112 return 113 } 114 w.Header().Set("Access-Control-Allow-Origin", "*") 115 fmt.Fprintf(w, "%s", data) 116 } 117 118 func handleDeal(w http.ResponseWriter, req *http.Request) { 119 id := req.FormValue("id") 120 accounitd, _ := strconv.Atoi(id) 121 userData := new(UserData) 122 db := mongoDB.Ref() 123 defer mongoDB.UnRef(db) 124 db.DB(DB).C("users").Find(bson.M{"accountid": accounitd}).One(userData) 125 if userData.UserID != 0 { 126 if r, ok := userIDRooms[userData.UserID]; ok { 127 roomm := r.(*LandlordRoom) 128 roomm.state = roomIdle 129 for _, userID := range roomm.positionUserIDs { 130 switch roomm.rule.RoomType { 131 case roomBaseScoreMatching, roomRedPacketMatching, roomRedPacketPrivate: 132 roomm.Leave(userID) 133 } 134 } 135 w.Write([]byte("成功")) 136 return 137 } 138 w.Write([]byte("玩家不在游戏中")) 139 } 140 w.Write([]byte("玩家不存在")) 141 return 142 } 143 func handleSystem(w http.ResponseWriter, req *http.Request) { 144 on := req.FormValue("on") 145 systemOn = (on == "true") 146 w.Write([]byte("成功")) 147 return 148 } 149 func handleUnionid(w http.ResponseWriter, req *http.Request) { 150 cardcode := req.FormValue("cardcode") 151 userData := new(UserData) 152 db := mongoDB.Ref() 153 defer mongoDB.UnRef(db) 154 m := map[string]interface{}{} 155 err := db.DB(DB).C("users").Find(bson.M{"cardcode": cardcode}).One(userData) 156 if err != nil { 157 m["code"] = 1001 158 m["msg"] = err.Error() 159 data, _ := json.Marshal(m) 160 w.Write(data) 161 return 162 } 163 m["code"] = 1000 164 m["msg"] = "成功" 165 m["data"] = userData 166 data, _ := json.Marshal(m) 167 w.Write(data) 168 return 169 } 170 func handleCollect(w http.ResponseWriter, req *http.Request) { 171 cardcode := req.FormValue("cardcode") 172 userData := new(UserData) 173 db := mongoDB.Ref() 174 defer mongoDB.UnRef(db) 175 err := db.DB(DB).C("users").Find(bson.M{"cardcode": cardcode}).One(userData) 176 if err != nil { 177 return 178 } 179 db.DB(DB).C("users").Update(bson.M{"cardcode": cardcode}, bson.M{ 180 "$set": bson.M{"taken": true}, 181 }) 182 if existUser, ok := userIDUsers[userData.UserID]; ok { 183 existUser.baseData.userData.Taken = true 184 existUser.WriteMsg(&msg.C2S_CardCodeState{}) 185 } 186 } 187 188 func handleFakerRedPacketRecord(w http.ResponseWriter, req *http.Request){ 189 accountid := req.FormValue("accountid") 190 aid, _ := strconv.Atoi(accountid) 191 grantType := req.FormValue("granttype") 192 desc := req.FormValue("desc") 193 gt,_ := strconv.Atoi(grantType) 194 db := mongoDB.Ref() 195 defer mongoDB.UnRef(db) 196 userdata:=new(UserData) 197 err:=db.DB(DB).C("users").Find(bson.M{"accountid":aid}) .One(userdata) 198 if err != nil { 199 log.Error("%v",err) 200 return 201 } 202 203 WriteRedPacketGrantRecord(userdata, gt, desc, 1.1) 204 }