-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2022 The Casdoor Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package go_sms_sender | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
type SmsBaoClient struct { | ||
username string | ||
apikey string | ||
sign string | ||
template string | ||
goodsid string | ||
} | ||
|
||
func GetSmsbaoClient(username string, apikey string, sign string, template string, other []string) (*SmsBaoClient, error) { | ||
var goodsid string | ||
if len(other) < 1 { | ||
goodsid = "" | ||
} else { | ||
goodsid = other[0] | ||
} | ||
return &SmsBaoClient{ | ||
username: username, | ||
apikey: apikey, | ||
sign: sign, | ||
template: template, | ||
goodsid: goodsid, | ||
}, nil | ||
} | ||
|
||
func (c *SmsBaoClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error { | ||
code, ok := param["code"] | ||
if !ok { | ||
return fmt.Errorf("missing parameter: msg code") | ||
} | ||
|
||
if len(targetPhoneNumber) < 1 { | ||
return fmt.Errorf("missing parameter: targetPhoneNumber") | ||
} | ||
|
||
smsContent := url.QueryEscape("【" + c.sign + "】" + fmt.Sprintf(c.template, code)) | ||
for _, mobile := range targetPhoneNumber { | ||
// https://api.smsbao.com/sms?u=USERNAME&p=PASSWORD&g=GOODSID&m=PHONE&c=CONTENT | ||
url := fmt.Sprintf("https://api.smsbao.com/sms?u=%s&p=%s&g=%s&m=%s&c=%s", c.username, c.apikey, c.goodsid, mobile, smsContent) | ||
|
||
client := &http.Client{} | ||
req, _ := http.NewRequest("GET", url, nil) | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return err | ||
} | ||
switch string(body) { | ||
case "30": | ||
return fmt.Errorf("password error") | ||
case "40": | ||
return fmt.Errorf("account not exist") | ||
case "41": | ||
return fmt.Errorf("overdue account") | ||
case "43": | ||
return fmt.Errorf("IP address limit") | ||
case "50": | ||
return fmt.Errorf("content contain forbidden words") | ||
case "51": | ||
return fmt.Errorf("phone number incorrect") | ||
} | ||
} | ||
|
||
return nil | ||
} |