refresh_login_q_r_logic.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package Wxhook
  2. import (
  3. "context"
  4. "github.com/zeromicro/go-zero/core/errorx"
  5. "time"
  6. "wechat-api/hook"
  7. "wechat-api/internal/svc"
  8. "wechat-api/internal/types"
  9. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type RefreshLoginQRLogic struct {
  13. logx.Logger
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. }
  17. func NewRefreshLoginQRLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RefreshLoginQRLogic {
  18. return &RefreshLoginQRLogic{
  19. Logger: logx.WithContext(ctx),
  20. ctx: ctx,
  21. svcCtx: svcCtx}
  22. }
  23. func (l *RefreshLoginQRLogic) RefreshLoginQR(req *types.RefreshLoginQRReq) (resp *types.RefreshLoginQRResp, err error) {
  24. server, err := l.svcCtx.DB.Server.Get(l.ctx, *req.ServerId)
  25. if err != nil {
  26. l.Error("获取服务器信息失败", err)
  27. return
  28. }
  29. hookClient := hook.NewHook(server.PrivateIP, server.AdminPort, *req.Port)
  30. portOccupied, err := hookClient.GetPortOccupiedInfo(*req.Port)
  31. if err != nil {
  32. l.Error("获取端口占用信息失败", err)
  33. return nil, err
  34. }
  35. if portOccupied.Occupied != "1" {
  36. l.Info("端口未被占用,启动微信")
  37. start, err := hookClient.StartWechat(*req.Port)
  38. if err != nil {
  39. l.Error("启动微信失败", err)
  40. return nil, err
  41. }
  42. if start.Success != "1" {
  43. err = errorx.NewDefaultError("微信启动失败")
  44. return nil, err
  45. }
  46. }
  47. // 间隔一秒钟
  48. time.Sleep(1 * time.Second)
  49. loginStatus, err := hookClient.IsLoginStatus()
  50. if err != nil {
  51. l.Error("查询登录状态失败", err)
  52. return nil, errorx.NewDefaultError("查询登录状态失败")
  53. }
  54. resp = &types.RefreshLoginQRResp{}
  55. resp.Msg = errormsg.Success
  56. resp.Data = types.LoginQRStatus{
  57. Status: loginStatus.Onlinestatus,
  58. StatusDesc: loginStatus.Msg,
  59. }
  60. if loginStatus.Onlinestatus == "5" {
  61. err = hookClient.RefreshLoginQRCode()
  62. if err != nil {
  63. return nil, err
  64. }
  65. }
  66. if loginStatus.Onlinestatus == "0" || loginStatus.Onlinestatus == "5" {
  67. LoginQRCode, err := hookClient.GetLoginQRCode()
  68. if err != nil {
  69. return nil, err
  70. }
  71. if LoginQRCode.Base64 == "" {
  72. err = errorx.NewDefaultError("二维码获取失败")
  73. return nil, err
  74. }
  75. resp.Data.QRCode = LoginQRCode.Base64
  76. resp.Data.StatusDesc = "扫码登陆"
  77. return resp, nil
  78. }
  79. return resp, nil
  80. }