74 lines
1.8 KiB
Go
74 lines
1.8 KiB
Go
package test
|
||
|
||
import (
|
||
"crypto/md5"
|
||
"fmt"
|
||
"golang.org/x/crypto/bcrypt"
|
||
"math/rand"
|
||
"net/http"
|
||
"net/url"
|
||
"testing"
|
||
"time"
|
||
)
|
||
|
||
const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||
|
||
func TestRain(t *testing.T) {
|
||
num := GenerateOrderNum()
|
||
fmt.Println("生成的订单号:", num)
|
||
}
|
||
|
||
func GenerateOrderNum() string {
|
||
rand.Seed(time.Now().UnixNano())
|
||
// 拼接用户ID和随机数
|
||
data := fmt.Sprintf("%d%d", 6, rand.Intn(1000000))
|
||
hash := md5.Sum([]byte(data))
|
||
code := ""
|
||
for i := 0; i < 12; i++ {
|
||
// 取哈希的前6位,每位映射到字符集
|
||
code += string(charset[int(hash[i])%len(charset)])
|
||
}
|
||
return code
|
||
}
|
||
|
||
func TestPwd(t *testing.T) {
|
||
password, _ := bcrypt.GenerateFromPassword([]byte("loser7659"), bcrypt.DefaultCost)
|
||
fmt.Println(string(password))
|
||
|
||
err := bcrypt.CompareHashAndPassword(password, []byte("122456"))
|
||
if err != nil {
|
||
fmt.Println("密码错误")
|
||
} else {
|
||
fmt.Println("密码正确")
|
||
}
|
||
}
|
||
|
||
func TestCode(t *testing.T) {
|
||
rand.New(rand.NewSource(time.Now().UnixNano()))
|
||
verifyCode := fmt.Sprintf("%06v", rand.Int31n(1000000))
|
||
// 测试验证码生成
|
||
sendCode(verifyCode)
|
||
}
|
||
|
||
func sendCode(code string) {
|
||
// 内容 通过urlEncode编码
|
||
content := "【海口龙华铁坚成电子商务商行】您的验证码是" + code + "。如非本人操作,请忽略本短信"
|
||
// urlencode编码内容
|
||
content = url.QueryEscape(content)
|
||
|
||
api := "https://api.smsbao.com/sms?u=lchz5599&p=7ea114c87a224cd38a0d616b9be3faed&g=海口龙华铁坚成电子商务商行&m=17754945397&c=" + content
|
||
|
||
// 发送GET请求
|
||
resp, err := http.Get(api)
|
||
if err != nil {
|
||
fmt.Println("请求失败:", err)
|
||
return
|
||
}
|
||
defer resp.Body.Close()
|
||
if resp.StatusCode == http.StatusOK {
|
||
fmt.Println("请求成功,短信发送成功")
|
||
} else {
|
||
fmt.Println("请求失败,状态码:", resp.StatusCode)
|
||
}
|
||
}
|