123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- package Wxhook
- import (
- "context"
- "github.com/zeromicro/go-zero/core/errorx"
- "time"
- "wechat-api/hook"
- "wechat-api/internal/svc"
- "wechat-api/internal/types"
- "github.com/suyuan32/simple-admin-common/msg/errormsg"
- "github.com/zeromicro/go-zero/core/logx"
- )
- type RefreshLoginQRLogic struct {
- logx.Logger
- ctx context.Context
- svcCtx *svc.ServiceContext
- }
- func NewRefreshLoginQRLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RefreshLoginQRLogic {
- return &RefreshLoginQRLogic{
- Logger: logx.WithContext(ctx),
- ctx: ctx,
- svcCtx: svcCtx}
- }
- func (l *RefreshLoginQRLogic) RefreshLoginQR(req *types.RefreshLoginQRReq) (resp *types.RefreshLoginQRResp, err error) {
- server, err := l.svcCtx.DB.Server.Get(l.ctx, *req.ServerId)
- if err != nil {
- l.Error("获取服务器信息失败", err)
- return
- }
- hookClient := hook.NewHook(server.PrivateIP, server.AdminPort, *req.Port)
- portOccupied, err := hookClient.GetPortOccupiedInfo(*req.Port)
- if err != nil {
- l.Error("获取端口占用信息失败", err)
- return nil, err
- }
- if portOccupied.Occupied != "1" {
- l.Info("端口未被占用,启动微信")
- start, err := hookClient.StartWechat(*req.Port)
- if err != nil {
- l.Error("启动微信失败", err)
- return nil, err
- }
- if start.Success != "1" {
- err = errorx.NewDefaultError("微信启动失败")
- return nil, err
- }
- }
- // 间隔一秒钟
- time.Sleep(1 * time.Second)
- loginStatus, err := hookClient.IsLoginStatus()
- if err != nil {
- l.Error("查询登录状态失败", err)
- return nil, errorx.NewDefaultError("查询登录状态失败")
- }
- resp = &types.RefreshLoginQRResp{}
- resp.Msg = errormsg.Success
- resp.Data = types.LoginQRStatus{
- Status: loginStatus.Onlinestatus,
- StatusDesc: loginStatus.Msg,
- }
- if loginStatus.Onlinestatus == "5" {
- err = hookClient.RefreshLoginQRCode()
- if err != nil {
- return nil, err
- }
- }
- if loginStatus.Onlinestatus == "0" || loginStatus.Onlinestatus == "5" {
- LoginQRCode, err := hookClient.GetLoginQRCode()
- if err != nil {
- return nil, err
- }
- if LoginQRCode.Base64 == "" {
- err = errorx.NewDefaultError("二维码获取失败")
- return nil, err
- }
- resp.Data.QRCode = LoginQRCode.Base64
- resp.Data.StatusDesc = "扫码登陆"
- return resp, nil
- }
- return resp, nil
- }
|