You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.0 KiB
86 lines
2.0 KiB
package juhe
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/url"
|
|
"recook/configs"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
actionCreditCode = "http://apis.juhe.cn/creditcode/query"
|
|
actionCreditCodeKey = configs.Config_Juhe_Creditcode_Key
|
|
)
|
|
|
|
type juhe struct {
|
|
}
|
|
|
|
var (
|
|
Juhe = &juhe{}
|
|
client = &http.Client{}
|
|
)
|
|
|
|
type CreditCode struct {
|
|
Name string `json:"name"` // 企业名称
|
|
Creditcode string `json:"creditcode"` // 社会统一信用代码(纳税人识别号)
|
|
Econkind string `json:"econkind"` // 企业类型
|
|
Status string `json:"status"` // 企业状态
|
|
Address string `json:"address"` // 地址
|
|
Tel string `json:"tel"` // 联系电话
|
|
Bank string `json:"bank"` // 开户行
|
|
Bankaccount string `json:"bankaccount"` // 开户行账号
|
|
Orderid string `json:"orderid"` // 单号
|
|
}
|
|
type replyCreditCode struct {
|
|
Reason string `json:"reason"`
|
|
ErrorCode string `json:"errorCode"`
|
|
Result CreditCode `json:"result"`
|
|
}
|
|
|
|
// @Style 企业开票信息查询
|
|
func (j *juhe) CreditCode(keyword string) (res CreditCode, err error) {
|
|
result := replyCreditCode{}
|
|
resByte, err := j.exec(actionCreditCode, url.Values{
|
|
"key": []string{actionCreditCodeKey},
|
|
"keyword": []string{keyword},
|
|
}.Encode())
|
|
if err != nil {
|
|
return
|
|
}
|
|
if err = json.Unmarshal(resByte, &result); err != nil {
|
|
return
|
|
}
|
|
res = result.Result
|
|
return
|
|
}
|
|
|
|
// @Style 执行请求
|
|
func (j *juhe) exec(action, data string) ([]byte, error) {
|
|
return request("GET", action+"?"+data, "")
|
|
}
|
|
|
|
// @Style 请求
|
|
func request(method, url, data string, headers ...map[string]string) ([]byte, error) {
|
|
reqest, err := http.NewRequest(method, url, strings.NewReader(data))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(headers) > 0 {
|
|
for key, value := range headers[0] {
|
|
reqest.Header.Add(key, value)
|
|
}
|
|
}
|
|
response, err := client.Do(reqest)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer response.Body.Close()
|
|
result, err := ioutil.ReadAll(response.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return result, nil
|
|
}
|