func (this *MusicController) MusicAction(w http.ResponseWriter, r *http.Request) { r.ParseForm() tokenCookie, err := r.Cookie("token") if err != nil { fmt.Println("cookie Error: ", err) NormalResponse(w, InvalidToken) } else if len(tokenCookie.Value) == 0 { NormalResponse(w, EmptyToken) } else { ret, userId := token.TokenManagerInstance().CheckTokenExist(tokenCookie.Value) if ret == false { fmt.Println("search Token Error: ", err) NormalResponse(w, InvalidToken) } else { fmt.Println("userId: ", userId) action := r.Form.Get("action") fmt.Println(action) switch action { case "fetchRandomList": music.MusicManagerInstance().SetPlayer(music.BaiduPlayer) this.fetchRandomList(w, r) case "fetchLoveList": this.fetchLoveList(userId, w, r) case "fetchListenedList": this.fetchListenedList(userId, w, r) case "loveMusic": this.loveMusic(userId, w, r) case "listenMusic": this.listenMusic(userId, w, r) case "searchMusic": this.searchMusic(w, r) case "musicProxy": // 先测试请求某一首歌 // action="musicProxy&player=baidu&type=fetchList&" var musicURL string = "http://yinyueshiting.baidu.com/data2/music/134380410/5963228158400128.mp3?xcode=55a3d05c2c746155f1962d1d1690fc93" this.fetchMusicWithProxy(musicURL, w, r) } } } }
func (this *AccountController) AccountAction(w http.ResponseWriter, r *http.Request) { body, err := ioutil.ReadAll(r.Body) if err != nil { NormalResponse(w, InvalidParam) return } js, err := simplejson.NewJson(body) if err != nil { NormalResponse(w, InvalidParam) return } action, err := js.Get("action").String() if err != nil { NormalResponse(w, InvalidParam) return } param := js.Get("param").MustMap() if param == nil { NormalResponse(w, InvalidParam) return } fmt.Println("action = ", action) switch action { case "register": username := param["username"].(string) password := param["password"].(string) sex := param["sex"].(string) age, err := param["age"].(json.Number).Int64() if len(username) == 0 || len(password) == 0 || len(sex) == 0 || err != nil { NormalResponse(w, InvalidParam) return } ret, err := model.AccountModelInstance().CheckUserIsExist(username) if err != nil { fmt.Println("register Error: ", err) NormalResponse(w, RegisterDatabaseError) } else if ret == true { fmt.Println("register ", username, " is exist") NormalResponse(w, RegisterInvalidUserName) } else { code := getMd5String(getMd5String(password) + username) model.AccountModelInstance().Register(username, code, sex, int(age)) NormalResponse(w, OK) } case "login": username := param["username"].(string) password := param["password"].(string) if len(username) == 0 || len(password) == 0 { NormalResponse(w, InvalidParam) return } userid, ret, err := model.AccountModelInstance().CheckUserAndPassword(username, password) if err != nil { fmt.Println("login Error: ", err) NormalResponse(w, LoginDatabaseError) } else if ret == true { token := token.TokenManagerInstance().AddToken(userid) LoginSuccessResponse(w, token) } else { NormalResponse(w, LoginPasswordError) } } }