From 47a0a892b672213525ffc7e7306ddf5b167a2131 Mon Sep 17 00:00:00 2001 From: gazebo Date: Fri, 6 Sep 2019 16:26:47 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E9=87=8D=E6=9E=84=E4=BA=86dao.GetAuthBind?= =?UTF-8?q?=E5=92=8Cdao.GetUserBindAuthInfo=20-=20AuthBind=E7=9A=84?= =?UTF-8?q?=E5=94=AF=E4=B8=80=E7=B4=A2=E5=BC=95=E8=B0=83=E6=95=B4=E4=B8=BA?= =?UTF-8?q?"AuthID",=20"Type",=20"BindType",=20"DeletedAt"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- business/auth2/auth2.go | 2 +- business/auth2/authprovider/defauther.go | 4 +- .../auth2/authprovider/password/password.go | 4 +- business/jxstore/cms/user2.go | 4 +- business/jxstore/tempop/tempop.go | 2 +- business/jxutils/jxutils.go | 16 ++++--- business/jxutils/weixinmsg/weixinmsg.go | 8 ++-- business/model/auth2.go | 10 ++--- business/model/dao/dao_auth2.go | 42 +++++++------------ 9 files changed, 44 insertions(+), 48 deletions(-) diff --git a/business/auth2/auth2.go b/business/auth2/auth2.go index 5eb6d7101..e0ebc5783 100644 --- a/business/auth2/auth2.go +++ b/business/auth2/auth2.go @@ -435,5 +435,5 @@ func DisableUser(userID, operatorUserName string) (err error) { } func GetUserBindAuthInfo(userID string) (authList []*model.AuthBind, err error) { - return dao.GetUserBindAuthInfo(dao.GetDB(), userID, model.AuthBindTypeAuth, "", nil) + return dao.GetUserBindAuthInfo(dao.GetDB(), userID, model.AuthBindTypeAuth, nil, "", "") } diff --git a/business/auth2/authprovider/defauther.go b/business/auth2/authprovider/defauther.go index d2d108e08..fd6b056f2 100644 --- a/business/auth2/authprovider/defauther.go +++ b/business/auth2/authprovider/defauther.go @@ -61,7 +61,7 @@ func (a *DefAuther) GetUserType() (userType int8) { func (a *DefAuther) UnionFindAuthBind(curAuthType string, unionAuthTypeList []string, openID, unionID string, authDetail interface{}) (authBindEx *auth2.AuthBindEx, err error) { db := dao.GetDB() var authBind *model.AuthBind - if authBind, err = dao.GetAuthBind(db, "", model.AuthBindTypeAuth, curAuthType, openID); err == nil { // 直接找到了 + if authBind, err = dao.GetAuthBind(db, model.AuthBindTypeAuth, curAuthType, openID); err == nil { // 直接找到了 authBindEx = &auth2.AuthBindEx{ AuthBind: *authBind, } @@ -71,7 +71,7 @@ func (a *DefAuther) UnionFindAuthBind(curAuthType string, unionAuthTypeList []st } else if dao.IsNoRowsError(err) { // 直接找不到,尝试unionID if unionID != "" { // 且有unionID var authBindList []*model.AuthBind - if authBindList, err = dao.GetUserBindAuthInfo(db, "", model.AuthBindTypeAuth, unionID, unionAuthTypeList); err == nil && len(authBindList) > 0 { // 通过unionID找到至少一个认证方式 + if authBindList, err = dao.GetUserBindAuthInfo(db, "", model.AuthBindTypeAuth, unionAuthTypeList, "", unionID); err == nil && len(authBindList) > 0 { // 通过unionID找到至少一个认证方式 authBind = authBindList[0] authBind.Type = curAuthType authBind.AuthID = openID diff --git a/business/auth2/authprovider/password/password.go b/business/auth2/authprovider/password/password.go index 26f26732d..06c472a6d 100644 --- a/business/auth2/authprovider/password/password.go +++ b/business/auth2/authprovider/password/password.go @@ -37,7 +37,7 @@ func init() { func (a *Auther) VerifySecret(userID, passMD5 string) (authBindEx *auth2.AuthBindEx, err error) { globals.SugarLogger.Debugf("localpass VerifySecret userID:%s", userID) var authBind *model.AuthBind - if authBind, err = dao.GetAuthBind(dao.GetDB(), "", model.AuthBindTypeAuth, AuthType, userID); err == nil { + if authBind, err = dao.GetAuthBind(dao.GetDB(), model.AuthBindTypeAuth, AuthType, userID); err == nil { if err = a.checkPassword(authBind, passMD5); err == nil { authBindEx = &auth2.AuthBindEx{ AuthBind: *authBind, @@ -55,7 +55,7 @@ func (a *Auther) ChangePassword(userID, oldPassMD5, newPassMD5 string) (err erro db := dao.GetDB() salt := utils.GetUUID() encryptPwd := a.encryptPassword(newPassMD5, salt) - if authBind, err = dao.GetAuthBind(db, "", model.AuthBindTypeAuth, AuthType, userID); err == nil { + if authBind, err = dao.GetAuthBind(db, model.AuthBindTypeAuth, AuthType, userID); err == nil { if err = a.checkPassword(authBind, oldPassMD5); err == nil || authBind.AuthSecret == "" { // 如果原密码为空,不判断原密码,代表重置密码 _, err = dao.UpdateEntityLogically(db, authBind, map[string]interface{}{ "AuthSecret": encryptPwd, diff --git a/business/jxstore/cms/user2.go b/business/jxstore/cms/user2.go index 954f66590..5e745dcbc 100644 --- a/business/jxstore/cms/user2.go +++ b/business/jxstore/cms/user2.go @@ -183,7 +183,7 @@ func HandleOrder4Consignee(order *model.GoodsOrder) (err error) { vendorUserID := order.VendorUserID if vendorUserID != "" { - authInfo, err2 := dao.GetAuthBind(db, "", model.AuthBindTypeID, authType, vendorUserID) + authInfo, err2 := dao.GetAuthBind(db, model.AuthBindTypeID, authType, vendorUserID) if err = err2; err != nil && !dao.IsNoRowsError(err) { return err } @@ -309,7 +309,7 @@ func OnDingDingMsg(msg map[string]interface{}) (callbackResponse *dingdingapi.Ca for _, userID := range msg[dingdingapi.KeyUserID].([]interface{}) { userIDStr := utils.Interface2String(userID) globals.SugarLogger.Debugf("OnDingDingMsg dingding user:%s left company", userIDStr) - if authBind, err = dao.GetAuthBind(db, "", model.AuthBindTypeAuth, dingding.AuthTypeStaff, userIDStr); err == nil { // 直接找到了 + if authBind, err = dao.GetAuthBind(db, model.AuthBindTypeAuth, dingding.AuthTypeStaff, userIDStr); err == nil { // 直接找到了 globals.SugarLogger.Debugf("OnDingDingMsg dingding user:%s, userID:%s left company", userIDStr, authBind.UserID) if err = DisableUser(jxcontext.AdminCtx, authBind.UserID); err != nil { globals.SugarLogger.Errorf("OnDingDingMsg failed with error:%v", err) diff --git a/business/jxstore/tempop/tempop.go b/business/jxstore/tempop/tempop.go index 2d7619ffe..a5afa763d 100644 --- a/business/jxstore/tempop/tempop.go +++ b/business/jxstore/tempop/tempop.go @@ -1066,7 +1066,7 @@ func CreateConsumerFromOrders(ctx *jxcontext.Context, vendorIDs []int, fromDate, } } } - if order2 == nil { + if order2 == nil && order.VendorID == model.VendorIDMTWM && time.Now().Sub(curDate) < 30*24*time.Hour { order2, err = handler.GetOrder(order.VendorOrderID) } if order2 != nil { diff --git a/business/jxutils/jxutils.go b/business/jxutils/jxutils.go index eb96e823d..6a36a8263 100644 --- a/business/jxutils/jxutils.go +++ b/business/jxutils/jxutils.go @@ -427,14 +427,14 @@ func HandleUserWXRemark(db *dao.DaoDB, mobile string, mobileIsUerID bool) (err e if db == nil { db = dao.GetDB() } - openID := "" + openIDs := []string{} storeID := 0 remark := "" if !globals.DisableWXAuth1 { wxinfo, err := dao.GetUserStoreInfo(db, "tel", mobile) if err == nil { - openID = wxinfo.OpenID + openIDs = []string{wxinfo.OpenID} storeID = wxinfo.JxStoreID } } @@ -449,9 +449,11 @@ func HandleUserWXRemark(db *dao.DaoDB, mobile string, mobileIsUerID bool) (err e } } if userID != "" { - authBind, err2 := dao.GetAuthBind(db, userID, model.AuthBindTypeAuth, weixin.AuthTypeMP, "") + authBindList, err2 := dao.GetUserBindAuthInfo(db, userID, model.AuthBindTypeAuth, []string{weixin.AuthTypeMP}, "", "") if err = err2; err == nil { - openID = authBind.AuthID + for _, v := range authBindList { + openIDs = append(openIDs, v.AuthID) + } } roleList, err2 := api2.RoleMan.GetUserRoleList(userID) if err = err2; err == nil && len(roleList) > 0 { @@ -460,7 +462,7 @@ func HandleUserWXRemark(db *dao.DaoDB, mobile string, mobileIsUerID bool) (err e } } - if openID != "" { + if len(openIDs) > 0 { if storeID > 0 { store := &model.Store{} store.ID = storeID @@ -475,7 +477,9 @@ func HandleUserWXRemark(db *dao.DaoDB, mobile string, mobileIsUerID bool) (err e } if err == nil { if globals.EnableStoreWrite { - err = api.WeixinAPI.CBUpdateRemark(openID, remark) + for _, openID := range openIDs { + err = api.WeixinAPI.CBUpdateRemark(openID, remark) + } } } } diff --git a/business/jxutils/weixinmsg/weixinmsg.go b/business/jxutils/weixinmsg/weixinmsg.go index 23b766383..cb4062c32 100644 --- a/business/jxutils/weixinmsg/weixinmsg.go +++ b/business/jxutils/weixinmsg/weixinmsg.go @@ -118,9 +118,11 @@ func GetWeixinOpenIDsFromStoreID(storeID int) (retVal []string) { if globals.EnableWXAuth2 { if userIDList, err2 := api2.RoleMan.GetRoleUserList(autils.NewRole(authz.StoreRoleBoss, storeID)); err2 == nil { for _, v := range userIDList { - if authInfo, err2 := dao.GetAuthBind(db, v, model.AuthBindTypeAuth, weixin.AuthTypeMP, ""); err2 == nil { - retVal = append(retVal, authInfo.AuthID) - openIDMap[authInfo.AuthID] = 1 + if authList, err2 := dao.GetUserBindAuthInfo(db, v, model.AuthBindTypeAuth, []string{weixin.AuthTypeMP}, "", ""); err2 == nil { + for _, v := range authList { + retVal = append(retVal, v.AuthID) + openIDMap[v.AuthID] = 1 + } } } } diff --git a/business/model/auth2.go b/business/model/auth2.go index 7ac38b378..cb15d76c9 100644 --- a/business/model/auth2.go +++ b/business/model/auth2.go @@ -16,12 +16,12 @@ const ( type AuthBind struct { ModelIDCULD - UserID string `orm:"size(48);column(user_id)" json:"userID"` + AuthID string `orm:"size(48);column(auth_id)" json:"authID"` BindType int8 `json:"bindType"` Type string `orm:"size(16)" json:"type"` - Status int8 `json:"status"` - AuthID string `orm:"size(48);column(auth_id)" json:"authID"` + UserID string `orm:"size(48);column(user_id)" json:"userID"` + Status int8 `json:"status"` AuthID2 string `orm:"size(48);column(auth_id2);index" json:"authID2"` AuthSecret string `orm:"size(48)" json:"-"` AuthSecret2 string `orm:"size(48)" json:"-"` @@ -31,7 +31,7 @@ type AuthBind struct { func (*AuthBind) TableUnique() [][]string { return [][]string{ - []string{"UserID", "Type", "DeletedAt"}, - []string{"AuthID", "Type", "DeletedAt"}, + // []string{"UserID", "Type", "DeletedAt"}, // 这个其实UserID是属性,而不是key + []string{"AuthID", "Type", "BindType", "DeletedAt"}, } } diff --git a/business/model/dao/dao_auth2.go b/business/model/dao/dao_auth2.go index dd1b5d6b4..e3e9c441b 100644 --- a/business/model/dao/dao_auth2.go +++ b/business/model/dao/dao_auth2.go @@ -7,41 +7,27 @@ import ( "git.rosy.net.cn/jx-callback/business/model" ) -func GetAuthBind(db *DaoDB, userID string, bindType int, authType, authID string) (authBind *model.AuthBind, err error) { - if userID == "" && authID == "" { - return nil, errors.New("userID, authID, authID2不能全为空") +func GetAuthBind(db *DaoDB, bindType int, authType, authID string) (authBind *model.AuthBind, err error) { + if bindType == model.AuthBindTypeAll || authType == "" || authID == "" { + return nil, errors.New("bindType不是能all, authType和authID都要有值") } sql := ` SELECT * FROM auth_bind t1 - WHERE t1.deleted_at = ? AND t1.status = ? - ` + WHERE t1.deleted_at = ? AND t1.status = ? AND t1.bind_type = ? AND t1.type = ? AND t1.auth_id = ?` sqlParams := []interface{}{ utils.DefaultTimeValue, model.AuthBindStatusNormal, - } - if userID != "" { - sql += " AND t1.user_id = ?" - sqlParams = append(sqlParams, userID) - } - if bindType != model.AuthBindTypeAll { - sql += " AND t1.bind_type = ?" - sqlParams = append(sqlParams, bindType) - } - if authType != "" { - sql += " AND t1.type = ?" - sqlParams = append(sqlParams, authType) - } - if authID != "" { - sql += " AND t1.auth_id = ?" - sqlParams = append(sqlParams, authID) + bindType, + authType, + authID, } // globals.SugarLogger.Debugf("GetAuthBind sql:%s, sqlParams:%s", sql, utils.Format4Output(sqlParams, false)) err = GetRow(db, &authBind, sql, sqlParams...) return authBind, err } -func GetUserBindAuthInfo(db *DaoDB, userID string, bindType int, authID2 string, typeList []string) (authList []*model.AuthBind, err error) { +func GetUserBindAuthInfo(db *DaoDB, userID string, bindType int, typeList []string, authID, authID2 string) (authList []*model.AuthBind, err error) { sql := ` SELECT * FROM auth_bind t1 @@ -58,14 +44,18 @@ func GetUserBindAuthInfo(db *DaoDB, userID string, bindType int, authID2 string, sql += " AND t1.bind_type = ?" sqlParams = append(sqlParams, bindType) } - if authID2 != "" { - sql += " AND t1.auth_id2 = ?" - sqlParams = append(sqlParams, authID2) - } if len(typeList) > 0 { sql += " AND t1.type IN (" + GenQuestionMarks(len(typeList)) + ")" sqlParams = append(sqlParams, typeList) } + if authID != "" { + sql += " AND t1.auth_id = ?" + sqlParams = append(sqlParams, authID) + } + if authID2 != "" { + sql += " AND t1.auth_id2 = ?" + sqlParams = append(sqlParams, authID2) + } err = GetRows(db, &authList, sql, sqlParams...) return authList, err