Commit 337bfd095d53fb30b54d30a5919c3eb9d10964aa
1 parent
0fc94993
Exists in
master
提交代码
Showing
6 changed files
with
266 additions
and
0 deletions
Show diff stats
src/HttpServer/logic/datadef.go
| @@ -87,6 +87,28 @@ type UserLoginResp struct { | @@ -87,6 +87,28 @@ type UserLoginResp struct { | ||
| 87 | 87 | ||
| 88 | } | 88 | } |
| 89 | 89 | ||
| 90 | + | ||
| 91 | +type RegeisterReq struct{ | ||
| 92 | + Account string `json:"account"` | ||
| 93 | + Password string `json:"password"` | ||
| 94 | +} | ||
| 95 | + | ||
| 96 | +type RegeisteraResp struct { | ||
| 97 | + Status string `json:"status"` | ||
| 98 | + Result CommonResult `json:"result"` | ||
| 99 | +} | ||
| 100 | + | ||
| 101 | +type AccounLoginReq struct{ | ||
| 102 | + Account string `json:"account"` | ||
| 103 | + Password string `json:"password"` | ||
| 104 | +} | ||
| 105 | + | ||
| 106 | +type AccounLoginResp struct { | ||
| 107 | + Status string `json:"status"` | ||
| 108 | + Result UserLoginResult `json:"result"` | ||
| 109 | +} | ||
| 110 | + | ||
| 111 | + | ||
| 90 | type SaveUserDataReq struct{ | 112 | type SaveUserDataReq struct{ |
| 91 | Token string `json:"token"` | 113 | Token string `json:"token"` |
| 92 | Hot int `json:"hot"` | 114 | Hot int `json:"hot"` |
src/HttpServer/logic/errordef.go
| @@ -36,4 +36,6 @@ const ( | @@ -36,4 +36,6 @@ const ( | ||
| 36 | ERROR_FRIENDDELFRIENDFAILED = 31 //删除好友失败 | 36 | ERROR_FRIENDDELFRIENDFAILED = 31 //删除好友失败 |
| 37 | ERROR_FRIENDGETFRIENDINFOFAILED = 32 //获取好友信息失败 | 37 | ERROR_FRIENDGETFRIENDINFOFAILED = 32 //获取好友信息失败 |
| 38 | ERROR_WECHATLOGINFAILED = 33 //获取好友信息失败 | 38 | ERROR_WECHATLOGINFAILED = 33 //获取好友信息失败 |
| 39 | + ERROR_REGEISTACCOUNTEXIST = 34 //注册账号重复 | ||
| 40 | + ERROR_ACCOUNTPWDWRONG= 35 //账号登录密码不对 | ||
| 39 | ) | 41 | ) |
| 40 | \ No newline at end of file | 42 | \ No newline at end of file |
src/HttpServer/logic/function.go
| @@ -4,7 +4,10 @@ import ( | @@ -4,7 +4,10 @@ import ( | ||
| 4 | "HttpServer/redishandler" | 4 | "HttpServer/redishandler" |
| 5 | "common/logger" | 5 | "common/logger" |
| 6 | "common/redis" | 6 | "common/redis" |
| 7 | + "crypto/md5" | ||
| 8 | + "encoding/hex" | ||
| 7 | "encoding/json" | 9 | "encoding/json" |
| 10 | + "errors" | ||
| 8 | "net/http" | 11 | "net/http" |
| 9 | "sort" | 12 | "sort" |
| 10 | "strconv" | 13 | "strconv" |
| @@ -721,4 +724,44 @@ func (flist *FriendList)GetRecommandFriendList(uuid,friendnum int) (*GetRecommen | @@ -721,4 +724,44 @@ func (flist *FriendList)GetRecommandFriendList(uuid,friendnum int) (*GetRecommen | ||
| 721 | //保存到缓存 | 724 | //保存到缓存 |
| 722 | SaveCacheRecommandFriendList(uuid,realrt) | 725 | SaveCacheRecommandFriendList(uuid,realrt) |
| 723 | return realrt,nil | 726 | return realrt,nil |
| 727 | +} | ||
| 728 | + | ||
| 729 | +func SaveAccount(account ,pwd string) error { | ||
| 730 | + //首先对pwd加密 | ||
| 731 | + h := md5.New() | ||
| 732 | + h.Write([]byte(pwd)) // 需要加密的字符串为 | ||
| 733 | + newpwd :=hex.EncodeToString(h.Sum(nil)) | ||
| 734 | + err := redishandler.GetRedisClient().HSet(redis.USER_ACCOUNT_PASSWORD_KEY,account,newpwd) | ||
| 735 | + return err | ||
| 736 | +} | ||
| 737 | + | ||
| 738 | +func CheckAccout(account ,pwd string) error { | ||
| 739 | + h := md5.New() | ||
| 740 | + h.Write([]byte(pwd)) // 需要加密的字符串为 | ||
| 741 | + newpwd :=hex.EncodeToString(h.Sum(nil)) | ||
| 742 | + pwdstr,err := redishandler.GetRedisClient().HGet(redis.USER_ACCOUNT_PASSWORD_KEY,account) | ||
| 743 | + if err != nil { | ||
| 744 | + return err | ||
| 745 | + } | ||
| 746 | + if pwdstr!= newpwd { | ||
| 747 | + return errors.New("pwdword not right!") | ||
| 748 | + } | ||
| 749 | + return nil | ||
| 750 | +} | ||
| 751 | + | ||
| 752 | +func SaveAccountToken(acc ,token string) { | ||
| 753 | + err := redishandler.GetRedisClient().HSet(redis.USER_ACCOUNT_PASSWORD_KEY,acc,token) | ||
| 754 | + if err != nil { | ||
| 755 | + logger.Error("SaveAccountToken err=%v",err) | ||
| 756 | + } | ||
| 757 | +} | ||
| 758 | + | ||
| 759 | +func GetAccountToken(acc string) string { | ||
| 760 | + token,err := redishandler.GetRedisClient().HGet(redis.USER_ACCOUNT_PASSWORD_KEY,acc) | ||
| 761 | + if err != nil { | ||
| 762 | + logger.Error("GetAccountToken failed err=%v",err) | ||
| 763 | + return "" | ||
| 764 | + } | ||
| 765 | + | ||
| 766 | + return token | ||
| 724 | } | 767 | } |
| 725 | \ No newline at end of file | 768 | \ No newline at end of file |
src/HttpServer/logic/httpserver.go
| @@ -30,6 +30,8 @@ func CheckErr(err error) { | @@ -30,6 +30,8 @@ func CheckErr(err error) { | ||
| 30 | 30 | ||
| 31 | func startServerHttpServe() { | 31 | func startServerHttpServe() { |
| 32 | http.HandleFunc("/catcafe/login", UserLogin) //游客登录 | 32 | http.HandleFunc("/catcafe/login", UserLogin) //游客登录 |
| 33 | + http.HandleFunc("/catcafe/user/regeister", Regeister) //账号注册 | ||
| 34 | + http.HandleFunc("/catcafe/user/accountlogin", AccounLogin) //账号登录 | ||
| 33 | http.HandleFunc("/catcafe/wechatlogin", Wechatlogin) //微信登录 | 35 | http.HandleFunc("/catcafe/wechatlogin", Wechatlogin) //微信登录 |
| 34 | http.HandleFunc("/catcafe/user/saveUserData", SaveUserData) //保存用户数据 | 36 | http.HandleFunc("/catcafe/user/saveUserData", SaveUserData) //保存用户数据 |
| 35 | http.HandleFunc("/catcafe/data/saveData", SaveData) //保存游戏自定义数据 | 37 | http.HandleFunc("/catcafe/data/saveData", SaveData) //保存游戏自定义数据 |
| @@ -60,6 +62,28 @@ func startServerHttpServe() { | @@ -60,6 +62,28 @@ func startServerHttpServe() { | ||
| 60 | CheckErr(err) | 62 | CheckErr(err) |
| 61 | } | 63 | } |
| 62 | 64 | ||
| 65 | +func Regeister(w http.ResponseWriter, r *http.Request) { | ||
| 66 | + | ||
| 67 | + result, _ := ioutil.ReadAll(r.Body) | ||
| 68 | + r.Body.Close() | ||
| 69 | + | ||
| 70 | + s := string(result) | ||
| 71 | + logger.Info("Regeister , body:%v,uuid=%v", s) | ||
| 72 | + | ||
| 73 | + HandleRegeister(w,s) | ||
| 74 | +} | ||
| 75 | + | ||
| 76 | +func AccounLogin(w http.ResponseWriter, r *http.Request) { | ||
| 77 | + | ||
| 78 | + result, _ := ioutil.ReadAll(r.Body) | ||
| 79 | + r.Body.Close() | ||
| 80 | + | ||
| 81 | + s := string(result) | ||
| 82 | + logger.Info("AccounLogin , body:%v,uuid=%v", s) | ||
| 83 | + | ||
| 84 | + HandleAccounLogin(w,s) | ||
| 85 | +} | ||
| 86 | + | ||
| 63 | 87 | ||
| 64 | func Wechatlogin(w http.ResponseWriter, r *http.Request) { | 88 | func Wechatlogin(w http.ResponseWriter, r *http.Request) { |
| 65 | 89 |
src/HttpServer/logic/logic.go
| @@ -6,6 +6,7 @@ import ( | @@ -6,6 +6,7 @@ import ( | ||
| 6 | "common/beegomap" | 6 | "common/beegomap" |
| 7 | "common/logger" | 7 | "common/logger" |
| 8 | "common/redis" | 8 | "common/redis" |
| 9 | + ksuid "github.com/segmentio" | ||
| 9 | "io/ioutil" | 10 | "io/ioutil" |
| 10 | "strconv" | 11 | "strconv" |
| 11 | 12 | ||
| @@ -65,6 +66,62 @@ func InitUserExt(req *UserLoginReq,resp *UserLoginResp,uuid int) { | @@ -65,6 +66,62 @@ func InitUserExt(req *UserLoginReq,resp *UserLoginResp,uuid int) { | ||
| 65 | SaveUserExt(udata) | 66 | SaveUserExt(udata) |
| 66 | } | 67 | } |
| 67 | 68 | ||
| 69 | +func InitAccountLogin(account string) { | ||
| 70 | + //首先生成token | ||
| 71 | + newToken := ksuid.New().String() | ||
| 72 | + //保存token与account的关系 | ||
| 73 | + SaveAccountToken(account,newToken) | ||
| 74 | + //首先生成user_base_data | ||
| 75 | + var basedata UserBaseData | ||
| 76 | + basedata.User_id = GetNewUUid() | ||
| 77 | + basedata.User_avatar_url = "" | ||
| 78 | + basedata.User_city = "天堂" | ||
| 79 | + basedata.User_gender = 1 | ||
| 80 | + basedata.User_nickname = "游客" + string(basedata.User_id) | ||
| 81 | + basedata.User_openid = "" | ||
| 82 | + basedata.User_token = newToken | ||
| 83 | + //保存base data | ||
| 84 | + basic, _ := json.Marshal(&basedata) | ||
| 85 | + SaveUserBasic(basedata.User_id,string(basic)) | ||
| 86 | + | ||
| 87 | + SetTouristUid(newToken,basedata.User_id) | ||
| 88 | + //然后生成user_ext_data | ||
| 89 | + var extdata UserExtData | ||
| 90 | + extdata.User_id = basedata.User_id | ||
| 91 | + extdata.Lv = 1 | ||
| 92 | + extdata.Bean = 0 | ||
| 93 | + extdata.Coin = 0 | ||
| 94 | + extdata.Exp = 0 | ||
| 95 | + extdata.Hot = 0 | ||
| 96 | + extdata.Invite_uid = 0 | ||
| 97 | + extdata.LoveExp = 0 | ||
| 98 | + extdata.Reg_time = int(time.Now().Unix()) | ||
| 99 | + extdata.ShopNum = 0 | ||
| 100 | + extdata.User_channel = 0 | ||
| 101 | + extdata.User_is_black = 0 | ||
| 102 | + extdata.User_reg_time = int(time.Now().Unix()) | ||
| 103 | + extdata.User_scene = 0 | ||
| 104 | + udata :=new(UserData) | ||
| 105 | + udata.Scene = extdata.User_scene | ||
| 106 | + udata.Hot = extdata.Hot | ||
| 107 | + udata.Exp = extdata.Exp | ||
| 108 | + udata.Coin = extdata.Coin | ||
| 109 | + udata.Bean = extdata.Bean | ||
| 110 | + udata.Lv = extdata.Lv | ||
| 111 | + udata.Channel = extdata.User_channel | ||
| 112 | + udata.InviteUid = extdata.Invite_uid | ||
| 113 | + udata.Isblack = extdata.User_is_black | ||
| 114 | + udata.Loevexp = extdata.LoveExp | ||
| 115 | + udata.Regtime = extdata.Reg_time | ||
| 116 | + udata.Shopnum = extdata.ShopNum | ||
| 117 | + udata.Userid = extdata.User_id | ||
| 118 | + udata.UserInviteId = extdata.User_invite_uid | ||
| 119 | + udata.Userregtime = extdata.User_reg_time | ||
| 120 | + udata.LoginTime = int(time.Now().Unix()) | ||
| 121 | + udata.LoginDay = 1 | ||
| 122 | + | ||
| 123 | + SaveUserExt(udata) | ||
| 124 | +} | ||
| 68 | 125 | ||
| 69 | func InitTourist(req *UserLoginReq,resp *UserLoginResp) { | 126 | func InitTourist(req *UserLoginReq,resp *UserLoginResp) { |
| 70 | //首先生成user_base_data | 127 | //首先生成user_base_data |
| @@ -174,6 +231,123 @@ func TransmitUserData(in *UserData ,out *UserExtData) { | @@ -174,6 +231,123 @@ func TransmitUserData(in *UserData ,out *UserExtData) { | ||
| 174 | out.Invite_uid = in.InviteUid | 231 | out.Invite_uid = in.InviteUid |
| 175 | } | 232 | } |
| 176 | 233 | ||
| 234 | +func HandleRegeister(w http.ResponseWriter, data string) { | ||
| 235 | + | ||
| 236 | + SetHeader(w) | ||
| 237 | + var resp RegeisteraResp | ||
| 238 | + resp.Status = "true" | ||
| 239 | + resp.Result.Code = ERROR_OK | ||
| 240 | + var rdata RegeisterReq | ||
| 241 | + err := json.Unmarshal([]byte(data), &rdata) | ||
| 242 | + for { | ||
| 243 | + if err != nil { | ||
| 244 | + logger.Error("HandleRegeister json unmarshal failed=%v", err) | ||
| 245 | + resp.Result.Code = ERROR_JSONUNMASHFAILED | ||
| 246 | + break | ||
| 247 | + } | ||
| 248 | + | ||
| 249 | + //todo 判断密码强度? | ||
| 250 | + | ||
| 251 | + //判断账号是否重复有 | ||
| 252 | + exist,err := redishandler.GetRedisClient().HExists(redis.USER_ACCOUNT_PASSWORD_KEY,rdata.Account) | ||
| 253 | + if err != nil { | ||
| 254 | + logger.Error("HandleRegeister redis failed failed=%v", err) | ||
| 255 | + resp.Result.Code = ERROR_SRVDB_FAILED | ||
| 256 | + break | ||
| 257 | + } | ||
| 258 | + | ||
| 259 | + if exist { | ||
| 260 | + logger.Error("HandleRegeister accountexist failed=%v", err) | ||
| 261 | + resp.Result.Code = ERROR_REGEISTACCOUNTEXIST | ||
| 262 | + break | ||
| 263 | + } | ||
| 264 | + | ||
| 265 | + err = SaveAccount(rdata.Account,rdata.Password) | ||
| 266 | + if err != nil { | ||
| 267 | + logger.Error("HandleRegeister redis failed failed=%v", err) | ||
| 268 | + resp.Result.Code = ERROR_SRVDB_FAILED | ||
| 269 | + break | ||
| 270 | + } | ||
| 271 | + | ||
| 272 | + //需要初始化数据 | ||
| 273 | + InitAccountLogin(rdata.Account) | ||
| 274 | + | ||
| 275 | + resp.Result.Code = ERROR_OK | ||
| 276 | + break | ||
| 277 | + } | ||
| 278 | + | ||
| 279 | + //回包 | ||
| 280 | + respstr, _ := json.Marshal(&resp) | ||
| 281 | + fmt.Fprint(w, string(respstr)) | ||
| 282 | +} | ||
| 283 | + | ||
| 284 | + | ||
| 285 | +func HandleAccounLogin(w http.ResponseWriter, data string) { | ||
| 286 | + | ||
| 287 | + SetHeader(w) | ||
| 288 | + var resp AccounLoginResp | ||
| 289 | + resp.Status = "true" | ||
| 290 | + resp.Result.Code = ERROR_OK | ||
| 291 | + var rdata AccounLoginReq | ||
| 292 | + err := json.Unmarshal([]byte(data), &rdata) | ||
| 293 | + for { | ||
| 294 | + if err != nil { | ||
| 295 | + logger.Error("HandleAccounLogin json unmarshal failed=%v", err) | ||
| 296 | + resp.Result.Code = ERROR_JSONUNMASHFAILED | ||
| 297 | + break | ||
| 298 | + } | ||
| 299 | + | ||
| 300 | + err := CheckAccout(rdata.Account,rdata.Password) | ||
| 301 | + | ||
| 302 | + if err != nil { | ||
| 303 | + logger.Error("HandleRegeister pwssword failed failed=%v", err) | ||
| 304 | + resp.Result.Code = ERROR_ACCOUNTPWDWRONG | ||
| 305 | + break | ||
| 306 | + } | ||
| 307 | + | ||
| 308 | + //查询数据 | ||
| 309 | + token := GetAccountToken(rdata.Account) | ||
| 310 | + | ||
| 311 | + uuid,err := GetTouristUid(token) | ||
| 312 | + if err != nil || uuid==0{ | ||
| 313 | + logger.Error("HandleAccounLogin getuuid failed=%v", err) | ||
| 314 | + resp.Result.Code = ERROR_GETUSERIDFAILED | ||
| 315 | + break | ||
| 316 | + } | ||
| 317 | + | ||
| 318 | + basic,err := GetUserBasic(uuid) | ||
| 319 | + if err != nil { | ||
| 320 | + logger.Error("HandleAccounLogin getbasic failed=%v", err) | ||
| 321 | + resp.Result.Code = ERROR_GETUSERBASICFAILED | ||
| 322 | + break | ||
| 323 | + } | ||
| 324 | + resp.Result.Data.Dasedata = *basic | ||
| 325 | + | ||
| 326 | + ext,err := GetUserExt(uuid) | ||
| 327 | + if err != nil { | ||
| 328 | + logger.Error("HandleAccounLogin getext failed=%v", err) | ||
| 329 | + resp.Result.Code = ERROR_GETUSEREXTFAILED | ||
| 330 | + break | ||
| 331 | + } | ||
| 332 | + TransmitUserData(ext,&resp.Result.Data.Extdata) | ||
| 333 | + | ||
| 334 | + //需要处理登录数据 | ||
| 335 | + HandleLoginTime(ext) | ||
| 336 | + | ||
| 337 | + m_userInfo.Set(uint32(ext.Userid),ext) | ||
| 338 | + SaveUserExt(ext) | ||
| 339 | + | ||
| 340 | + | ||
| 341 | + resp.Result.Code = ERROR_OK | ||
| 342 | + break | ||
| 343 | + } | ||
| 344 | + | ||
| 345 | + //回包 | ||
| 346 | + respstr, _ := json.Marshal(&resp) | ||
| 347 | + fmt.Fprint(w, string(respstr)) | ||
| 348 | +} | ||
| 349 | + | ||
| 350 | + | ||
| 177 | //处理微信登录 | 351 | //处理微信登录 |
| 178 | func HandleWechatlogin(w http.ResponseWriter, data string) { | 352 | func HandleWechatlogin(w http.ResponseWriter, data string) { |
| 179 | 353 |
src/common/redis/def.go
| @@ -15,4 +15,5 @@ const ( | @@ -15,4 +15,5 @@ const ( | ||
| 15 | FRIEND_APPLYLIST_KEY = "CATCAFE_FRIEND_APPLYLIST_KEY" //玩家申请好友列表,需要在末尾加死":uuid" | 15 | FRIEND_APPLYLIST_KEY = "CATCAFE_FRIEND_APPLYLIST_KEY" //玩家申请好友列表,需要在末尾加死":uuid" |
| 16 | FRIEND_APPROVELIST_KEY = "CATCAFE_FRIEND_APPROVELIST_KEY" //待批准好友列表,需要在末尾加死":uuid" | 16 | FRIEND_APPROVELIST_KEY = "CATCAFE_FRIEND_APPROVELIST_KEY" //待批准好友列表,需要在末尾加死":uuid" |
| 17 | FRIEND_RECOMMANDLIST_KEY = "CATCAFE_FRIEND_RECOMMANDLIST_KEY" //推荐好友缓存key | 17 | FRIEND_RECOMMANDLIST_KEY = "CATCAFE_FRIEND_RECOMMANDLIST_KEY" //推荐好友缓存key |
| 18 | + USER_ACCOUNT_PASSWORD_KEY = "CATCAFE_USER_ACCOUNT_PASSWORD_KEY" //玩家账号密码的key | ||
| 18 | ) | 19 | ) |