smscode_service.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "math/rand"
  6. "strings"
  7. "time"
  8. "xiaoniaokuaiyan.com/xiaoniao/config"
  9. "xiaoniaokuaiyan.com/xiaoniao/dal"
  10. "xiaoniaokuaiyan.com/xiaoniao/entity"
  11. "xiaoniaokuaiyan.com/xiaoniao/util"
  12. )
  13. type SMSService struct {
  14. dal.ISMSCode
  15. }
  16. func generateValidateCode(length int) string {
  17. var (
  18. chars = "0123456789"
  19. rnum int
  20. vcode string
  21. )
  22. for i := 0; i < length; i++ {
  23. rnum = rand.Intn(10)
  24. vcode += chars[rnum : rnum+1]
  25. }
  26. return vcode
  27. }
  28. /*func (srv *SMSService) SendSMS(mobile string, smsType int, params map[string]string) (rel interface{}, err error) {
  29. if ok := util.IsMobile(mobile); !ok {
  30. return nil, errors.New("手机号错误")
  31. }
  32. ctype := constants.SMSType(smsType)
  33. var (
  34. smsContent string
  35. codeType int
  36. hasCode bool
  37. )
  38. switch ctype {
  39. case constants.SMS_LOGIN:
  40. smsContent = constants.SMSCONTENT_LOGIN
  41. codeType = int(constants.SMSCODE_LOGIN)
  42. hasCode = true
  43. case constants.SMS_CHECKMOBILE:
  44. smsContent = constants.SMSCONTENT_CHECKMOBILE
  45. codeType = int(constants.SMSCODE_CHECKMOBILE)
  46. hasCode = true
  47. case constants.SMS_REGIST:
  48. smsContent = constants.SMSCONTENT_REGIST
  49. codeType = int(constants.SMSCODE_REGIST)
  50. hasCode = true
  51. case constants.SMS_FORGOT_PASSWORD:
  52. smsContent = constants.SMSCONTENT_FORGOT_PASSWORD
  53. codeType = int(constants.SMSCODE_FORGOT_PASSWORD)
  54. hasCode = true
  55. case constants.SMS_CHECK_RESULT:
  56. smsContent = constants.SMSCONTENT_CHECKRESULT
  57. codeType = int(constants.SMSCODE_CHECK_RESULT)
  58. hasCode = true
  59. case constants.SMS_ORDER_PAID_INFORM, constants.SMS_ORDER_PAID_INFORM_MBH:
  60. smsContent = constants.SMSCONTENT_PAYSUCCESS
  61. if ctype == constants.SMS_ORDER_PAID_INFORM_MBH {
  62. smsContent = constants.SMSCONTENT_MBH_PAYSUCCESS
  63. }
  64. case constants.SMS_JDBOOK:
  65. smsContent = constants.SMSCONTENT_JDBOOK_SUCCESS
  66. case constants.SMS_ORDER_PAID_INFORM_JIYIN:
  67. smsContent = constants.SMSCONTENT_JIYIN_PAYSUCCESS
  68. case constants.SMS_RESULT_TURNOUT:
  69. smsContent = constants.SMSCONTENT_RESULT_TURNOUT
  70. codeType = int(constants.SMSCODE_CHECK_RESULT)
  71. hasCode = true
  72. case constants.SMS_HOUSEKEEPING_QUERY:
  73. smsContent = constants.SMSCONTENT_HOUSEKEEPING
  74. codeType = int(constants.SMSCODE_HOUSEKEEPING_QUERY)
  75. hasCode = true
  76. case constants.SMS_ACT_YOUKE_LOGIN:
  77. smsContent = constants.SMSCONTENT_ACT_YOUKE_LOGIN
  78. codeType = int(constants.SMSCODE_ACT_YOUKE_LOGIN)
  79. hasCode = true
  80. default:
  81. return nil, errors.New("wrong code type")
  82. }
  83. if params == nil {
  84. params = map[string]string{}
  85. }
  86. if hasCode {
  87. vcode := generateValidateCode(6)
  88. params["code"] = vcode
  89. //smsContent = strings.Replace(smsContent, "{code}", vcode, 1)
  90. codeItem := &entity.SMSCode{
  91. Code: vcode,
  92. Mobile: mobile,
  93. CodeType: codeType,
  94. CreatedAt: time.Now().Format("2006-01-02 15:04:05"),
  95. }
  96. _, err = srv.ISMSCode.Add(codeItem)
  97. if err != nil {
  98. return
  99. }
  100. }
  101. for k, v := range params {
  102. smsContent = strings.Replace(smsContent, "{"+k+"}", v, 1)
  103. }
  104. err = util.SendSMS(mobile, smsContent)
  105. return rel, err
  106. }*/
  107. func (srv *SMSService) ValidateCode(mobile string, code string, codeType int) (interface{}, error) {
  108. if v := util.IsMobile(mobile); !v {
  109. return nil, errors.New("wrong mobile")
  110. }
  111. codeItem, err := srv.ISMSCode.Get(mobile, code, codeType)
  112. if err != nil {
  113. return nil, err
  114. }
  115. if codeItem.Code == "" {
  116. return nil, errors.New("can not find code")
  117. }
  118. go func() {
  119. db := util.GetWriteSqlDB()
  120. db.Exec("update t_sms_code set is_used = 1 where mobile=? and code_type = ? and code=?", mobile, codeType, code)
  121. if codeType == 3 {
  122. usrv := &UserService{
  123. IUser: dal.DefaultUserDal,
  124. }
  125. usrv._registUser(mobile, false)
  126. }
  127. }()
  128. return codeItem, nil
  129. }
  130. func (srv *SMSService) SendSMS(mobile string, smsType int, params map[string]string) (interface{}, error) {
  131. var limit, _ = config.IniConf.Section("server").Key("black_limit").Int64()
  132. db := util.GetSqlDB()
  133. var count int
  134. db.Get(&count, "select count(1) from t_black_mobile where mobile = ?", mobile)
  135. if count > 0 {
  136. return false, nil
  137. }
  138. c := util.GetRedis()
  139. c.Select(14)
  140. key := "BLACK:" + mobile
  141. value := c.Get(key)
  142. if value.Val() == "" {
  143. t := time.Now()
  144. midnight := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()).AddDate(0, 0, 1)
  145. set := c.SetEx(key, midnight.Sub(t), "1")
  146. _ = set
  147. } else {
  148. incrBy := c.Incr(key)
  149. if incrBy.Val() >= limit {
  150. db.Exec("insert into t_black_mobile (mobile) values(?)", mobile)
  151. return false, nil
  152. }
  153. }
  154. strSql := "select * from t_sms_tpl where id = ?;"
  155. //db := util.GetSqlDB()
  156. var smsTpl = struct {
  157. Content string `db:"content"`
  158. Id int `db:"id"`
  159. HasCode uint8 `db:"has_code"`
  160. CodeType int `db:"code_type"`
  161. }{}
  162. db.Get(&smsTpl, strSql, smsType)
  163. if smsTpl.Content == "" {
  164. return false, nil
  165. }
  166. if params == nil {
  167. params = map[string]string{}
  168. }
  169. if smsTpl.HasCode > 0 {
  170. vcode := generateValidateCode(6)
  171. params["code"] = vcode
  172. //smsContent = strings.Replace(smsContent, "{code}", vcode, 1)
  173. codeItem := &entity.SMSCode{
  174. Code: vcode,
  175. Mobile: mobile,
  176. CodeType: smsTpl.CodeType,
  177. CreatedAt: time.Now().Format("2006-01-02 15:04:05"),
  178. }
  179. _, err := srv.ISMSCode.Add(codeItem)
  180. if err != nil {
  181. return nil, err
  182. }
  183. }
  184. fmt.Println(smsTpl.Content, params)
  185. var smsContent string = smsTpl.Content
  186. for k, v := range params {
  187. smsContent = strings.Replace(smsContent, "{"+k+"}", v, -1)
  188. }
  189. err := util.SendSMS(mobile, smsContent)
  190. if err != nil {
  191. return false, err
  192. }
  193. strSql = "select is_accept_up from t_custom where mobile = ?;"
  194. var isAcceptUP string
  195. db.Get(&isAcceptUP, strSql, mobile)
  196. return map[string]interface{}{
  197. "sms_result": true,
  198. "is_accept_proto": isAcceptUP,
  199. }, nil
  200. }
  201. func (srv *SMSService) SendWorldSMS(mobile string, smsType int, params map[string]string) (interface{}, error) {
  202. strSql := "select * from t_sms_tpl where id = ?;"
  203. db := util.GetSqlDB()
  204. var smsTpl = struct {
  205. Content string `db:"content"`
  206. Id int `db:"id"`
  207. HasCode uint8 `db:"has_code"`
  208. CodeType int `db:"code_type"`
  209. }{}
  210. db.Get(&smsTpl, strSql, smsType)
  211. if smsTpl.Content == "" {
  212. return false, nil
  213. }
  214. if params == nil {
  215. params = map[string]string{}
  216. }
  217. if smsTpl.HasCode > 0 {
  218. vcode := generateValidateCode(6)
  219. params["code"] = vcode
  220. //smsContent = strings.Replace(smsContent, "{code}", vcode, 1)
  221. codeItem := &entity.SMSCode{
  222. Code: vcode,
  223. Mobile: mobile,
  224. CodeType: smsTpl.CodeType,
  225. CreatedAt: time.Now().Format("2006-01-02 15:04:05"),
  226. }
  227. _, err := srv.ISMSCode.Add(codeItem)
  228. if err != nil {
  229. return nil, err
  230. }
  231. }
  232. fmt.Println(smsTpl.Content, params)
  233. var smsContent string = smsTpl.Content
  234. for k, v := range params {
  235. smsContent = strings.Replace(smsContent, "{"+k+"}", v, -1)
  236. }
  237. err := util.SendWorldSMS(mobile, smsContent)
  238. if err != nil {
  239. return false, err
  240. }
  241. return true, nil
  242. }