github.com/fastwego/offiaccount@v1.0.1/example_offiaccount_test.go (about)

     1  // Copyright 2020 FastWeGo
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package offiaccount_test
    16  
    17  import (
    18  	"log"
    19  	"os"
    20  
    21  	"github.com/faabiosr/cachego/file"
    22  	"github.com/faabiosr/cachego/sync"
    23  	"github.com/fastwego/offiaccount"
    24  	"github.com/gomodule/redigo/redis"
    25  )
    26  
    27  func ExampleOffiAccount_SetAccessTokenCacheDriver() {
    28  	var App *offiaccount.OffiAccount
    29  
    30  	// 使用内存
    31  	App.AccessToken.Cache = sync.New()
    32  
    33  	// 使用指定目录下的 文件
    34  	App.AccessToken.Cache = file.New(os.TempDir())
    35  
    36  	// 更多驱动 请查看 https://github.com/faabiosr/cachego
    37  }
    38  
    39  func ExampleOffiAccount_SetLogger() {
    40  
    41  	var Ctx *offiaccount.OffiAccount
    42  
    43  	// 输出日志到控制台
    44  	Ctx.Logger = log.New(os.Stdout, "[offiaccount]", log.LstdFlags)
    45  
    46  	// 记录日志到指定文件
    47  	logFile, _ := os.OpenFile("/path/to/file", os.O_WRONLY, 0644)
    48  	Ctx.Logger = log.New(logFile, "[offiaccount]", log.LstdFlags)
    49  
    50  	// 关闭日志
    51  	Ctx.Logger = nil
    52  }
    53  
    54  func ExampleOffiAccount_SetGetAccessTokenHandler() {
    55  	var Ctx *offiaccount.OffiAccount
    56  
    57  	conn, _ := redis.Dial("tcp", "127.0.0.1:6379")
    58  	_, _ = conn.Do("AUTH", "PASSWORD")
    59  
    60  	// 从远程 Redis 服务器 获取 AccessToken
    61  	Ctx.AccessToken.GetAccessTokenHandler = func(ctx *offiaccount.OffiAccount) (accessToken string, err error) {
    62  		accessToken, _ = redis.String(conn.Do("GET", "access_token:"+ctx.Config.Appid))
    63  		return
    64  	}
    65  }