From f64dd9f191dff341b4eb430d7bacc44a3db9a279 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期五, 04 三月 2022 10:12:45 +0800
Subject: [PATCH] fix nil

---
 service/userService.go |   47 ++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 38 insertions(+), 9 deletions(-)

diff --git a/service/userService.go b/service/userService.go
index 8f66c41..7d0c770 100644
--- a/service/userService.go
+++ b/service/userService.go
@@ -7,7 +7,9 @@
 	"errors"
 	"fmt"
 	"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
+	"github.com/astaxie/beego"
 	"github.com/satori/go.uuid"
+	"strings"
 	"sync"
 	"time"
 )
@@ -37,7 +39,7 @@
 		}
 
 		var tmpUser models.User
-		err := tmpUser.SelectByPhoneNum(phoneNum)
+		err := tmpUser.SelectById(hikPersonId)
 		fmt.Println("login err:", err)
 		if err != nil { //鐢ㄦ埛涓嶅瓨鍦�,鍒欐柊澧�
 			u := models.User{
@@ -58,6 +60,13 @@
 				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(),
@@ -66,27 +75,27 @@
 								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)
 						}
-						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: plateNos,
+					IsAdmin: isAdmin(phoneNum),
 				}, nil
 			} else {
 				fmt.Println("u.Insert err:", e)
 				return false, nil, errors.New("娉ㄥ唽澶辫触")
 			}
 		} else { //鐢ㄦ埛宸插瓨鍦�
-			if hikPersonId != tmpUser.Id {
-				tmpUser.Id = hikPersonId
-				tmpUser.SyncHikPersonId(tmpUser.PhoneNum, hikPersonId)
+			if phoneNum != tmpUser.PhoneNum {
+				tmpUser.UpdatePhoneNum(phoneNum, hikPersonId)
 			}
 			var plateNos = make([]string, 0)
-			hikVehicles := carSv.GetVehicleListByPerson(tmpUser.Id)
+			hikVehicles := carSv.GetVehicleListByPerson(hikPersonId)
 
 			if hikVehicles != nil {
 				for _,up := range hikVehicles {
@@ -97,6 +106,13 @@
 			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(),
@@ -105,15 +121,16 @@
 							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)
 					}
-					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 {
@@ -121,6 +138,18 @@
 	}
 }
 
+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) {

--
Gitblit v1.8.0