From a5434390a0f47cfb51bb453a6693e8f80d271d38 Mon Sep 17 00:00:00 2001 From: shiweifu Date: Mon, 7 Jun 2021 18:43:18 +0800 Subject: [PATCH 1/2] chore: add reqeust sms code and verify sms code --- leancloud/sms.go | 37 +++++++++++++++++++++++++++++++++++++ leancloud/sms_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 leancloud/sms.go create mode 100644 leancloud/sms_test.go diff --git a/leancloud/sms.go b/leancloud/sms.go new file mode 100644 index 0000000..17805b5 --- /dev/null +++ b/leancloud/sms.go @@ -0,0 +1,37 @@ +package leancloud + +import "fmt" + +type SMS struct { + c *Client +} + +func (ref *SMS) RequestSMSCode(number string) error { + path := "/1.1/requestSmsCode" + options := ref.c.getRequestOptions() + options.JSON = map[string]string{ + "mobilePhoneNumber": number, + } + + _, err := ref.c.request(methodPost, path, options) + if err != nil { + return err + } + + return nil +} + +func (ref *SMS) VerifySMSCode(number, smsCode string) error { + path := fmt.Sprintf("/1.1/verifySmsCode/%s", smsCode) + options := ref.c.getRequestOptions() + options.JSON = map[string]string{ + "mobilePhoneNumber": number, + } + + _, err := ref.c.request(methodPost, path, options) + if err != nil { + return err + } + + return nil +} diff --git a/leancloud/sms_test.go b/leancloud/sms_test.go new file mode 100644 index 0000000..d9541ca --- /dev/null +++ b/leancloud/sms_test.go @@ -0,0 +1,28 @@ +package leancloud + +import ( + "os" + "testing" +) + +func TestSMSCodeRequest(t *testing.T) { + client = NewEnvClient() + sms := &SMS{c} + mobile := os.Getenv("TEST_SMS_REQUEST_MOBILE") + err := sms.RequestSMSCode(mobile) + if err != nil { + t.Error(err) + t.Fail() + } +} + +func TestSMSCodeVerify(t *testing.T) { + client = NewEnvClient() + sms := &SMS{c} + mobile := os.Getenv("TEST_SMS_REQUEST_MOBILE") + code := os.Getenv("TEST_SMS_REQUEST_MOBILE_CODE") + err := sms.VerifySMSCode(mobile, code) + if err != nil { + t.Error(err) + } +} From 86a00e12171be7d22e88cd69004a1c61b56b7b72 Mon Sep 17 00:00:00 2001 From: shiweifu Date: Thu, 10 Jun 2021 20:52:06 +0800 Subject: [PATCH 2/2] add SMS field --- leancloud/client.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/leancloud/client.go b/leancloud/client.go index 27769a3..518ce7d 100644 --- a/leancloud/client.go +++ b/leancloud/client.go @@ -18,6 +18,7 @@ type Client struct { Users Users Files Files Roles Roles + SMS SMS } type ClientOptions struct { @@ -51,6 +52,7 @@ func NewClient(options *ClientOptions) *Client { client.Users.c = client client.Files.c = client client.Roles.c = client + client.SMS.c = client return client }