@@ -4,12 +4,13 @@ import (
44 "context"
55 "errors"
66 "fmt"
7+ "net/http"
8+ "time"
9+
710 "github.com/ArtisanCloud/PowerLibs/v3/object"
811 "github.com/ArtisanCloud/PowerWeChat/v3/src/basicService/jssdk"
912 "github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
1013 response2 "github.com/ArtisanCloud/PowerWeChat/v3/src/work/jssdk/response"
11- "net/http"
12- "time"
1314)
1415
1516type Client struct {
@@ -48,7 +49,7 @@ func (comp *Client) GetAgentConfigArray(
4849 url string ,
4950 nonce string ,
5051 timestamp int64 ,
51- ) * object.HashMap {
52+ ) ( * object.HashMap , error ) {
5253
5354 // url 为空时使用默认
5455 if url == "" {
@@ -68,7 +69,7 @@ func (comp *Client) GetAgentConfigArray(
6869 // 获取 agent ticket
6970 ticketInfo , err := comp .GetAgentTicket (request .Context (), agentID , false , "agent_config" )
7071 if err != nil {
71- return nil
72+ return nil , err
7273 }
7374 ticket := ticketInfo ["ticket" ].(string )
7475
@@ -82,7 +83,7 @@ func (comp *Client) GetAgentConfigArray(
8283 "timestamp" : timestamp ,
8384 "url" : url ,
8485 "signature" : signature ,
85- }
86+ }, nil
8687}
8788
8889func (comp * Client ) GetTicket (ctx context.Context ) (* response2.ResponseGetTicket , error ) {
@@ -97,7 +98,7 @@ func (comp *Client) GetTicket(ctx context.Context) (*response2.ResponseGetTicket
9798 return result , err
9899}
99100
100- func (c * Client ) GetAgentTicket (
101+ func (comp * Client ) GetAgentTicket (
101102 ctx context.Context ,
102103 agentID int ,
103104 refresh bool ,
@@ -112,20 +113,22 @@ func (c *Client) GetAgentTicket(
112113 "powerwechat.work.jssdk.ticket.%d.%s.%s" ,
113114 agentID ,
114115 ticketType ,
115- c .GetAppID (),
116+ comp .GetAppID (),
116117 )
117118
118- if ! refresh && c .Cache .Has (cacheKey ) {
119- value , err := c .Cache .Get (cacheKey , nil )
119+ if ! refresh && comp .Cache .Has (cacheKey ) {
120+ value , err := comp .Cache .Get (cacheKey , nil )
120121 if err == nil {
121- if data , ok := value .(object.HashMap ); ok {
122- return data , nil
122+ if data , ok := value .(map [string ]interface {}); ok {
123+ hashMap := object .HashMap (data )
124+ return hashMap , nil
123125 }
124126 }
125127 }
126128
127129 resp := object.HashMap {}
128- _ , err := c .BaseClient .RequestRaw (
130+
131+ _ , err := comp .BaseClient .RequestRaw (
129132 ctx ,
130133 "cgi-bin/ticket/get" ,
131134 "GET" ,
@@ -141,12 +144,16 @@ func (c *Client) GetAgentTicket(
141144 return nil , err
142145 }
143146
147+ if resp .Get ("errcode" ).(float64 ) != 0 {
148+ return nil , errors .New (resp .Get ("errmsg" ).(string ))
149+ }
150+
144151 expiresIn := int (resp ["expires_in" ].(float64 ))
145152 ttl := time .Duration (expiresIn - 500 ) * time .Second
146153
147- _ = c .Cache .Set (cacheKey , resp , ttl )
154+ _ = comp .Cache .Set (cacheKey , resp , ttl )
148155
149- if ! c .Cache .Has (cacheKey ) {
156+ if ! comp .Cache .Has (cacheKey ) {
150157 return nil , errors .New ("failed to cache jssdk ticket" )
151158 }
152159
0 commit comments