ai-week/udesk/function.go

189 lines
4.8 KiB
Go
Raw Normal View History

2024-05-01 20:03:20 +08:00
package udesk
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/google/uuid"
"github.com/tidwall/gjson"
"io"
"log"
"net/http"
"strconv"
"strings"
"time"
)
//新建请求体对象
func newReqUdeskToken() RequestUdeskBody {
return RequestUdeskBody{
Email: Email,
Password: Password,
}
}
// 新建返回体对象
func newRespUdeskBody() UdeskToken {
return UdeskToken{
Code: "",
Open_api_auth_token: "",
}
}
//获取Unix时间戳
func GetTimeStamp() string {
return strconv.FormatInt(time.Now().Unix(), 10)
}
//获取nonce
func GetNonce() string {
randomUUID := uuid.New()
nonce := randomUUID.String()
return nonce
}
// sha256转换函数
func calculateSHA256(input string) string {
// 将字符串转换为字节数组
inputBytes := []byte(input)
// 创建SHA-256哈希对象
hasher := sha256.New()
// 将字节数组写入哈希对象
hasher.Write(inputBytes)
// 计算哈希值并返回
hashInBytes := hasher.Sum(nil)
hashString := hex.EncodeToString(hashInBytes)
return hashString
}
// 获取鉴权token对象
func GetUdeskAuthToken() UdeskToken {
reqBody := newReqUdeskToken()
jsonData, err := json.Marshal(reqBody)
if err != nil {
fmt.Printf("获取udesk管理员token过程中转换请求体json失败错误是%v", err)
}
payload := bytes.NewBufferString(string(jsonData))
resp, err := http.Post(Auth_token_url, "application/json", payload)
if err != nil {
fmt.Printf("获取udesk管理员token过程中请求获取token接口失败错误是%v", err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
return
}
}(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("获取udesk管理员token过程中获取token接口响应失败错误是%v", err)
}
json.Unmarshal(body, &u)
return u
}
//把udeskid转换为cloudid
func Id2CloudId(id string) string {
detailUrl := Geturlstring("https://servicecenter-alauda.udesk.cn/open_api_v1/tickets/detail?") + "&id=" + id
resp, err := http.Get(detailUrl)
if err != nil {
// 处理错误
log.Printf("获取工单过滤器下的cloudid失败发送请求过程错误是%v", err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
// 处理错误
log.Printf("获取工单过滤器下的cloudid失败读取返回体过程错误是%v", err)
}
jsonData := string(body)
cloudId := gjson.Get(jsonData, "ticket.custom_fields.TextField_219254").String()
return cloudId
}
//* 获取工单过滤器下的符合条件的工单id *
// 获取工单总共的页数
func GetUdeskTicketId() (*int64, error) {
filterUrl := Geturlstring("https://servicecenter-alauda.udesk.cn/open_api_v1/tickets/tickets_in_filter?filter_id=16523464&")
resp, err := http.Get(filterUrl)
if err != nil {
// 处理错误
log.Printf("获取工单过滤器下的工单信息失败,发送请求过程,错误是:%v", err)
return nil, err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
// 处理错误
log.Printf("获取工单过滤器下的工单信息失败,读取返回体过程,错误是:%v", err)
return nil, err
}
jsonData := string(body)
//fmt.Println(jsonData)
totalPages := gjson.Get(jsonData, "meta.total_pages").Int()
return &totalPages, nil
}
// 根据上面获取的页数,分别请求接口取到工单id切片
func LoopGetTicketId(totalPages *int64) ([]string, error) {
var idSlice = make([]string, 0)
for i := int64(1); i <= *totalPages; i++ {
getIdUrl := Geturlstring("https://servicecenter-alauda.udesk.cn/open_api_v1/tickets/tickets_in_filter?filter_id=16523464&") + "&page=" + strconv.FormatInt(i, 10)
resp, err := http.Get(getIdUrl)
if err != nil {
// 处理错误
log.Printf("获取工单过滤器下的工单id失败发送请求过程错误是%v", err)
return nil, err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Printf("获取工单过滤器下的工单id失败关闭返回体错误是%v", err)
}
}(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
// 处理错误
log.Printf("获取工单过滤器下的工单id失败读取返回体过程错误是%v", err)
return nil, err
}
jsonData := string(body)
ticketIdstr := gjson.Get(jsonData, "contents.#.ticket.id").String()
sliceStr := strings.Trim(ticketIdstr, "[]")
numbers := strings.Split(sliceStr, ",")
log.Printf("处理id切片中...第%v次处理...\n", i)
idSlice = append(idSlice, numbers...)
log.Printf("%T,%v", idSlice, idSlice)
}
log.Println("处理完成~owo~")
return idSlice, nil
}