159 lines
4.1 KiB
Go
159 lines
4.1 KiB
Go
|
|
package controller
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"encoding/xml"
|
|||
|
|
"log"
|
|||
|
|
"net/http"
|
|||
|
|
"net/url"
|
|||
|
|
"os"
|
|||
|
|
"path/filepath"
|
|||
|
|
"strings"
|
|||
|
|
"xiawan/wx/api/req"
|
|||
|
|
"xiawan/wx/api/service"
|
|||
|
|
"xiawan/wx/api/vo"
|
|||
|
|
|
|||
|
|
"github.com/gin-gonic/gin"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// GetMyQrCode 获取我的二维码
|
|||
|
|
func GetMyQrCode(ctx *gin.Context) {
|
|||
|
|
reqModel := new(req.GetQrCodeModel)
|
|||
|
|
|
|||
|
|
queryKey, isExist := ctx.GetQuery("key")
|
|||
|
|
if !isExist || strings.Trim(queryKey, "") == "" || strings.Trim(queryKey, "") == "null" {
|
|||
|
|
//确保每次都有Key
|
|||
|
|
ctx.JSON(http.StatusOK, vo.NewFailUUId(""))
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if !validateData(ctx, &reqModel) {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
result := service.GetMyQrCodeService(queryKey, *reqModel)
|
|||
|
|
ctx.JSON(http.StatusOK, result)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetPeopleNearbyApi 查看附近的人
|
|||
|
|
func GetPeopleNearbyApi(ctx *gin.Context) {
|
|||
|
|
reqModel := new(req.PeopleNearbyModel)
|
|||
|
|
queryKey, isExist := ctx.GetQuery("key")
|
|||
|
|
if !isExist || strings.Trim(queryKey, "") == "" || strings.Trim(queryKey, "") == "null" {
|
|||
|
|
//确保每次都有Key
|
|||
|
|
ctx.JSON(http.StatusOK, vo.NewFailUUId(""))
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
if !validateData(ctx, &reqModel) {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
result := service.GetPeopleNearbyService(queryKey, *reqModel)
|
|||
|
|
ctx.JSON(http.StatusOK, result)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// UpdateCmdStatusApi 更新指令状态, key 为指令 id,Value 为指令状态 0|1,ValueStr 为字符串值
|
|||
|
|
func UpdateCmdStatusApi(ctx *gin.Context) {
|
|||
|
|
reqModel := new(req.ModifyCmdStatusModelNew)
|
|||
|
|
queryKey, isExist := ctx.GetQuery("key")
|
|||
|
|
if !isExist || strings.Trim(queryKey, "") == "" || strings.Trim(queryKey, "") == "null" {
|
|||
|
|
//确保每次都有Key
|
|||
|
|
ctx.JSON(http.StatusOK, vo.NewFailUUId(""))
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
if !validateData(ctx, &reqModel) {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
result := service.UpdateCmdStatusService(queryKey, *reqModel)
|
|||
|
|
ctx.JSON(http.StatusOK, result)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetProjectFullPathApi 获取项目完整路径
|
|||
|
|
func GetProjectFullPathApi(ctx *gin.Context) {
|
|||
|
|
execPath, err := os.Executable() // 获取可执行文件的路径
|
|||
|
|
if err != nil {
|
|||
|
|
log.Fatalf("Failed to get executable path: %v", err)
|
|||
|
|
ctx.JSON(http.StatusNotFound, "")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
execPath, err = filepath.EvalSymlinks(execPath) // 解析符号链接,获取真实路径
|
|||
|
|
if err != nil {
|
|||
|
|
log.Fatalf("Failed to evaluate symlinks: %v", err)
|
|||
|
|
ctx.JSON(http.StatusNotFound, "")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
// 获取可执行文件所在目录
|
|||
|
|
dir := filepath.Dir(execPath)
|
|||
|
|
ctx.JSON(http.StatusOK, dir)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 企微图片下载
|
|||
|
|
func QWImageDownloadApi(ctx *gin.Context) {
|
|||
|
|
reqModel := new(req.DownloadQWImageModel)
|
|||
|
|
|
|||
|
|
queryKey, isExist := ctx.GetQuery("key")
|
|||
|
|
if !isExist || strings.Trim(queryKey, "") == "" || strings.Trim(queryKey, "") == "null" {
|
|||
|
|
ctx.JSON(http.StatusOK, vo.NewFailUUId(""))
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if !validateData(ctx, &reqModel) {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 解析XML,提取 AesKey / FileURL / FileType
|
|||
|
|
type imageXML struct {
|
|||
|
|
XMLName xml.Name `xml:"msg"`
|
|||
|
|
Img struct {
|
|||
|
|
AESKey string `xml:"aeskey,attr"`
|
|||
|
|
CdnMidImgURL string `xml:"cdnmidimgurl,attr"`
|
|||
|
|
CdnThumbURL string `xml:"cdnthumburl,attr"`
|
|||
|
|
TpURL string `xml:"tpurl,attr"`
|
|||
|
|
TpThumbAESKey string `xml:"tpthumbaeskey,attr"`
|
|||
|
|
} `xml:"img"`
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var parsed imageXML
|
|||
|
|
if err := xml.Unmarshal([]byte(reqModel.Xml), &parsed); err != nil {
|
|||
|
|
ctx.JSON(http.StatusOK, vo.NewFail("XML解析失败:"+err.Error()))
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
aesKey := strings.TrimSpace(parsed.Img.AESKey)
|
|||
|
|
if aesKey == "" {
|
|||
|
|
aesKey = strings.TrimSpace(parsed.Img.TpThumbAESKey)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
fileURL := ""
|
|||
|
|
tpurl := strings.TrimSpace(parsed.Img.TpURL)
|
|||
|
|
if tpurl != "" {
|
|||
|
|
if u, err := url.Parse(tpurl); err == nil {
|
|||
|
|
f := u.Query().Get("f")
|
|||
|
|
if f != "" {
|
|||
|
|
fileURL = f
|
|||
|
|
} else {
|
|||
|
|
// 回退为完整tpurl(如果缺少f参数)
|
|||
|
|
fileURL = tpurl
|
|||
|
|
}
|
|||
|
|
} else {
|
|||
|
|
fileURL = tpurl
|
|||
|
|
}
|
|||
|
|
} else {
|
|||
|
|
fileURL = strings.TrimSpace(parsed.Img.CdnMidImgURL)
|
|||
|
|
if fileURL == "" {
|
|||
|
|
fileURL = strings.TrimSpace(parsed.Img.CdnThumbURL)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if aesKey == "" || fileURL == "" {
|
|||
|
|
ctx.JSON(http.StatusOK, vo.NewFail("参数错误:无法从XML提取必要字段(AesKey或FileURL)"))
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
model := req.DownMediaModel{
|
|||
|
|
AesKey: aesKey,
|
|||
|
|
FileURL: fileURL,
|
|||
|
|
FileType: 1, // 默认 1
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
result := service.SendCdnDownloadService(queryKey, model)
|
|||
|
|
ctx.JSON(http.StatusOK, result)
|
|||
|
|
}
|