HJWeiXinAPIManager.m 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. @end