|
@@ -1,26 +1,17 @@
|
|
|
package jy
|
|
|
|
|
|
import (
|
|
|
- "database/sql"
|
|
|
"fmt"
|
|
|
- "github.com/go-xweb/httpsession"
|
|
|
- "go.mongodb.org/mongo-driver/bson"
|
|
|
- "log"
|
|
|
. "mongodb"
|
|
|
qutil "qfw/util"
|
|
|
"qfw/util/mysql"
|
|
|
- "qfw/util/redis"
|
|
|
"strings"
|
|
|
- "time"
|
|
|
-)
|
|
|
|
|
|
-var (
|
|
|
- aboutTableRemove = []string{"pushspace", "pushspace_entniche", "pushspace_entniche_project", "pushspace_entniche_temp", "pushspace_entniche_wait", "pushspace_fail", "pushspace_project", "pushspace_statistic", "pushspace_temp", "pushspace_vip", "pushspace_fail"}
|
|
|
- WxField = []string{"s_appponetype", "s_jpushid", "s_opushid", "s_appversion", "s_unionid", "s_name", "s_nickname", "i_sex", "s_country", "s_province", "s_city", "s_headimageurl", "s_m_openid", "a_m_openid", "i_ispush", "i_applystatus"}
|
|
|
- PhField = []string{"s_appponetype", "s_jpushid", "s_opushid", "s_appversion", "s_phone", "s_password"}
|
|
|
+ "github.com/go-xweb/httpsession"
|
|
|
+ "go.mongodb.org/mongo-driver/bson"
|
|
|
)
|
|
|
|
|
|
-//p19 账号合并
|
|
|
+//dev3.7 账号合并
|
|
|
type UserMerge struct {
|
|
|
mysqlDb *mysql.Mysql
|
|
|
mg MongodbSim
|
|
@@ -35,11 +26,15 @@ func CreateUserMerge(mg MongodbSim, msql *mysql.Mysql, sess *httpsession.Session
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+var (
|
|
|
+ MergeEncrypt = qutil.SimpleEncrypt{Key: "jy_merge_user"}
|
|
|
+)
|
|
|
+
|
|
|
//查询合并
|
|
|
//wId 微信用户id
|
|
|
//pId 手机用户id
|
|
|
//当wId和pId都为空时 即不需要合并
|
|
|
-func (this *UserMerge) MergeQuery() (wId, pId, wName, bPhone, mT string, err error) {
|
|
|
+func (this *UserMerge) MergeQuery() (wId, pId, bPhone string, err error) {
|
|
|
userId, _ := this.sess.Get("userId").(string)
|
|
|
if userId == "" {
|
|
|
err = fmt.Errorf("未获取到用户身份")
|
|
@@ -63,14 +58,11 @@ func (this *UserMerge) MergeQuery() (wId, pId, wName, bPhone, mT string, err err
|
|
|
if qutil.ObjToString((*tud)["s_phone"]) == "" {
|
|
|
wId = userId
|
|
|
pId = BsonIdToSId((*mud)["_id"])
|
|
|
- wName = qutil.ObjToString((*tud)["s_nickname"])
|
|
|
bPhone = qutil.ObjToString((*mud)["s_phone"])
|
|
|
} else {
|
|
|
pId = userId
|
|
|
wId = BsonIdToSId((*mud)["_id"])
|
|
|
- wName = qutil.ObjToString((*mud)["s_nickname"])
|
|
|
}
|
|
|
- mT = "wxMerge"
|
|
|
return
|
|
|
}
|
|
|
}
|
|
@@ -83,8 +75,6 @@ func (this *UserMerge) MergeQuery() (wId, pId, wName, bPhone, mT string, err err
|
|
|
if mud != nil && len(*mud) > 0 {
|
|
|
wId = BsonIdToSId((*tud)["_id"])
|
|
|
pId = BsonIdToSId((*mud)["_id"])
|
|
|
- wName = qutil.ObjToString((*tud)["s_nickname"])
|
|
|
- mT = "phoneMerge"
|
|
|
return
|
|
|
}
|
|
|
} else if s_phone != "" { //当前用户为手机号用户;查询对应的微信用户
|
|
@@ -92,172 +82,16 @@ func (this *UserMerge) MergeQuery() (wId, pId, wName, bPhone, mT string, err err
|
|
|
if mud != nil && len(*mud) > 0 {
|
|
|
wId = BsonIdToSId((*mud)["_id"])
|
|
|
pId = BsonIdToSId((*tud)["_id"])
|
|
|
- wName = qutil.ObjToString((*mud)["s_nickname"])
|
|
|
- mT = "phoneMerge"
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//新用户绑定手机号后 自动合并以手机号账户为主
|
|
|
-//ids[0]=微信id ids[1]=手机号id
|
|
|
-func (this *UserMerge) AutoMerge(ids ...string) (bool, error) {
|
|
|
- wId, pId := "", ""
|
|
|
- var err error
|
|
|
- if len(ids) != 0 {
|
|
|
- if len(ids) != 2 {
|
|
|
- err = fmt.Errorf("自动合并传参异常")
|
|
|
- } else {
|
|
|
- wId, pId = ids[0], ids[1]
|
|
|
- }
|
|
|
- } else { //没有传入合并ids,则查询
|
|
|
- wId, pId, _, _, _, err = this.MergeQuery()
|
|
|
- }
|
|
|
- if err != nil {
|
|
|
- return false, err
|
|
|
- }
|
|
|
- return this.Merge(wId, pId, "22")
|
|
|
-}
|
|
|
-
|
|
|
-//wxid phid 微信id和手机用户id
|
|
|
-//choose 1:保留微信 2:保留手机用户
|
|
|
-//22 自动合并
|
|
|
-func (this *UserMerge) Merge(wId, pId, choose string, noteFunc ...func(...interface{})) (bool, error) {
|
|
|
- nowId, _ := this.sess.Get("userId").(string)
|
|
|
- if !(wId == nowId || pId == nowId) || wId == pId || !(choose == "1" || choose == "2" || choose == "22") {
|
|
|
- return false, fmt.Errorf("%s非法合并操作,wId:%s,pId:%s", nowId, wId, pId)
|
|
|
- }
|
|
|
- wxRes, _ := this.mg.FindById("user", wId, nil)
|
|
|
- phRes, _ := this.mg.FindById("user", pId, nil)
|
|
|
- if wxRes == nil || len(*wxRes) == 0 || phRes == nil || len(*phRes) == 0 {
|
|
|
- return false, fmt.Errorf("查询异常")
|
|
|
- }
|
|
|
-
|
|
|
- //开始合并流程
|
|
|
- var backUserErr, orderErr, updateErr error
|
|
|
- if choose == "1" {
|
|
|
- upset := map[string]interface{}{}
|
|
|
- for _, item := range PhField {
|
|
|
- if value, exists := (*phRes)[item]; exists {
|
|
|
- if _, ok := (*wxRes)[item]; !ok {
|
|
|
- upset[item] = value
|
|
|
- (*wxRes)[item] = value
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- change := map[string]interface{}{
|
|
|
- "$set": upset,
|
|
|
- }
|
|
|
- if (*wxRes)["s_m_phone"] == (*wxRes)["s_phone"] { //s_m_phone和s_phone都存在 保留s_phone
|
|
|
- change["$unset"] = map[string]interface{}{"s_m_phone": ""}
|
|
|
- }
|
|
|
- if !this.mg.UpdateById("user", wId, change) {
|
|
|
- updateErr = fmt.Errorf("自动合并更新异常")
|
|
|
- }
|
|
|
- this.FlushSession(wxRes, "") //更新session
|
|
|
- backUserErr = this.backupsUser(*phRes, choose, wId) //删除用户
|
|
|
- //orderErr = this.mergeOrder(pId, wId) //合并订单
|
|
|
- } else if choose == "2" || choose == "22" {
|
|
|
- upset := map[string]interface{}{}
|
|
|
- for _, item := range WxField {
|
|
|
- if value, exists := (*wxRes)[item]; exists {
|
|
|
- if _, ok := (*phRes)[item]; !ok {
|
|
|
- upset[item] = value
|
|
|
- (*phRes)[item] = value
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- //默认开启所有微信推送
|
|
|
- for _, item := range []string{"o_jy", "o_vipjy"} {
|
|
|
- if (*phRes)[item] != nil {
|
|
|
- upset[fmt.Sprintf("%s.i_wxpush", item)] = 1
|
|
|
- }
|
|
|
- }
|
|
|
- if !this.mg.UpdateById("user", pId, map[string]interface{}{
|
|
|
- "$set": upset,
|
|
|
- }) {
|
|
|
- updateErr = fmt.Errorf("自动合并更新异常")
|
|
|
- }
|
|
|
- this.FlushSession(phRes, "") //更新session
|
|
|
- backUserErr = this.backupsUser(*wxRes, choose, pId) //删除用户
|
|
|
- //orderErr = this.mergeOrder(wId, pId) //合并订单
|
|
|
- }
|
|
|
- if backUserErr != nil || orderErr != nil || updateErr != nil {
|
|
|
- for _, fuc := range noteFunc { //发送合并异常通知
|
|
|
- fuc(backUserErr, orderErr, updateErr)
|
|
|
- }
|
|
|
- }
|
|
|
- return true, nil
|
|
|
-}
|
|
|
-
|
|
|
-//备份用户
|
|
|
-func (wum *UserMerge) backupsUser(remove map[string]interface{}, flag, saveId string) error {
|
|
|
- userId := BsonIdToSId(remove["_id"])
|
|
|
- now := time.Now().Unix()
|
|
|
- remove["l_mergeData"] = now
|
|
|
- remove["s_userId"] = userId //保留账户id
|
|
|
- remove["s_save_userId"] = saveId
|
|
|
- if len(flag) == 1 {
|
|
|
- remove["mergeType"] = "chooseMerge"
|
|
|
- } else {
|
|
|
- remove["mergeType"] = "autoMerge"
|
|
|
- }
|
|
|
- if wum.mg.Save("user_merge", remove) == "" {
|
|
|
- log.Println("p19用户合并 保存user_merge失败", remove)
|
|
|
- }
|
|
|
- if !wum.mg.Del("user", map[string]interface{}{"_id": StringTOBsonId(userId)}) {
|
|
|
- log.Println("p19用户合并 删除user信息失败", remove)
|
|
|
- return fmt.Errorf("p19用户合并 删除user:%s信息失败", userId)
|
|
|
- }
|
|
|
- //清除被清除用户session
|
|
|
- redis.Put("session", fmt.Sprintf("usermerge_delete_%s", userId), saveId, 7*24*60*60)
|
|
|
- //清除推送表数据
|
|
|
- for _, tableName := range aboutTableRemove {
|
|
|
- if !wum.mg.Del(tableName, map[string]interface{}{"userid": userId}) {
|
|
|
- log.Printf("p19用户合并 删除推送相关数据出错:用户%s,表名%s\n", userId, tableName)
|
|
|
- }
|
|
|
- }
|
|
|
- //user表记录合并 用于统计
|
|
|
- wum.mg.UpdateById("user", saveId, map[string]interface{}{
|
|
|
- "$set": map[string]interface{}{
|
|
|
- "s_merged_id": userId,
|
|
|
- "l_mergeDate": now,
|
|
|
- },
|
|
|
- })
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-//订单合并
|
|
|
-func (wum *UserMerge) mergeOrder(queryId, setId string) error {
|
|
|
- //记录备份订单
|
|
|
- //orderList := wum.mysqlDb.Find("dataexport_order", map[string]interface{}{
|
|
|
- // "": "",
|
|
|
- //}, "", "", -1, -1)
|
|
|
- //if orderList == nil || len(*orderList) == 0 {
|
|
|
- // log.Printf("p19用户合并-订单迁移[无需要迁移订单] %s->%s \n", queryId, setId)
|
|
|
- // return
|
|
|
- //}
|
|
|
- //wum.mg.SaveBulk("user_merge_order", *orderList...)
|
|
|
-
|
|
|
- ok := wum.mysqlDb.ExecTx("用户合并-订单转移", func(tx *sql.Tx) bool {
|
|
|
- //关闭订单
|
|
|
- ok_1 := wum.mysqlDb.UpdateOrDeleteBySqlByTx(tx, `UPDATE dataexport_order SET order_status = -2 WHERE user_id = ? and order_status =0`, queryId) != -1
|
|
|
- //订单转移
|
|
|
- ok_2 := wum.mysqlDb.UpdateOrDeleteBySqlByTx(tx, `UPDATE dataexport_order SET user_id = ? WHERE user_id = ? `, setId, queryId) != -1
|
|
|
- return ok_1 && ok_2
|
|
|
- })
|
|
|
- log.Printf("p19用户合并-订单迁移%s %s->%s \n", qutil.If(ok, "成功", "失败").(string), queryId, setId)
|
|
|
- if !ok {
|
|
|
- return fmt.Errorf("p19用户合并-订单迁移失败 %s->%s", queryId, setId)
|
|
|
- }
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
//更新session
|
|
|
func (this *UserMerge) FlushSession(userData *map[string]interface{}, userId string) {
|
|
|
if userData == nil {
|
|
|
- userData, _ = this.mg.FindById("user", userId, `{"_id":1,"i_shareknow":1,"s_m_openid":1,"s_nickname":1,"s_headimage":1,"s_headimageurl":1,"s_phone":1,"s_jpushid":1,"s_opushid":1,"s_appponetype":1,"s_appversion":1}`)
|
|
|
+ userData, _ = this.mg.FindById("user", userId, `{"_id":1,"i_shareknow":1,"s_m_openid":1,"s_nickname":1,"s_headimage":1,"s_headimageurl":1,"s_phone":1,"s_m_phone":1,"s_jpushid":1,"s_opushid":1,"s_appponetype":1,"s_appversion":1}`)
|
|
|
}
|
|
|
sessionVal := make(map[string]interface{})
|
|
|
sessionVal["user"] = *userData
|
|
@@ -287,6 +121,10 @@ func (this *UserMerge) FlushSession(userData *map[string]interface{}, userId str
|
|
|
sessionVal["openid"] = sessionVal["s_m_openid"]
|
|
|
//app端
|
|
|
sessionVal["s_phone"] = phone
|
|
|
+ if phone == "" {
|
|
|
+ phone, _ = (*userData)["s_m_phone"].(string)
|
|
|
+ }
|
|
|
+ sessionVal["phone"] = phone
|
|
|
sessionVal["s_headimageurl"] = strings.Replace(avatar, "http://", "https://", 1)
|
|
|
sessionVal["s_jpushid"], _ = (*userData)["s_jpushid"].(string)
|
|
|
sessionVal["s_opushid"], _ = (*userData)["s_opushid"].(string)
|