1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package wechat
  2. import (
  3. "wechat-conf/models"
  4. "wechat-conf/models/model"
  5. "wechat-conf/utils"
  6. "github.com/go-xorm/xorm"
  7. )
  8. // WechatDAO 当前数据库操作对象
  9. type WechatDAO struct {
  10. ctx *utils.Context
  11. db *xorm.Session
  12. }
  13. // NewWechatDAO New Inst
  14. func NewWechatDAO(ctx *utils.Context) *WechatDAO {
  15. return &WechatDAO{
  16. ctx: ctx,
  17. db: ctx.DB,
  18. }
  19. }
  20. // GetWechatConfByAppID 根据appid获取配置信息
  21. func (m *WechatDAO) GetWechatConfByAppID(appid string) (*model.SysWechatConf, error) {
  22. var conf model.SysWechatConf
  23. _, err := m.db.Where("appid=?", appid).And("status>?", models.STATUS_DEL).Get(&conf)
  24. return &conf, err
  25. }
  26. // AddWechatConf 保存微信配置信息
  27. func (m *WechatDAO) AddWechatConf(conf *model.SysWechatConf) error {
  28. conf.ConfId = utils.GetGUID()
  29. conf.Status = models.STATUS_READY
  30. _, err := m.db.Insert(conf)
  31. return err
  32. }
  33. // UpdateWechatConf 更新微信配置信息
  34. func (m *WechatDAO) UpdateWechatConf(conf model.SysWechatConf, cols []string) error {
  35. _, err := m.db.Cols(cols...).Where("conf_id = ?", conf.ConfId).Update(conf)
  36. return err
  37. }
  38. // SaveWechatConf 保存微信配置
  39. func (m *WechatDAO) SaveWechatConf(conf model.SysWechatConf) error {
  40. wechatConf, err := m.GetWechatConfByAppID(conf.Appid)
  41. if err != nil {
  42. utils.LogError("根据appid获取微信配置失败:", err)
  43. return err
  44. }
  45. if wechatConf != nil && wechatConf.ConfId != "" {
  46. // 修改
  47. conf.ConfId = wechatConf.ConfId
  48. var cols = []string{
  49. "refresh_token",
  50. "token",
  51. }
  52. if conf.AuthorizationInfo != "" {
  53. cols = append(cols, "authorization_info")
  54. }
  55. err := m.UpdateWechatConf(conf, cols)
  56. if err != nil {
  57. utils.LogError("修改微信配置失败:", err)
  58. return err
  59. }
  60. } else {
  61. // 新增
  62. err := m.AddWechatConf(&conf)
  63. if err != nil {
  64. utils.LogError("新增微信配置失败:", err)
  65. return err
  66. }
  67. }
  68. return nil
  69. }