user_tel.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package gatesink
  2. import (
  3. "bet24.com/servers/transaction"
  4. "encoding/json"
  5. "fmt"
  6. "math/rand"
  7. "strings"
  8. "bet24.com/log"
  9. "bet24.com/public"
  10. "bet24.com/redis"
  11. "bet24.com/servers/fishhall/config"
  12. )
  13. const (
  14. _ = iota // 无效值
  15. SMSCODE_TYPE_Bind // 1=手机号绑定
  16. )
  17. // 发送手机验证码
  18. func (this *user) sendSms(msg, data string) {
  19. obj := transaction.NewTransExistTel()
  20. if err := json.Unmarshal([]byte(data), &obj.In); err != nil {
  21. retData := fmt.Sprintf("sendSms Unmarshal data failed %v", data)
  22. log.Error(retData)
  23. this.WriteMsg(msg, retData)
  24. return
  25. }
  26. // 去掉空格
  27. obj.In.Tel = strings.ReplaceAll(obj.In.Tel, " ", "")
  28. obj.In.Tel = strings.Replace(obj.In.Tel, "00620", "0062", 1)
  29. // 校验手机号
  30. validTel, err := public.CheckTelValid(obj.In.Tel)
  31. if err != nil {
  32. log.Debug("sendSms tel valid err:%v", err)
  33. this.WriteMsg(msg, "12")
  34. return
  35. }
  36. obj.DoAction(nil)
  37. // 手机号已存在
  38. if obj.Out.RetCode == 1 {
  39. log.Debug("sendSms tel is exist ==> userId=%d obj.In=%+v", this.getUserId(), obj.In)
  40. this.WriteMsg(msg, "11")
  41. return
  42. }
  43. // 获取验证码
  44. value := this.getSmsCode(validTel, obj.In.CodeType)
  45. // 没有验证,需要生成
  46. if len(value) <= 0 {
  47. // 缓存验证码(6位验证码)
  48. key := fmt.Sprintf("smsCode:%s:%d", validTel, obj.In.CodeType)
  49. value = this.GenCode(6)
  50. if !redis.String_SetEx(key, value, 300) {
  51. log.Release("send SmsCode redis failed ==> userId=%d key=%s value=%s", this.getUserId(), key, value)
  52. this.WriteMsg(msg, "13")
  53. return
  54. }
  55. }
  56. // 默认短信内容
  57. sendMsg := config.HallConfig.SMSSendMsg
  58. // 向短信平台发送 Post 请求,并获取响应数据
  59. go func(SMSSendMsg string) {
  60. body, err := public.HttpSMSCode(validTel,
  61. config.HallConfig.SMSConfig.SMSSendMsg,
  62. config.HallConfig.SMSConfig.SMSAccessID+config.HallConfig.SMSConfig.SMSAccessSecrect,
  63. config.HallConfig.SMSConfig.SMSPostUrl,
  64. value)
  65. if err != nil {
  66. log.Error("HttpSMSCode %s", err)
  67. return
  68. }
  69. log.Debug("httpSMSCode ==> %s", body)
  70. }(sendMsg)
  71. this.WriteMsg(msg, "1")
  72. }
  73. // 校验验证码==>返回 isSuccess、isTest、isVerify
  74. func (this *user) checkCode(tel string, code string, smsCodeType int) (bool, int, int) {
  75. //为空信息,直接返回
  76. if tel == "" || code == "" {
  77. log.Debug("checkCode tel=%s code=%s", tel, code)
  78. return false, 0, 0
  79. }
  80. //注册,且是万能验证码 000000
  81. if smsCodeType == SMSCODE_TYPE_Bind && code == "000000" {
  82. return true, 0, 0
  83. }
  84. //内部码(只能用于非提现部分,即实名信息)
  85. if code == config.HallConfig.SMSConfig.SMSTestCode {
  86. return true, 1, 1
  87. }
  88. return code == this.getSmsCode(tel, smsCodeType), 0, 1
  89. }
  90. // 获取验证码
  91. func (this *user) getSmsCode(tel string, smsCodeType int) string {
  92. key := fmt.Sprintf("smsCode:%s:%d", tel, smsCodeType)
  93. value, succ := redis.String_Get(key)
  94. if !succ {
  95. log.Debug("get SmsCode redis failed")
  96. return ""
  97. }
  98. return value
  99. }
  100. // 生成短信验证码
  101. func (this *user) GenCode(length int) string {
  102. // var chars = []byte("ABCDEFGHJKLMNPQRTUVWXYZ123456789")
  103. var chars = []byte("0123456789")
  104. code := make([]byte, length)
  105. clen := len(chars)
  106. for i := 0; i < length; i++ {
  107. index := rand.Intn(clen)
  108. code[i] = byte(chars[index])
  109. }
  110. if string(code) == config.HallConfig.SMSConfig.SMSTestCode {
  111. code[0] = 0
  112. }
  113. return string(code)
  114. }