ljw 1 год назад
Родитель
Сommit
02232fc343
3 измененных файлов с 39 добавлено и 9 удалено
  1. 25 9
      http/controller/api/ab.go
  2. 5 0
      service/addressBook.go
  3. 9 0
      utils/tools.go

+ 25 - 9
http/controller/api/ab.go

@@ -689,9 +689,9 @@ func (a *Ab) PeerDel(c *gin.Context) {
689 689
 // @Router /ab/peer/update/{guid} [put]
690 690
 // @Security BearerAuth
691 691
 func (a *Ab) PeerUpdate(c *gin.Context) {
692
-	//f := &gin.H{}
693
-	f := &requstform.PersonalAddressBookForm{}
694
-	err := c.ShouldBindJSON(f)
692
+	f := gin.H{}
693
+	//f := &requstform.PersonalAddressBookForm{}
694
+	err := c.ShouldBindJSON(&f)
695 695
 	if err != nil {
696 696
 		response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
697 697
 		return
@@ -709,17 +709,33 @@ func (a *Ab) PeerUpdate(c *gin.Context) {
709 709
 		response.Error(c, response.TranslateMsg(c, "NoAccess"))
710 710
 		return
711 711
 	}
712
-
713 712
 	//fmt.Println(f)
714
-	//return
715
-	ab := service.AllService.AddressBookService.InfoByUserIdAndIdAndCid(uid, f.Id, cid)
713
+	//判断f["Id"]是否存在
714
+	fid, ok := f["id"]
715
+	if !ok {
716
+		response.Error(c, response.TranslateMsg(c, "ParamsError"))
717
+		return
718
+	}
719
+	fidstr := fid.(string)
720
+
721
+	ab := service.AllService.AddressBookService.InfoByUserIdAndIdAndCid(uid, fidstr, cid)
716 722
 	if ab == nil || ab.RowId == 0 {
717 723
 		response.Error(c, response.TranslateMsg(c, "ItemNotFound"))
718 724
 		return
719 725
 	}
720
-	nab := f.ToAddressBook()
721
-	nab.RowId = ab.RowId
722
-	err = service.AllService.AddressBookService.Update(nab)
726
+	//允许的字段
727
+	allowUp := []string{"password", "hash", "tags", "alias"}
728
+	//f中的字段如果不在allowUp中,就删除
729
+	for k := range f {
730
+		if !utils.InArray(k, allowUp) {
731
+			delete(f, k)
732
+		}
733
+	}
734
+	//fmt.Println(f)
735
+	if tags, _ok := f["tags"]; _ok {
736
+		f["tags"], _ = json.Marshal(tags)
737
+	}
738
+	err = service.AllService.AddressBookService.UpdateByMap(ab, f)
723 739
 	if err != nil {
724 740
 		response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
725 741
 		return

+ 5 - 0
service/addressBook.go

@@ -130,6 +130,11 @@ func (s *AddressBookService) Update(u *model.AddressBook) error {
130 130
 	return global.DB.Model(u).Updates(u).Error
131 131
 }
132 132
 
133
+// UpdateByMap 更新
134
+func (s *AddressBookService) UpdateByMap(u *model.AddressBook, data map[string]interface{}) error {
135
+	return global.DB.Model(u).Updates(data).Error
136
+}
137
+
133 138
 // UpdateAll 更新
134 139
 func (s *AddressBookService) UpdateAll(u *model.AddressBook) error {
135 140
 	return global.DB.Model(u).Select("*").Omit("created_at").Updates(u).Error

+ 9 - 0
utils/tools.go

@@ -91,3 +91,12 @@ func Values[K comparable, V any](m map[K]V) []V {
91 91
 	}
92 92
 	return values
93 93
 }
94
+
95
+func InArray(k string, arr []string) bool {
96
+	for _, v := range arr {
97
+		if k == v {
98
+			return true
99
+		}
100
+	}
101
+	return false
102
+}