123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- package share
- import (
- qu "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/redis"
- "app.yhyue.com/moapp/message/config"
- "app.yhyue.com/moapp/message/db"
- "bytes"
- cr "crypto/rand"
- "math/big"
- "math/rand"
- "regexp"
- "strings"
- "sync"
- "time"
- )
- var PhoneReg = regexp.MustCompile("^[1][3-9][0-9]{9}$")
- //获取用户头像,手机号
- func GetInfo(userid string) map[string]interface{} {
- rdata, ok := db.Mgo.FindById("user", userid, `{"s_headimageurl":1,"s_headimage":1,"s_phone":1,"s_m_phone":1,"s_nickname":1,"l_registedate":1}`)
- if ok && len(*rdata) > 0 && rdata != nil {
- nickname, headimageurl := "", ""
- if s_phone, _ := (*rdata)["s_phone"].(string); s_phone != "" {
- nickname = s_phone
- } else if s_m_phone, _ := (*rdata)["s_m_phone"].(string); s_m_phone != "" {
- nickname = s_m_phone
- } else if s_nickname, _ := (*rdata)["s_nickname"].(string); s_nickname != "" {
- nickname = s_nickname
- }
- if IsPhone(nickname) {
- nickname = string(nickname[0:3]) + "****" + string(nickname[len(nickname)-4:])
- }
- if s_headimageurl, _ := (*rdata)["s_headimageurl"].(string); s_headimageurl != "" {
- headimageurl = s_headimageurl
- } else if s_headimage, _ := (*rdata)["s_headimage"].(string); s_headimage != "" {
- headimageurl = config.PushConfig.Webdomain + s_headimage
- }
- return map[string]interface{}{
- "nickname": nickname,
- "headimageurl": headimageurl,
- "createtime": (*rdata)["l_registedate"],
- }
- }
- return nil
- }
- //手机号校验
- func IsPhone(phone string) bool {
- return PhoneReg.MatchString(phone)
- }
- //到期时间
- func timeOut() int {
- now := time.Now()
- return int(now.AddDate(0, 0, 7).Unix() - now.Unix())
- }
- type UserInfo struct {
- HeadImg interface{} `json:"headimg"`
- Name interface{} `json:"name"`
- Createtime interface{} `json:"createtime"`
- }
- func Info(data *[]map[string]interface{}) []*UserInfo {
- userinfo := []*UserInfo{}
- if len(*data) > 0 {
- for _, v := range *data {
- sharedId := qu.ObjToString(v["shared_uid"])
- if rdata := GetInfo(sharedId); rdata != nil {
- userinfo = append(userinfo, &UserInfo{
- HeadImg: rdata["headimageurl"],
- Name: rdata["nickname"],
- Createtime: rdata["createtime"],
- })
- }
- }
- }
- return userinfo
- }
- var VarLSCPool = &LSCPool{
- JobQueue: make(chan string, 10),
- WorkerNum: 10,
- Lock: &sync.Mutex{},
- Randoms: "",
- }
- type LSCPool struct {
- JobQueue chan string //待取的口令
- WorkerNum int //当前工作的协程数
- Lock *sync.Mutex //口令锁
- Randoms string //口令集合
- }
- func (this *LSCPool) GetJob() string {
- return <-this.JobQueue
- }
- var r *rand.Rand
- func init() {
- r = rand.New(rand.NewSource(time.Now().Unix()))
- for i := 0; i < VarLSCPool.WorkerNum/2; i++ {
- go func() {
- for {
- VarLSCPool.JobQueue <- VarLSCPool.GetRandom()
- }
- }()
- }
- }
- //获取随机字符串
- func (this *LSCPool) GetRandom() string {
- this.Lock.Lock()
- defer this.Lock.Unlock()
- //如果使用过了 重新获取
- for {
- LSC := this.SpecialChar(0) + this.LetterRandom(8)
- if strings.Contains(this.Randoms, LSC) {
- continue
- }
- this.Randoms += LSC + "|"
- ok, _ := redis.Exists("other", "shareId_"+LSC)
- if ok {
- continue
- }
- return LSC
- }
- return ""
- }
- //字母随机串
- func (this *LSCPool) LetterRandom(LL int) (LR string) {
- bytes := make([]byte, LL)
- for i := 0; i < LL; i++ {
- b := r.Intn(26) + 65
- bytes[i] = byte(b)
- }
- LR = string(bytes)
- return
- }
- //特殊字符随机串
- func (this *LSCPool) SpecialChar(LL int) (SC string) {
- var str = "$#"
- b := bytes.NewBufferString(str)
- length := b.Len()
- bigInt := big.NewInt(int64(length))
- for i := 0; i < LL; i++ {
- randomInt, _ := cr.Int(cr.Reader, bigInt)
- SC += string(str[randomInt.Int64()])
- }
- return
- }
|