wechat_service_api.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. package gopay
  2. import (
  3. "crypto/aes"
  4. "crypto/cipher"
  5. "crypto/hmac"
  6. "crypto/md5"
  7. "crypto/sha256"
  8. "encoding/base64"
  9. "encoding/hex"
  10. "encoding/json"
  11. "encoding/xml"
  12. "errors"
  13. "fmt"
  14. "hash"
  15. "io/ioutil"
  16. "net/http"
  17. "reflect"
  18. "strings"
  19. )
  20. // 获取微信支付所需参数里的Sign值(通过支付参数计算Sign值)
  21. // 注意:BodyMap中如无 sign_type 参数,默认赋值 sign_type 为 MD5
  22. // appId:应用ID
  23. // mchId:商户ID
  24. // ApiKey:API秘钥值
  25. // 返回参数 sign:通过Appid、MchId、ApiKey和BodyMap中的参数计算出的Sign值
  26. func GetWeChatParamSign(appId, mchId, apiKey string, bm BodyMap) (sign string) {
  27. bm.Set("appid", appId)
  28. bm.Set("mch_id", mchId)
  29. var (
  30. signType string
  31. h hash.Hash
  32. )
  33. signType = bm.Get("sign_type")
  34. if signType == null {
  35. bm.Set("sign_type", SignType_MD5)
  36. }
  37. if signType == SignType_HMAC_SHA256 {
  38. h = hmac.New(sha256.New, []byte(apiKey))
  39. } else {
  40. h = md5.New()
  41. }
  42. h.Write([]byte(bm.EncodeWeChatSignParams(apiKey)))
  43. sign = strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
  44. return
  45. }
  46. // 获取微信支付沙箱环境所需参数里的Sign值(通过支付参数计算Sign值)
  47. // 注意:沙箱环境默认 sign_type 为 MD5
  48. // appId:应用ID
  49. // mchId:商户ID
  50. // ApiKey:API秘钥值
  51. // 返回参数 sign:通过Appid、MchId、ApiKey和BodyMap中的参数计算出的Sign值
  52. func GetWeChatSanBoxParamSign(appId, mchId, apiKey string, bm BodyMap) (sign string, err error) {
  53. bm.Set("appid", appId)
  54. bm.Set("mch_id", mchId)
  55. bm.Set("sign_type", SignType_MD5)
  56. var (
  57. sandBoxApiKey string
  58. hashMd5 hash.Hash
  59. )
  60. if sandBoxApiKey, err = getSanBoxKey(mchId, GetRandomString(32), apiKey, SignType_MD5); err != nil {
  61. return
  62. }
  63. hashMd5 = md5.New()
  64. hashMd5.Write([]byte(bm.EncodeWeChatSignParams(sandBoxApiKey)))
  65. sign = strings.ToUpper(hex.EncodeToString(hashMd5.Sum(nil)))
  66. return
  67. }
  68. // 解析微信支付异步通知的结果到BodyMap
  69. // req:*http.Request
  70. // 返回参数bm:Notify请求的参数
  71. // 返回参数err:错误信息
  72. func ParseWeChatNotifyResultToBodyMap(req *http.Request) (bm BodyMap, err error) {
  73. var bs []byte
  74. if bs, err = ioutil.ReadAll(req.Body); err != nil {
  75. return nil, fmt.Errorf("ioutil.ReadAll:%s", err.Error())
  76. }
  77. bm = make(BodyMap)
  78. if err = xml.Unmarshal(bs, &bm); err != nil {
  79. return nil, fmt.Errorf("xml.Unmarshal:%s", err.Error())
  80. }
  81. return
  82. }
  83. // 解析微信支付异步通知的参数
  84. // req:*http.Request
  85. // 返回参数notifyReq:Notify请求的参数
  86. // 返回参数err:错误信息
  87. func ParseWeChatNotifyResult(req *http.Request) (notifyReq *WeChatNotifyRequest, err error) {
  88. notifyReq = new(WeChatNotifyRequest)
  89. if err = xml.NewDecoder(req.Body).Decode(notifyReq); err != nil {
  90. return nil, fmt.Errorf("xml.NewDecoder:%s", err.Error())
  91. }
  92. return
  93. }
  94. // 解析微信退款异步通知的参数
  95. // req:*http.Request
  96. // 返回参数notifyReq:Notify请求的参数
  97. // 返回参数err:错误信息
  98. func ParseWeChatRefundNotifyResult(req *http.Request) (notifyReq *WeChatRefundNotifyRequest, err error) {
  99. notifyReq = new(WeChatRefundNotifyRequest)
  100. if err = xml.NewDecoder(req.Body).Decode(notifyReq); err != nil {
  101. return nil, fmt.Errorf("xml.NewDecoder:%s", err.Error())
  102. }
  103. return
  104. }
  105. // 解密微信退款异步通知的加密数据
  106. // reqInfo:gopay.ParseWeChatRefundNotifyResult() 方法获取的加密数据 req_info
  107. // apiKey:API秘钥值
  108. // 返回参数refundNotify:RefundNotify请求的加密数据
  109. // 返回参数err:错误信息
  110. // 文档:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_16&index=10
  111. func DecryptRefundNotifyReqInfo(reqInfo, apiKey string) (refundNotify *RefundNotify, err error) {
  112. var (
  113. encryptionB, bs []byte
  114. block cipher.Block
  115. blockSize int
  116. )
  117. if encryptionB, err = base64.StdEncoding.DecodeString(reqInfo); err != nil {
  118. return nil, err
  119. }
  120. h := md5.New()
  121. h.Write([]byte(apiKey))
  122. key := strings.ToLower(hex.EncodeToString(h.Sum(nil)))
  123. if len(encryptionB)%aes.BlockSize != 0 {
  124. return nil, errors.New("encryptedData is error")
  125. }
  126. if block, err = aes.NewCipher([]byte(key)); err != nil {
  127. return nil, err
  128. }
  129. blockSize = block.BlockSize()
  130. func(dst, src []byte) {
  131. if len(src)%blockSize != 0 {
  132. panic("crypto/cipher: input not full blocks")
  133. }
  134. if len(dst) < len(src) {
  135. panic("crypto/cipher: output smaller than input")
  136. }
  137. for len(src) > 0 {
  138. block.Decrypt(dst, src[:blockSize])
  139. src = src[blockSize:]
  140. dst = dst[blockSize:]
  141. }
  142. }(encryptionB, encryptionB)
  143. bs = PKCS7UnPadding(encryptionB)
  144. refundNotify = new(RefundNotify)
  145. if err = xml.Unmarshal(bs, refundNotify); err != nil {
  146. return nil, fmt.Errorf("xml.Unmarshal:%s", err.Error())
  147. }
  148. return
  149. }
  150. // 微信同步返回参数验签或异步通知参数验签
  151. // ApiKey:API秘钥值
  152. // signType:签名类型(调用API方法时填写的类型)
  153. // bean:微信同步返回的结构体 wxRsp 或 异步通知解析的结构体 notifyReq
  154. // 返回参数ok:是否验签通过
  155. // 返回参数err:错误信息
  156. func VerifyWeChatSign(apiKey, signType string, bean interface{}) (ok bool, err error) {
  157. if bean == nil {
  158. return false, errors.New("bean is nil")
  159. }
  160. var (
  161. bm BodyMap
  162. bs []byte
  163. kind reflect.Kind
  164. bodySign string
  165. )
  166. kind = reflect.ValueOf(bean).Kind()
  167. if kind == reflect.Map {
  168. bm = bean.(BodyMap)
  169. goto Verify
  170. }
  171. if bs, err = json.Marshal(bean); err != nil {
  172. return false, fmt.Errorf("json.Marshal:%s", err.Error())
  173. }
  174. bm = make(BodyMap)
  175. if err = json.Unmarshal(bs, &bm); err != nil {
  176. return false, fmt.Errorf("json.Unmarshal:%s", err.Error())
  177. }
  178. Verify:
  179. bodySign = bm.Get("sign")
  180. bm.Remove("sign")
  181. return getWeChatReleaseSign(apiKey, signType, bm) == bodySign, nil
  182. }
  183. type WeChatNotifyResponse struct {
  184. ReturnCode string `xml:"return_code"`
  185. ReturnMsg string `xml:"return_msg"`
  186. }
  187. // 返回数据给微信
  188. func (w *WeChatNotifyResponse) ToXmlString() (xmlStr string) {
  189. var buffer strings.Builder
  190. buffer.WriteString("<xml><return_code><![CDATA[")
  191. buffer.WriteString(w.ReturnCode)
  192. buffer.WriteString("]]></return_code>")
  193. buffer.WriteString("<return_msg><![CDATA[")
  194. buffer.WriteString(w.ReturnMsg)
  195. buffer.WriteString("]]></return_msg></xml>")
  196. xmlStr = buffer.String()
  197. return
  198. }
  199. // JSAPI支付,统一下单获取支付参数后,再次计算出小程序用的paySign
  200. // appId:APPID
  201. // nonceStr:随即字符串
  202. // prepayId:统一下单成功后得到的值
  203. // signType:签名类型
  204. // timeStamp:时间
  205. // ApiKey:API秘钥值
  206. // 微信小程序支付API:https://developers.weixin.qq.com/miniprogram/dev/api/open-api/payment/wx.requestPayment.html
  207. func GetMiniPaySign(appId, nonceStr, prepayId, signType, timeStamp, apiKey string) (paySign string) {
  208. var (
  209. buffer strings.Builder
  210. h hash.Hash
  211. )
  212. buffer.WriteString("appId=")
  213. buffer.WriteString(appId)
  214. buffer.WriteString("&nonceStr=")
  215. buffer.WriteString(nonceStr)
  216. buffer.WriteString("&package=")
  217. buffer.WriteString(prepayId)
  218. buffer.WriteString("&signType=")
  219. buffer.WriteString(signType)
  220. buffer.WriteString("&timeStamp=")
  221. buffer.WriteString(timeStamp)
  222. buffer.WriteString("&key=")
  223. buffer.WriteString(apiKey)
  224. if signType == SignType_HMAC_SHA256 {
  225. h = hmac.New(sha256.New, []byte(apiKey))
  226. } else {
  227. h = md5.New()
  228. }
  229. h.Write([]byte(buffer.String()))
  230. return strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
  231. }
  232. // 微信内H5支付,统一下单获取支付参数后,再次计算出微信内H5支付需要用的paySign
  233. // appId:APPID
  234. // nonceStr:随即字符串
  235. // packages:统一下单成功后拼接得到的值
  236. // signType:签名类型
  237. // timeStamp:时间
  238. // ApiKey:API秘钥值
  239. // 微信内H5支付官方文档:https://pay.weixin.qq.com/wiki/doc/api/external/jsapi.php?chapter=7_7&index=6
  240. func GetH5PaySign(appId, nonceStr, packages, signType, timeStamp, apiKey string) (paySign string) {
  241. var (
  242. buffer strings.Builder
  243. h hash.Hash
  244. )
  245. buffer.WriteString("appId=")
  246. buffer.WriteString(appId)
  247. buffer.WriteString("&nonceStr=")
  248. buffer.WriteString(nonceStr)
  249. buffer.WriteString("&package=")
  250. buffer.WriteString(packages)
  251. buffer.WriteString("&signType=")
  252. buffer.WriteString(signType)
  253. buffer.WriteString("&timeStamp=")
  254. buffer.WriteString(timeStamp)
  255. buffer.WriteString("&key=")
  256. buffer.WriteString(apiKey)
  257. if signType == SignType_HMAC_SHA256 {
  258. h = hmac.New(sha256.New, []byte(apiKey))
  259. } else {
  260. h = md5.New()
  261. }
  262. h.Write([]byte(buffer.String()))
  263. paySign = strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
  264. return
  265. }
  266. // APP支付,统一下单获取支付参数后,再次计算APP支付所需要的的sign
  267. // appId:APPID
  268. // partnerid:partnerid
  269. // nonceStr:随即字符串
  270. // prepayId:统一下单成功后得到的值
  271. // signType:此处签名方式,务必与统一下单时用的签名方式一致
  272. // timeStamp:时间
  273. // ApiKey:API秘钥值
  274. // APP支付官方文档:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_12
  275. func GetAppPaySign(appid, partnerid, noncestr, prepayid, signType, timestamp, apiKey string) (paySign string) {
  276. var (
  277. buffer strings.Builder
  278. h hash.Hash
  279. )
  280. buffer.WriteString("appid=")
  281. buffer.WriteString(appid)
  282. buffer.WriteString("&noncestr=")
  283. buffer.WriteString(noncestr)
  284. buffer.WriteString("&package=Sign=WXPay")
  285. buffer.WriteString("&partnerid=")
  286. buffer.WriteString(partnerid)
  287. buffer.WriteString("&prepayid=")
  288. buffer.WriteString(prepayid)
  289. buffer.WriteString("&timestamp=")
  290. buffer.WriteString(timestamp)
  291. buffer.WriteString("&key=")
  292. buffer.WriteString(apiKey)
  293. if signType == SignType_HMAC_SHA256 {
  294. h = hmac.New(sha256.New, []byte(apiKey))
  295. } else {
  296. h = md5.New()
  297. }
  298. h.Write([]byte(buffer.String()))
  299. paySign = strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
  300. return
  301. }
  302. // 解密开放数据到结构体
  303. // encryptedData:包括敏感数据在内的完整用户信息的加密数据,小程序获取到
  304. // iv:加密算法的初始向量,小程序获取到
  305. // sessionKey:会话密钥,通过 gopay.Code2Session() 方法获取到
  306. // beanPtr:需要解析到的结构体指针,操作完后,声明的结构体会被赋值
  307. // 文档:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/signature.html
  308. func DecryptWeChatOpenDataToStruct(encryptedData, iv, sessionKey string, beanPtr interface{}) (err error) {
  309. var (
  310. cipherText, aesKey, ivKey, plainText []byte
  311. block cipher.Block
  312. blockMode cipher.BlockMode
  313. )
  314. beanValue := reflect.ValueOf(beanPtr)
  315. if beanValue.Kind() != reflect.Ptr {
  316. return errors.New("传入beanPtr类型必须是以指针形式")
  317. }
  318. if beanValue.Elem().Kind() != reflect.Struct {
  319. return errors.New("传入interface{}必须是结构体")
  320. }
  321. cipherText, _ = base64.StdEncoding.DecodeString(encryptedData)
  322. aesKey, _ = base64.StdEncoding.DecodeString(sessionKey)
  323. ivKey, _ = base64.StdEncoding.DecodeString(iv)
  324. if len(cipherText)%len(aesKey) != 0 {
  325. return errors.New("encryptedData is error")
  326. }
  327. if block, err = aes.NewCipher(aesKey); err != nil {
  328. return fmt.Errorf("aes.NewCipher:%s", err.Error())
  329. }
  330. blockMode = cipher.NewCBCDecrypter(block, ivKey)
  331. plainText = make([]byte, len(cipherText))
  332. blockMode.CryptBlocks(plainText, cipherText)
  333. if len(plainText) > 0 {
  334. plainText = PKCS7UnPadding(plainText)
  335. }
  336. if err = json.Unmarshal(plainText, beanPtr); err != nil {
  337. return fmt.Errorf("json.Unmarshal:%s", err.Error())
  338. }
  339. return
  340. }
  341. // 解密开放数据到 BodyMap
  342. // encryptedData:包括敏感数据在内的完整用户信息的加密数据,小程序获取到
  343. // iv:加密算法的初始向量,小程序获取到
  344. // sessionKey:会话密钥,通过 gopay.Code2Session() 方法获取到
  345. // 文档:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/signature.html
  346. func DecryptWeChatOpenDataToBodyMap(encryptedData, iv, sessionKey string) (bm BodyMap, err error) {
  347. var (
  348. cipherText, aesKey, ivKey, plainText []byte
  349. block cipher.Block
  350. blockMode cipher.BlockMode
  351. )
  352. cipherText, _ = base64.StdEncoding.DecodeString(encryptedData)
  353. aesKey, _ = base64.StdEncoding.DecodeString(sessionKey)
  354. ivKey, _ = base64.StdEncoding.DecodeString(iv)
  355. if len(cipherText)%len(aesKey) != 0 {
  356. return nil, errors.New("encryptedData is error")
  357. }
  358. if block, err = aes.NewCipher(aesKey); err != nil {
  359. return nil, fmt.Errorf("aes.NewCipher:%s", err.Error())
  360. } else {
  361. blockMode = cipher.NewCBCDecrypter(block, ivKey)
  362. plainText = make([]byte, len(cipherText))
  363. blockMode.CryptBlocks(plainText, cipherText)
  364. if len(plainText) > 0 {
  365. plainText = PKCS7UnPadding(plainText)
  366. }
  367. bm = make(BodyMap)
  368. if err = json.Unmarshal(plainText, &bm); err != nil {
  369. return nil, fmt.Errorf("json.Unmarshal:%s", err.Error())
  370. }
  371. return
  372. }
  373. }
  374. // App应用微信第三方登录,code换取access_token
  375. // appId:应用唯一标识,在微信开放平台提交应用审核通过后获得
  376. // appSecret:应用密钥AppSecret,在微信开放平台提交应用审核通过后获得
  377. // code:App用户换取access_token的code
  378. func GetAppWeChatLoginAccessToken(appId, appSecret, code string) (accessToken *AppWeChatLoginAccessToken, err error) {
  379. accessToken = new(AppWeChatLoginAccessToken)
  380. url := "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appId + "&secret=" + appSecret + "&code=" + code + "&grant_type=authorization_code"
  381. if _, _, errs := HttpAgent().Get(url).EndStruct(accessToken); len(errs) > 0 {
  382. return nil, errs[0]
  383. }
  384. return
  385. }
  386. // 刷新App应用微信第三方登录后,获取的 access_token
  387. // appId:应用唯一标识,在微信开放平台提交应用审核通过后获得
  388. // appSecret:应用密钥AppSecret,在微信开放平台提交应用审核通过后获得
  389. // code:App用户换取access_token的code
  390. func RefreshAppWeChatLoginAccessToken(appId, refreshToken string) (accessToken *RefreshAppWeChatLoginAccessTokenRsp, err error) {
  391. accessToken = new(RefreshAppWeChatLoginAccessTokenRsp)
  392. url := "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + appId + "&grant_type=refresh_token&refresh_token=" + refreshToken
  393. if _, _, errs := HttpAgent().Get(url).EndStruct(accessToken); len(errs) > 0 {
  394. return nil, errs[0]
  395. }
  396. return
  397. }
  398. // 获取微信小程序用户的OpenId、SessionKey、UnionId
  399. // appId:APPID
  400. // appSecret:AppSecret
  401. // wxCode:小程序调用wx.login 获取的code
  402. // 文档:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
  403. func Code2Session(appId, appSecret, wxCode string) (sessionRsp *Code2SessionRsp, err error) {
  404. sessionRsp = new(Code2SessionRsp)
  405. url := "https://api.weixin.qq.com/sns/jscode2session?appid=" + appId + "&secret=" + appSecret + "&js_code=" + wxCode + "&grant_type=authorization_code"
  406. if _, _, errs := HttpAgent().Get(url).EndStruct(sessionRsp); len(errs) > 0 {
  407. return nil, errs[0]
  408. }
  409. return
  410. }
  411. // 获取微信小程序全局唯一后台接口调用凭据(AccessToken:157字符)
  412. // appId:APPID
  413. // appSecret:AppSecret
  414. // 获取access_token文档:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
  415. func GetWeChatAppletAccessToken(appId, appSecret string) (accessToken *AccessToken, err error) {
  416. accessToken = new(AccessToken)
  417. url := "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret
  418. if _, _, errs := HttpAgent().Get(url).EndStruct(accessToken); len(errs) > 0 {
  419. return nil, errs[0]
  420. }
  421. return
  422. }
  423. // 授权码查询openid(AccessToken:157字符)
  424. // appId:APPID
  425. // mchId:商户号
  426. // ApiKey:apiKey
  427. // authCode:用户授权码
  428. // nonceStr:随即字符串
  429. // 文档:https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_13&index=9
  430. func GetOpenIdByAuthCode(appId, mchId, apiKey, authCode, nonceStr string) (openIdRsp *OpenIdByAuthCodeRsp, err error) {
  431. var (
  432. url string
  433. bm BodyMap
  434. bs []byte
  435. errs []error
  436. )
  437. url = "https://api.mch.weixin.qq.com/tools/authcodetoopenid"
  438. bm = make(BodyMap)
  439. bm.Set("appid", appId)
  440. bm.Set("mch_id", mchId)
  441. bm.Set("auth_code", authCode)
  442. bm.Set("nonce_str", nonceStr)
  443. bm.Set("sign", getWeChatReleaseSign(apiKey, SignType_MD5, bm))
  444. if _, bs, errs = HttpAgent().Post(url).Type("xml").SendString(generateXml(bm)).EndBytes(); len(errs) > 0 {
  445. return nil, errs[0]
  446. }
  447. openIdRsp = new(OpenIdByAuthCodeRsp)
  448. if err = xml.Unmarshal(bs, openIdRsp); err != nil {
  449. return nil, fmt.Errorf("xml.Unmarshal:%s", err.Error())
  450. }
  451. return
  452. }
  453. // 微信小程序用户支付完成后,获取该用户的 UnionId,无需用户授权。
  454. // accessToken:接口调用凭据
  455. // openId:用户的OpenID
  456. // transactionId:微信支付订单号
  457. // 文档:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/user-info/auth.getPaidUnionId.html
  458. func GetWeChatAppletPaidUnionId(accessToken, openId, transactionId string) (unionId *PaidUnionId, err error) {
  459. unionId = new(PaidUnionId)
  460. url := "https://api.weixin.qq.com/wxa/getpaidunionid?access_token=" + accessToken + "&openid=" + openId + "&transaction_id=" + transactionId
  461. if _, _, errs := HttpAgent().Get(url).EndStruct(unionId); len(errs) > 0 {
  462. return nil, errs[0]
  463. }
  464. return
  465. }
  466. // 获取用户基本信息(UnionID机制)
  467. // accessToken:接口调用凭据
  468. // openId:用户的OpenID
  469. // lang:默认为 zh_CN ,可选填 zh_CN 简体,zh_TW 繁体,en 英语
  470. // 获取用户基本信息(UnionID机制)文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140839
  471. func GetWeChatUserInfo(accessToken, openId string, lang ...string) (userInfo *WeChatUserInfo, err error) {
  472. userInfo = new(WeChatUserInfo)
  473. var url string
  474. if len(lang) > 0 {
  475. url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + accessToken + "&openid=" + openId + "&lang=" + lang[0]
  476. } else {
  477. url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + accessToken + "&openid=" + openId + "&lang=zh_CN"
  478. }
  479. if _, _, errs := HttpAgent().Get(url).EndStruct(userInfo); len(errs) > 0 {
  480. return nil, errs[0]
  481. }
  482. return
  483. }