| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- package utils
- import (
- "git.qianqiusoft.com/qianqiusoft/light-apiengine/config"
- "git.qianqiusoft.com/qianqiusoft/light-apiengine/logs"
- "github.com/dchest/captcha"
- "errors"
- "bytes"
- "time"
- "fmt"
- )
- func init() {
- imageCaptchaConfig.length = captcha.DefaultLen
- imageCaptchaConfig.collect_num = captcha.CollectNum
- imageCaptchaConfig.expiration = captcha.Expiration
- sms_url := config.AppConfig.GetKey("sms_url")
- sms_tmpl := config.AppConfig.GetKey("sms_tmpl")
- smsConfig.send_format = fmt.Sprintf("%s/send?mobile=%%s&name=%s", sms_url, sms_tmpl)
- smsConfig.check_format = fmt.Sprintf("%s/check?mobile=%%s&code=%%s", sms_url)
- }
- var imageCaptchaConfig struct {
- // 验证码长度
- length int
- // 验证码缓存数量
- collect_num int
- // 验证码超时时间
- expiration time.Duration
- }
- // @Description 生成图片验证码
- // @Params width int 生成图片的宽高
- // @Return id string 验证码id
- // @Return image *bytes.Buffer 图片buffer
- // @Return err error 错误
- func GenerateImageCaptcha(width, height, length int) (string, *bytes.Buffer, error) {
- var captchaId string
- var image bytes.Buffer
- if width <= 0 {
- width = 240
- }
- if height <= 0 {
- height = 80
- }
- if length <= 0 {
- length = imageCaptchaConfig.length
- }
- captchaId = captcha.NewLen(length)
- err := captcha.WriteImage(&image, captchaId, width, height)
- return captchaId, &image, err
- }
- // @Description 图片验证码校验
- // @Param code string 验证码
- // @Param id 验证码id 手机号码
- // @Return result bool 验证结果 true 成功 false 失败
- func ImageCaptchaCheck(code, id string) bool {
- if code == "" || id == "" {
- return false
- }
- return captcha.VerifyString(id, code)
- }
- // @Description 设置图片验证码长度
- // @Param length int 验证码长度
- func SetImageCaptchaLength(length int) {
- if length != imageCaptchaConfig.length {
- imageCaptchaConfig.length = length
- }
- }
- // @Description 设置图片验证码超时时间
- // @Param expiration time.Duration 超时时间
- func SetImageCaptchaExpiration(expiration time.Duration) {
- if expiration != imageCaptchaConfig.expiration {
- imageCaptchaConfig.expiration = expiration
- memoryStore := captcha.NewMemoryStore(imageCaptchaConfig.collect_num, expiration)
- captcha.SetCustomStore(memoryStore)
- }
- }
- // @Description 设置图片验证码缓存数量
- // @Param collect_num int 缓存数量
- func SetImageCaptchaCollectNum(collect_num int) {
- if collect_num != imageCaptchaConfig.collect_num {
- imageCaptchaConfig.collect_num = collect_num
- memoryStore := captcha.NewMemoryStore(collect_num, imageCaptchaConfig.expiration)
- captcha.SetCustomStore(memoryStore)
- }
- }
- var smsConfig struct {
- send_format string
- check_format string
- }
- // @Description 发送短信验证码
- // @Param mobile string 手机号码
- // @Return data []byte 发送结果
- // @Return err error 错误
- func SendSmsCaptcha(mobile string) ([]byte, error) {
- if mobile == "" {
- return nil, errors.New("mobile is cannot empty")
- }
-
- sms_url := fmt.Sprintf(smsConfig.send_format, mobile)
- fmt.Println("------>sms_url", sms_url)
- data, err := NewHttpUtil().Get(sms_url, nil, nil)
- logs.Debug(string(data))
- if err != nil {
- logs.Error("send sms captcha failure:", err)
- }
- return data, err
- }
- // @Description 短信验证码校验
- // @Param captcha string 验证码
- // @Param mobile string 手机号码
- // @Return result bool 验证结果 true 成功 false 失败
- func SmsCaptchaCheck(captcha, mobile string) bool {
- if captcha == "" || mobile == "" {
- return false
- }
- sms_url := fmt.Sprintf(smsConfig.check_format, mobile, captcha)
- logs.Debug("-----sms_check_url-------")
- logs.Debug(sms_url)
- data, err := NewHttpUtil().Get(sms_url, nil, nil)
- return err == nil && string(data) != "0"
- }
|