(code string)
| 21 | } |
| 22 | |
| 23 | func getWeChatIdByCode(code string) (string, error) { |
| 24 | if code == "" { |
| 25 | return "", errors.New("无效的参数") |
| 26 | } |
| 27 | req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/wechat/user?code=%s", common.WeChatServerAddress, code), nil) |
| 28 | if err != nil { |
| 29 | return "", err |
| 30 | } |
| 31 | req.Header.Set("Authorization", common.WeChatServerToken) |
| 32 | client := http.Client{ |
| 33 | Timeout: 5 * time.Second, |
| 34 | } |
| 35 | httpResponse, err := client.Do(req) |
| 36 | if err != nil { |
| 37 | return "", err |
| 38 | } |
| 39 | defer httpResponse.Body.Close() |
| 40 | var res wechatLoginResponse |
| 41 | err = json.NewDecoder(httpResponse.Body).Decode(&res) |
| 42 | if err != nil { |
| 43 | return "", err |
| 44 | } |
| 45 | if !res.Success { |
| 46 | return "", errors.New(res.Message) |
| 47 | } |
| 48 | if res.Data == "" { |
| 49 | return "", errors.New("验证码错误或已过期") |
| 50 | } |
| 51 | return res.Data, nil |
| 52 | } |
| 53 | |
| 54 | func WeChatAuth(c *gin.Context) { |
| 55 | if !common.WeChatAuthEnabled { |
no outgoing calls
no test coverage detected