From 6e3b9a82151badeaaff5536d09725c2d19085008 Mon Sep 17 00:00:00 2001 From: gazebo Date: Thu, 11 Apr 2019 11:59:04 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=B0=86=E5=BE=AE=E4=BF=A1=E7=9A=84?= =?UTF-8?q?=E5=A4=87=E6=B3=A8=E6=94=B9=E4=B8=BA=E5=BA=97=E5=90=8D=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E7=9A=84=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- business/jxcallback/auth/weixin/weixin.go | 3 ++ business/jxstore/cms/user.go | 57 +++++------------------ business/jxstore/tempop/tempop.go | 23 +++++++++ business/jxutils/jxutils.go | 29 ++++++++++++ business/model/dao/dao_user.go | 29 ++++++++++++ business/model/dao/dao_user_test.go | 8 ++++ controllers/temp_op.go | 15 ++++++ routers/commentsRouter_controllers.go | 9 ++++ 8 files changed, 128 insertions(+), 45 deletions(-) diff --git a/business/jxcallback/auth/weixin/weixin.go b/business/jxcallback/auth/weixin/weixin.go index 534872f6b..cc0bf5896 100644 --- a/business/jxcallback/auth/weixin/weixin.go +++ b/business/jxcallback/auth/weixin/weixin.go @@ -10,6 +10,7 @@ import ( "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/jx-callback/business/jxcallback/auth" "git.rosy.net.cn/jx-callback/business/jxcallback/auth/mobile" + "git.rosy.net.cn/jx-callback/business/jxutils" "git.rosy.net.cn/jx-callback/business/jxutils/jxcontext" "git.rosy.net.cn/jx-callback/business/model/dao" "git.rosy.net.cn/jx-callback/globals" @@ -134,6 +135,7 @@ func BindMobile(token, mobileNum, code, nickname string) (err error) { err = auth.ConvertErr2NoUser(dao.UpdateWeiXinUser(dao.GetDB(), mobileNum, nickname, wxUserinfo.UnionID, wxUserinfo.OpenID, ""), mobileNum) } } + jxutils.HandleUserWXRemark(nil, mobileNum) return err } @@ -162,6 +164,7 @@ func BindMobile2(openid, secret, mobileNum, verifyCode, nickname string) (loginI } } } + jxutils.HandleUserWXRemark(nil, mobileNum) return loginInfo, err } diff --git a/business/jxstore/cms/user.go b/business/jxstore/cms/user.go index 48b6983df..6263d164f 100644 --- a/business/jxstore/cms/user.go +++ b/business/jxstore/cms/user.go @@ -7,6 +7,7 @@ import ( "git.rosy.net.cn/jx-callback/business/jxcallback/auth" "git.rosy.net.cn/jx-callback/business/jxcallback/auth/mobile" "git.rosy.net.cn/jx-callback/business/jxcallback/auth/weixin" + "git.rosy.net.cn/jx-callback/business/jxutils" "git.rosy.net.cn/jx-callback/business/jxutils/jxcontext" "git.rosy.net.cn/jx-callback/business/model" "git.rosy.net.cn/jx-callback/business/model/dao" @@ -15,13 +16,6 @@ import ( "github.com/astaxie/beego/orm" ) -type StoreUserInfo struct { - legacymodel.WeiXins - ParentMobile string `json:"parentMobile"` - Members []*legacymodel.WeiXins `orm:"-" json:"members"` - MembersStr string `json:"-"` -} - var ( LoginTypeFieldMap = map[string]string{ mobile.LoginType: "tel", @@ -30,7 +24,7 @@ var ( } ) -func GetStoreUsers(ctx *jxcontext.Context, storeID int) (storeUserInfos []*StoreUserInfo, err error) { +func GetStoreUsers(ctx *jxcontext.Context, storeID int) (storeUserInfos []*dao.StoreUserInfo, err error) { sql := ` SELECT t1.id, t1.jxstoreid, t1.openid, t1.tel, t1.nickname, t1.parentid, t3.tel parent_mobile, CONCAT("[", GROUP_CONCAT(CONCAT('{"id":', t2.id, ',"parentID":', t2.parentid, ',"tel":"', t2.tel, '","nickname":"', IF(t2.nickname IS NULL, "", t2.nickname), '"}')), "]") members_str @@ -51,61 +45,32 @@ func GetStoreUsers(ctx *jxcontext.Context, storeID int) (storeUserInfos []*Store return storeUserInfos, err } -func GetUserInfo(ctx *jxcontext.Context, mobile string) (storeUserInfo *StoreUserInfo, err error) { - sql := ` - SELECT t1.id, IF(t3.id IS NULL, t1.jxstoreid, t3.jxstoreid) jxstoreid, t1.openid, t1.tel, t1.nickname, t1.parentid, t3.tel parent_mobile, - CONCAT("[", GROUP_CONCAT(CONCAT('{"id":', t2.id, ',"tel":"', t2.tel, '","nickname":"', IF(t2.nickname IS NULL, "", t2.nickname), '"}')), "]") members_str - FROM weixins t1 - LEFT JOIN weixins t2 ON t2.parentid = t1.id - LEFT JOIN weixins t3 ON t1.parentid = t3.id - WHERE t1.tel = ? - GROUP BY 1,2,3,4,5,6,7; - ` - storeUserInfo = new(StoreUserInfo) - if err = dao.GetRow(nil, storeUserInfo, sql, mobile); err == nil { - if storeUserInfo.MembersStr != "" { - err = utils.UnmarshalUseNumber([]byte(storeUserInfo.MembersStr), &storeUserInfo.Members) - } - } - return storeUserInfo, err +func GetUserInfo(ctx *jxcontext.Context, mobile string) (storeUserInfo *dao.StoreUserInfo, err error) { + return dao.GetUserStoreInfo(dao.GetDB(), "tel", mobile) } -func GetSelfInfo(ctx *jxcontext.Context) (storeUserInfo *StoreUserInfo, err error) { +func GetSelfInfo(ctx *jxcontext.Context) (storeUserInfo *dao.StoreUserInfo, err error) { loginInfo := ctx.GetLoginInfo() if loginInfo == nil { return nil, auth.ErrAPINeedRealLogin } - fieldName := LoginTypeFieldMap[loginInfo.GetAuthType()] if fieldName == "" { return nil, auth.ErrIllegalLoginType } - sql := fmt.Sprintf(` - SELECT t1.id, IF(t3.id IS NULL, t1.jxstoreid, t3.jxstoreid) jxstoreid, t1.openid, t1.tel, t1.nickname, t1.parentid, t3.tel parent_mobile, - CONCAT("[", GROUP_CONCAT(CONCAT('{"id":', t2.id, ',"tel":"', t2.tel, '","nickname":"', IF(t2.nickname IS NULL, "", t2.nickname), '"}')), "]") members_str - FROM weixins t1 - LEFT JOIN weixins t2 ON t2.parentid = t1.id - LEFT JOIN weixins t3 ON t1.parentid = t3.id - WHERE t1.%s = ? - GROUP BY 1,2,3,4,5,6,7; - `, fieldName) - storeUserInfo = new(StoreUserInfo) - if err = dao.GetRow(nil, storeUserInfo, sql, loginInfo.GetAuthID()); err == nil || err == orm.ErrNoRows { // todo - err = nil - if storeUserInfo.MembersStr != "" { - err = utils.UnmarshalUseNumber([]byte(storeUserInfo.MembersStr), &storeUserInfo.Members) - } - } - return storeUserInfo, err + return dao.GetUserStoreInfo(dao.GetDB(), fieldName, loginInfo.GetAuthID()) } func UnbindMobile(ctx *jxcontext.Context, mobile string) (num int64, err error) { - return dao.UpdateEntityByKV(nil, &legacymodel.WeiXins{}, map[string]interface{}{ + db := dao.GetDB() + num, err = dao.UpdateEntityByKV(db, &legacymodel.WeiXins{}, map[string]interface{}{ "JxStoreID": 0, "ParentID": -1, }, map[string]interface{}{ "Tel": mobile, }) + jxutils.HandleUserWXRemark(db, mobile) + return num, err } func BindMobile2Store(ctx *jxcontext.Context, mobile string, storeID int) (num int64, err error) { @@ -126,6 +91,7 @@ func BindMobile2Store(ctx *jxcontext.Context, mobile string, storeID int) (num i } } } + jxutils.HandleUserWXRemark(db, mobile) return num, err } @@ -157,6 +123,7 @@ func AddMobile2Mobile(ctx *jxcontext.Context, parentMobile, mobile string) (num err = fmt.Errorf("%s本身是成员", parentMobile) } } + jxutils.HandleUserWXRemark(db, mobile) return num, err } diff --git a/business/jxstore/tempop/tempop.go b/business/jxstore/tempop/tempop.go index 30a9d239e..dd9a82e48 100644 --- a/business/jxstore/tempop/tempop.go +++ b/business/jxstore/tempop/tempop.go @@ -892,3 +892,26 @@ func PrintMsg(ctx *jxcontext.Context, vendorID int, id1, id2, msgTitle, msgConte } return handler.PrintMsg(ctx, id1, id2, msgTitle, msgContent) } + +func UpdateAllWeiXinRemark(ctx *jxcontext.Context, isAsync, isContinueWhenError bool) (hint string, err error) { + var weixinList []*legacymodel.WeiXins + if err = dao.GetRows(dao.GetDB(), &weixinList, ` + SELECT * + FROM weixins + WHERE openid <> '' AND tel <> '' + `); err == nil { + rootTask := tasksch.NewParallelTask("刷新微信备注", tasksch.NewParallelConfig().SetIsContinueWhenError(isContinueWhenError), ctx, + func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { + weixins := batchItemList[0].(*legacymodel.WeiXins) + err = jxutils.HandleUserWXRemark(dao.GetDB(), weixins.Tel) + return nil, err + }, weixinList) + tasksch.ManageTask(rootTask).Run() + if !isAsync { + _, err = rootTask.GetResult(0) + } else { + hint = rootTask.ID + } + } + return hint, err +} diff --git a/business/jxutils/jxutils.go b/business/jxutils/jxutils.go index 791e35da9..ad8fcbe15 100644 --- a/business/jxutils/jxutils.go +++ b/business/jxutils/jxutils.go @@ -13,6 +13,7 @@ import ( "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/baseapi/utils/routinepool" "git.rosy.net.cn/jx-callback/business/model" + "git.rosy.net.cn/jx-callback/business/model/dao" "git.rosy.net.cn/jx-callback/globals/api" ) @@ -385,3 +386,31 @@ func Strings2Objs(strAndObjAddPairs ...interface{}) (err error) { } return nil } + +func HandleUserWXRemark(db *dao.DaoDB, mobile string) (err error) { + if db == nil { + db = dao.GetDB() + } + wxinfo, err := dao.GetUserStoreInfo(db, "tel", mobile) + if err == nil { + if wxinfo.OpenID != "" { + remark := "" + if wxinfo.JxStoreID > 0 { + store := &model.Store{} + store.ID = wxinfo.JxStoreID + if err = dao.GetEntity(db, store); err == nil { + city := &model.Place{ + Code: store.CityCode, + } + if err = dao.GetEntity(db, city, "Code"); err == nil { + remark = city.Name + "-" + store.Name + } + } + } + if err == nil { + err = api.WeixinAPI.CBUpdateRemark(wxinfo.OpenID, remark) + } + } + } + return err +} diff --git a/business/model/dao/dao_user.go b/business/model/dao/dao_user.go index 7f15f0fd3..604f8bbb8 100644 --- a/business/model/dao/dao_user.go +++ b/business/model/dao/dao_user.go @@ -1,9 +1,19 @@ package dao import ( + "fmt" + + "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/jx-callback/business/model/legacymodel" ) +type StoreUserInfo struct { + legacymodel.WeiXins + ParentMobile string `json:"parentMobile"` + Members []*legacymodel.WeiXins `orm:"-" json:"members"` + MembersStr string `json:"-"` +} + func CreateWeiXins(db *DaoDB, user *legacymodel.WeiXins) (err error) { Begin(db) if err = CreateEntity(db, user); err != nil { @@ -87,3 +97,22 @@ func UpdateWeiXinUser(db *DaoDB, tel, nickname, unionID, openID, miniOpenID stri } return err } + +func GetUserStoreInfo(db *DaoDB, fieldName, fieldValue string) (storeUserInfo *StoreUserInfo, err error) { + sql := fmt.Sprintf(` + SELECT t1.id, IF(t3.id IS NULL, t1.jxstoreid, t3.jxstoreid) jxstoreid, t1.openid, t1.tel, t1.nickname, t1.parentid, t3.tel parent_mobile, + CONCAT("[", GROUP_CONCAT(CONCAT('{"id":', t2.id, ',"tel":"', t2.tel, '","nickname":"', IF(t2.nickname IS NULL, "", t2.nickname), '"}')), "]") members_str + FROM weixins t1 + LEFT JOIN weixins t2 ON t2.parentid = t1.id + LEFT JOIN weixins t3 ON t1.parentid = t3.id + WHERE t1.%s = ? + GROUP BY 1,2,3,4,5,6,7; + `, fieldName) + if err = GetRow(db, &storeUserInfo, sql, fieldValue); err == nil { // todo + err = nil + if storeUserInfo.MembersStr != "" { + err = utils.UnmarshalUseNumber([]byte(storeUserInfo.MembersStr), &storeUserInfo.Members) + } + } + return storeUserInfo, err +} diff --git a/business/model/dao/dao_user_test.go b/business/model/dao/dao_user_test.go index 0ca12960c..1de45e6be 100644 --- a/business/model/dao/dao_user_test.go +++ b/business/model/dao/dao_user_test.go @@ -31,3 +31,11 @@ func TestUpdateWeiXinUser(t *testing.T) { t.Fatal(err) } } + +func TestGetUserStoreInfo(t *testing.T) { + user, err := GetUserStoreInfo(GetDB(), "tel", "18180948107") + if err != nil { + t.Fatal(err) + } + globals.SugarLogger.Debug(utils.Format4Output(user, false)) +} diff --git a/controllers/temp_op.go b/controllers/temp_op.go index 99117d3ad..9dff79877 100644 --- a/controllers/temp_op.go +++ b/controllers/temp_op.go @@ -180,3 +180,18 @@ func (c *InitDataController) PrintMsg() { return retVal, "", err }) } + +// @Title 刷新微信备注 +// @Description 刷新微信备注 +// @Param token header string true "认证token" +// @Param isAsync formData bool false "是否异步操作" +// @Param isContinueWhenError formData bool false "单个同步失败是否继续,缺省false" +// @Success 200 {object} controllers.CallResult +// @Failure 200 {object} controllers.CallResult +// @router /UpdateAllWeiXinRemark [post] +func (c *InitDataController) UpdateAllWeiXinRemark() { + c.callUpdateAllWeiXinRemark(func(params *tInitdataUpdateAllWeiXinRemarkParams) (retVal interface{}, errCode string, err error) { + retVal, err = tempop.UpdateAllWeiXinRemark(params.Ctx, params.IsAsync, params.IsContinueWhenError) + return retVal, "", err + }) +} diff --git a/routers/commentsRouter_controllers.go b/routers/commentsRouter_controllers.go index 06ae89353..19febb6ca 100644 --- a/routers/commentsRouter_controllers.go +++ b/routers/commentsRouter_controllers.go @@ -457,6 +457,15 @@ func init() { Filters: nil, Params: nil}) + beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:InitDataController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:InitDataController"], + beego.ControllerComments{ + Method: "UpdateAllWeiXinRemark", + Router: `/UpdateAllWeiXinRemark`, + AllowHTTPMethods: []string{"post"}, + MethodParams: param.Make(), + Filters: nil, + Params: nil}) + beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:InitDataController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:InitDataController"], beego.ControllerComments{ Method: "UploadWeimobImg4SkuName",