|
| 1 | +package halalcloudopen |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + "time" |
| 6 | + |
| 7 | + sdkUser "github.com/halalcloud/golang-sdk-lite/halalcloud/services/user" |
| 8 | +) |
| 9 | + |
| 10 | +var ( |
| 11 | + slicePostErrorRetryInterval = time.Second * 120 |
| 12 | + retryTimes = 5 |
| 13 | +) |
| 14 | + |
| 15 | +type halalCommon struct { |
| 16 | + // *AuthService // 登录信息 |
| 17 | + UserInfo *sdkUser.User // 用户信息 |
| 18 | + refreshTokenFunc func(token string) error |
| 19 | + // serv *AuthService |
| 20 | + configs sync.Map |
| 21 | +} |
| 22 | + |
| 23 | +func (m *halalCommon) GetAccessToken() (string, error) { |
| 24 | + value, exists := m.configs.Load("access_token") |
| 25 | + if !exists { |
| 26 | + return "", nil // 如果不存在,返回空字符串 |
| 27 | + } |
| 28 | + return value.(string), nil // 返回配置项的值 |
| 29 | +} |
| 30 | + |
| 31 | +// GetRefreshToken implements ConfigStore. |
| 32 | +func (m *halalCommon) GetRefreshToken() (string, error) { |
| 33 | + value, exists := m.configs.Load("refresh_token") |
| 34 | + if !exists { |
| 35 | + return "", nil // 如果不存在,返回空字符串 |
| 36 | + } |
| 37 | + return value.(string), nil // 返回配置项的值 |
| 38 | +} |
| 39 | + |
| 40 | +// SetAccessToken implements ConfigStore. |
| 41 | +func (m *halalCommon) SetAccessToken(token string) error { |
| 42 | + m.configs.Store("access_token", token) |
| 43 | + return nil |
| 44 | +} |
| 45 | + |
| 46 | +// SetRefreshToken implements ConfigStore. |
| 47 | +func (m *halalCommon) SetRefreshToken(token string) error { |
| 48 | + m.configs.Store("refresh_token", token) |
| 49 | + if m.refreshTokenFunc != nil { |
| 50 | + return m.refreshTokenFunc(token) |
| 51 | + } |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +// SetToken implements ConfigStore. |
| 56 | +func (m *halalCommon) SetToken(accessToken string, refreshToken string, expiresIn int64) error { |
| 57 | + m.configs.Store("access_token", accessToken) |
| 58 | + m.configs.Store("refresh_token", refreshToken) |
| 59 | + m.configs.Store("expires_in", expiresIn) |
| 60 | + if m.refreshTokenFunc != nil { |
| 61 | + return m.refreshTokenFunc(refreshToken) |
| 62 | + } |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +// ClearConfigs implements ConfigStore. |
| 67 | +func (m *halalCommon) ClearConfigs() error { |
| 68 | + m.configs = sync.Map{} // 清空map |
| 69 | + return nil |
| 70 | +} |
| 71 | + |
| 72 | +// DeleteConfig implements ConfigStore. |
| 73 | +func (m *halalCommon) DeleteConfig(key string) error { |
| 74 | + _, exists := m.configs.Load(key) |
| 75 | + if !exists { |
| 76 | + return nil // 如果不存在,直接返回 |
| 77 | + } |
| 78 | + m.configs.Delete(key) // 删除指定的配置项 |
| 79 | + return nil |
| 80 | +} |
| 81 | + |
| 82 | +// GetConfig implements ConfigStore. |
| 83 | +func (m *halalCommon) GetConfig(key string) (string, error) { |
| 84 | + value, exists := m.configs.Load(key) |
| 85 | + if !exists { |
| 86 | + return "", nil // 如果不存在,返回空字符串 |
| 87 | + } |
| 88 | + return value.(string), nil // 返回配置项的值 |
| 89 | +} |
| 90 | + |
| 91 | +// ListConfigs implements ConfigStore. |
| 92 | +func (m *halalCommon) ListConfigs() (map[string]string, error) { |
| 93 | + configs := make(map[string]string) |
| 94 | + m.configs.Range(func(key, value interface{}) bool { |
| 95 | + configs[key.(string)] = value.(string) // 将每个配置项添加到map中 |
| 96 | + return true // 继续遍历 |
| 97 | + }) |
| 98 | + return configs, nil // 返回所有配置项 |
| 99 | +} |
| 100 | + |
| 101 | +// SetConfig implements ConfigStore. |
| 102 | +func (m *halalCommon) SetConfig(key string, value string) error { |
| 103 | + m.configs.Store(key, value) // 使用Store方法设置或更新配置项 |
| 104 | + return nil // 成功设置配置项后返回nil |
| 105 | +} |
| 106 | + |
| 107 | +func NewHalalCommon() *halalCommon { |
| 108 | + return &halalCommon{ |
| 109 | + configs: sync.Map{}, |
| 110 | + } |
| 111 | +} |
0 commit comments