美文网首页
go使用阿里云地址标准化服务实现获取地址的省市区域信息

go使用阿里云地址标准化服务实现获取地址的省市区域信息

作者: EasyNetCN | 来源:发表于2022-11-01 18:57 被阅读0次
    package object
    
    import (
        "encoding/json"
    
        "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
        "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
    )
    
    type GetAddressDivisionCodeResponse struct {
        RequestId string `json:"RequestId"`
        Data      string `json:"Data"`
    }
    
    type GetAddressDivisionCodeResponseData struct {
        Status       string       `json:"status"`
        DivisionInfo DivisionInfo `json:"division_info"`
        Timesed      TimeUsed     `json:"time_used"`
    }
    
    type DivisionInfo struct {
        DivCode      string `json:"divcode"`
        DivisionName string `json:"division_name"`
    }
    
    type TimeUsed struct {
        DivisionInfo TimeUsedRt `json:"division_info"`
        Start        float64    `json:"start"`
    }
    
    type TimeUsedRt struct {
        DivisionInfo float64 `json:"division_info"`
        Complete     float64 `json:"Complete"`
        Structure    float64 `json:"structure"`
    }
    
    var aliyunClient *sdk.Client
    
    func GetAddressDivisionCode(address string) (*GetAddressDivisionCodeResponseData, error) {
        request := requests.NewCommonRequest()
    
        request.Method = "POST"
        request.Product = "address-purification"
        request.Domain = "address-purification.cn-hangzhou.aliyuncs.com"
        request.Version = "2019-11-18"
        request.ApiName = "GetAddressDivisionCode"
    
        request.QueryParams["AppKey"] = "Axbsaftcmm8r"
        request.QueryParams["ServiceCode"] = "addrp"
        request.QueryParams["Text"] = address
    
        request.TransToAcsRequest()
    
        if response, err := aliyunClient.ProcessCommonRequest(request); err != nil {
            return nil, err
        } else {
            getAddressDivisionCodeResponse := &GetAddressDivisionCodeResponse{}
    
            if err := json.Unmarshal(response.GetHttpContentBytes(), &getAddressDivisionCodeResponse); err != nil {
                return nil, err
            }
    
            responseData := &GetAddressDivisionCodeResponseData{}
    
            if err := json.Unmarshal([]byte(getAddressDivisionCodeResponse.Data), &responseData); err != nil {
                return nil, err
            }
    
            return responseData, nil
        }
    }
    
    func init() {
        if client, err := sdk.NewClientWithAccessKey("cn-hangzhou", config.GetConfig().Aliyun.AccessKeyId, config.GetConfig().Aliyun.AccessKeySecret); err != nil {
            util.Logger.Error("aliyunClient", util.Any("error", err))
        } else {
            aliyunClient = client
        }
    }
    
    

    相关文章

      网友评论

          本文标题:go使用阿里云地址标准化服务实现获取地址的省市区域信息

          本文链接:https://www.haomeiwen.com/subject/qhnjtdtx.html