138 lines
4.9 KiB
Go
138 lines
4.9 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"git.5iot.com/iotlcm-public/iotlcm-app/internal/consts"
|
|
"git.5iot.com/iotlcm-public/iotlcm-app/internal/dao"
|
|
"git.5iot.com/iotlcm-public/iotlcm-app/internal/model"
|
|
"git.5iot.com/iotlcm-public/iotlcm-app/internal/model/do"
|
|
"git.5iot.com/iotlcm-public/iotlcm-app/internal/model/entity"
|
|
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
)
|
|
|
|
func warningPreferenceKey(userID int64) string {
|
|
return fmt.Sprintf("app.wechat.warning.enabled.%d", userID)
|
|
}
|
|
|
|
func (s *sAppUser) WarningPreference(ctx context.Context) (*model.AppWarningPreferenceModel, error) {
|
|
user, err := appCurrentUser(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var config *entity.SysConfig
|
|
err = dao.SysConfig.Ctx(ctx).Where("tenant_id", user.TenantId).Where("config_key", warningPreferenceKey(user.UserId)).OrderDesc("config_id").Scan(&config)
|
|
if err != nil {
|
|
return nil, gerror.Wrap(err, "读取消息提醒设置失败")
|
|
}
|
|
bound, _, err := appUserWechatBindingStatus(ctx, user)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &model.AppWarningPreferenceModel{Enabled: bound && (config == nil || config.ConfigValue == "1"), WechatBound: bound}, nil
|
|
}
|
|
|
|
func saveWarningPreference(ctx context.Context, tx gdb.TX, user *entity.SysUser, enabled bool) error {
|
|
value := "0"
|
|
if enabled {
|
|
value = "1"
|
|
}
|
|
var config *entity.SysConfig
|
|
if err := dao.SysConfig.Ctx(ctx).TX(tx).Where("tenant_id", user.TenantId).Where("config_key", warningPreferenceKey(user.UserId)).OrderDesc("config_id").Scan(&config); err != nil {
|
|
return gerror.Wrap(err, "读取消息提醒设置失败")
|
|
}
|
|
data := do.SysConfig{ConfigValue: value, UpdatedBy: user.UserId}
|
|
if config != nil {
|
|
_, err := dao.SysConfig.Ctx(ctx).TX(tx).Where("tenant_id", user.TenantId).Where("config_key", warningPreferenceKey(user.UserId)).Data(data).Update()
|
|
return gerror.Wrap(err, "保存消息提醒设置失败")
|
|
}
|
|
data.TenantId = user.TenantId
|
|
data.ConfigKey = warningPreferenceKey(user.UserId)
|
|
data.ConfigName = "小程序用户微信预警提醒偏好"
|
|
data.ConfigType = "N"
|
|
data.CreatedBy = user.UserId
|
|
data.CreatedDept = user.DeptId
|
|
_, err := dao.SysConfig.Ctx(ctx).TX(tx).Data(data).Insert()
|
|
return gerror.Wrap(err, "保存消息提醒设置失败")
|
|
}
|
|
|
|
func (s *sAppUser) SaveWarningPreference(ctx context.Context, param *model.AppWarningPreferenceParam) (*model.AppWarningPreferenceModel, error) {
|
|
if param == nil || param.Enabled == nil {
|
|
return nil, gerror.New("请选择是否开启预警订阅")
|
|
}
|
|
user, err := appCurrentUser(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = dao.SysUser.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
|
var locked *entity.SysUser
|
|
lockModel := dao.SysUser.Ctx(ctx).TX(tx).WherePri(user.UserId).Where("tenant_id", user.TenantId)
|
|
if dao.SysUser.DB().GetConfig().Type != "sqlite" {
|
|
lockModel = lockModel.LockUpdate()
|
|
}
|
|
if err := lockModel.Scan(&locked); err != nil {
|
|
return err
|
|
}
|
|
if locked == nil {
|
|
return gerror.New("账号不存在")
|
|
}
|
|
if *param.Enabled {
|
|
count, err := dao.SysSocial.Ctx(ctx).TX(tx).Where("tenant_id", user.TenantId).Where("user_id", user.UserId).Where("source", consts.SourceWechatMp).Where("open_id <> ?", "").Count()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if count == 0 {
|
|
return gerror.New("请先在个人资料中绑定微信")
|
|
}
|
|
}
|
|
return saveWarningPreference(ctx, tx, user, *param.Enabled)
|
|
})
|
|
if err != nil {
|
|
return nil, gerror.Wrap(err, "保存消息提醒设置失败")
|
|
}
|
|
return s.WarningPreference(ctx)
|
|
}
|
|
|
|
func (s *sAppUser) UnbindWechat(ctx context.Context) (*model.AppWechatBindModel, error) {
|
|
user, err := appCurrentUser(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if strings.TrimSpace(user.UserName) == "" || strings.TrimSpace(user.Password) == "" {
|
|
return nil, gerror.New("请先设置可用的账号密码,再解除微信绑定")
|
|
}
|
|
err = unbindWechatBindings(ctx, user)
|
|
if err != nil {
|
|
return nil, gerror.Wrap(err, "解除微信绑定失败")
|
|
}
|
|
info, err := getAppUserInfoById(ctx, user.UserId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &model.AppWechatBindModel{Success: true, UserInfo: info}, nil
|
|
}
|
|
|
|
func unbindWechatBindings(ctx context.Context, user *entity.SysUser) error {
|
|
return dao.SysUser.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
|
var locked *entity.SysUser
|
|
lockModel := dao.SysUser.Ctx(ctx).TX(tx).WherePri(user.UserId).Where("tenant_id", user.TenantId)
|
|
if dao.SysUser.DB().GetConfig().Type != "sqlite" {
|
|
lockModel = lockModel.LockUpdate()
|
|
}
|
|
if err := lockModel.Scan(&locked); err != nil {
|
|
return err
|
|
}
|
|
if locked == nil || strings.TrimSpace(locked.UserName) == "" || strings.TrimSpace(locked.Password) == "" {
|
|
return gerror.New("请先设置可用的账号密码")
|
|
}
|
|
if err := saveWarningPreference(ctx, tx, user, false); err != nil {
|
|
return err
|
|
}
|
|
_, err := dao.SysSocial.Ctx(ctx).TX(tx).Where("tenant_id", user.TenantId).Where("user_id", user.UserId).Where("source", consts.SourceWechatMp).Delete()
|
|
return err
|
|
})
|
|
}
|