Files
arenax-server/internal/controller/wx/wx_v1_we_chat_polling.go

57 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package wx
import (
"context"
"encoding/json"
"fmt"
v1 "server/api/wx/v1"
"server/internal/consts"
"server/internal/model"
"server/utility/ecode"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/glog"
)
func (c *ControllerV1) WeChatPolling(ctx context.Context, req *v1.WeChatPollingReq) (res *v1.WeChatPollingRes, err error) {
loginCacheKey := fmt.Sprintf(consts.WeChatLoginCache, req.SceneId)
glog.Infof(ctx, "开始处理微信长轮询请求SceneID: %s", req.SceneId)
var loginCache model.LoginCache
data, err := g.Redis().Get(ctx, loginCacheKey)
if err != nil {
glog.Errorf(ctx, "从 Redis 获取登录缓存失败SceneID: %s错误: %v", req.SceneId, err)
return nil, ecode.Fail.Sub("获取登录状态失败")
}
if data.IsEmpty() {
glog.Warningf(ctx, "用户尚未扫码登录SceneID: %s", req.SceneId)
return nil, ecode.InvalidOperation.Sub("请先调用获取二维码登录")
}
if err = json.Unmarshal(data.Bytes(), &loginCache); err != nil {
glog.Errorf(ctx, "解析登录状态失败SceneID: %s错误: %v", req.SceneId, err)
return nil, ecode.Fail.Sub("解析登录状态失败")
}
switch loginCache.Status {
case 0:
glog.Infof(ctx, "用户尚未扫码登录SceneID: %s", req.SceneId)
return &v1.WeChatPollingRes{
Status: "waiting",
Token: "",
}, nil
case 1:
// 直接返回缓存的token
glog.Infof(ctx, "用户扫码登录成功SceneID: %s返回缓存Token", req.SceneId)
return &v1.WeChatPollingRes{
Status: "success",
Token: loginCache.Token,
}, nil
default:
glog.Warningf(ctx, "未知登录状态 %dSceneID: %s", loginCache.Status, req.SceneId)
return nil, ecode.InvalidOperation.Sub("未知登录状态")
}
}