wechat.go 3.2KB

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. // beego.Error("修改wechatConf,", conf)
  49. // conf.ConfId = wechatConf.ConfId
  50. // var cols = []string{
  51. // "refresh_token",
  52. // "token",
  53. // }
  54. // if conf.AuthorizationInfo != "" {
  55. // cols = append(cols, "authorization_info")
  56. // }
  57. // err := m.UpdateWechatConf(conf, cols)
  58. // if err != nil {
  59. // utils.LogError("修改微信配置失败:", err)
  60. // return err
  61. // }
  62. // } else {
  63. // // 新增
  64. beego.Error("开始新增:", conf)
  65. err := m.AddWechatConf(conf)
  66. if err != nil {
  67. utils.LogError("新增微信配置失败:", err)
  68. return err
  69. }
  70. // }
  71. return nil
  72. }
  73. // GetComponentInfo 获取第三方conf
  74. func (m *WechatDAO) GetComponentInfo() (*model.SysComponentConf, error) {
  75. var conf = model.SysComponentConf{}
  76. _, err := m.db.Get(&conf)
  77. return &conf, err
  78. }
  79. // UpdateComponentTicket 更新第三方ticket
  80. func (m *WechatDAO) UpdateComponentTicket(conf *model.SysComponentConf) error {
  81. _, err := m.db.Cols([]string{
  82. "ticket",
  83. }...).Where("appid = ?", conf.Appid).Update(conf)
  84. return err
  85. }
  86. // GetWxByCode 根据code获取微信信息
  87. func (m *WechatDAO) GetWxByCode(code string) (*model.SysWechatConf, error) {
  88. var conf model.SysWechatConf
  89. _, err := m.db.Where("authorization_code=?", code).And("status>?", models.STATUS_DEL).Get(&conf)
  90. return &conf, err
  91. }
  92. // GetWechatConfByConfId 根据confid获取微信信息
  93. func (m *WechatDAO) GetWechatConfByConfId(confid string) (*model.SysWechatConf, error) {
  94. var conf model.SysWechatConf
  95. _, err := m.db.Where("conf_id=?", confid).And("status>?", models.STATUS_DEL).Get(&conf)
  96. return &conf, err
  97. }
  98. // DelWechatConf 删除微信信息
  99. func (m *WechatDAO) DelWechatConf(confid string) error {
  100. sql := `delete from sys_wechat_conf where conf_id=?`
  101. _, err := m.db.Exec(sql, confid)
  102. return err
  103. }