123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844 |
- package service
- import (
- "context"
- "encoding/json"
- "strconv"
- "time"
- "go-common/app/job/main/passport-sns/model"
- "go-common/library/log"
- "go-common/library/queue/databus"
- "github.com/go-sql-driver/mysql"
- "github.com/pkg/errors"
- )
- const (
- _asoAccountSnsTable = "aso_account_sns"
- _insertAction = "insert"
- _updateAction = "update"
- _deleteAction = "delete"
- _mySQLErrCodeDuplicateEntry = 1062
- )
- type asoAccountSnsBMsg struct {
- Action string
- Table string
- New *model.AsoAccountSns
- Old *model.AsoAccountSns
- Timestamp int64
- }
- func (s *Service) asoBinLogConsume() {
- s.group.New = func(msg *databus.Message) (res interface{}, err error) {
- bmsg := new(model.BMsg)
- if err = json.Unmarshal(msg.Value, bmsg); err != nil {
- log.Error("json.Unmarshal(%s) error(%+v)", string(msg.Value), err)
- return
- }
- log.Info("receive msg action(%s) table(%s) key(%s) partition(%d) offset(%d) timestamp(%d) New(%s) Old(%s)",
- bmsg.Action, bmsg.Table, msg.Key, msg.Partition, msg.Offset, msg.Timestamp, string(bmsg.New), string(bmsg.Old))
- if bmsg.Table == _asoAccountSnsTable {
- asoAccountSnsBMsg := &asoAccountSnsBMsg{
- Action: bmsg.Action,
- Table: bmsg.Table,
- Timestamp: msg.Timestamp,
- }
- newAccountSns := new(model.AsoAccountSns)
- if err = json.Unmarshal(bmsg.New, newAccountSns); err != nil {
- log.Error("json.Unmarshal(%s) error(%+v)", string(bmsg.New), err)
- return
- }
- asoAccountSnsBMsg.New = newAccountSns
- if bmsg.Action == _updateAction {
- oldAccountSns := new(model.AsoAccountSns)
- if err = json.Unmarshal(bmsg.Old, oldAccountSns); err != nil {
- log.Error("json.Unmarshal(%s) error(%+v)", string(bmsg.Old), err)
- return
- }
- asoAccountSnsBMsg.Old = oldAccountSns
- }
- return asoAccountSnsBMsg, nil
- }
- return
- }
- s.group.Split = func(msg *databus.Message, data interface{}) int {
- if t, ok := data.(*asoAccountSnsBMsg); ok {
- return int(t.New.Mid)
- }
- return 0
- }
- s.group.Do = func(msgs []interface{}) {
- for _, m := range msgs {
- if msg, ok := m.(*asoAccountSnsBMsg); ok {
- for {
- if err := s.handleAsoAccountSns(msg); err != nil {
- log.Error("fail to handleAsoAccountSns msg(%+v) new(%+v) error(%+v)", msg, msg.New, err)
- time.Sleep(100 * time.Millisecond)
- continue
- }
- break
- }
- }
- }
- }
- // start the group
- s.group.Start()
- log.Info("s.group.Start()")
- }
- func (s *Service) handleAsoAccountSns(msg *asoAccountSnsBMsg) (err error) {
- switch msg.Action {
- case _insertAction:
- if msg.New.QQOpenid != "" {
- if err = s.addSnsQQ(msg.New); err != nil {
- return
- }
- }
- if msg.New.SinaUID != 0 {
- if err = s.addSnsWeibo(msg.New); err != nil {
- return
- }
- }
- case _updateAction:
- if msg.New.QQOpenid != msg.Old.QQOpenid {
- if msg.New.QQOpenid == "" {
- if _, err = s.d.DelSnsUser(context.Background(), msg.New.Mid, model.PlatformQQ); err != nil {
- return
- }
- s.cache.Do(context.Background(), func(c context.Context) {
- s.d.DelSnsCache(c, msg.New.Mid, model.PlatformQQStr)
- })
- }
- if msg.New.QQOpenid != "" {
- var user *model.SnsUser
- if user, err = s.d.SnsUserByMid(context.Background(), msg.New.Mid, model.PlatformQQ); err != nil {
- return
- }
- if user == nil {
- if err = s.addSnsQQ(msg.New); err != nil {
- return
- }
- } else {
- if err = s.updateSnsQQ(msg.New); err != nil {
- return
- }
- }
- }
- return
- }
- if msg.New.SinaUID != msg.Old.SinaUID {
- if msg.New.SinaUID == 0 {
- if _, err = s.d.DelSnsUser(context.Background(), msg.New.Mid, model.PlatformWEIBO); err != nil {
- return
- }
- s.cache.Do(context.Background(), func(c context.Context) {
- s.d.DelSnsCache(c, msg.New.Mid, model.PlatformWEIBOStr)
- })
- }
- if msg.New.SinaUID != 0 {
- var user *model.SnsUser
- if user, err = s.d.SnsUserByMid(context.Background(), msg.New.Mid, model.PlatformWEIBO); err != nil {
- return
- }
- if user == nil {
- if err = s.addSnsWeibo(msg.New); err != nil {
- return
- }
- } else {
- if err = s.updateSnsWeibo(msg.New); err != nil {
- return
- }
- }
- }
- return
- }
- if msg.New.SinaAccessExpires != msg.Old.SinaAccessExpires {
- return s.updateSns(msg.New.Mid, msg.New.SinaAccessExpires, strconv.FormatInt(msg.New.SinaUID, 10), msg.New.SinaAccessToken, model.PlatformWEIBO)
- }
- if msg.New.QQAccessExpires != msg.Old.QQAccessExpires {
- var qqUnionID string
- if qqUnionID, err = s.d.QQUnionID(context.Background(), msg.New.QQOpenid); err != nil {
- return
- }
- if qqUnionID == "" {
- log.Error("update qq expires, qqUnionID is null, oldMsg(%+v) newMsg(%+v)", msg.Old, msg.New)
- return
- }
- return s.updateSns(msg.New.Mid, msg.New.QQAccessExpires, qqUnionID, msg.New.QQAccessToken, model.PlatformQQ)
- }
- case _deleteAction:
- if _, err = s.d.DelSnsUser(context.Background(), msg.New.Mid, model.PlatformQQ); err != nil {
- return
- }
- if _, err = s.d.DelSnsUser(context.Background(), msg.New.Mid, model.PlatformWEIBO); err != nil {
- return
- }
- s.cache.Do(context.Background(), func(c context.Context) {
- s.d.DelSnsCache(c, msg.New.Mid, model.PlatformQQStr)
- })
- s.cache.Do(context.Background(), func(c context.Context) {
- s.d.DelSnsCache(c, msg.New.Mid, model.PlatformWEIBOStr)
- })
- }
- return
- }
- func (s *Service) updateSns(mid, expires int64, unionID, token string, platform int) (err error) {
- tx, err := s.d.BeginSnsTran(context.Background())
- if err != nil {
- log.Error("s.d.BeginTran error(%+v)", err)
- return
- }
- defer func() {
- if err != nil {
- if err1 := tx.Rollback(); err1 != nil {
- log.Error("tx.Rollback() error(%v)", err1)
- }
- return
- }
- if err = tx.Commit(); err != nil {
- log.Error("tx.Commit() error(%v)", err)
- }
- }()
- snsUser := &model.SnsUser{
- Mid: mid,
- UnionID: unionID,
- Platform: platform,
- Expires: expires,
- }
- if _, err = s.d.TxUpdateSnsUserExpires(tx, snsUser); err != nil {
- return
- }
- snsToken := &model.SnsToken{
- Mid: mid,
- Platform: platform,
- Token: token,
- Expires: expires,
- }
- if _, err = s.d.TxUpdateSnsToken(tx, snsToken); err != nil {
- return
- }
- s.cache.Do(context.Background(), func(c context.Context) {
- proto := &model.SnsProto{
- Mid: snsUser.Mid,
- Platform: int32(platform),
- UnionID: snsUser.UnionID,
- Expires: snsUser.Expires,
- }
- s.d.SetSnsCache(c, mid, parsePlatformStr(platform), proto)
- })
- return
- }
- func (s *Service) addSnsQQ(sns *model.AsoAccountSns) (err error) {
- unionID, err := s.d.GetUnionIDCache(context.Background(), sns.QQOpenid)
- if err != nil || unionID == "" {
- unionID, err = s.d.QQUnionID(context.Background(), sns.QQOpenid)
- if err != nil || unionID == "" {
- return
- }
- }
- tx, err := s.d.BeginSnsTran(context.Background())
- if err != nil {
- log.Error("s.d.BeginTran error(%+v)", err)
- return
- }
- defer func() {
- if err != nil {
- if err1 := tx.Rollback(); err1 != nil {
- log.Error("tx.Rollback() error(%v)", err1)
- }
- return
- }
- if err = tx.Commit(); err != nil {
- log.Error("tx.Commit() error(%v)", err)
- }
- }()
- snsUser := &model.SnsUser{
- Mid: sns.Mid,
- UnionID: unionID,
- Platform: model.PlatformQQ,
- Expires: sns.QQAccessExpires,
- }
- if _, err = s.d.TxAddSnsUser(tx, snsUser); err != nil {
- switch nErr := errors.Cause(err).(type) {
- case *mysql.MySQLError:
- if nErr.Number == _mySQLErrCodeDuplicateEntry {
- u, e := s.d.SnsUserByMid(context.Background(), snsUser.Mid, snsUser.Platform)
- if e != nil {
- return e
- }
- err = nil
- if u != nil && u.Mid == snsUser.Mid {
- return
- }
- log.Error("add sns qq duplicate (%+v)", snsUser)
- return
- }
- }
- return
- }
- snsOpenID := &model.SnsOpenID{
- Mid: sns.Mid,
- OpenID: sns.QQOpenid,
- UnionID: unionID,
- AppID: model.OldAppID,
- Platform: model.PlatformQQ,
- }
- if _, err = s.d.TxAddSnsOpenID(tx, snsOpenID); err != nil {
- return
- }
- snsToken := &model.SnsToken{
- Mid: sns.Mid,
- OpenID: sns.QQOpenid,
- UnionID: unionID,
- Platform: model.PlatformQQ,
- Token: sns.QQAccessToken,
- Expires: sns.QQAccessExpires,
- AppID: model.OldAppID,
- }
- if _, err = s.d.TxAddSnsToken(tx, snsToken); err != nil {
- return
- }
- proto := &model.SnsProto{
- Mid: snsUser.Mid,
- Platform: model.PlatformQQ,
- UnionID: snsUser.UnionID,
- Expires: snsUser.Expires,
- }
- s.cache.Do(context.Background(), func(c context.Context) {
- s.d.SetSnsCache(c, sns.Mid, model.PlatformQQStr, proto)
- })
- return
- }
- func (s *Service) addSnsWeibo(sns *model.AsoAccountSns) (err error) {
- tx, err := s.d.BeginSnsTran(context.Background())
- if err != nil {
- log.Error("s.d.BeginTran error(%+v)", err)
- return
- }
- defer func() {
- if err != nil {
- if err1 := tx.Rollback(); err1 != nil {
- log.Error("tx.Rollback() error(%v)", err1)
- }
- return
- }
- if err = tx.Commit(); err != nil {
- log.Error("tx.Commit() error(%v)", err)
- }
- }()
- snsUser := &model.SnsUser{
- Mid: sns.Mid,
- UnionID: strconv.FormatInt(sns.SinaUID, 10),
- Platform: model.PlatformWEIBO,
- Expires: sns.SinaAccessExpires,
- }
- if _, err = s.d.TxAddSnsUser(tx, snsUser); err != nil {
- switch nErr := errors.Cause(err).(type) {
- case *mysql.MySQLError:
- if nErr.Number == _mySQLErrCodeDuplicateEntry {
- u, e := s.d.SnsUserByMid(context.Background(), snsUser.Mid, snsUser.Platform)
- if e != nil {
- return e
- }
- err = nil
- if u != nil && u.Mid == snsUser.Mid {
- return
- }
- log.Error("add sns weibo duplicate (%+v)", snsUser)
- return
- }
- }
- return
- }
- snsOpenID := &model.SnsOpenID{
- Mid: sns.Mid,
- OpenID: strconv.FormatInt(sns.SinaUID, 10),
- UnionID: strconv.FormatInt(sns.SinaUID, 10),
- AppID: model.OldAppID,
- Platform: model.PlatformWEIBO,
- }
- if _, err = s.d.TxAddSnsOpenID(tx, snsOpenID); err != nil {
- return
- }
- snsToken := &model.SnsToken{
- Mid: sns.Mid,
- OpenID: strconv.FormatInt(sns.SinaUID, 10),
- UnionID: strconv.FormatInt(sns.SinaUID, 10),
- Platform: model.PlatformWEIBO,
- Token: sns.SinaAccessToken,
- Expires: sns.SinaAccessExpires,
- AppID: model.OldAppID,
- }
- if _, err = s.d.TxAddSnsToken(tx, snsToken); err != nil {
- return
- }
- proto := &model.SnsProto{
- Mid: snsUser.Mid,
- Platform: model.PlatformWEIBO,
- UnionID: snsUser.UnionID,
- Expires: snsUser.Expires,
- }
- s.cache.Do(context.Background(), func(c context.Context) {
- s.d.SetSnsCache(c, sns.Mid, model.PlatformWEIBOStr, proto)
- })
- return
- }
- func (s *Service) updateSnsQQ(sns *model.AsoAccountSns) (err error) {
- unionID, err := s.d.GetUnionIDCache(context.Background(), sns.QQOpenid)
- if err != nil || unionID == "" {
- unionID, err = s.d.QQUnionID(context.Background(), sns.QQOpenid)
- if err != nil || unionID == "" {
- return
- }
- }
- tx, err := s.d.BeginSnsTran(context.Background())
- if err != nil {
- log.Error("s.d.BeginTran error(%+v)", err)
- return
- }
- defer func() {
- if err != nil {
- if err1 := tx.Rollback(); err1 != nil {
- log.Error("tx.Rollback() error(%v)", err1)
- }
- return
- }
- if err = tx.Commit(); err != nil {
- log.Error("tx.Commit() error(%v)", err)
- }
- }()
- if _, err = s.d.TxUpdateSnsUser(tx, sns.Mid, sns.QQAccessExpires, unionID, model.PlatformQQ); err != nil {
- switch nErr := errors.Cause(err).(type) {
- case *mysql.MySQLError:
- if nErr.Number == _mySQLErrCodeDuplicateEntry {
- u, e := s.d.SnsUserByMid(context.Background(), sns.Mid, model.PlatformQQ)
- if e != nil {
- return e
- }
- err = nil
- if u != nil && u.Mid == sns.Mid {
- return
- }
- log.Error("update sns qq duplicate, mid(%d) unionID(%s)", sns.Mid, unionID)
- return
- }
- }
- return
- }
- snsToken := &model.SnsToken{
- Mid: sns.Mid,
- OpenID: sns.QQOpenid,
- UnionID: unionID,
- Platform: model.PlatformQQ,
- Token: sns.QQAccessToken,
- Expires: sns.QQAccessExpires,
- AppID: model.OldAppID,
- }
- if _, err = s.d.TxAddSnsToken(tx, snsToken); err != nil {
- return
- }
- proto := &model.SnsProto{
- Mid: sns.Mid,
- Platform: model.PlatformQQ,
- UnionID: unionID,
- Expires: sns.QQAccessExpires,
- }
- s.cache.Do(context.Background(), func(c context.Context) {
- s.d.SetSnsCache(c, sns.Mid, model.PlatformQQStr, proto)
- })
- return
- }
- func (s *Service) updateSnsWeibo(sns *model.AsoAccountSns) (err error) {
- tx, err := s.d.BeginSnsTran(context.Background())
- if err != nil {
- log.Error("s.d.BeginTran error(%+v)", err)
- return
- }
- defer func() {
- if err != nil {
- if err1 := tx.Rollback(); err1 != nil {
- log.Error("tx.Rollback() error(%v)", err1)
- }
- return
- }
- if err = tx.Commit(); err != nil {
- log.Error("tx.Commit() error(%v)", err)
- }
- }()
- if _, err = s.d.TxUpdateSnsUser(tx, sns.Mid, sns.SinaAccessExpires, strconv.FormatInt(sns.SinaUID, 10), model.PlatformWEIBO); err != nil {
- switch nErr := errors.Cause(err).(type) {
- case *mysql.MySQLError:
- if nErr.Number == _mySQLErrCodeDuplicateEntry {
- u, e := s.d.SnsUserByMid(context.Background(), sns.Mid, model.PlatformWEIBO)
- if e != nil {
- return e
- }
- err = nil
- if u != nil && u.Mid == sns.Mid {
- return
- }
- log.Error("update sns weibo duplicate, mid(%d) unionID(%s)", sns.Mid, strconv.FormatInt(sns.SinaUID, 10))
- return
- }
- }
- return
- }
- snsToken := &model.SnsToken{
- Mid: sns.Mid,
- OpenID: strconv.FormatInt(sns.SinaUID, 10),
- UnionID: strconv.FormatInt(sns.SinaUID, 10),
- Platform: model.PlatformWEIBO,
- Token: sns.SinaAccessToken,
- Expires: sns.SinaAccessExpires,
- AppID: model.OldAppID,
- }
- if _, err = s.d.TxAddSnsToken(tx, snsToken); err != nil {
- return
- }
- proto := &model.SnsProto{
- Mid: sns.Mid,
- Platform: model.PlatformWEIBO,
- UnionID: strconv.FormatInt(sns.SinaUID, 10),
- Expires: sns.SinaAccessExpires,
- }
- s.cache.Do(context.Background(), func(c context.Context) {
- s.d.SetSnsCache(c, sns.Mid, model.PlatformWEIBOStr, proto)
- })
- return
- }
- func (s *Service) fullSyncSns() {
- var (
- start int64
- chanNum = int64(s.c.SyncConf.ChanNum)
- )
- for {
- log.Info("fullSyncSns, start %d", start)
- res, err := s.d.AsoAccountSns(context.Background(), start)
- if err != nil {
- log.Error("fail to get AsoAccountSns error(%+v)", err)
- time.Sleep(100 * time.Millisecond)
- continue
- }
- for _, a := range res {
- s.snsChan[a.Mid%chanNum] <- a
- }
- if len(res) == 0 {
- log.Info("fullSyncSns finished! endID(%d)", start)
- break
- }
- start = res[len(res)-1].Mid
- }
- }
- func (s *Service) fullSyncSnsConsume(c chan *model.AsoAccountSns) {
- for {
- a, ok := <-c
- if !ok {
- log.Error("snsChan closed")
- return
- }
- for {
- if a.SinaUID != 0 {
- if err := s.addSnsWeibo(a); err != nil {
- continue
- }
- }
- if a.QQOpenid != "" {
- if err := s.addSnsQQ(a); err != nil {
- continue
- }
- }
- break
- }
- }
- }
- func (s *Service) checkAll() {
- ticker := time.NewTicker(time.Duration(s.c.SyncConf.CheckTicker))
- for {
- s.checkSnsUser()
- <-ticker.C
- }
- }
- func (s *Service) checkConsume(c chan *model.AsoAccountSns) {
- for {
- a, ok := <-c
- if !ok {
- log.Error("snsChan closed")
- return
- }
- for {
- if err := s.checkWeibo(a); err != nil {
- time.Sleep(100 * time.Millisecond)
- continue
- }
- if err := s.checkQQ(a); err != nil {
- time.Sleep(100 * time.Millisecond)
- continue
- }
- break
- }
- }
- }
- func (s *Service) checkSnsUser() {
- var (
- start int64
- chanNum = int64(s.c.SyncConf.ChanNum)
- )
- for {
- log.Info("checkSnsUser, start %d", start)
- res, err := s.d.AsoAccountSnsAll(context.Background(), start)
- if err != nil {
- log.Error("fail to get AsoAccountSns error(%+v)", err)
- time.Sleep(100 * time.Millisecond)
- continue
- }
- for _, a := range res {
- s.checkChan[a.Mid%chanNum] <- a
- }
- if len(res) == 0 {
- log.Info("checkSnsUser finished! endID(%d)", start)
- break
- }
- start = res[len(res)-1].Mid
- }
- }
- func (s *Service) checkQQ(a *model.AsoAccountSns) (err error) {
- sns, err := s.d.SnsUserByMid(context.Background(), a.Mid, model.PlatformQQ)
- if err != nil {
- return
- }
- if a.QQOpenid == "" {
- if sns != nil {
- log.Error("qq not match, old is nil,old(%+v) new(%+v)", a, sns)
- if _, err = s.d.DelSnsUser(context.Background(), a.Mid, model.PlatformQQ); err != nil {
- return
- }
- s.cache.Do(context.Background(), func(c context.Context) {
- s.d.DelSnsCache(c, a.Mid, model.PlatformQQStr)
- })
- }
- return
- }
- qqUnionID, err := s.d.QQUnionID(context.Background(), a.QQOpenid)
- if err != nil || qqUnionID == "" {
- return
- }
- if sns == nil {
- log.Error("qq not match, new not exists, old(%+v), ", a)
- if _, err = s.d.AddSnsUser(context.Background(), a.Mid, a.QQAccessExpires, qqUnionID, model.PlatformQQ); err != nil {
- switch nErr := errors.Cause(err).(type) {
- case *mysql.MySQLError:
- if nErr.Number == _mySQLErrCodeDuplicateEntry {
- log.Error("checkQQ, add qq duplicate, mid(%d) unionid(%s)", a.Mid, qqUnionID)
- err = nil
- //var (
- // u *model.SnsUser
- // aso *model.AsoAccountSns
- //)
- //if u, err = s.d.SnsUserByUnionID(context.Background(), qqUnionID, model.PlatformQQ); err != nil {
- // return
- //}
- //if _, err = s.d.DelSnsUser(context.Background(), u.Mid, model.PlatformQQ); err != nil {
- // return
- //}
- //if _, err = s.d.AddSnsUser(context.Background(), a.Mid, a.QQAccessExpires, qqUnionID, model.PlatformQQ); err != nil {
- // return
- //}
- //if aso, err = s.d.AsoAccountSnsByMid(context.Background(), u.Mid); err != nil {
- // return
- //}
- //if qqUnionID, err = s.d.QQUnionID(context.Background(), a.QQOpenid); err != nil {
- // return
- //}
- //if _, err = s.d.AddSnsUser(context.Background(), aso.Mid, aso.QQAccessExpires, qqUnionID, model.PlatformQQ); err != nil {
- // return
- //}
- }
- }
- return
- }
- } else if sns.UnionID != qqUnionID {
- log.Error("qq not match, new(%s), mid(%d) oldOpenID(%s) oldUnionID(%s) ", sns.UnionID, a.Mid, a.QQOpenid, qqUnionID)
- if _, err = s.d.UpdateSnsUser(context.Background(), a.Mid, a.QQAccessExpires, qqUnionID, model.PlatformQQ); err != nil {
- switch nErr := errors.Cause(err).(type) {
- case *mysql.MySQLError:
- if nErr.Number == _mySQLErrCodeDuplicateEntry {
- log.Error("checkQQ, update qq duplicate, mid(%d) unionid(%s)", a.Mid, qqUnionID)
- err = nil
- //var (
- // u *model.SnsUser
- // aso *model.AsoAccountSns
- //)
- //if u, err = s.d.SnsUserByUnionID(context.Background(), qqUnionID, model.PlatformQQ); err != nil {
- // return
- //}
- //if _, err = s.d.DelSnsUser(context.Background(), u.Mid, model.PlatformQQ); err != nil {
- // return
- //}
- //if _, err = s.d.UpdateSnsUser(context.Background(), a.Mid, a.QQAccessExpires, qqUnionID, model.PlatformQQ); err != nil {
- // return
- //}
- //if aso, err = s.d.AsoAccountSnsByMid(context.Background(), u.Mid); err != nil {
- // return
- //}
- //if qqUnionID, err = s.d.QQUnionID(context.Background(), a.QQOpenid); err != nil {
- // return
- //}
- //if _, err = s.d.AddSnsUser(context.Background(), aso.Mid, aso.QQAccessExpires, qqUnionID, model.PlatformQQ); err != nil {
- // return
- //}
- }
- }
- return
- }
- } else if sns.Expires != a.QQAccessExpires {
- log.Error("qq expires not match, new(%+v), old(%+v)", sns, a)
- if err = s.updateSns(a.Mid, a.QQAccessExpires, a.QQOpenid, a.QQAccessToken, model.PlatformQQ); err != nil {
- return
- }
- return
- }
- s.cache.Do(context.Background(), func(c context.Context) {
- proto := &model.SnsProto{
- Mid: a.Mid,
- Platform: int32(model.PlatformQQ),
- UnionID: qqUnionID,
- Expires: a.QQAccessExpires,
- }
- s.d.SetSnsCache(c, a.Mid, model.PlatformQQStr, proto)
- })
- return
- }
- func (s *Service) checkWeibo(a *model.AsoAccountSns) (err error) {
- sns, err := s.d.SnsUserByMid(context.Background(), a.Mid, model.PlatformWEIBO)
- if err != nil {
- return
- }
- if a.SinaUID == 0 {
- if sns != nil {
- log.Error("weibo not match, old is nil,old(%+v) new(%+v)", a, sns)
- if _, err = s.d.DelSnsUser(context.Background(), a.Mid, model.PlatformWEIBO); err != nil {
- return
- }
- s.cache.Do(context.Background(), func(c context.Context) {
- s.d.DelSnsCache(c, a.Mid, model.PlatformWEIBOStr)
- })
- }
- return
- }
- if sns == nil {
- log.Error("weibo not match, new not exists, old(%+v)", a)
- if _, err = s.d.AddSnsUser(context.Background(), a.Mid, a.SinaAccessExpires, strconv.FormatInt(a.SinaUID, 10), model.PlatformWEIBO); err != nil {
- switch nErr := errors.Cause(err).(type) {
- case *mysql.MySQLError:
- if nErr.Number == _mySQLErrCodeDuplicateEntry {
- log.Error("checkWeibo, add weibo duplicate, mid(%d) unionid(%s)", a.Mid, strconv.FormatInt(a.SinaUID, 10))
- err = nil
- //var (
- // u *model.SnsUser
- // aso *model.AsoAccountSns
- //)
- //if u, err = s.d.SnsUserByUnionID(context.Background(), strconv.FormatInt(a.SinaUID, 10), model.PlatformWEIBO); err != nil {
- // return
- //}
- //if _, err = s.d.DelSnsUser(context.Background(), u.Mid, model.PlatformWEIBO); err != nil {
- // return
- //}
- //if _, err = s.d.AddSnsUser(context.Background(), a.Mid, a.SinaAccessExpires, strconv.FormatInt(a.SinaUID, 10), model.PlatformWEIBO); err != nil {
- // return
- //}
- //if aso, err = s.d.AsoAccountSnsByMid(context.Background(), u.Mid); err != nil {
- // return
- //}
- //if _, err = s.d.AddSnsUser(context.Background(), aso.Mid, aso.SinaAccessExpires, strconv.FormatInt(aso.SinaUID, 10), model.PlatformWEIBO); err != nil {
- // return
- //}
- }
- }
- return
- }
- } else if sns.UnionID != strconv.FormatInt(a.SinaUID, 10) {
- log.Error("weibo not match, new(%s), mid(%d) old(%d)", sns.UnionID, a.Mid, a.SinaUID)
- if _, err = s.d.UpdateSnsUser(context.Background(), a.Mid, a.SinaAccessExpires, strconv.FormatInt(a.SinaUID, 10), model.PlatformWEIBO); err != nil {
- switch nErr := errors.Cause(err).(type) {
- case *mysql.MySQLError:
- if nErr.Number == _mySQLErrCodeDuplicateEntry {
- log.Error("checkWeibo, update weibo duplicate, mid(%d) unionid(%s)", a.Mid, strconv.FormatInt(a.SinaUID, 10))
- err = nil
- //var (
- // u *model.SnsUser
- // aso *model.AsoAccountSns
- //)
- //if u, err = s.d.SnsUserByUnionID(context.Background(), strconv.FormatInt(a.SinaUID, 10), model.PlatformWEIBO); err != nil {
- // return
- //}
- //if _, err = s.d.DelSnsUser(context.Background(), u.Mid, model.PlatformWEIBO); err != nil {
- // return
- //}
- //if _, err = s.d.UpdateSnsUser(context.Background(), a.Mid, a.SinaAccessExpires, strconv.FormatInt(a.SinaUID, 10), model.PlatformWEIBO); err != nil {
- // return
- //}
- //if aso, err = s.d.AsoAccountSnsByMid(context.Background(), u.Mid); err != nil {
- // return
- //}
- //if _, err = s.d.AddSnsUser(context.Background(), aso.Mid, aso.SinaAccessExpires, strconv.FormatInt(aso.SinaUID, 10), model.PlatformWEIBO); err != nil {
- // return
- //}
- }
- }
- return
- }
- } else if sns.Expires != a.SinaAccessExpires {
- log.Error("weibo expires not match, new(%+v), old(%+v)", sns, a)
- if err = s.updateSns(a.Mid, a.SinaAccessExpires, strconv.FormatInt(a.SinaUID, 10), a.SinaAccessToken, model.PlatformWEIBO); err != nil {
- return
- }
- return
- }
- s.cache.Do(context.Background(), func(c context.Context) {
- proto := &model.SnsProto{
- Mid: a.Mid,
- Platform: int32(model.PlatformWEIBO),
- UnionID: strconv.FormatInt(a.SinaUID, 10),
- Expires: a.SinaAccessExpires,
- }
- s.d.SetSnsCache(c, a.Mid, model.PlatformWEIBOStr, proto)
- })
- return
- }
|