123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package wechat
  2. import (
  3. "wechat-conf/models"
  4. "wechat-conf/models/model"
  5. "wechat-conf/utils"
  6. "github.com/astaxie/beego"
  7. "github.com/go-xorm/xorm"
  8. )
  9. // WechatDAO 当前数据库操作对象
  10. type WechatDAO struct {
  11. ctx *utils.Context
  12. db *xorm.Session
  13. }
  14. // NewWechatDAO New Inst
  15. func NewWechatDAO(ctx *utils.Context) *WechatDAO {
  16. return &WechatDAO{
  17. ctx: ctx,
  18. db: ctx.DB,
  19. }
  20. }
  21. // GetWechatConfByAppID 根据appid获取配置信息
  22. func (m *WechatDAO) GetWechatConfByAppID(appid string) (*model.SysWechatConf, error) {
  23. var conf model.SysWechatConf
  24. _, err := m.db.Where("appid=?", appid).And("status>?", models.STATUS_DEL).Get(&conf)
  25. return &conf, err
  26. }
  27. // AddWechatConf 保存微信配置信息
  28. func (m *WechatDAO) AddWechatConf(conf model.SysWechatConf) error {
  29. conf.ConfId = utils.GetGUID()
  30. conf.Status = models.STATUS_READY
  31. _, err := m.db.Insert(conf)
  32. return err
  33. }
  34. // UpdateWechatConf 更新微信配置信息
  35. func (m *WechatDAO) UpdateWechatConf(conf model.SysWechatConf, cols []string) error {
  36. _, err := m.db.Cols(cols...).Where("conf_id = ?", conf.ConfId).Update(conf)
  37. return err
  38. }
  39. // SaveWechatConf 保存微信配置
  40. func (m *WechatDAO) SaveWechatConf(conf model.SysWechatConf) error {
  41. wechatConf, err := m.GetWechatConfByAppID(conf.Appid)
  42. if err != nil {
  43. utils.LogError("根据appid获取微信配置失败:", err)
  44. return err
  45. }
  46. if wechatConf != nil && wechatConf.ConfId != "" {
  47. // 修改
  48. conf.ConfId = wechatConf.ConfId
  49. var cols = []string{
  50. "refresh_token",
  51. "token",
  52. }
  53. if conf.AuthorizationInfo != "" {
  54. cols = append(cols, "authorization_info")
  55. }
  56. err := m.UpdateWechatConf(conf, cols)
  57. if err != nil {
  58. utils.LogError("修改微信配置失败:", err)
  59. return err
  60. }
  61. } else {
  62. // 新增
  63. beego.Error("开始新增:", conf)
  64. err := m.AddWechatConf(conf)
  65. if err != nil {
  66. utils.LogError("新增微信配置失败:", err)
  67. return err
  68. }
  69. }
  70. return nil
  71. }
  72. // GetComponentInfo 获取第三方conf
  73. func (m *WechatDAO) GetComponentInfo() (*model.SysComponentConf, error) {
  74. var conf = model.SysComponentConf{}
  75. _, err := m.db.Get(&conf)
  76. return &conf, err
  77. }
  78. // UpdateComponentTicket 更新第三方ticket
  79. func (m *WechatDAO) UpdateComponentTicket(conf *model.SysComponentConf) error {
  80. _, err := m.db.Cols([]string{
  81. "ticket",
  82. }...).Where("appid = ?", conf.Appid).Update(conf)
  83. return err
  84. }
  85. // GetWxByCode 根据code获取微信信息
  86. func (m *WechatDAO) GetWxByCode(code string) (*model.SysWechatConf, error) {
  87. var conf model.SysWechatConf
  88. _, err := m.db.Where("authorization_code=?", code).And("status>?", models.STATUS_DEL).Get(&conf)
  89. return &conf, err
  90. }
  91. // GetWechatConfByConfId 根据confid获取微信信息
  92. func (m *WechatDAO) GetWechatConfByConfId(confid string) (*model.SysWechatConf, error) {
  93. var conf model.SysWechatConf
  94. _, err := m.db.Where("conf_id=?", confid).And("status>?", models.STATUS_DEL).Get(&conf)
  95. return &conf, err
  96. }
  97. // DelWechatConf 删除微信信息
  98. func (m *WechatDAO) DelWechatConf(confid string) error {
  99. sql := `delete from sys_wechat_conf where conf_id=?`
  100. _, err := m.db.Exec(sql, confid)
  101. return err
  102. }