123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- package service
- import (
- "errors"
- "fmt"
- "math/rand"
- "strings"
- "time"
- "xiaoniaokuaiyan.com/xiaoniao/config"
- "xiaoniaokuaiyan.com/xiaoniao/dal"
- "xiaoniaokuaiyan.com/xiaoniao/entity"
- "xiaoniaokuaiyan.com/xiaoniao/util"
- )
- type SMSService struct {
- dal.ISMSCode
- }
- func generateValidateCode(length int) string {
- var (
- chars = "0123456789"
- rnum int
- vcode string
- )
- for i := 0; i < length; i++ {
- rnum = rand.Intn(10)
- vcode += chars[rnum : rnum+1]
- }
- return vcode
- }
- func (srv *SMSService) ValidateCode(mobile string, code string, codeType int) (interface{}, error) {
- if v := util.IsMobile(mobile); !v {
- return nil, errors.New("wrong mobile")
- }
- codeItem, err := srv.ISMSCode.Get(mobile, code, codeType)
- if err != nil {
- return nil, err
- }
- if codeItem.Code == "" {
- return nil, errors.New("can not find code")
- }
- go func() {
- db := util.GetWriteSqlDB()
- db.Exec("update t_sms_code set is_used = 1 where mobile=? and code_type = ? and code=?", mobile, codeType, code)
- if codeType == 3 {
- usrv := &UserService{
- IUser: dal.DefaultUserDal,
- }
- usrv._registUser(mobile, false)
- }
- }()
- return codeItem, nil
- }
- func (srv *SMSService) SendSMS(mobile string, smsType int, params map[string]string) (interface{}, error) {
- var limit, _ = config.IniConf.Section("server").Key("black_limit").Int64()
- db := util.GetSqlDB()
- var count int
- db.Get(&count, "select count(1) from t_black_mobile where mobile = ?", mobile)
- if count > 0 {
- return false, nil
- }
- c := util.GetRedis()
- c.Select(14)
- key := "BLACK:" + mobile
- value := c.Get(key)
- if value.Val() == "" {
- t := time.Now()
- midnight := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()).AddDate(0, 0, 1)
- set := c.SetEx(key, midnight.Sub(t), "1")
- _ = set
- } else {
- incrBy := c.Incr(key)
- if incrBy.Val() >= limit {
- db.Exec("insert into t_black_mobile (mobile) values(?)", mobile)
- return false, nil
- }
- }
- strSql := "select * from t_sms_tpl where id = ?;"
-
- var smsTpl = struct {
- Content string `db:"content"`
- Id int `db:"id"`
- HasCode uint8 `db:"has_code"`
- CodeType int `db:"code_type"`
- }{}
- db.Get(&smsTpl, strSql, smsType)
- if smsTpl.Content == "" {
- return false, nil
- }
- if params == nil {
- params = map[string]string{}
- }
- if smsTpl.HasCode > 0 {
- vcode := generateValidateCode(6)
- params["code"] = vcode
-
- codeItem := &entity.SMSCode{
- Code: vcode,
- Mobile: mobile,
- CodeType: smsTpl.CodeType,
- CreatedAt: time.Now().Format("2006-01-02 15:04:05"),
- }
- _, err := srv.ISMSCode.Add(codeItem)
- if err != nil {
- return nil, err
- }
- }
- fmt.Println(smsTpl.Content, params)
- var smsContent string = smsTpl.Content
- for k, v := range params {
- smsContent = strings.Replace(smsContent, "{"+k+"}", v, -1)
- }
- err := util.SendSMS(mobile, smsContent)
- if err != nil {
- return false, err
- }
- strSql = "select is_accept_up from t_custom where mobile = ?;"
- var isAcceptUP string
- db.Get(&isAcceptUP, strSql, mobile)
- return map[string]interface{}{
- "sms_result": true,
- "is_accept_proto": isAcceptUP,
- }, nil
- }
- func (srv *SMSService) SendWorldSMS(mobile string, smsType int, params map[string]string) (interface{}, error) {
- strSql := "select * from t_sms_tpl where id = ?;"
- db := util.GetSqlDB()
- var smsTpl = struct {
- Content string `db:"content"`
- Id int `db:"id"`
- HasCode uint8 `db:"has_code"`
- CodeType int `db:"code_type"`
- }{}
- db.Get(&smsTpl, strSql, smsType)
- if smsTpl.Content == "" {
- return false, nil
- }
- if params == nil {
- params = map[string]string{}
- }
- if smsTpl.HasCode > 0 {
- vcode := generateValidateCode(6)
- params["code"] = vcode
-
- codeItem := &entity.SMSCode{
- Code: vcode,
- Mobile: mobile,
- CodeType: smsTpl.CodeType,
- CreatedAt: time.Now().Format("2006-01-02 15:04:05"),
- }
- _, err := srv.ISMSCode.Add(codeItem)
- if err != nil {
- return nil, err
- }
- }
- fmt.Println(smsTpl.Content, params)
- var smsContent string = smsTpl.Content
- for k, v := range params {
- smsContent = strings.Replace(smsContent, "{"+k+"}", v, -1)
- }
- err := util.SendWorldSMS(mobile, smsContent)
- if err != nil {
- return false, err
- }
- return true, nil
- }
|