From 27f1012d2fecda7bcc3d9f44c5dd4c10a7cf38a4 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期二, 18 八月 2020 12:13:54 +0800
Subject: [PATCH] add log
---
service/userService.go | 193 +++++++++++++++++++++++++++++++++++++++--------
1 files changed, 158 insertions(+), 35 deletions(-)
diff --git a/service/userService.go b/service/userService.go
index 15ed042..a468dca 100644
--- a/service/userService.go
+++ b/service/userService.go
@@ -1,50 +1,127 @@
package service
import (
- "car-service/cache"
"car-service/extend/util"
"car-service/models"
"car-service/vo"
"errors"
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
- "github.com/gomodule/redigo/redis"
"github.com/satori/go.uuid"
+ "sync"
+ "time"
)
type UserService struct {
}
-func (sv *UserService) Login(phoneNum, code string) (bool,*vo.UserInfo,error) {
+func (sv *UserService) Login(phoneNum, code, cid string) (bool,*vo.UserInfo,error) {
if verifyCode(phoneNum, code) {
+ carSv := NewCarService()
+
+ //鍏堝垽鏂鎵嬫満鍙锋槸鍚﹀湪娴峰悍骞冲彴涓�
+ 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("璇疯仈绯诲仠杞﹀満绠$悊鍛�")
+ }
+
var tmpUser models.User
- err := tmpUser.SelectByPhoneNum(phoneNum)
+ err := tmpUser.SelectById(hikPersonId)
fmt.Println("login err:", err)
if err != nil { //鐢ㄦ埛涓嶅瓨鍦�,鍒欐柊澧�
u := models.User{
- Id: uuid.NewV4().String(),
+ 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)
+ }
+ }
+ //瀹㈡埛绔痗id缁戝畾鍒悕
+ if cid != "" {
+ go func() {
+ var uc models.UserClient
+ ucList := uc.GetByCid(cid)
+ if ucList != nil && len(ucList) >0 {
+ if len(ucList) >1 || ucList[0].PhoneNum != phoneNum {
+ unbindB, unE := UnbindAlias(cid)
+ fmt.Println("unbindB:", unbindB, "err:", unE)
+ }
+ }
+ if !uc.ExistByCid(phoneNum, cid) {
+ new := models.UserClient{
+ Id:uuid.NewV4().String(),
+ PhoneNum: phoneNum,
+ ClientId: cid,
+ BindTime: time.Now().Format("2006-01-02 15:04:05"),
+ }
+ new.Insert()
+ bindR, bindE := BindAlias(cid, phoneNum)
+ fmt.Println("bind cid:",cid, "phoneNum:",phoneNum,"result:", bindR, "err:", bindE)
+ }
+ }()
+ }
return true, &vo.UserInfo{
UserId: u.Id,
PhoneNum: phoneNum,
- PlateNos: []string{},
+ PlateNos: plateNos,
}, nil
} else {
fmt.Println("u.Insert err:", e)
return false, nil, errors.New("娉ㄥ唽澶辫触")
}
} else { //鐢ㄦ埛宸插瓨鍦�
- var userCar models.UserCar
- var plateNos []string
- all, e := userCar.GetByUserId(tmpUser.Id)
- if e == nil && all != nil {
- for _,up := range all {
+ if phoneNum != tmpUser.PhoneNum {
+ tmpUser.UpdatePhoneNum(phoneNum, hikPersonId)
+ }
+ var plateNos = make([]string, 0)
+ hikVehicles := carSv.GetVehicleListByPerson(hikPersonId)
+
+ if hikVehicles != nil {
+ for _,up := range hikVehicles {
plateNos = append(plateNos, up.PlateNo)
}
+ }
+ //瀹㈡埛绔痗id缁戝畾鍒悕
+ if cid != "" {
+ go func() {
+ var uc models.UserClient
+ ucList := uc.GetByCid(cid)
+ if ucList != nil && len(ucList) >0 {
+ if len(ucList) >1 || ucList[0].PhoneNum != phoneNum {
+ unbindB, unE := UnbindAlias(cid)
+ fmt.Println("unbindB:", unbindB, "err:", unE)
+ }
+ }
+ if !uc.ExistByCid(phoneNum, cid) {
+ new := models.UserClient{
+ Id:uuid.NewV4().String(),
+ PhoneNum: phoneNum,
+ ClientId: cid,
+ BindTime: time.Now().Format("2006-01-02 15:04:05"),
+ }
+ new.Insert()
+ bindR, bindE := BindAlias(cid, phoneNum)
+ fmt.Println("bind cid:",cid, "phoneNum:",phoneNum,"result:", bindR, "err:", bindE)
+ }
+ }()
}
return true, &vo.UserInfo{
UserId: tmpUser.Id,
@@ -75,6 +152,27 @@
}
func NewVerifyCode(phoneNum string) error {
+ carSv := NewCarService()
+ personList := carSv.GetHikPersonList()
+ found := false
+ if personList ==nil {
+ return errors.New("鎵嬫満鍙蜂笉瀛樺湪锛岃鑱旂郴鍋滆溅鍦虹鐞嗗憳")
+ } else {
+ for _,p := range personList {
+ if p.PhoneNo == phoneNum {
+ found = true
+ }
+ }
+ }
+ if !found {
+ return errors.New("鎵嬫満鍙蜂笉瀛樺湪锛岃鑱旂郴鍋滆溅鍦虹鐞嗗憳")
+ }
+ //var tmpUser models.User
+ //err := tmpUser.SelectByPhoneNum(phoneNum)
+ //if err != nil { //鐢ㄦ埛涓嶅瓨鍦�,娉ㄥ唽鑾峰彇楠岃瘉鐮侊紝姝ゆ墜鏈哄彿蹇呴』鍦ㄨ偛鑻辨捣搴峰钩鍙颁腑
+ //
+ //}
+
regionId := "cn-hangzhou"
accessKeyId := "LTAIkHFaStA1JKk5"
AccessSecret := "oE7LhSqBWWUBzV0B7l1G9aVmgHPddM"
@@ -90,7 +188,7 @@
request.PhoneNumbers = phoneNum
request.TemplateParam = "{\"code\":"+verifyCode+"}"
response, err := client.SendSms(request)
- fmt.Println("sendSms err:", err)
+ fmt.Println("sendSms err:", err, "phoneNum:", phoneNum, "cod:", verifyCode)
if err != nil {
return errors.New("鍙戦�佺煭淇¢獙璇佺爜澶辫触锛岃鑱旂郴绠$悊鍛�")
}
@@ -101,35 +199,60 @@
}
func verifyCode(phoneNum string, cod string) bool {
- return true
- //if b,r := existCode(phoneNum);b && r == cod {
- // return true
- //} else {
- // fmt.Println("verifyCode false,cod:",cod, "r:",r,"b:",b)
- //}
- //return false
+ 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)
+ //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,""
+ //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, ""
}
- return true,r
}
\ No newline at end of file
--
Gitblit v1.8.0