refresh_login_q_r_logic.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. // 获取服务器信息
  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. // 如端口未被占用则启动微信
  31. portOccupied, err := hookClient.GetPortOccupiedInfo(*req.Port)
  32. if err != nil {
  33. l.Error("获取端口占用信息失败", err)
  34. return nil, err
  35. }
  36. if portOccupied.Occupied != "1" {
  37. l.Info("端口未被占用,启动微信")
  38. start, err := hookClient.StartWechat(*req.Port)
  39. if err != nil {
  40. l.Error("启动微信失败", err)
  41. return nil, err
  42. }
  43. if start.Success != "1" {
  44. err = errorx.NewDefaultError("微信启动失败")
  45. return nil, err
  46. }
  47. }
  48. loginStatus, err := hookClient.IsLoginStatus()
  49. if err != nil {
  50. l.Error("查询登录状态失败", err)
  51. return nil, errorx.NewDefaultError("查询登录状态失败")
  52. }
  53. resp = &types.RefreshLoginQRResp{}
  54. resp.Msg = errormsg.Success
  55. resp.Data = types.LoginQRStatus{
  56. Status: loginStatus.Onlinestatus,
  57. StatusDesc: loginStatus.Msg,
  58. }
  59. if loginStatus.Onlinestatus == "5" {
  60. err = hookClient.RefreshLoginQRCode()
  61. if err != nil {
  62. return nil, err
  63. }
  64. }
  65. if loginStatus.Onlinestatus == "0" || loginStatus.Onlinestatus == "5" {
  66. LoginQRCode, err := hookClient.GetLoginQRCode()
  67. if err != nil {
  68. return nil, err
  69. }
  70. if LoginQRCode.Base64 == "" {
  71. err = errorx.NewDefaultError("二维码获取失败")
  72. return nil, err
  73. }
  74. resp.Data.QRCode = LoginQRCode.Base64
  75. resp.Data.StatusDesc = "扫码登陆"
  76. return resp, nil
  77. }
  78. return resp, nil
  79. }