go微信开发sdk-简单使用_已设置图床

go微信开发sdk-简单使用

GitHub - silenceper/wechat: WeChat SDK for Go (微信SDK:简单、易用)

使用的sdk为上述的,这边给出快速的项目实例

复制代码
git clone https://github.com/gowechat/example.git

简单的项目结构

这边简单用docker跑一个redis

复制代码
version: '3'
services:
  redis:
    image: redis:latest
    ports:
      - "6379:6379"
    command: ["redis-server", "--requirepass", ""]

在微信公众平台可以获取到必要的信息,注意申请接口权限(微信认证)

实例

本人项目需要操作公众号,这里给出一些基本的操作的实例

在克隆基本的项目地址后

完成配置和redis的开启,可以做到指定功能的获取

使用redis(推荐)

复制代码
// 在这里已经设置了全局cache,则在具体获取公众号/小程序等操作实例之后无需再设置,设置即覆盖
// 在这里已经设置了全局cache,则在具体获取公众号/小程序等操作实例之后无需再设置,设置即覆盖
func InitWechat() *wechat.Wechat {
	wc := wechat.NewWechat()
	redisOpts := &cache.RedisOpts{
		Host:        config.GVA_CONFIG.Redis.Host,
		Password:    config.GVA_CONFIG.Redis.Password,
		Database:    config.GVA_CONFIG.Redis.Database,
		MaxActive:   config.GVA_CONFIG.Redis.MaxActive,
		MaxIdle:     config.GVA_CONFIG.Redis.MaxIdle,
		IdleTimeout: config.GVA_CONFIG.Redis.IdleTimeout,
	}
	ctx := context.Background()
	redisCache := cache.NewRedis(ctx, redisOpts)

	//redisCache := cache.NewRedis(redisOpts)
	wc.SetCache(redisCache)
	return wc
}
func Wx() {
	wc := InitWechat()

	cfg := &offConfig.Config{
		AppID:     "",
		AppSecret: "",
		Token:     "xxx",
	}
	//officialAccount := wc.GetOfficialAccount(cfg)
	oa := wc.GetOfficialAccount(cfg)
	fmt.Println(oa.GetAccessToken())
	u := oa.GetUser()
	us, _ := u.ListUserOpenIDs()
	if us == nil {
		fmt.Print("heellwdjwd")
	}
	fmt.Println(us.Data.OpenIDs)
}

使用内存的场合(不推荐)

复制代码
package main

import (
    "fmt"
    wechat "github.com/silenceper/wechat/v2"
    "github.com/silenceper/wechat/v2/cache"
    offConfig "github.com/silenceper/wechat/v2/officialaccount/config"
)
func main() {
    wc := wechat.NewWechat()
    //这里本地内存保存access_token,也可选择redis,memcache或者自定cache
    memory := cache.NewMemory()
    cfg := &offConfig.Config{
        AppID:     "",
        AppSecret: "",
        Token:     "xxx",
        Cache: memory,
    }
    oa := wc.GetOfficialAccount(cfg)
    fmt.Println(oa.GetAccessToken())
    u := oa.GetUser()
    us, _ := u.ListUserOpenIDs()
    if us == nil {
        fmt.Print("获取用户列表失败")
    }
    //fmt.Println("获取到的openid列表为",us.Data)
    fmt.Println("获取到的openid列表为", us.Data.OpenIDs)
    fmt.Println("获取到的openid总数为", us.Total)

}
相关推荐
Y1rong11 小时前
C++ QT之记事本
开发语言·qt
diegoXie14 小时前
Python / R 向量顺序分割与跨步分割
开发语言·python·r语言
程序员小白条14 小时前
0经验如何找实习?
java·开发语言·数据结构·数据库·链表
liulilittle15 小时前
C++ 浮点数封装。
linux·服务器·开发语言·前端·网络·数据库·c++
韩立学长15 小时前
基于Springboot流浪动物领养网站0kh2iyb4(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·后端
Moment15 小时前
从美团全栈化看 AI 冲击:前端转全栈,是自救还是必然 🤔🤔🤔
前端·后端·面试
失散1315 小时前
Python——1 概述
开发语言·python
萧鼎15 小时前
Python 图像哈希库 imagehash——从原理到实践
开发语言·python·哈希算法
小小8程序员15 小时前
STL 库(C++ Standard Template Library)全面介绍
java·开发语言·c++
立志成为大牛的小牛15 小时前
数据结构——五十六、排序的基本概念(王道408)
开发语言·数据结构·程序人生·算法