1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- package wechat
-
- import (
- "wechat-conf/models"
- "wechat-conf/models/model"
- "wechat-conf/utils"
-
- "github.com/astaxie/beego"
-
- "github.com/go-xorm/xorm"
- )
-
-
- type WechatDAO struct {
- ctx *utils.Context
- db *xorm.Session
- }
-
-
- func NewWechatDAO(ctx *utils.Context) *WechatDAO {
- return &WechatDAO{
- ctx: ctx,
- db: ctx.DB,
- }
- }
-
-
- func (m *WechatDAO) GetWechatConfByAppID(appid string) (*model.SysWechatConf, error) {
- var conf model.SysWechatConf
- _, err := m.db.Where("appid=?", appid).And("status>?", models.STATUS_DEL).Get(&conf)
- return &conf, err
- }
-
-
- func (m *WechatDAO) AddWechatConf(conf model.SysWechatConf) error {
- conf.ConfId = utils.GetGUID()
- conf.Status = models.STATUS_READY
- _, err := m.db.Insert(conf)
- return err
- }
-
-
- func (m *WechatDAO) UpdateWechatConf(conf model.SysWechatConf, cols []string) error {
- _, err := m.db.Cols(cols...).Where("conf_id = ?", conf.ConfId).Update(conf)
- return err
- }
-
-
- func (m *WechatDAO) SaveWechatConf(conf model.SysWechatConf) error {
- wechatConf, err := m.GetWechatConfByAppID(conf.Appid)
- if err != nil {
- utils.LogError("根据appid获取微信配置失败:", err)
- return err
- }
- if wechatConf != nil && wechatConf.ConfId != "" {
-
- conf.ConfId = wechatConf.ConfId
- var cols = []string{
- "refresh_token",
- "token",
- }
- if conf.AuthorizationInfo != "" {
- cols = append(cols, "authorization_info")
- }
- err := m.UpdateWechatConf(conf, cols)
- if err != nil {
- utils.LogError("修改微信配置失败:", err)
- return err
- }
- } else {
-
- beego.Error("开始新增:", conf)
- err := m.AddWechatConf(conf)
- if err != nil {
- utils.LogError("新增微信配置失败:", err)
- return err
- }
- }
- return nil
- }
-
-
- func (m *WechatDAO) GetComponentInfo() (*model.SysComponentConf, error) {
- var conf = model.SysComponentConf{}
- _, err := m.db.Get(&conf)
- return &conf, err
- }
-
-
- func (m *WechatDAO) UpdateComponentTicket(conf *model.SysComponentConf) error {
- _, err := m.db.Cols([]string{
- "ticket",
- }...).Where("appid = ?", conf.Appid).Update(conf)
- return err
- }
|