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)
}
Exemplo n.º 2
0
func TestOpenUser(t *testing.T) {
	openuser, err := services.OpenUser("1", entities.OPEN_USER_TYPE_QQ)

	fmt.Println("openuser", openuser, "err", err)
}