| Conditions | 8 |
| Total Lines | 53 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | package backcard |
||
| 25 | func BankCardInfo(cardNO string) (error, *BankCardInfoStruct) { |
||
| 26 | form := url.Values{} |
||
| 27 | form.Add("_input_charset", "utf-8") |
||
| 28 | form.Add("cardNo", cardNO) |
||
| 29 | form.Add("cardBinCheck", "true") |
||
| 30 | req, err := http.NewRequest(http.MethodPost, fmt.Sprintf(getBankInfo, form.Encode()), nil) |
||
| 31 | if err != nil { |
||
| 32 | return err, nil |
||
| 33 | } |
||
| 34 | resp, err := commonHttpClient5s.Do(req) |
||
| 35 | if err != nil { |
||
| 36 | return err, nil |
||
| 37 | } |
||
| 38 | defer resp.Body.Close() |
||
| 39 | body := new(bytes.Buffer) |
||
| 40 | if length, err := io.Copy(body, resp.Body); err != nil && length > 0 { |
||
| 41 | return err, nil |
||
| 42 | } |
||
| 43 | type AliPayBankInfo struct { |
||
| 44 | Messages []struct { |
||
| 45 | ErrorCodes string `json:"errorCodes"` |
||
| 46 | Name string `json:"name"` |
||
| 47 | } `json:"messages"` |
||
| 48 | CardType string `json:"cardType"` |
||
| 49 | Bank string `json:"bank"` |
||
| 50 | Key string `json:"key"` |
||
| 51 | Validated bool `json:"validated"` |
||
| 52 | Stat string `json:"stat"` |
||
| 53 | } |
||
| 54 | result := new(AliPayBankInfo) |
||
| 55 | if err := json.Unmarshal(body.Bytes(), result); err != nil { |
||
| 56 | return err, nil |
||
| 57 | } |
||
| 58 | if !result.Validated { |
||
| 59 | err := errors.New(result.Messages[0].ErrorCodes) |
||
| 60 | return err, &BankCardInfoStruct{ |
||
| 61 | Validated: false, |
||
| 62 | ValidatedMsg: "cardNO error", |
||
| 63 | } |
||
| 64 | } |
||
| 65 | bankName := "" |
||
| 66 | if bank, ok := bankShortNameMap[result.Bank]; ok { |
||
| 67 | bankName = bank |
||
| 68 | } |
||
| 69 | return nil, &BankCardInfoStruct{ |
||
| 70 | Validated: true, |
||
| 71 | BankENShortName: result.Bank, |
||
| 72 | BankENFullName: result.Bank, |
||
| 73 | BankCNShortName: bankName, |
||
| 74 | BankCNFullName: bankName, |
||
| 75 | BankImg: fmt.Sprintf(bankImageUrl, result.Bank), |
||
| 76 | CardType: result.CardType, |
||
| 77 | CardTypeName: bankCardTypeMap[result.CardType], |
||
| 78 | } |
||
| 80 |