refresh_login_q_r_logic.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package Wxhook
  2. import (
  3. "context"
  4. "github.com/zeromicro/go-zero/core/errorx"
  5. "wechat-api/ent/wx"
  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. wxInfo, err := l.svcCtx.DB.Wx.Query().Where(wx.ServerIDEQ(*req.ServerId)).Where(wx.PortEQ(*req.Port)).Limit(1).Only(l.ctx)
  25. if err != nil {
  26. l.Error("查询微信信息失败", err)
  27. return
  28. }
  29. // 获取服务器信息
  30. server, err := l.svcCtx.DB.Server.Get(l.ctx, *req.ServerId)
  31. if err != nil {
  32. l.Error("获取服务器信息失败", err)
  33. return
  34. }
  35. hookClient := hook.NewHook(server.PrivateIP, server.AdminPort, *req.Port)
  36. _, _ = hookClient.TerminateThisWeChat(wxInfo.ProcessID)
  37. // 如端口未被占用则启动微信
  38. portOccupied, err := hookClient.GetPortOccupiedInfo(*req.Port)
  39. if err != nil {
  40. l.Error("获取端口占用信息失败", err)
  41. return nil, err
  42. }
  43. if portOccupied.Occupied != "1" {
  44. l.Info("端口未被占用,启动微信")
  45. start, err := hookClient.StartWechat(*req.Port)
  46. if err != nil {
  47. l.Error("启动微信失败", err)
  48. return nil, err
  49. }
  50. if start.Success != "1" {
  51. err = errorx.NewDefaultError("微信启动失败")
  52. return nil, err
  53. }
  54. }
  55. loginStatus, err := hookClient.IsLoginStatus()
  56. if err != nil {
  57. l.Error("查询登录状态失败", err)
  58. return nil, errorx.NewDefaultError("查询登录状态失败")
  59. }
  60. resp = &types.RefreshLoginQRResp{}
  61. resp.Msg = errormsg.Success
  62. resp.Data = types.LoginQRStatus{
  63. Status: loginStatus.Onlinestatus,
  64. StatusDesc: loginStatus.Msg,
  65. }
  66. if loginStatus.Onlinestatus == "5" {
  67. err = hookClient.RefreshLoginQRCode()
  68. if err != nil {
  69. return nil, err
  70. }
  71. }
  72. if loginStatus.Onlinestatus == "0" || loginStatus.Onlinestatus == "5" {
  73. LoginQRCode, err := hookClient.GetLoginQRCode()
  74. if err != nil {
  75. return nil, err
  76. }
  77. if LoginQRCode.Base64 == "" {
  78. err = errorx.NewDefaultError("二维码获取失败")
  79. return nil, err
  80. }
  81. resp.Data.QRCode = LoginQRCode.Base64
  82. resp.Data.StatusDesc = "扫码登陆"
  83. return resp, nil
  84. }
  85. return resp, nil
  86. }