refresh_login_q_r_logic.go 2.5 KB

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