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 | 200 ++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 161 insertions(+), 39 deletions(-) diff --git a/service/userService.go b/service/userService.go index 5bd2303..7d0c770 100644 --- a/service/userService.go +++ b/service/userService.go @@ -1,78 +1,153 @@ 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() - 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 - } + //鍏堝垽鏂鎵嬫満鍙锋槸鍚﹀湪娴峰悍骞冲彴涓� + 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("璇疯仈绯诲仠杞﹀満绠$悊鍛�") - } + } + if !found { + return false, nil, errors.New("璇疯仈绯诲仠杞﹀満绠$悊鍛�") + } + var tmpUser models.User + err := tmpUser.SelectById(hikPersonId) + fmt.Println("login err:", err) + if err != nil { //鐢ㄦ埛涓嶅瓨鍦�,鍒欐柊澧� 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) + } + } + //瀹㈡埛绔痗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 { //鐢ㄦ埛宸插瓨鍦� + 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 { 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 { return false,nil, errors.New("楠岃瘉鐮侀敊璇�") } +} + +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 { @@ -93,6 +168,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" @@ -108,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("鍙戦�佺煭淇¢獙璇佺爜澶辫触锛岃鑱旂郴绠$悊鍛�") } @@ -127,26 +223,52 @@ 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