初始
This commit is contained in:
84
api/video_hat/video_token.go
Normal file
84
api/video_hat/video_token.go
Normal file
@ -0,0 +1,84 @@
|
||||
package video_hat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/tiger1103/gfast/v3/api/constant"
|
||||
)
|
||||
|
||||
type RefreshTokenReq struct {
|
||||
g.Meta `path:"/video/refreshToken" method:"post" tags:"视频安全帽相关" summary:"刷新Token(不需要前端调用)"`
|
||||
}
|
||||
|
||||
type RefreshTokenRes struct{}
|
||||
|
||||
func (v VideoHat) RefreshToken(ctx context.Context, req *RefreshTokenReq) (res *RefreshTokenRes, err error) {
|
||||
res = new(RefreshTokenRes)
|
||||
InitToken()
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// 用于接收获取 Token 的响应
|
||||
type TokenRes struct {
|
||||
Status bool `json:"status"`
|
||||
Token string `json:"token"`
|
||||
SessionId string `json:"session_id"`
|
||||
MsgCode string `json:"msg_code"`
|
||||
}
|
||||
|
||||
type PKeyResponse struct {
|
||||
Status bool `json:"status"`
|
||||
Data string `json:"data"`
|
||||
Msg string `json:"msg"`
|
||||
MsgCode string `json:"msg_code"`
|
||||
}
|
||||
|
||||
type TokenResponse struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
// 发送 POST 请求
|
||||
func fetchPostForm(url string, data url.Values) ([]byte, error) {
|
||||
resp, err := http.PostForm(url, data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return io.ReadAll(resp.Body)
|
||||
}
|
||||
|
||||
// InitToken 项目初始化启动获取 Token【以及刷新 Token】
|
||||
func InitToken() error {
|
||||
data := url.Values{}
|
||||
data.Set("user_name", constant.Username)
|
||||
data.Set("pwd", constant.Password)
|
||||
|
||||
respBody, err := fetchPostForm("https://caps.runde.pro/api/index.php?ctl=tool&act=get_pkey", data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var pkeyRes PKeyResponse
|
||||
if err := json.Unmarshal(respBody, &pkeyRes); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data.Set("pkey", pkeyRes.Data)
|
||||
respBody, err = fetchPostForm("https://caps.runde.pro/api/index.php?ctl=tool&act=get_token", data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var tokenRes TokenResponse
|
||||
if err := json.Unmarshal(respBody, &tokenRes); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
constant.Token = tokenRes.Token
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user