|
| 1 | +package credential |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "sync" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/silenceper/wechat/v2/cache" |
| 10 | + "github.com/silenceper/wechat/v2/util" |
| 11 | +) |
| 12 | + |
| 13 | +// TicketType ticket类型 |
| 14 | +type TicketType int |
| 15 | + |
| 16 | +const ( |
| 17 | + // TicketTypeCorpJs 企业jsapi ticket |
| 18 | + TicketTypeCorpJs TicketType = iota |
| 19 | + // TicketTypeAgentJs 应用jsapi ticket |
| 20 | + TicketTypeAgentJs |
| 21 | +) |
| 22 | + |
| 23 | +// 企业微信相关的 ticket URL |
| 24 | +const ( |
| 25 | + // 企业微信 jsapi ticket |
| 26 | + getWorkJsTicketURL = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=%s" |
| 27 | + // 企业微信应用 jsapi ticket |
| 28 | + getWorkAgentJsTicketURL = "https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token=%s&type=agent_config" |
| 29 | +) |
| 30 | + |
| 31 | +// WorkJsTicket 企业微信js ticket获取 |
| 32 | +type WorkJsTicket struct { |
| 33 | + corpID string |
| 34 | + agentID string |
| 35 | + cacheKeyPrefix string |
| 36 | + cache cache.Cache |
| 37 | + jsAPITicketLock *sync.Mutex |
| 38 | +} |
| 39 | + |
| 40 | +// NewWorkJsTicket new WorkJsTicket |
| 41 | +func NewWorkJsTicket(corpID, agentID, cacheKeyPrefix string, cache cache.Cache) *WorkJsTicket { |
| 42 | + return &WorkJsTicket{ |
| 43 | + corpID: corpID, |
| 44 | + agentID: agentID, |
| 45 | + cache: cache, |
| 46 | + cacheKeyPrefix: cacheKeyPrefix, |
| 47 | + jsAPITicketLock: new(sync.Mutex), |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// GetTicket 根据类型获取相应的jsapi_ticket |
| 52 | +func (js *WorkJsTicket) GetTicket(accessToken string, ticketType TicketType) (ticketStr string, err error) { |
| 53 | + var cacheKey string |
| 54 | + switch ticketType { |
| 55 | + case TicketTypeCorpJs: |
| 56 | + cacheKey = fmt.Sprintf("%s_corp_jsapi_ticket_%s", js.cacheKeyPrefix, js.corpID) |
| 57 | + case TicketTypeAgentJs: |
| 58 | + if js.agentID == "" { |
| 59 | + err = fmt.Errorf("agentID is empty") |
| 60 | + return |
| 61 | + } |
| 62 | + cacheKey = fmt.Sprintf("%s_agent_jsapi_ticket_%s_%s", js.cacheKeyPrefix, js.corpID, js.agentID) |
| 63 | + default: |
| 64 | + err = fmt.Errorf("unsupported ticket type: %v", ticketType) |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + if val := js.cache.Get(cacheKey); val != nil { |
| 69 | + return val.(string), nil |
| 70 | + } |
| 71 | + |
| 72 | + js.jsAPITicketLock.Lock() |
| 73 | + defer js.jsAPITicketLock.Unlock() |
| 74 | + |
| 75 | + // 双检,防止重复从微信服务器获取 |
| 76 | + if val := js.cache.Get(cacheKey); val != nil { |
| 77 | + return val.(string), nil |
| 78 | + } |
| 79 | + |
| 80 | + var ticket ResTicket |
| 81 | + ticket, err = js.getTicketFromServer(accessToken, ticketType) |
| 82 | + if err != nil { |
| 83 | + return |
| 84 | + } |
| 85 | + expires := ticket.ExpiresIn - 1500 |
| 86 | + err = js.cache.Set(cacheKey, ticket.Ticket, time.Duration(expires)*time.Second) |
| 87 | + ticketStr = ticket.Ticket |
| 88 | + return |
| 89 | +} |
| 90 | + |
| 91 | +// getTicketFromServer 从服务器中获取ticket |
| 92 | +func (js *WorkJsTicket) getTicketFromServer(accessToken string, ticketType TicketType) (ticket ResTicket, err error) { |
| 93 | + var url string |
| 94 | + switch ticketType { |
| 95 | + case TicketTypeCorpJs: |
| 96 | + url = fmt.Sprintf(getWorkJsTicketURL, accessToken) |
| 97 | + case TicketTypeAgentJs: |
| 98 | + url = fmt.Sprintf(getWorkAgentJsTicketURL, accessToken) |
| 99 | + default: |
| 100 | + err = fmt.Errorf("unsupported ticket type: %v", ticketType) |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + var response []byte |
| 105 | + response, err = util.HTTPGet(url) |
| 106 | + if err != nil { |
| 107 | + return |
| 108 | + } |
| 109 | + err = json.Unmarshal(response, &ticket) |
| 110 | + if err != nil { |
| 111 | + return |
| 112 | + } |
| 113 | + if ticket.ErrCode != 0 { |
| 114 | + err = fmt.Errorf("getTicket Error : errcode=%d , errmsg=%s", ticket.ErrCode, ticket.ErrMsg) |
| 115 | + return |
| 116 | + } |
| 117 | + return |
| 118 | +} |
0 commit comments