智盟项目
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

103 řádky
2.9 KiB

  1. package sms
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/model"
  5. "applet/app/lib/zhimeng"
  6. "applet/app/utils"
  7. "applet/app/utils/cache"
  8. "applet/app/utils/logx"
  9. "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/aliyun"
  10. "errors"
  11. "fmt"
  12. "github.com/tidwall/gjson"
  13. "math/rand"
  14. "time"
  15. "github.com/gin-gonic/gin"
  16. )
  17. // NewZhimengSMS is 智盟的短信服务
  18. func NewZhimengSMS(c *gin.Context) *zhimeng.SDK {
  19. sms := new(zhimeng.SDK)
  20. key := db.SysCfgGet(c, "third_zm_sms_key")
  21. secret := db.SysCfgGet(c, "third_zm_sms_secret")
  22. if key == "" || secret == "" {
  23. _ = logx.Warn("短信服务配置错误")
  24. }
  25. sms.Init("send_msg", key, secret)
  26. return sms
  27. }
  28. func GetSmsPlatform(c *gin.Context) string {
  29. var smsPlatform = "ljioe"
  30. key := fmt.Sprintf("%s:sms_platform", c.GetString("master_id"))
  31. smsPlatformTmp, _ := cache.GetString(key)
  32. if smsPlatformTmp == "" {
  33. smsPlatformTmp = GetWebSiteAppSmsPlatform(c.GetString("master_id"))
  34. if smsPlatformTmp != "" {
  35. cache.SetEx(key, smsPlatformTmp, 300)
  36. }
  37. }
  38. if smsPlatformTmp != "" {
  39. smsPlatform = smsPlatformTmp
  40. }
  41. return smsPlatform
  42. }
  43. func GetWebSiteAppSmsPlatform(mid string) string {
  44. obj := new(model.UserAppList)
  45. has, err := db.Db.Where("uuid=? ", mid).Asc("id").Get(obj)
  46. if err != nil || !has {
  47. return ""
  48. }
  49. return utils.AnyToString(obj.SmsPlatform)
  50. }
  51. func GetTplId(c *gin.Context, zone, types string) string {
  52. // 校验短信验证码
  53. tplId := ""
  54. if zone != "86" {
  55. tplId = db.SysCfgGet(c, "mob_sms_sdk_international_template_id")
  56. } else {
  57. tplId = db.SysCfgGet(c, "mob_sms_sdk_template_id")
  58. }
  59. if c.GetString("app_type") == "o2o" {
  60. tplId = db.SysCfgGet(c, "biz_mob_sms_sdk_template_id")
  61. }
  62. normal := gjson.Get(tplId, types).String()
  63. if normal == "" {
  64. normal = gjson.Get(tplId, "normal").String()
  65. }
  66. return normal
  67. }
  68. func GetSmsConfig(c *gin.Context, zone string, postData map[string]interface{}) error {
  69. postData["is_mob"] = "1"
  70. postData["type"] = "mob"
  71. postData["sms_type"] = "putong"
  72. zone = "86"
  73. postData["templateCode"] = "14373673"
  74. postData["zone"] = zone
  75. postData["smsmsg_key"] = "30dc33054b635"
  76. postData["uid"] = c.GetString("master_id")
  77. captcha := createCaptcha()
  78. data := AliyunSmsBase(c)
  79. err := aliyun.AliyunSendSmsOwn(data["aliyun_sms_id"], data["aliyun_sms_secret"], postData["mobile"].(string), data["aliyun_sms_sign_name"], data["aliyun_sms_code"], "{\"code\":\""+captcha+"\"}")
  80. if err != nil {
  81. return errors.New("发送失败")
  82. }
  83. cache.SetEx("zhimeng:sms."+c.GetString("master_id")+":"+postData["mobile"].(string), captcha, 300)
  84. return nil
  85. }
  86. func createCaptcha() string {
  87. return fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(10000))
  88. }
  89. func AliyunSmsBase(c *gin.Context) map[string]string {
  90. data := map[string]string{
  91. "aliyun_sms_id": "LTAI5tFzD9fUoYkp9SE6zcQb",
  92. "aliyun_sms_secret": "B8Jz7x2tn5YhHbba4vxmcdIufNtkAh",
  93. "aliyun_sms_code": "SMS_492445554",
  94. "aliyun_sms_sign_name": "广东智莺科技",
  95. }
  96. return data
  97. }