Ejemplo n.º 1
0
func TestSaveOrUpdateOpenUser(t *testing.T) {
	openUser := new(entities.OpenUser)
	openUser.Age = 26
	openUser.City = "beijing"
	openUser.Head = "www.baidu.com/header.png"
	openUser.Nick = "nick"
	openUser.OpenId = "openid"
	openUser.Province = "province"
	openUser.Sex = 2
	openUser.Type = entities.OPEN_USER_TYPE_QQ
	services.SaveOrUpdateOpenUser(openUser)
}
func (c *OpenUserController) QqToken() {

	code := c.GetString("code")

	var loginPageUrl = "/users/login"
	//获取token
	tokenRes, err := services.QueryToken(code)
	beego.Debug("****************tokenRes:", tokenRes, "****************")
	if nil != err {
		c.Redirect(loginPageUrl, 302)
		return
	}

	accessToken := tokenRes["access_token"]
	beego.Debug("****************accessToken:", accessToken, "****************")
	if len(accessToken) <= 0 {
		c.Redirect(loginPageUrl, 302)
		return
	}

	//获取openid
	openIdRes, err := services.QueryOpenId(accessToken)
	beego.Debug("****************openIdRes:", openIdRes, "****************")
	if nil != err {
		beego.Error(err)
		c.Redirect("login.html", 302)
		return
	}

	openId := openIdRes["openid"]
	if len(openId) <= 0 {
		beego.Error(errors.New("openid[" + openId + "] error"))
		c.Redirect(loginPageUrl, 302)
		return
	}

	//获取user_info
	openUser, err := services.OpenUserInfo(accessToken, openId)
	beego.Debug("err:", err, "userInfoRes:", openUser)

	if nil != err {
		beego.Error(err)
		c.Redirect(loginPageUrl, 302)
		return
	}

	err = services.SaveOrUpdateOpenUser(openUser)
	if nil != err {
		beego.Error(err)
		c.Redirect(loginPageUrl, 302)
		return
	}

	openUser, _ = services.OpenUser(openId, entities.OPEN_USER_TYPE_QQ)
	beego.Debug("openUser:"******"user", user)
	} else {
		c.SetSession("openUser", openUser)
		c.Redirect("/users/oauth_register", 302)
		return
	}

	c.Redirect("/", 302)
}