Sin Descripción

WXUserInfoRemoteAPI.swift 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // WXUserInfoRemoteAPI.swift
  3. // PaiaiDataKit
  4. //
  5. // Created by FFIB on 2018/12/26.
  6. // Copyright © 2018 FFIB. All rights reserved.
  7. //
  8. import Foundation
  9. import RxSwift
  10. private let WXAppid = "wx4e22a0c8ae6d766d"
  11. private let WXSecret = "636ac848016c593575d11143c55c8333"
  12. struct WXUserInfoRemoteAPI: UserInfoRemoteAPI {
  13. let disposeBag = DisposeBag()
  14. init() {}
  15. func login() -> Single<UserInfo> {
  16. #if (arch(i386) || arch(x86_64))
  17. let photoPath = "https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJibSYLgvXpMakvD9FaCqfiaWqcMiaiaz905YxWPuO4hy8F2lGheV7kVr9vKKXFgmL1S5s4QJgxwuwtVw/132" //swiftlint:disable:this line_length
  18. return Single.create(subscribe: { (observer) in
  19. observer(.success(UserInfo(json: ["user_id": "fiDz2Ms" as AnyObject,
  20. "nickname": "郑剑飞" as AnyObject,
  21. "photoPath": photoPath as AnyObject])))
  22. return Disposables.create()
  23. })
  24. #else
  25. loginWithWeChat()
  26. return addWXLoginDidFinish().flatMap({
  27. return self.getWXToken(param: $0)
  28. }).flatMap({
  29. return self.getWXUserInfo(param: $0)
  30. }).flatMap({
  31. return self.wxLogin(param: $0)
  32. }).do(onSuccess: { res in
  33. print(res)
  34. })
  35. #endif
  36. }
  37. fileprivate func loginWithWeChat() {
  38. let request = SendAuthReq()
  39. request.scope = "snsapi_userinfo,snsapi_base"
  40. request.state = "skilogin"
  41. WXApi.send(request)
  42. }
  43. fileprivate func addWXLoginDidFinish() -> Single<[String: String]> {
  44. return Single.create(subscribe: { observer -> Disposable in
  45. NotificationCenter.default.rx.notification(.wxLogin).subscribe(onNext: { notification in
  46. guard let userInfo = notification.userInfo,
  47. let errcode = userInfo["errCode"] as? Int,
  48. let code = userInfo["code"] as? String else {
  49. Toast.show(message: "获取code失败")
  50. return observer(.error(ParseError()))
  51. }
  52. switch errcode {
  53. case 0:
  54. return observer(.success(["appid": WXAppid, "secret": WXSecret,
  55. "code": code, "grant_type": "authorization_code"]))
  56. case -4:
  57. Toast.show(message: "您拒绝授权,登录失败")
  58. case -2:
  59. Toast.show(message: "您取消了登录,登录失败")
  60. default:
  61. Toast.show(message: "发生未知错误,登录失败")
  62. }
  63. return observer(.error(BusinessError(id: errcode)))
  64. }).disposed(by: self.disposeBag)
  65. return Disposables.create()
  66. })
  67. }
  68. fileprivate func parseWxToken(json: JSON) -> [String: String]? {
  69. guard let token = json["access_token"] as? String,
  70. let openId = json["openid"] as? String else { return nil }
  71. return ["access_token": token, "openid": openId]
  72. }
  73. fileprivate func getWXToken(param: [String: String]) -> Single<[String: String]> {
  74. let resource = ContentResource<[String: String]>(host: "https://api.weixin.qq.com",
  75. path: .wxAccessToken,
  76. parameter: param,
  77. parseJSON: parseWxToken)
  78. return NetworkApi.share.get(resource: resource)
  79. }
  80. fileprivate func pareseWXUserInfo(json: JSON) -> [String: Any]? {
  81. var data = json
  82. data.removeValue(forKey: "privilege")
  83. return data
  84. }
  85. fileprivate func getWXUserInfo(param: [String: String]) -> Single<[String: Any]> {
  86. let resource = ContentResource<[String: Any]>(host: "https://api.weixin.qq.com",
  87. path: .wxUserInfo,
  88. parameter: param,
  89. parseJSON: pareseWXUserInfo)
  90. return NetworkApi.share.get(resource: resource)
  91. }
  92. fileprivate func parseAuthorize(json: JSON) -> UserInfo? {
  93. guard let data = json["data"] as? [String: AnyObject] else { return nil }
  94. return UserInfo(json: data)
  95. }
  96. fileprivate func wxLogin(param: [String: Any]) -> Single<UserInfo> {
  97. let resource = ContentResource<UserInfo>(path: .authorize, parameter: param, parseJSON: parseAuthorize)
  98. return resource.loadContent()
  99. }
  100. }