浏览代码

feats: add some funcs

张延森 3 年前
父节点
当前提交
08292f705b
共有 6 个文件被更改,包括 77 次插入4 次删除
  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 查看文件

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

+ 27
- 0
authorization.go 查看文件

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 查看文件

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

+ 24
- 0
init.go 查看文件

13
 package wxcomponent
13
 package wxcomponent
14
 
14
 
15
 import (
15
 import (
16
+	"errors"
17
+
18
+	"gitee.com/yansen_zh/wxcomponent/api/authorization"
19
+	"gitee.com/yansen_zh/wxcomponent/config"
16
 	"gitee.com/yansen_zh/wxcomponent/utils/log"
20
 	"gitee.com/yansen_zh/wxcomponent/utils/log"
17
 )
21
 )
18
 
22
 
20
 func InitLogger(logger log.Logger) {
24
 func InitLogger(logger log.Logger) {
21
 	log.SetLogger(logger)
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 查看文件

22
 
22
 
23
 var inst Logger
23
 var inst Logger
24
 
24
 
25
+// SetLogger 设置日志对象
25
 func SetLogger(logger Logger) {
26
 func SetLogger(logger Logger) {
26
 	inst = logger
27
 	inst = logger
27
 }
28
 }
28
 
29
 
30
+// GetLogger 获取日志对象
29
 func GetLogger() Logger {
31
 func GetLogger() Logger {
30
 	return inst
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 查看文件

22
 
22
 
23
 	return strings.Join(dst, "")
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
+}