|
123456789101112131415161718192021222324252627282930313233343536373839404142 |
- package svc
-
- import (
- "applet/app/customer/md"
- "applet/app/db"
- "applet/app/enum"
- "applet/app/utils/cache"
- "applet/app/utils/logx"
- "github.com/medivhzhan/weapp/v2"
- )
-
- func GetWxAccessToken() (accessToken string, err error) {
- cacheKey := md.WxAccessToken
- // 获取之前生成的token
- accessToken, err = cache.GetString(cacheKey)
- if err != nil {
- _ = logx.Error(err)
- }
-
- // 没有获取到
- if err != nil || accessToken == "" {
- sysCfgDb := db.SysCfgDb{}
- sysCfgDb.Set()
- cfg := sysCfgDb.SysCfgFindWithDb(enum.WxAppletAppId, enum.WxAppletAppSecret)
-
- // 生成
- res, err1 := weapp.GetAccessToken(cfg[enum.WxAppletAppId], cfg[enum.WxAppletAppSecret])
- if err1 != nil {
- return "", err1
- }
- accessToken = res.AccessToken
-
- // 缓存
- _, err = cache.SetEx(cacheKey, accessToken, md.WxAccessTokenCacheTime)
- if err != nil {
- return "", err
- }
- return accessToken, nil
- }
-
- return accessToken, nil
- }
|