コード例 #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)
}
コード例 #2
0
func OpenUserInfo(accessToken string, openId string) (*entities.OpenUser, error) {
	var baseUrl = AuthConfig.String("get_user_info_url")
	params := make(map[string]string)
	params["access_token"] = accessToken
	params["openid"] = openId
	params["oauth_consumer_key"] = AuthConfig.String("app_id")

	subUrl := utils.BuildQueryString(params)

	var fullRequestStr = baseUrl + subUrl

	resp, err := http.Get(fullRequestStr)
	if nil != err {
		beego.Error(err)
		return nil, err
	}

	result, err := ioutil.ReadAll(resp.Body)
	defer resp.Body.Close()

	if nil != err {
		beego.Error(err)
		return nil, err
	}

	content := string(result)
	if len(content) == 0 {
		beego.Error(err)
		return nil, err
	}

	beego.Debug("******************OpenUserInfo", content)

	var paramMap map[string]interface{}

	err = json.Unmarshal([]byte(content), &paramMap)

	if len(paramMap) == 0 {
		return nil, errors.New("open_user_info get fail")
	}

	city, _ := paramMap["city"].(string)
	province, _ := paramMap["province"].(string)
	nick, _ := paramMap["nickname"].(string)
	gender, _ := paramMap["gender"].(string)
	head, _ := paramMap["figureurl_qq_1"].(string)
	year, _ := paramMap["year"].(string)
	sex := 1
	if "女" == gender {
		sex = 2
	}
	age := 0
	if len(year) > 0 {
		yearInt, err := strconv.Atoi(year)
		if nil != err {
			currYear := time.Now().Year()
			age = currYear - yearInt + 1
		}
	}

	openUser := new(entities.OpenUser)

	openUser.OpenId = openId
	openUser.Nick = nick
	openUser.Age = age
	openUser.City = city
	openUser.Province = province
	openUser.Sex = sex
	openUser.Type = entities.OPEN_USER_TYPE_QQ
	openUser.Head = head

	return openUser, err
}