蛋蛋星球-客户端
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

svc_sms.go 2.5 KiB

6日前
6日前
6日前
6日前
6日前
6日前
6日前
6日前
6日前
6日前
6日前
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package svc
  2. import (
  3. "applet/app/e"
  4. "applet/app/utils/cache"
  5. "code.fnuoos.com/EggPlanet/egg_system_rules.git/aliyun"
  6. "code.fnuoos.com/EggPlanet/egg_system_rules.git/mob"
  7. "errors"
  8. "fmt"
  9. "github.com/gin-gonic/gin"
  10. "math/rand"
  11. "time"
  12. )
  13. func createCaptcha() string {
  14. return fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(10000))
  15. }
  16. func CommSmsSend(c *gin.Context, mobile, types string) error {
  17. smsPlatform := GetSysCfgStr("sms_platform")
  18. var err error
  19. if smsPlatform == "mob" {
  20. err = mob.MobSms(GetSysCfgStr("mob_key"), mobile, GetSysCfgStr("mob_temple_code"))
  21. if err != nil {
  22. fmt.Println("短信错误:" + err.Error())
  23. return err
  24. }
  25. } else if smsPlatform == "aliyun_own" {
  26. data := AliyunSmsBase(c, types)
  27. captcha := createCaptcha()
  28. // 2、加锁 防止并发提取
  29. mutexKey := fmt.Sprintf("sms_check:%s", mobile)
  30. withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 60, "NX")
  31. if err != nil {
  32. return e.NewErr(400000, "请1分钟后再试")
  33. }
  34. if withdrawAvailable != "OK" {
  35. return e.NewErr(400000, "请1分钟后再试")
  36. }
  37. err = aliyun.AliyunSendSmsOwn(data["aliyun_sms_id"], data["aliyun_sms_secret"], mobile, data["aliyun_sms_sign_name"], data["aliyun_sms_code"], "{\"code\":\""+captcha+"\"}")
  38. if err != nil {
  39. return e.NewErr(400, "验证码错误,请重试")
  40. }
  41. cache.SetEx("sms:"+mobile, captcha, 300)
  42. } else {
  43. data := AliyunSmsBase(c, types)
  44. //发送短信
  45. err = aliyun.AliyunSendSms(data["aliyun_sms_id"], data["aliyun_sms_secret"], mobile, data["aliyun_sms_sign_name"], data["aliyun_sms_code"], "")
  46. if err != nil {
  47. fmt.Println("短信错误:" + err.Error())
  48. return errors.New("发送失败")
  49. }
  50. }
  51. return nil
  52. }
  53. func CommSmsCheck(c *gin.Context, mobile, code string) error {
  54. //校验短信
  55. smsPlatform := GetSysCfgStr("sms_platform")
  56. var err error
  57. var bools bool
  58. if smsPlatform == "mob" {
  59. bools, err = mob.MobSMSCheck(GetSysCfgStr("mob_key"), mobile, code)
  60. if err != nil {
  61. fmt.Println("短信错误:" + err.Error())
  62. return err
  63. }
  64. if bools == false {
  65. return e.NewErr(400, "验证码错误,请重试")
  66. }
  67. } else if smsPlatform == "aliyun_own" {
  68. captcha, _ := cache.GetString("sms:" + mobile)
  69. if captcha != code || code == "" {
  70. return e.NewErr(400, "验证码错误,请重试")
  71. }
  72. cache.Del("sms:" + mobile)
  73. } else {
  74. data := AliyunSmsBase(c, "")
  75. err = aliyun.AliyunCheckSms(data["aliyun_sms_id"], data["aliyun_sms_secret"], mobile, code)
  76. if err != nil {
  77. return e.NewErr(400, "验证码错误,请重试")
  78. }
  79. }
  80. return nil
  81. }