HJWeiXinAPIManager.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // HJWeiXinAPIManager.m
  3. // HappyJob
  4. //
  5. // Created by Bob on 2019/4/1.
  6. // Copyright © 2019 Bob. All rights reserved.
  7. //
  8. #import "HJWeiXinAPIManager.h"
  9. @implementation HJWeiXinAPIManager
  10. + (instancetype)sharedManager {
  11. static dispatch_once_t onceToken;
  12. static HJWeiXinAPIManager *instance;
  13. dispatch_once(&onceToken, ^{
  14. instance = [[HJWeiXinAPIManager alloc] init];
  15. });
  16. return instance;
  17. }
  18. - (void)wxSendAuthReqWithViewController:(UIViewController *)viewController {
  19. SendAuthReq *req = [[SendAuthReq alloc] init];
  20. req.state = @"wx_oauth_authorization_state";//用于保持请求和回调的状态,授权请求或原样带回
  21. req.scope = @"snsapi_userinfo";//授权作用域:获取用户个人信息
  22. //判断用户是否已安装微信App
  23. if ([WXApi isWXAppInstalled])
  24. {//安装
  25. [WXApi sendReq:req];
  26. }
  27. else
  28. {//未安装
  29. [WXApi sendAuthReq:req viewController:viewController delegate:self];
  30. }
  31. }
  32. #pragma mark - WXApiDelegate
  33. - (void)onResp:(BaseResp *)resp {
  34. // 判断是否为授权登录类
  35. if ([resp isKindOfClass:[SendAuthResp class]])
  36. {
  37. SendAuthResp *sendAuthResp = (SendAuthResp *)resp;
  38. if ([sendAuthResp.state isEqualToString:@"wx_oauth_authorization_state"])
  39. {
  40. // 微信授权成功
  41. if (sendAuthResp.errCode == 0)
  42. {
  43. if ([self.delegate respondsToSelector:@selector(wxSendAuthResponseDidSuccess:)])
  44. {
  45. [self.delegate wxSendAuthResponseDidSuccess:sendAuthResp];
  46. }
  47. }
  48. else
  49. {
  50. if ([self.delegate respondsToSelector:@selector(wxSendAuthResponseDidFailed:)])
  51. {
  52. [self.delegate wxSendAuthResponseDidFailed:sendAuthResp];
  53. }
  54. }
  55. }
  56. else
  57. {
  58. if ([self.delegate respondsToSelector:@selector(wxSendAuthResponseDidFailed:)])
  59. {
  60. [self.delegate wxSendAuthResponseDidFailed:sendAuthResp];
  61. }
  62. }
  63. }
  64. //支付的
  65. if ([resp isKindOfClass:[PayResp class]])
  66. {
  67. if (resp.errCode == 0)
  68. {//成功
  69. [[NSNotificationCenter defaultCenter] postNotificationName:kNotifiWeChatPaySucceed object:nil];
  70. }
  71. else if (resp.errCode == -1)
  72. {//错误
  73. [[NSNotificationCenter defaultCenter] postNotificationName:kNotifiWeChatPayFailed object:nil];
  74. }
  75. else
  76. {//用户取消
  77. [[NSNotificationCenter defaultCenter] postNotificationName:kNotifiWeChatPayCancel object:nil];
  78. }
  79. }
  80. }
  81. @end