wechat_service_api.go 18 KB

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