Skip to content

Commit 77e4b13

Browse files
committed
feat(user): bind or change phone number by sms code
close #380
1 parent beb9896 commit 77e4b13

File tree

1 file changed

+191
-49
lines changed

1 file changed

+191
-49
lines changed

Sources/Foundation/User.swift

Lines changed: 191 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -620,47 +620,50 @@ open class LCUser: LCObject {
620620
/// - Parameters:
621621
/// - application: The application the user belong to, default is `LCApplication.default`.
622622
/// - mobilePhoneNumber: The mobile phone number where the verification code will be sent to.
623-
public static func requestVerificationCode(application: LCApplication = .default, mobilePhoneNumber: String) -> LCBooleanResult {
623+
/// - Returns: `LCBooleanResult`
624+
public static func requestVerificationCode(
625+
application: LCApplication = .default,
626+
mobilePhoneNumber: String) -> LCBooleanResult
627+
{
624628
return expect { fulfill in
625-
self.requestVerificationCode(
629+
self._requestVerificationCode(
626630
application: application,
627-
mobilePhoneNumber: mobilePhoneNumber,
628-
completionInBackground: { result in
629-
fulfill(result)
630-
})
631+
mobilePhoneNumber: mobilePhoneNumber)
632+
{ result in
633+
fulfill(result)
634+
}
631635
}
632636
}
633637

634638
/// Request to send a verification code to specified mobile phone number asynchronously.
635639
/// - Parameters:
636640
/// - application: The application the user belong to, default is `LCApplication.default`.
637641
/// - mobilePhoneNumber: The mobile phone number where the verification code will be sent to.
638-
/// - completionQueue: The queue where `completion` be executed, default is main.
639-
/// - completion: Result callback.
642+
/// - completionQueue: The queue where the completion be invoked, default is `DispatchQueue.main`.
643+
/// - completion: The result callback.
644+
/// - Returns: `LCRequest`
640645
@discardableResult
641646
public static func requestVerificationCode(
642647
application: LCApplication = .default,
643648
mobilePhoneNumber: String,
644649
completionQueue: DispatchQueue = .main,
645-
completion: @escaping (LCBooleanResult) -> Void)
646-
-> LCRequest
650+
completion: @escaping (LCBooleanResult) -> Void) -> LCRequest
647651
{
648-
return self.requestVerificationCode(
652+
return self._requestVerificationCode(
649653
application: application,
650-
mobilePhoneNumber: mobilePhoneNumber,
651-
completionInBackground: { result in
652-
completionQueue.async {
653-
completion(result)
654-
}
655-
})
654+
mobilePhoneNumber: mobilePhoneNumber)
655+
{ result in
656+
completionQueue.async {
657+
completion(result)
658+
}
659+
}
656660
}
657-
661+
658662
@discardableResult
659-
private static func requestVerificationCode(
663+
private static func _requestVerificationCode(
660664
application: LCApplication,
661665
mobilePhoneNumber: String,
662-
completionInBackground completion: @escaping (LCBooleanResult) -> Void)
663-
-> LCRequest
666+
completion: @escaping (LCBooleanResult) -> Void) -> LCRequest
664667
{
665668
return application.httpClient.request(
666669
.post, "requestMobilePhoneVerify",
@@ -669,68 +672,207 @@ open class LCUser: LCObject {
669672
completion(LCBooleanResult(response: response))
670673
}
671674
}
672-
675+
676+
/// Request to send a verification code to bind or update mobile phone number synchronously.
677+
/// - Parameters:
678+
/// - application: The application the user belong to, default is `LCApplication.default`.
679+
/// - mobilePhoneNumber: The mobile phone number where the verification code will be sent to.
680+
/// - timeToLive: The time-to-live of the code.
681+
/// - Returns: `LCBooleanResult`
682+
public static func requestVerificationCode(
683+
application: LCApplication = .default,
684+
forUpdatingMobilePhoneNumber mobilePhoneNumber: String,
685+
timeToLive: Int? = nil) -> LCBooleanResult
686+
{
687+
return expect { (fulfill) in
688+
self._requestVerificationCode(
689+
application: application,
690+
forUpdatingMobilePhoneNumber: mobilePhoneNumber,
691+
timeToLive: timeToLive)
692+
{ (result) in
693+
fulfill(result)
694+
}
695+
}
696+
}
697+
698+
/// Request to send a verification code to bind or update mobile phone number asynchronously.
699+
/// - Parameters:
700+
/// - application: The application the user belong to, default is `LCApplication.default`.
701+
/// - mobilePhoneNumber: The mobile phone number where the verification code will be sent to.
702+
/// - timeToLive: The time-to-live of the code.
703+
/// - completionQueue: The queue where the completion be invoked, default is `DispatchQueue.main`.
704+
/// - completion: The result callback.
705+
/// - Returns: `LCRequest`
706+
@discardableResult
707+
public static func requestVerificationCode(
708+
application: LCApplication = .default,
709+
forUpdatingMobilePhoneNumber mobilePhoneNumber: String,
710+
timeToLive: Int? = nil,
711+
completionQueue: DispatchQueue = .main,
712+
completion: @escaping (LCBooleanResult) -> Void) -> LCRequest
713+
{
714+
return self._requestVerificationCode(
715+
application: application,
716+
forUpdatingMobilePhoneNumber: mobilePhoneNumber,
717+
timeToLive: timeToLive)
718+
{ (result) in
719+
completionQueue.async {
720+
completion(result)
721+
}
722+
}
723+
}
724+
725+
@discardableResult
726+
private static func _requestVerificationCode(
727+
application: LCApplication,
728+
forUpdatingMobilePhoneNumber mobilePhoneNumber: String,
729+
timeToLive: Int?,
730+
completion: @escaping (LCBooleanResult) -> Void) -> LCRequest
731+
{
732+
var parameters: [String: Any] = ["mobilePhoneNumber": mobilePhoneNumber]
733+
if let timeToLive = timeToLive {
734+
parameters["ttl"] = timeToLive
735+
}
736+
return application.httpClient.request(
737+
.post, "requestChangePhoneNumber",
738+
parameters: parameters)
739+
{ response in
740+
completion(LCBooleanResult(response: response))
741+
}
742+
}
743+
673744
// MARK: Verify phone number
674745

675746
/// Verify mobile phone number with code synchronously.
676747
/// - Parameters:
677748
/// - application: The application the user belong to, default is `LCApplication.default`.
678749
/// - mobilePhoneNumber: The mobile phone number of the user.
679-
/// - verificationCode: The verification code sent to `mobilePhoneNumber`.
680-
public static func verifyMobilePhoneNumber(application: LCApplication = .default, _ mobilePhoneNumber: String, verificationCode: String) -> LCBooleanResult {
750+
/// - verificationCode: The verification code sent to mobile phone number.
751+
/// - Returns: `LCBooleanResult`
752+
public static func verifyMobilePhoneNumber(
753+
application: LCApplication = .default,
754+
_ mobilePhoneNumber: String,
755+
verificationCode: String) -> LCBooleanResult
756+
{
681757
return expect { fulfill in
682-
self.verifyMobilePhoneNumber(
758+
self._verifyMobilePhoneNumber(
683759
application: application,
684-
mobilePhoneNumber,
685-
verificationCode: verificationCode,
686-
completionInBackground: { result in
687-
fulfill(result)
688-
})
760+
mobilePhoneNumber: mobilePhoneNumber,
761+
verificationCode: verificationCode)
762+
{ result in
763+
fulfill(result)
764+
}
689765
}
690766
}
691767

692768
/// Verify mobile phone number with code asynchronously.
693769
/// - Parameters:
694770
/// - application: The application the user belong to, default is `LCApplication.default`.
695771
/// - mobilePhoneNumber: The mobile phone number of the user.
696-
/// - verificationCode: The verification code sent to `mobilePhoneNumber`.
697-
/// - completionQueue: The queue where `completion` be executed, default is main.
698-
/// - completion: Result callback.
772+
/// - verificationCode: The verification code sent to mobile phone number.
773+
/// - completionQueue: The queue where the completion be invoked, default is `DispatchQueue.main`.
774+
/// - completion: The result callback.
775+
/// - Returns: `LCRequest`
699776
@discardableResult
700777
public static func verifyMobilePhoneNumber(
701778
application: LCApplication = .default,
702779
_ mobilePhoneNumber: String,
703780
verificationCode: String,
704781
completionQueue: DispatchQueue = .main,
705-
completion: @escaping (LCBooleanResult) -> Void)
706-
-> LCRequest
782+
completion: @escaping (LCBooleanResult) -> Void) -> LCRequest
707783
{
708-
return self.verifyMobilePhoneNumber(
784+
return self._verifyMobilePhoneNumber(
709785
application: application,
710-
mobilePhoneNumber,
711-
verificationCode: verificationCode,
712-
completionInBackground: { result in
713-
completionQueue.async {
714-
completion(result)
715-
}
716-
})
786+
mobilePhoneNumber: mobilePhoneNumber,
787+
verificationCode: verificationCode)
788+
{ result in
789+
completionQueue.async {
790+
completion(result)
791+
}
792+
}
717793
}
718-
794+
719795
@discardableResult
720-
private static func verifyMobilePhoneNumber(
796+
private static func _verifyMobilePhoneNumber(
721797
application: LCApplication,
722-
_ mobilePhoneNumber: String,
798+
mobilePhoneNumber: String,
723799
verificationCode: String,
724-
completionInBackground completion: @escaping (LCBooleanResult) -> Void)
725-
-> LCRequest
800+
completion: @escaping (LCBooleanResult) -> Void) -> LCRequest
726801
{
727802
return application.httpClient.request(
728-
.get, "verifyMobilePhone/\(verificationCode)",
803+
.post, "verifyMobilePhone/\(verificationCode)",
729804
parameters: ["mobilePhoneNumber": mobilePhoneNumber])
730805
{ response in
731806
completion(LCBooleanResult(response: response))
732807
}
733808
}
809+
810+
/// Verify code to bind or update mobile phone number synchronously.
811+
/// - Parameters:
812+
/// - application: The application the user belong to, default is `LCApplication.default`.
813+
/// - verificationCode: The verification code sent to mobile phone number.
814+
/// - mobilePhoneNumber: The mobile phone number to be bound or updated.
815+
/// - Returns: `LCBooleanResult`
816+
public static func verifyVerificationCode(
817+
application: LCApplication = .default,
818+
_ verificationCode: String,
819+
toUpdateMobilePhoneNumber mobilePhoneNumber: String) -> LCBooleanResult
820+
{
821+
return expect { (fulfill) in
822+
self._verifyVerificationCode(
823+
application: application,
824+
verificationCode: verificationCode,
825+
toUpdateMobilePhoneNumber: mobilePhoneNumber)
826+
{ (result) in
827+
fulfill(result)
828+
}
829+
}
830+
}
831+
832+
/// Verify code to bind or update mobile phone number asynchronously.
833+
/// - Parameters:
834+
/// - application: The application the user belong to, default is `LCApplication.default`.
835+
/// - verificationCode: The verification code sent to mobile phone number.
836+
/// - mobilePhoneNumber: The mobile phone number to be bound or updated.
837+
/// - completionQueue: The queue where the completion be invoked, default is `DispatchQueue.main`.
838+
/// - completion: The result callback.
839+
/// - Returns: `LCRequest`
840+
@discardableResult
841+
public static func verifyVerificationCode(
842+
application: LCApplication = .default,
843+
_ verificationCode: String,
844+
toUpdateMobilePhoneNumber mobilePhoneNumber: String,
845+
completionQueue: DispatchQueue = .main,
846+
completion: @escaping (LCBooleanResult) -> Void) -> LCRequest
847+
{
848+
return self._verifyVerificationCode(
849+
application: application,
850+
verificationCode: verificationCode,
851+
toUpdateMobilePhoneNumber: mobilePhoneNumber)
852+
{ (result) in
853+
completionQueue.async {
854+
completion(result)
855+
}
856+
}
857+
}
858+
859+
@discardableResult
860+
private static func _verifyVerificationCode(
861+
application: LCApplication,
862+
verificationCode: String,
863+
toUpdateMobilePhoneNumber mobilePhoneNumber: String,
864+
completion: @escaping (LCBooleanResult) -> Void) -> LCRequest
865+
{
866+
return application.httpClient.request(
867+
.post, "changePhoneNumber",
868+
parameters: [
869+
"mobilePhoneNumber": mobilePhoneNumber,
870+
"code": verificationCode
871+
])
872+
{ response in
873+
completion(LCBooleanResult(response: response))
874+
}
875+
}
734876

735877
// MARK: Send a login verification code
736878

0 commit comments

Comments
 (0)