Kaynağa Gözat

feats: add some funcs

张延森 3 yıl önce
ebeveyn
işleme
08292f705b
6 değiştirilmiş dosya ile 77 ekleme ve 4 silme
  1. 4
    4
      README.md
  2. 27
    0
      authorization.go
  3. 5
    0
      config/index.go
  4. 24
    0
      init.go
  5. 12
    0
      utils/log/logger.go
  6. 5
    0
      utils/utils.go

+ 4
- 4
README.md Dosyayı Görüntüle

@@ -3,10 +3,7 @@
3 3
 `wxcomponent` 是一个[微信开放平台](https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/product/Third_party_platform_appid.html)的 `go sdk`
4 4
 
5 5
 ## 功能列表
6
-- [ ] openApi管理
7
-- [ ] 第三方平台域名管理
8
-- [ ] 小程序用户隐私保护指引
9
-- [x] 授权相关接口
6
+- [x] 授权与Token
10 7
   1. 获取验证票据
11 8
   2. 启动票据推送服务
12 9
   3. 获取令牌
@@ -15,6 +12,9 @@
15 12
   6. 获取/刷新接口调用令牌
16 13
   7. 获取授权帐号信息
17 14
   8. 授权变更通知推送
15
+- [ ] openApi管理
16
+- [ ] 第三方平台域名管理
17
+- [ ] 小程序用户隐私保护指引
18 18
 - [x] 授权方账号管理
19 19
   1. 拉取授权方帐号基本信息
20 20
   2. 获取授权方选项信息

+ 27
- 0
authorization.go Dosyayı Görüntüle

@@ -0,0 +1,27 @@
1
+package wxcomponent
2
+
3
+import (
4
+	"gitee.com/yansen_zh/wxcomponent/api/authorization"
5
+	"gitee.com/yansen_zh/wxcomponent/config"
6
+	"gitee.com/yansen_zh/wxcomponent/utils"
7
+)
8
+
9
+// 授权与Token
10
+
11
+// RefreshToken 刷新Token
12
+// 主要是定时任务调用
13
+func RefreshToken() error {
14
+	result, err := authorization.GetComponentToken()
15
+	if err != nil {
16
+		return err
17
+	}
18
+
19
+	token := result.ComponentAccessToken
20
+	expire := utils.GetExpireTime(result.ExpiresIn)
21
+
22
+	if e := config.RefreshToken(token, expire); e != nil {
23
+		return e
24
+	}
25
+
26
+	return nil
27
+}

+ 5
- 0
config/index.go Dosyayı Görüntüle

@@ -23,6 +23,11 @@ func Init(config Config) error {
23 23
 	return nil
24 24
 }
25 25
 
26
+// GetConfiger 获取配置实例
27
+func GetConfiger() Config {
28
+	return conf
29
+}
30
+
26 31
 // GetAppID 获取平台 APPID
27 32
 func GetAppID() string {
28 33
 	return conf.GetAppID()

+ 24
- 0
init.go Dosyayı Görüntüle

@@ -13,6 +13,10 @@
13 13
 package wxcomponent
14 14
 
15 15
 import (
16
+	"errors"
17
+
18
+	"gitee.com/yansen_zh/wxcomponent/api/authorization"
19
+	"gitee.com/yansen_zh/wxcomponent/config"
16 20
 	"gitee.com/yansen_zh/wxcomponent/utils/log"
17 21
 )
18 22
 
@@ -20,3 +24,23 @@ import (
20 24
 func InitLogger(logger log.Logger) {
21 25
 	log.SetLogger(logger)
22 26
 }
27
+
28
+// InitConfig 初始化配置
29
+func InitConfig(conf config.Config) error {
30
+	return config.Init(conf)
31
+}
32
+
33
+// Start 启动服务
34
+func Start() error {
35
+	if config.GetConfiger() == nil {
36
+		errors.New("请先进行配置初始化")
37
+	}
38
+
39
+	if log.GetLogger() == nil {
40
+		errors.New("请先进行日志初始化")
41
+	}
42
+
43
+	// 启动ticket推送服务, 之后微信服务器会每隔 10 分钟推送 component_verify_ticket
44
+	err := authorization.StartPushTicket()
45
+	return err
46
+}

+ 12
- 0
utils/log/logger.go Dosyayı Görüntüle

@@ -22,10 +22,22 @@ type Logger interface {
22 22
 
23 23
 var inst Logger
24 24
 
25
+// SetLogger 设置日志对象
25 26
 func SetLogger(logger Logger) {
26 27
 	inst = logger
27 28
 }
28 29
 
30
+// GetLogger 获取日志对象
29 31
 func GetLogger() Logger {
30 32
 	return inst
31 33
 }
34
+
35
+// Error 级别日志
36
+func Error(v ...interface{}) {
37
+	inst.Error(v...)
38
+}
39
+
40
+// Info 级别错误
41
+func Info(v ...interface{}) {
42
+	inst.Info(v...)
43
+}

+ 5
- 0
utils/utils.go Dosyayı Görüntüle

@@ -22,3 +22,8 @@ func RandStr(n int) string {
22 22
 
23 23
 	return strings.Join(dst, "")
24 24
 }
25
+
26
+// GetExpireTime 获取过期时间
27
+func GetExpireTime(sec int) time.Time {
28
+	return time.Now().Add(time.Duration(sec) * time.Second)
29
+}