服务发现基础逻辑
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package views
|
||||
|
||||
import (
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/container"
|
||||
"fyne.io/fyne/v2/dialog"
|
||||
"fyne.io/fyne/v2/widget"
|
||||
"work_cation/global"
|
||||
"work_cation/models"
|
||||
"work_cation/repo"
|
||||
"work_cation/service"
|
||||
)
|
||||
|
||||
func otherUser(w fyne.Window) fyne.CanvasObject {
|
||||
gridWrap := container.NewGridWrap(fyne.NewSize(200, 190))
|
||||
findService, err := service.Zeroconf.FindService()
|
||||
if err != nil {
|
||||
return widget.NewLabel("网络异常请稍后尝试")
|
||||
}
|
||||
go func() {
|
||||
for online := range findService {
|
||||
var (
|
||||
onlineCopy = online
|
||||
)
|
||||
baseCardV, err := itemOnlineUserView(w, onlineCopy)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
gridWrap.Add(baseCardV)
|
||||
}
|
||||
}()
|
||||
scroll := container.NewScroll(gridWrap)
|
||||
return container.NewBorder(nil, nil, nil, nil, scroll)
|
||||
}
|
||||
|
||||
func itemOnlineUserView(w fyne.Window, data *models.Online) (fyne.CanvasObject, error) {
|
||||
|
||||
u := repo.UserFollow.GetUser(global.DB, data.ID)
|
||||
|
||||
var followLabel *widget.Label
|
||||
const (
|
||||
noFollow = "未关注"
|
||||
isFollow = "已关注"
|
||||
)
|
||||
|
||||
if u == nil {
|
||||
followLabel = widget.NewLabel(noFollow)
|
||||
} else {
|
||||
followLabel = widget.NewLabel(isFollow)
|
||||
}
|
||||
|
||||
user, err := service.Client.GetUser(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
followButton := widget.NewButton("关注", func() {
|
||||
if followLabel.Text == isFollow {
|
||||
dialog.ShowInformation("结果", "已关注成功", w)
|
||||
return
|
||||
}
|
||||
err = repo.UserFollow.Follow(global.DB, &models.UserFollows{Users: *user})
|
||||
if err != nil {
|
||||
dialog.ShowInformation("关注失败", err.Error(), w)
|
||||
return
|
||||
}
|
||||
dialog.ShowInformation("结果", "关注成功", w)
|
||||
followLabel.SetText(isFollow)
|
||||
})
|
||||
|
||||
showErrButton := widget.NewButton("查看主页", func() {
|
||||
dialog.ShowInformation("错误", "", w)
|
||||
})
|
||||
|
||||
card := widget.NewCard(user.Name, user.Ip, container.NewVBox(followLabel, showErrButton, followButton))
|
||||
return card, nil
|
||||
}
|
||||
Reference in New Issue
Block a user