|
- package svc
-
- import (
- "applet/app/e"
- "applet/app/utils/cache"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/aliyun"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/mob"
- "errors"
- "fmt"
- "github.com/gin-gonic/gin"
- "math/rand"
- "time"
- )
-
- func createCaptcha() string {
- return fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(10000))
- }
-
- func CommSmsSend(c *gin.Context, mobile, types string) error {
- smsPlatform := GetSysCfgStr("sms_platform")
- var err error
- if smsPlatform == "mob" {
- err = mob.MobSms(GetSysCfgStr("mob_key"), mobile, GetSysCfgStr("mob_temple_code"))
- if err != nil {
- fmt.Println("短信错误:" + err.Error())
- return err
- }
- } else if smsPlatform == "aliyun_own" {
- data := AliyunSmsBase(c, types)
- captcha := createCaptcha()
- // 2、加锁 防止并发提取
- mutexKey := fmt.Sprintf("sms_check:%s", mobile)
- withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 60, "NX")
- if err != nil {
- return e.NewErr(400000, "请1分钟后再试")
- }
- if withdrawAvailable != "OK" {
- return e.NewErr(400000, "请1分钟后再试")
- }
- err = aliyun.AliyunSendSmsOwn(data["aliyun_sms_id"], data["aliyun_sms_secret"], mobile, data["aliyun_sms_sign_name"], data["aliyun_sms_code"], "{\"code\":\""+captcha+"\"}")
- if err != nil {
- return e.NewErr(400, "验证码错误,请重试")
- }
- cache.SetEx("sms:"+mobile, captcha, 300)
- } else {
- data := AliyunSmsBase(c, types)
- //发送短信
- err = aliyun.AliyunSendSms(data["aliyun_sms_id"], data["aliyun_sms_secret"], mobile, data["aliyun_sms_sign_name"], data["aliyun_sms_code"], "")
- if err != nil {
- fmt.Println("短信错误:" + err.Error())
- return errors.New("发送失败")
- }
- }
- return nil
- }
-
- func CommSmsCheck(c *gin.Context, mobile, code string) error {
- //校验短信
- smsPlatform := GetSysCfgStr("sms_platform")
- var err error
- var bools bool
- if smsPlatform == "mob" {
- bools, err = mob.MobSMSCheck(GetSysCfgStr("mob_key"), mobile, code)
- if err != nil {
- fmt.Println("短信错误:" + err.Error())
- return err
- }
- if bools == false {
- return e.NewErr(400, "验证码错误,请重试")
- }
- } else if smsPlatform == "aliyun_own" {
- captcha, _ := cache.GetString("sms:" + mobile)
- if captcha != code || code == "" {
- return e.NewErr(400, "验证码错误,请重试")
- }
- cache.Del("sms:" + mobile)
- } else {
- data := AliyunSmsBase(c, "")
- err = aliyun.AliyunCheckSms(data["aliyun_sms_id"], data["aliyun_sms_secret"], mobile, code)
- if err != nil {
- return e.NewErr(400, "验证码错误,请重试")
- }
- }
- return nil
- }
|