智慧食堂
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

svc_wx_access_token.go 922 B

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package svc
  2. import (
  3. "applet/app/customer/md"
  4. "applet/app/db"
  5. "applet/app/enum"
  6. "applet/app/utils/cache"
  7. "applet/app/utils/logx"
  8. "github.com/medivhzhan/weapp/v2"
  9. )
  10. func GetWxAccessToken() (accessToken string, err error) {
  11. cacheKey := md.WxAccessToken
  12. // 获取之前生成的token
  13. accessToken, err = cache.GetString(cacheKey)
  14. if err != nil {
  15. _ = logx.Error(err)
  16. }
  17. // 没有获取到
  18. if err != nil || accessToken == "" {
  19. sysCfgDb := db.SysCfgDb{}
  20. sysCfgDb.Set()
  21. cfg := sysCfgDb.SysCfgFindWithDb(enum.WxAppletAppId, enum.WxAppletAppSecret)
  22. // 生成
  23. res, err1 := weapp.GetAccessToken(cfg[enum.WxAppletAppId], cfg[enum.WxAppletAppSecret])
  24. if err1 != nil {
  25. return "", err1
  26. }
  27. accessToken = res.AccessToken
  28. // 缓存
  29. _, err = cache.SetEx(cacheKey, accessToken, md.WxAccessTokenCacheTime)
  30. if err != nil {
  31. return "", err
  32. }
  33. return accessToken, nil
  34. }
  35. return accessToken, nil
  36. }