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)

}
相关推荐
用户2986985301427 分钟前
Python 实战:把代码写入 Word 文档的两种实现方式
后端·python·api
瑞雪兆丰年兮34 分钟前
JavaWeb-day10-SpringBootWeb案例
java·开发语言·javaweb·web
挖掘狂人35 分钟前
六条分支,一条主干:我的 Git 分支管理心得
git·后端·程序员
Elastic 中国社区官方博客42 分钟前
更少的位数,相同的召回率:DiskBBQ 更保守的自动校准扫描
大数据·运维·开发语言·人工智能·elasticsearch·搜索引擎
程序员cxuan1 小时前
再见了 Typora
人工智能·后端·程序员
Rain的Java大神之路1 小时前
你背的Spring答案,全是5.x时代的:7.0.8源码一翻,80%的博客都过时了
java·后端·面试
阿卡好可爱1 小时前
Lua 热更新不生效:改完文件线上仍是旧逻辑,package.loaded 与 upvalue 重绑定详解
数据结构·分布式·后端·架构·lua
三雷科技1 小时前
第6章第4节:自定义分词器
开发语言·c#
Adellle2 小时前
设计模式指南 (Java 版) ㊁
java·开发语言·设计模式
Delightrs2 小时前
Rust LeetCode238 算法题解
开发语言·算法·rust