wechat.go 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. }