liuxiaolong
2020-07-29 61a10d29390848c9bf71e889d1c1d75ba8a94a55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package service
 
import (
    "car-service/extend/util"
    "car-service/models"
    "car-service/vo"
    "errors"
    "fmt"
    "github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
    "github.com/satori/go.uuid"
    "sync"
    "time"
)
 
type UserService struct {
 
}
 
func (sv *UserService) Login(phoneNum, code string) (bool,*vo.UserInfo,error) {
    if verifyCode(phoneNum, code) {
        carSv := NewCarService()
 
        var tmpUser models.User
        err := tmpUser.SelectByPhoneNum(phoneNum)
        fmt.Println("login err:", err)
        if err != nil { //用户不存在,则新增
            //先判断此手机号是否在海康平台中
            hikPersons := carSv.GetHikPersonList()
            found := false
            hikPersonId := ""
            if hikPersons != nil {
                for _,hikP := range hikPersons {
                    if hikP.PhoneNo == phoneNum {
                        found = true
                        hikPersonId = hikP.PersonId
                    }
                }
            }
            if !found {
                return false, nil, errors.New("请联系停车场管理员")
            }
 
            u := models.User{
                Id: hikPersonId,
                PhoneNum: phoneNum,
                IsDelete: false,
            }
            if _, e := u.Insert();e ==nil {
                var plateNos = make([]string, 0)
                hikVehicles := carSv.GetVehicleListByPerson(u.Id)
 
                if hikVehicles != nil {
                    for _,up := range hikVehicles {
                        plateNos = append(plateNos, up.PlateNo)
                    }
                }
                return true, &vo.UserInfo{
                    UserId: u.Id,
                    PhoneNum: phoneNum,
                    PlateNos: plateNos,
                }, nil
            } else {
                fmt.Println("u.Insert err:", e)
                return false, nil, errors.New("注册失败")
            }
        } else { //用户已存在
            var plateNos = make([]string, 0)
            hikVehicles := carSv.GetVehicleListByPerson(tmpUser.Id)
 
            if hikVehicles != nil {
                for _,up := range hikVehicles {
                    plateNos = append(plateNos, up.PlateNo)
                }
            }
            return true, &vo.UserInfo{
                UserId: tmpUser.Id,
                PhoneNum: phoneNum,
                PlateNos: plateNos,
            }, nil
        }
    } else {
        return false,nil, errors.New("验证码错误")
    }
}
 
func (sv *UserService) AddPlateNo(userId, plateNo string) bool {
    var uc models.UserCar
    if uc.Exist(userId, plateNo) {
        return true
    }
    tmp := models.UserCar{
        Id: uuid.NewV4().String(),
        UserId: userId,
        PlateNo: plateNo,
    }
    _, err := tmp.Insert()
    if err == nil {
        return true
    }
    return false
}
 
func NewVerifyCode(phoneNum string) error {
    regionId := "cn-hangzhou"
    accessKeyId := "LTAIkHFaStA1JKk5"
    AccessSecret := "oE7LhSqBWWUBzV0B7l1G9aVmgHPddM"
    client, err := dysmsapi.NewClientWithAccessKey(regionId, accessKeyId, AccessSecret)
    if err != nil {
        return errors.New("短信服务器出错,请联系管理员")
    }
    verifyCode := util.GenValidateCode(6)
    request := dysmsapi.CreateSendSmsRequest()
    request.Scheme = "https"
    request.SignName = "贝思科"
    request.TemplateCode = "SMS_186578313"
    request.PhoneNumbers = phoneNum
    request.TemplateParam = "{\"code\":"+verifyCode+"}"
    response, err := client.SendSms(request)
    fmt.Println("sendSms err:", err)
    if err != nil {
        return errors.New("发送短信验证码失败,请联系管理员")
    }
 
    go add2Cache(phoneNum, verifyCode)
    fmt.Println("send sms resp:", response)
    return nil
}
 
func verifyCode(phoneNum string, cod string) bool {
    if b,r := existCode(phoneNum);b && r == cod {
        return true
    } else {
        fmt.Println("verifyCode false,cod:",cod, "r:",r,"b:",b)
    }
    return false
}
 
type CodCache struct {
    phoneNum    string
    cod         string
    ex             time.Time
}
var codMap = make(map[string]CodCache)
var codLock sync.RWMutex
func add2Cache(phoneNum string, code string) {
    //写到redis缓存中
    //c := cache.Get()
    //defer c.Close()
    //
    //reply, err := c.Do("SET", phoneNum, code, "EX", 5 * 60)//超时五分钟
    //if err != nil {
    //    fmt.Println("写入redis失败")
    //} else {
    //    fmt.Println("写入redis成功,reply:", reply)
    //}
    codLock.Lock()
    defer codLock.Unlock()
    expireTime := time.Now().Add(time.Minute * 5)
    codMap[phoneNum] = CodCache{
        phoneNum:phoneNum,
        cod: code,
        ex:expireTime,
    }
}
func existCode(phoneNum string) (bool,string) {
    //c := cache.Get()
    //defer c.Close()
    //
    //r, err := redis.String(c.Do("GET", phoneNum))
    //if err != nil {
    //    fmt.Println("existCode err:", err)
    //    return false,""
    //}
    //return true,r
    codLock.Lock()
    defer codLock.Unlock()
    if cache,exist := codMap[phoneNum];exist {
        if cache.ex.Before(time.Now()) {//已过期
            return false, ""
        } else {
            return true, cache.cod
        }
    } else {
        return false, ""
    }
}