使用Go调用第三方接口

package api

import (
    "encoding/json"
    "fmt"
    "github.com/gin-gonic/gin"
    "go_im/pkg/config"
    "go_im/pkg/redis"
    "go_im/pkg/response"
    "io/ioutil"
    "net/http"
    "net/url"
    "time"
)

type SmApiController struct {}

var username = config.GetString("app.sm_name")
var password = config.GetString("app.sm_password")

type ResponseData struct {
    Success bool     `json:"success"`
    Code string `json:"code"`
    Message string `json:"message"`
    Data Data `json:"data"`
    RequestId string `json:"RequestId"`
}
type Data struct {
    Token string `json:"token"`
}

func (*SmApiController) GetApiToken(c *gin.Context){

    stringCmd := redis.RedisDB.Get("sm_token")
    if len(stringCmd.Val()) !=0 {
        resp := new(ResponseData)
        resp.Code="success"
        resp.Data.Token=stringCmd.Val()
        resp.Success=true
        c.JSON(200,resp)
        return
    }
    data := url.Values{"username":{username},"password":{password}}
    j,err :=http.PostForm("https://sm.ms/api/v2/token",data)
    if err !=nil {
        fmt.Println(err)
    }
    defer j.Body.Close()
    bodyC, _ := ioutil.ReadAll(j.Body)
    resp := new(ResponseData)
    json.Unmarshal(bodyC,resp)
    if resp.Success {
        response.FailResponse(500,resp.Message)
        return
    }
    redis.RedisDB.Set("sm_token",resp.Data.Token,time.Hour*1)
    c.JSON(200,resp)
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
不成大牛,不改個簽
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!