HJWeiXinAPIManager.m 3.4 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. if ([WXApi isWXAppInstalled]) {//判断用户是否已安装微信App
  23. [WXApi sendReq:req];
  24. } else {
  25. [WXApi sendAuthReq:req viewController:viewController delegate:self];
  26. }
  27. // if ([WXApi isWXAppInstalled]) {//判断用户是否已安装微信App
  28. // SendAuthReq *req = [[SendAuthReq alloc] init];
  29. // req.state = @"wx_oauth_authorization_state";//用于保持请求和回调的状态,授权请求或原样带回
  30. // req.scope = @"snsapi_userinfo";//授权作用域:获取用户个人信息
  31. //
  32. // if ([WXApi sendReq:req]) {
  33. // NSLog(@"发起微信授权请求成功");
  34. // } else {
  35. // NSLog(@"发起微信授权请求失败");
  36. // }
  37. //
  38. // } else {
  39. // // 提示:未安装微信应用跳转到AppStore下载
  40. // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/wechat/id414478124"]];
  41. //
  42. // }
  43. }
  44. #pragma mark - WXApiDelegate
  45. - (void)onResp:(BaseResp *)resp {
  46. // 判断是否为授权登录类
  47. if ([resp isKindOfClass:[SendAuthResp class]]) {
  48. SendAuthResp *sendAuthResp = (SendAuthResp *)resp;
  49. if ([sendAuthResp.state isEqualToString:@"wx_oauth_authorization_state"]) {
  50. // 微信授权成功
  51. if (sendAuthResp.errCode == 0) {
  52. if ([self.delegate respondsToSelector:@selector(wxSendAuthResponseDidSuccess:)]) {
  53. [self.delegate wxSendAuthResponseDidSuccess:sendAuthResp];
  54. }
  55. } else {
  56. if ([self.delegate respondsToSelector:@selector(wxSendAuthResponseDidFailed:)]) {
  57. [self.delegate wxSendAuthResponseDidFailed:sendAuthResp];
  58. }
  59. }
  60. } else {
  61. if ([self.delegate respondsToSelector:@selector(wxSendAuthResponseDidFailed:)]) {
  62. [self.delegate wxSendAuthResponseDidFailed:sendAuthResp];
  63. }
  64. }
  65. }
  66. // if ([resp isKindOfClass:[PayResp class]]) {
  67. // PayResp *payResp = (PayResp *)resp;
  68. // [self.delegate wxPayResponse:payResp];
  69. // //支付返回结果,实际支付结果需要去微信服务器端查询
  70. // NSString *strMsg;
  71. // switch (resp.errCode) {
  72. // case WXSuccess:
  73. // strMsg = @"支付结果:成功!";
  74. // NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);
  75. // break;
  76. // default:
  77. // strMsg = [NSString stringWithFormat:@"支付结果:失败!retcode = %d, retstr = %@", resp.errCode,resp.errStr];
  78. // NSLog(@"错误,retcode = %d, retstr = %@", resp.errCode,resp.errStr);
  79. // break;
  80. // }
  81. // }
  82. }
  83. @end