🎨 优化接口,添加前置检查并更新返回类型

This commit is contained in:
李寻欢 2025-04-18 10:08:40 +08:00
parent e106d17676
commit 5407be63c0
10 changed files with 166 additions and 7 deletions

14
base/check.go Normal file
View File

@ -0,0 +1,14 @@
package base
import "errors"
// PreCheck
// @description: 调用前检查
// @param wxId
// @return canUse
func PreCheck(wxId string) (err error) {
if wxId == "" {
return errors.New("wxId不能为空")
}
return nil
}

View File

@ -41,10 +41,6 @@ func NewClient(wxId, baseUrl string, debug bool) (cli *Client, err error) {
err = errors.New("baseUrl 不得为空,格式为: http://10.0.0.11:9001")
return
}
if wxId == "" {
err = errors.New("wxId 不得为空")
return
}
cli = &Client{}
cli.httpClient = resty.New().

View File

@ -28,6 +28,9 @@ func (s service) SendFriendRequest(scene int, v1, v2, verifyContent string) (use
// @param v2
// @return err
func (s service) AcceptFriend(scene int, v1, v2 string) (err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[base.EmptyResponse]
_, err = s.client.R().
SetResult(&result).
@ -48,6 +51,9 @@ func (s service) AcceptFriend(scene int, v1, v2 string) (err error) {
// @return contactList
// @return err
func (s service) GetContact(wxIds []string) (contactList []ContactListItem, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[GetContactResponse]
_, err = s.client.R().
SetResult(&result).
@ -69,6 +75,9 @@ func (s service) GetContact(wxIds []string) (contactList []ContactListItem, err
// @return contactList
// @return err
func (s service) GetContractDetail(wxIds []string) (contactList []ContactListItem, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
if len(wxIds) > 20 {
err = errors.New("一次最多查询20个联系人")
return
@ -96,6 +105,9 @@ func (s service) GetContractDetail(wxIds []string) (contactList []ContactListIte
// @return wxIds
// @return err
func (s service) GetContractList(clearSystemAccount bool) (wxIds []string, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[GetContractListResponse]
_, err = s.client.R().
SetResult(&result).

View File

@ -12,6 +12,9 @@ import (
// @param inviteWxId
// @return err
func (s service) AddChatroomMember(chatroom string, inviteWxId string) (err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[base.EmptyResponse]
_, err = s.client.R().
SetResult(&result).
@ -31,6 +34,9 @@ func (s service) AddChatroomMember(chatroom string, inviteWxId string) (err erro
// @param inviteWxIds
// @return err
func (s service) InviteChatroomMember(chatroom string, inviteWxIds []string) (err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[base.EmptyResponse]
_, err = s.client.R().
SetResult(&result).
@ -50,6 +56,9 @@ func (s service) InviteChatroomMember(chatroom string, inviteWxIds []string) (er
// @return info
// @return err
func (s service) GetChatroomInfo(chatroom string) (info GetChatroomInfoResponse, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[GetChatroomInfoResponse]
_, err = s.client.R().
SetResult(&result).
@ -71,6 +80,9 @@ func (s service) GetChatroomInfo(chatroom string) (info GetChatroomInfoResponse,
// @return info
// @return err
func (s service) GetChatroomInfoNoAnnounce(chatroom []string) (info []ChatroomInfoItem, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[GetChatroomInfoNoAnnounce]
_, err = s.client.R().
SetResult(&result).
@ -92,6 +104,9 @@ func (s service) GetChatroomInfoNoAnnounce(chatroom []string) (info []ChatroomIn
// @return members
// @return err
func (s service) GetChatroomMemberDetail(chatroom string) (members []ChatRoomMemberItem, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[GetChatroomMemberDetailResponse]
_, err = s.client.R().
SetResult(&result).
@ -114,6 +129,9 @@ func (s service) GetChatroomMemberDetail(chatroom string) (members []ChatRoomMem
// @return notify
// @return err
func (s service) GetChatroomQRCode(chatroom string) (imgBase64Str, notify string, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[GetChatroomQRCodeResponse]
_, err = s.client.R().
SetResult(&result).

View File

@ -11,6 +11,10 @@ import "gitee.ltd/lxh/xybot/base"
// @return resp
// @return err
func (s service) GetHongBaoDetail(xml, encryptKey, encryptUserinfo string) (resp any, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[any]
_, err = s.client.R().
SetResult(&result).

View File

@ -34,6 +34,9 @@ func (s service) GetQRCode(deviceId, deviceName string) (resp GetQRCodeResponse,
// @return resp
// @return err
func (s service) AwakenLogin() (resp AwakenLoginResponse, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[AwakenLoginResponse]
_, err = s.client.R().SetResult(&result).SetBody(map[string]any{"Wxid": s.wxId}).Post("/AwakenLogin")
if err = result.CheckError(err); err != nil {
@ -68,6 +71,9 @@ func (s service) CheckUuid(uuid string) (resp CheckUuidResponse, err error) {
// @receiver s
// @return err
func (s service) AutoHeartbeatStart() (err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[base.EmptyResponse]
_, err = s.client.R().SetResult(&result).SetBody(map[string]any{"Wxid": s.wxId}).Post("/AutoHeartbeatStart")
err = result.CheckError(err)
@ -80,6 +86,9 @@ func (s service) AutoHeartbeatStart() (err error) {
// @return running
// @return err
func (s service) AutoHeartbeatStatus() (running bool, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result AutoHeartbeatStatusResponse
_, err = s.client.R().SetResult(&result).SetBody(map[string]any{"Wxid": s.wxId}).Post("/AutoHeartbeatStatus")
if err == nil {
@ -97,6 +106,9 @@ func (s service) AutoHeartbeatStatus() (running bool, err error) {
// @receiver s
// @return err
func (s service) AutoHeartbeatStop() (err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[base.EmptyResponse]
_, err = s.client.R().SetResult(&result).SetBody(map[string]any{"Wxid": s.wxId}).Post("/AutoHeartbeatStop")
err = result.CheckError(err)
@ -108,6 +120,9 @@ func (s service) AutoHeartbeatStop() (err error) {
// @receiver s
// @return err
func (s service) Heartbeat() (err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[base.EmptyResponse]
_, err = s.client.R().SetResult(&result).SetBody(map[string]any{"Wxid": s.wxId}).Post("/Heartbeat")
err = result.CheckError(err)
@ -120,6 +135,9 @@ func (s service) Heartbeat() (err error) {
// @return resp
// @return err
func (s service) GetCachedInfo() (resp GetCachedInfoResponse, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[GetCachedInfoResponse]
_, err = s.client.R().SetResult(&result).SetBody(map[string]any{"Wxid": s.wxId}).Post("/GetCachedInfo")
if err = result.CheckError(err); err != nil {
@ -134,6 +152,9 @@ func (s service) GetCachedInfo() (resp GetCachedInfoResponse, err error) {
// @receiver s
// @return err
func (s service) Logout() (err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[base.EmptyResponse]
_, err = s.client.R().SetResult(&result).SetBody(map[string]any{"Wxid": s.wxId}).Post("/Logout")
err = result.CheckError(err)

View File

@ -15,6 +15,10 @@ import (
// @param newMsgId int 返回消息的NewMsgId字段
// @return err
func (s service) RevokeMsg(toWxId string, clientMsgId, createTime, newMsgId int) (err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[base.EmptyResponse]
_, err = s.client.R().
SetResult(&result).
@ -40,6 +44,10 @@ func (s service) RevokeMsg(toWxId string, clientMsgId, createTime, newMsgId int)
// @return newMsgId int 返回消息的NewMsgId字段
// @return err
func (s service) SendAppMsg(toWxId, xml string, appMsgType int) (clientMsgId, createTime, newMsgId int, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[SendMessageResponse]
_, err = s.client.R().
SetResult(&result).
@ -68,6 +76,10 @@ func (s service) SendAppMsg(toWxId, xml string, appMsgType int) (clientMsgId, cr
// @return newMsgId
// @return err
func (s service) SendCDNFileMsg(toWxId, xml string) (clientMsgId, createTime, newMsgId int, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[SendMessageResponse]
_, err = s.client.R().
SetResult(&result).
@ -95,6 +107,10 @@ func (s service) SendCDNFileMsg(toWxId, xml string) (clientMsgId, createTime, ne
// @return newMsgId
// @return err
func (s service) SendCDNImgMsg(toWxId, xml string) (clientMsgId, createTime, newMsgId int, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[SendMessageResponse]
_, err = s.client.R().
SetResult(&result).
@ -122,6 +138,10 @@ func (s service) SendCDNImgMsg(toWxId, xml string) (clientMsgId, createTime, new
// @return newMsgId
// @return err
func (s service) SendCDNVideoMsg(toWxId, xml string) (clientMsgId, createTime, newMsgId int, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[SendMessageResponse]
_, err = s.client.R().
SetResult(&result).
@ -151,6 +171,10 @@ func (s service) SendCDNVideoMsg(toWxId, xml string) (clientMsgId, createTime, n
// @return newMsgId
// @return err
func (s service) SendCardMsg(toWxId, cardWxId, cardNickname, cardAlias string) (clientMsgId, createTime, newMsgId int, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[SendMessageResponse]
_, err = s.client.R().
SetResult(&result).
@ -179,6 +203,10 @@ func (s service) SendCardMsg(toWxId, cardWxId, cardNickname, cardAlias string) (
// @return resp
// @return err
func (s service) SendEmojiMsg(toWxId, md5 string, length int) (resp any, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[SendMessageResponse]
_, err = s.client.R().
SetResult(&result).
@ -205,6 +233,10 @@ func (s service) SendEmojiMsg(toWxId, md5 string, length int) (resp any, err err
// @return newMsgId
// @return err
func (s service) SendImageMsg(toWxId, imgBase64Str string) (clientMsgId, createTime, newMsgId int, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[SendMessageResponse]
_, err = s.client.R().
SetResult(&result).
@ -235,6 +267,10 @@ func (s service) SendImageMsg(toWxId, imgBase64Str string) (clientMsgId, createT
// @return newMsgId
// @return err
func (s service) SendShareLink(toWxId, url, title, description, thumbUrl string) (clientMsgId, createTime, newMsgId int, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[SendMessageResponse]
_, err = s.client.R().
SetResult(&result).
@ -266,6 +302,10 @@ func (s service) SendShareLink(toWxId, url, title, description, thumbUrl string)
// @return newMsgId
// @return err
func (s service) SendTextMsg(toWxId, content string, atUser []string) (clientMsgId, createTime, newMsgId int, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[SendMessageResponse]
_, err = s.client.R().
SetResult(&result).
@ -297,6 +337,10 @@ func (s service) SendTextMsg(toWxId, content string, atUser []string) (clientMsg
// @return newMsgId
// @return err
func (s service) SendVideoMsg(toWxId, video, cover string, duration int) (clientMsgId, createTime, newMsgId int, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[SendMessageResponse]
_, err = s.client.R().
SetResult(&result).
@ -328,6 +372,10 @@ func (s service) SendVideoMsg(toWxId, video, cover string, duration int) (client
// @return newMsgId
// @return err
func (s service) SendVoiceMsg(toWxId, voice, format string, duration int) (clientMsgId, createTime, newMsgId int, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
formatMap := map[string]int{"amr": 0, "wav": 4, "mp3": 4}
if _, ok := formatMap[format]; !ok {
err = errors.New("不支持的语音格式")
@ -359,6 +407,10 @@ func (s service) SendVoiceMsg(toWxId, voice, format string, duration int) (clien
// @return msg
// @return err
func (s service) Sync() (msg []SyncMessage, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[SyncResponse]
_, err = s.client.R().
SetResult(&result).

View File

@ -39,6 +39,10 @@ func (s service) IsRunning() (flag bool, err error) {
// @return dataBase64Str
// @return err
func (s service) CdnDownloadImg(aesKey, cdnMidImgUrl string) (dataBase64Str string, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[string]
_, err = s.client.R().
SetResult(&result).
@ -61,6 +65,10 @@ func (s service) CdnDownloadImg(aesKey, cdnMidImgUrl string) (dataBase64Str stri
// @return dataBase64Str
// @return err
func (s service) DownloadAttach(attachId string) (dataBase64Str string, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[DownloadAttachResponse]
_, err = s.client.R().
SetResult(&result).
@ -82,6 +90,10 @@ func (s service) DownloadAttach(attachId string) (dataBase64Str string, err erro
// @return dataBase64Str
// @return err
func (s service) DownloadVideo(msgId int) (dataBase64Str string, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[DownloadAttachResponse]
_, err = s.client.R().
SetResult(&result).
@ -105,6 +117,10 @@ func (s service) DownloadVideo(msgId int) (dataBase64Str string, err error) {
// @return dataBase64Str
// @return err
func (s service) DownloadVoice(msgId int, voiceUrl string, length int) (dataBase64Str string, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[DownloadAttachResponse]
_, err = s.client.R().
SetResult(&result).
@ -127,6 +143,10 @@ func (s service) DownloadVoice(msgId int, voiceUrl string, length int) (dataBase
// @param param
// @return err
func (s service) SetProxy(param SetProxyRequest) (err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[base.EmptyResponse]
_, err = s.client.R().
SetResult(&result).
@ -151,6 +171,10 @@ func (s service) SetProxy(param SetProxyRequest) (err error) {
// @param stepCount
// @return err
func (s service) SetStep(stepCount int) (err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[base.EmptyResponse]
_, err = s.client.R().
SetResult(&result).

View File

@ -4,7 +4,7 @@ import "github.com/go-resty/resty/v2"
type API interface {
GetMyQRCode() (str string, err error) // 获取个人二维码
GetProfile() (resp any, err error) // 获取个人信息
GetProfile() (resp GetProfileResponse, err error) // 获取个人信息
}
type service struct {

View File

@ -8,6 +8,10 @@ import "gitee.ltd/lxh/xybot/base"
// @return str string 图片的base64编码字符串
// @return err error 错误信息
func (s service) GetMyQRCode() (str string, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[GetMyQRCodeResponse]
_, err = s.client.R().
SetResult(&result).
@ -25,6 +29,20 @@ func (s service) GetMyQRCode() (str string, err error) {
// @receiver s
// @return resp 用户信息
// @return err
func (s service) GetProfile() (resp any, err error) {
func (s service) GetProfile() (resp GetProfileResponse, err error) {
if err = base.PreCheck(s.wxId); err != nil {
return
}
var result base.Response[GetProfileResponse]
_, err = s.client.R().
SetResult(&result).
SetBody(map[string]any{"Wxid": s.wxId}).
Post("/GetProfile")
if err = result.CheckError(err); err != nil {
return
}
resp = result.Data
return
}