From f58d6c7e121dc6a16f6b1e12e5beba1e6ce1b992 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期二, 08 九月 2020 15:11:05 +0800
Subject: [PATCH] Push2Manager add isTest

---
 service/userService.go |  227 +++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 191 insertions(+), 36 deletions(-)

diff --git a/service/userService.go b/service/userService.go
index 24db8c1..7d0c770 100644
--- a/service/userService.go
+++ b/service/userService.go
@@ -1,52 +1,136 @@
 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/astaxie/beego"
 	"github.com/satori/go.uuid"
+	"strings"
+	"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
-		if err := tmpUser.SelectByPhoneNum(phoneNum); err != nil { //鐢ㄦ埛涓嶅瓨鍦�,鍒欐柊澧�
+		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 i, e := u.Insert();e ==nil && i >0 {
+			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,
+					IsAdmin: isAdmin(phoneNum),
 				}, 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,
 				PhoneNum: phoneNum,
 				PlateNos: plateNos,
+				IsAdmin: isAdmin(phoneNum),
 			}, nil
 		}
 	} else {
@@ -54,11 +138,57 @@
 	}
 }
 
+func isAdmin(phoneNum string) bool {
+	managers := beego.AppConfig.String("nightManagerPhones")
+	arr := strings.Split(managers, ",")
+	for _,s := range arr {
+		if s == phoneNum {
+			return true
+		}
+	}
+
+	return false
+}
+
 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 {
+	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"
@@ -74,7 +204,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("鍙戦�佺煭淇¢獙璇佺爜澶辫触锛岃鑱旂郴绠$悊鍛�")
 	}
@@ -85,35 +215,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