Example #1
0
///	获取用户列表并报错到redis数据库中 KEY=wx_UserList
func GetUserList() {
	url := "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + GetAccessToken()
	key := "wx_UserList"
	var v WXUserList
	c, status := global.HttpGet(url)
	if status >= 0 {
		dec := json.NewDecoder(strings.NewReader(c))
		for {
			if err := dec.Decode(&v); err == io.EOF {
				break
			} else if err != nil {
				log.Fatal(err)
			}
		}

		var redcli redis.Client
		redcli.Addr = beego.AppConfig.String("redis_addr")

		if ok, err := redcli.Exists(key); err != nil {
			log.Println(err.Error())
		} else {
			if ok {
				redcli.Del(key)
			}
		}
		for _, value := range v.Data.Openid {
			redcli.Rpush(key, []byte(value))
		}
		log.Println("Successful: Add Userlist ")
	}
}
Example #2
0
// 更新微信的AccessToken到Redis中 key=REDIS_KEY_WC_ACCESS_TOKEN
func UpdateAccessToken() (expires_in int, err error) {
	url := "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + config.WECHAT_APPID +
		"&secret=" + config.WECHAT_SECRET
	if c, status := global.HttpGet(url); status < 0 {
		err = errors.New("access_token 获取异常 " + strconv.Itoa(status))
	} else {
		log.Println(c)
		var v AccessToken
		dec := json.NewDecoder(strings.NewReader(c))
		for {
			if err = dec.Decode(&v); err == io.EOF {
				break
			} else if err != nil {
				log.Fatal(err)
				return
			}
		}

		expires_in = (int)(v.Expires_in)
		err = redcli.Setex(REDIS_KEY_WC_ACCESS_TOKEN, v.Expires_in, []byte(v.Access_token))
		if err != nil {
			log.Println(err.Error())
		} else {
			log.Println("Successful: get AccessToken ")
		}
	}
	return
}
Example #3
0
/// 获取用户基本信息,并保存到mysql 表tb_wx_user_info 中
func GetUserInfo(openid string) (this *WXUserInfo) {
	url := "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + GetAccessToken() + "&openid=" + openid + "&lang=zh_CN"
	c, status := global.HttpGet(url)
	this = &WXUserInfo{}
	if status >= 0 {
		dec := json.NewDecoder(strings.NewReader(c))
		for {
			if err := dec.Decode(this); err == io.EOF {
				break
			} else if err != nil {
				log.Println(err)
			}
		}
	}
	this.saveUserInfo()
	return
}
Example #4
0
// 刷新网页端 AccessToken
func (this *WebAccessToken) RefreshAccessToken() {
	url := "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + config.WECHAT_APPID +
		"&grant_type=refresh_token&refresh_token=" + this.Refresh_token
	content, status := global.HttpGet(url)
	if status >= 0 {
		log.Println(content)
		dec := json.NewDecoder(strings.NewReader(content))
		var v WebAccessToken
		for {
			if err := dec.Decode(&v); err == io.EOF {
				break
			} else if err != nil {
				log.Fatal(err)
			}
		}
		this = &v
	}
}
Example #5
0
// 获取用户信息
func (this *WebAccessToken) GetUserInfo() *WebUserInfo {
	url := "https://api.weixin.qq.com/sns/userinfo?access_token=" + this.Access_token +
		"&openid=" + this.Openid + "&lang=zh_CN"
	content, status := global.HttpGet(url)
	if status >= 0 {
		log.Println(content)
		dec := json.NewDecoder(strings.NewReader(content))
		var v WebUserInfo
		for {
			if err := dec.Decode(&v); err == io.EOF {
				break
			} else if err != nil {
				log.Fatal(err)
			}
		}
		return &v
	}
	return nil
}
Example #6
0
// 获取网页端的 AccessToken
func GetWebAccessToken(code string) *WebAccessToken {
	url := "https://api.weixin.qq.com/sns/oauth2/access_token?" +
		"appid=" + config.WECHAT_APPID +
		"&secret=" + config.WECHAT_SECRET +
		"&code=" + code +
		"&grant_type=authorization_code"
	content, status := global.HttpGet(url)
	if status >= 0 {
		log.Println(content)
		dec := json.NewDecoder(strings.NewReader(content))
		var v WebAccessToken
		for {
			if err := dec.Decode(&v); err == io.EOF {
				break
			} else if err != nil {
				log.Fatal(err)
			}
		}
		return &v
	}
	return nil
}