feat(企微签到):逻辑:员工使用企微扫码后直接跳出签到页面,在签到页面对该课程进行点击签到!
deploy-hxtesting / build-and-deploy (push) Successful in 1m14s
deploy-hxtesting / build-and-deploy (push) Successful in 1m14s
规则:1.签到要校验是否为培训当天签到,如果不是当天扫码签到则进行友好提示;2. 签到需提供员工工号及随机时间戳(超过签到二维码有效期后需重新扫码签到);3.签到二维码每5分钟自动更新一次(更新后之前扫码签到自动作废需重新扫码); 补充说明:1.签到页面需展示该签到课程相关信息;2.展示签到倒计时还有多久过期,过期后需重新扫码进行优化提示;3.签到后提示签到成功!自动登录进入平台系统并进入该课程详情页面
This commit is contained in:
1 parent
9950a28346
commit
f43fbef5db
24 files changed
+1336
-67
No files matched your search
@@ -13,6 +13,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
neturl "net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -111,6 +112,65 @@ func (c *ProactiveClient) SendMarkdown(ctx context.Context, userIDs []string, co
|
||||
return nil
|
||||
}
|
||||
|
||||
// wecomOAuthAuthorizeBase 企微网页授权入口(公开固定地址,不走 apiBaseURL ——
|
||||
// 后者可被私有化部署改写,而 OAuth 授权页始终由微信侧提供)。
|
||||
const wecomOAuthAuthorizeBase = "https://open.weixin.qq.com/connect/oauth2/authorize"
|
||||
|
||||
// BuildOAuthAuthorizeURL 企微网页授权链接(snsapi_base 静默免登,培训扫码签到用):
|
||||
// 学员在企微内置浏览器打开后无感授权,302 回 redirect_uri 并携带 code 与 state。
|
||||
func (c *ProactiveClient) BuildOAuthAuthorizeURL(redirectURI, state string) string {
|
||||
return fmt.Sprintf(
|
||||
"%s?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_base&state=%s&agentid=%d#wechat_redirect",
|
||||
wecomOAuthAuthorizeBase,
|
||||
neturl.QueryEscape(c.corpID),
|
||||
neturl.QueryEscape(redirectURI),
|
||||
neturl.QueryEscape(state),
|
||||
c.corpAgentID,
|
||||
)
|
||||
}
|
||||
|
||||
// GetOAuthUserID 网页授权 code 换用户身份(GET /cgi-bin/auth/getuserinfo):
|
||||
// snsapi_base 静默授权即可拿到 userid;code 由企微保证一次性(5 分钟有效)。
|
||||
func (c *ProactiveClient) GetOAuthUserID(ctx context.Context, code string) (string, error) {
|
||||
accessToken, err := c.getAccessToken(ctx)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("get access token: %w", err)
|
||||
}
|
||||
userURL := fmt.Sprintf("%s/cgi-bin/auth/getuserinfo?access_token=%s&code=%s",
|
||||
c.apiBaseURL, accessToken, neturl.QueryEscape(code))
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, userURL, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("create request: %w", err)
|
||||
}
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("request oauth userinfo: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var result struct {
|
||||
ErrCode int `json:"errcode"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
UserID string `json:"userid"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||
return "", fmt.Errorf("decode oauth userinfo: %w", err)
|
||||
}
|
||||
if result.ErrCode != 0 {
|
||||
// 42001/40014 token 过期类错误清缓存,让下次调用重新获取。
|
||||
if result.ErrCode == 42001 || result.ErrCode == 40014 {
|
||||
c.tokenMu.Lock()
|
||||
c.tokenCache = ""
|
||||
c.tokenExpAt = time.Time{}
|
||||
c.tokenMu.Unlock()
|
||||
}
|
||||
return "", fmt.Errorf("wecom oauth api error: code=%d msg=%s", result.ErrCode, result.ErrMsg)
|
||||
}
|
||||
if result.UserID == "" {
|
||||
return "", fmt.Errorf("wecom oauth api returned empty userid")
|
||||
}
|
||||
return result.UserID, nil
|
||||
}
|
||||
|
||||
// getAccessToken retrieves the WeCom access token with caching.
|
||||
func (c *ProactiveClient) getAccessToken(ctx context.Context) (string, error) {
|
||||
c.tokenMu.Lock()
|
||||
|
||||
@@ -91,6 +91,12 @@ func (Backend) ClientForTenant(ctx context.Context, db *gorm.DB, tenantID uint64
|
||||
return TenantClient(ctx, db, tenantID)
|
||||
}
|
||||
|
||||
// OAuthClientForTenant resolves the tenant's client for web OAuth (培训扫码
|
||||
// 签到免登):同一个自建应用凭据既推消息也做网页授权,复用一套配置。
|
||||
func (Backend) OAuthClientForTenant(ctx context.Context, db *gorm.DB, tenantID uint64) (*wecom.ProactiveClient, error) {
|
||||
return TenantClient(ctx, db, tenantID)
|
||||
}
|
||||
|
||||
// TenantWecomUserIDs resolves the userid bindings.
|
||||
func (Backend) TenantWecomUserIDs(ctx context.Context, db *gorm.DB, tenantID uint64, userIDs []string) (map[string]string, error) {
|
||||
return TenantWecomUserIDs(ctx, db, tenantID, userIDs)
|
||||
|
||||
Reference in new issue
Block a user