71 lines
1.6 KiB
Go
71 lines
1.6 KiB
Go
|
package robot
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"log"
|
||
|
"wechat-robot/internal/database"
|
||
|
"wechat-robot/model/entity"
|
||
|
robotParam "wechat-robot/model/param/robot"
|
||
|
"wechat-robot/pkg/wxhelper"
|
||
|
)
|
||
|
|
||
|
// Save
|
||
|
// @description: 保存机器人
|
||
|
// @param p
|
||
|
// @return err
|
||
|
func Save(p robotParam.Save) (err error) {
|
||
|
if p.Id != "" {
|
||
|
// 是修改,暂时只支持修改备注
|
||
|
err = database.Client.Model(&entity.Robot{}).Where("id = ?", p.Id).Update("remark", p.Remark).Error
|
||
|
return
|
||
|
}
|
||
|
// 新增
|
||
|
|
||
|
// 获取机器人信息,如果获取失败,就返回错误信息
|
||
|
cli, err := wxhelper.New(p.HookApi, p.Version)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
isLogin := cli.CheckLogin()
|
||
|
log.Printf("机器人是否登录: %v", isLogin)
|
||
|
if !isLogin {
|
||
|
err = errors.New("机器人未登录")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// 取出登录账号信息
|
||
|
info, err := cli.UserInfo()
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
log.Printf("机器人Id: %s", info.WxId)
|
||
|
log.Printf("机器人微信号: %s", info.Account)
|
||
|
log.Printf("机器人名称: %s", info.Name)
|
||
|
|
||
|
// 先查询是否存在
|
||
|
var ent entity.Robot
|
||
|
var count int64
|
||
|
if err = database.Client.Model(&ent).Where("wxid = ?", info.WxId).Count(&count).Error; err != nil {
|
||
|
return
|
||
|
}
|
||
|
if count > 0 {
|
||
|
err = errors.New("机器人已存在")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
ent.WxId = info.WxId
|
||
|
ent.Account = info.Account
|
||
|
ent.Nickname = info.Name
|
||
|
ent.Avatar = info.HeadImage
|
||
|
ent.Mobile = info.Mobile
|
||
|
ent.CurrentDataPath = info.CurrentDataPath
|
||
|
ent.DataSavePath = info.DataSavePath
|
||
|
ent.DbKey = info.DbKey
|
||
|
ent.HookApi = p.HookApi
|
||
|
ent.Remark = p.Remark
|
||
|
ent.Version = int(p.Version)
|
||
|
ent.VncUrl = p.VncUrl
|
||
|
err = database.Client.Create(&ent).Error
|
||
|
return
|
||
|
}
|