|
@@ -0,0 +1,183 @@
|
|
|
+package jy
|
|
|
+
|
|
|
+import (
|
|
|
+ "encoding/json"
|
|
|
+ "errors"
|
|
|
+ "fmt"
|
|
|
+ "log"
|
|
|
+ qutil "qfw/util"
|
|
|
+ "qfw/util/mongodb"
|
|
|
+ "qfw/util/redis"
|
|
|
+ "regexp"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "github.com/go-xweb/httpsession"
|
|
|
+ "gopkg.in/mgo.v2/bson"
|
|
|
+)
|
|
|
+
|
|
|
+var WxClientReg = regexp.MustCompile(`.*micromessenger.*`)
|
|
|
+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"}
|
|
|
+
|
|
|
+//自动合并两个账户、其中一个为空(无订阅、无关注项目、无关注企业)
|
|
|
+func AutoMerge(mg mongodb.MongodbSim, sess *httpsession.Session) (bool, error) {
|
|
|
+ userId := qutil.ObjToString(sess.Get("userId"))
|
|
|
+ if userId == "" {
|
|
|
+ return false, errors.New("未获取到userId")
|
|
|
+ }
|
|
|
+ thisUser, _ := mg.FindById("user", userId, nil)
|
|
|
+ if thisUser == nil || len(*thisUser) == 0 {
|
|
|
+ return false, errors.New("未获取到用户信息")
|
|
|
+ }
|
|
|
+ s_m_phone := qutil.ObjToString((*thisUser)["s_m_phone"])
|
|
|
+ s_phone := qutil.ObjToString((*thisUser)["s_phone"])
|
|
|
+ if s_phone == s_m_phone {
|
|
|
+ return true, nil
|
|
|
+ }
|
|
|
+ otherUser := &map[string]interface{}{}
|
|
|
+ if s_m_phone != "" {
|
|
|
+ otherUser, _ = mg.FindOneByField("user", bson.M{"s_phone": s_m_phone, "s_m_phone": bson.M{"$ne": s_m_phone}}, nil)
|
|
|
+ if otherUser == nil || len(*otherUser) == 0 { //微信端合并 app端
|
|
|
+ return true, nil
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ otherUser, _ = mg.FindOneByField("user", bson.M{"s_m_phone": s_phone, "s_phone": bson.M{"$ne": s_phone}}, nil)
|
|
|
+ if otherUser == nil || len(*otherUser) == 0 { //app端合并 微信端
|
|
|
+ return true, nil
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ thisNull, otherNull := func() (t, o bool) {
|
|
|
+ t, o = true, true
|
|
|
+ if len(*qutil.ObjToMap((*thisUser)["o_jy"])) > 2 || (*thisUser)["o_vipjy"] != nil {
|
|
|
+ t = false
|
|
|
+ }
|
|
|
+ if len(*qutil.ObjToMap((*otherUser)["o_jy"])) > 2 || (*otherUser)["o_vipjy"] != nil {
|
|
|
+ o = false
|
|
|
+ }
|
|
|
+ if !t && !o { //账户都不为空
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if t {
|
|
|
+ id := qutil.BsonIdToSId((*thisUser)["_id"])
|
|
|
+ if mg.Count("jylab_followent", bson.M{"s_userid": id}) != 0 {
|
|
|
+ t = false
|
|
|
+ } else {
|
|
|
+ if mg.Count("follow_project", bson.M{"s_userid": id}) != 0 {
|
|
|
+ t = false
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if o {
|
|
|
+ id := qutil.BsonIdToSId((*otherUser)["_id"])
|
|
|
+ if mg.Count("jylab_followent", bson.M{"s_userid": id}) != 0 {
|
|
|
+ o = false
|
|
|
+ } else {
|
|
|
+ if mg.Count("follow_project", bson.M{"s_userid": id}) != 0 {
|
|
|
+ o = false
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+ }()
|
|
|
+
|
|
|
+ if !thisNull && !otherNull {
|
|
|
+ return true, nil
|
|
|
+ }
|
|
|
+ log.Println("null log", thisNull, otherNull)
|
|
|
+ //存在一个空账户
|
|
|
+ var finalUser, removeUser *map[string]interface{}
|
|
|
+ if thisNull {
|
|
|
+ finalUser = otherUser
|
|
|
+ removeUser = thisUser
|
|
|
+ } else {
|
|
|
+ finalUser = thisUser
|
|
|
+ removeUser = otherUser
|
|
|
+ }
|
|
|
+
|
|
|
+ if !MergeData(mg, finalUser, removeUser) {
|
|
|
+ return false, errors.New("数据库操作出错")
|
|
|
+ }
|
|
|
+ log.Printf("[%s,%s]账户自动合并 合并结果 %+v\n", qutil.BsonIdToSId((*finalUser)["_id"]), qutil.BsonIdToSId((*removeUser)["_id"]), finalUser)
|
|
|
+ go MoveUser(mg, removeUser)
|
|
|
+ FlushSession(sess, finalUser, true)
|
|
|
+ return true, nil
|
|
|
+}
|
|
|
+
|
|
|
+func MergeData(mg mongodb.MongodbSim, saveRes, otherRes *map[string]interface{}) bool {
|
|
|
+ for k, v := range *otherRes {
|
|
|
+ if _, ok := (*saveRes)[k]; !ok {
|
|
|
+ (*saveRes)[k] = v
|
|
|
+ }
|
|
|
+ }
|
|
|
+ delete(*saveRes, "s_m_phone")
|
|
|
+ if !mg.UpdateById("user", qutil.BsonIdToSId((*saveRes)["_id"]), saveRes) {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
+func FlushSession(sess *httpsession.Session, userData *map[string]interface{}, isWx bool) {
|
|
|
+ sess.Set("userId", qutil.BsonIdToSId((*userData)["_id"]))
|
|
|
+ sess.Set("s_jpushid", (*userData)["s_jpushid"])
|
|
|
+ sess.Set("s_m_openid", (*userData)["s_m_openid"])
|
|
|
+ sess.Set("s_opushid", (*userData)["s_opushid"])
|
|
|
+ sess.Set("s_appponetype", (*userData)["s_appponetype"])
|
|
|
+ sess.Set("s_appversion", (*userData)["s_appversion"])
|
|
|
+ sess.Set("s_headimageurl", strings.Replace(qutil.ObjToString((*userData)["s_headimageurl"]), "http://", "https://", 1))
|
|
|
+ log.Println("isWx", isWx)
|
|
|
+ if phone := qutil.ObjToString((*userData)["s_phone"]); phone != "" && !isWx {
|
|
|
+ sess.Set("s_phone", phone)
|
|
|
+ phone = string(phone[0:3]) + "****" + string(phone[len(phone)-4:])
|
|
|
+ sess.Set("s_nickname", phone)
|
|
|
+ } else {
|
|
|
+ sess.Set("s_nickname", (*userData)["s_nickname"])
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func MoveUser(mg mongodb.MongodbSim, otherRes *map[string]interface{}) {
|
|
|
+ (*otherRes)["l_mergeData"] = time.Now().Unix()
|
|
|
+ userId := qutil.BsonIdToSId((*otherRes)["_id"])
|
|
|
+ (*otherRes)["s_userId"] = userId
|
|
|
+ if mg.Save("user_merge", otherRes) == "" {
|
|
|
+ log.Println("保存user_merge失败", otherRes)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if !mg.Del("user", map[string]interface{}{"_id": qutil.StringTOBsonId(userId)}) {
|
|
|
+ log.Println("删除user信息失败", otherRes)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ redis.Put("session", fmt.Sprintf("usermerge_delete_%s", userId), 1, 7*24*60*60) //session最长期限7天
|
|
|
+ //删除mysql表相关数据
|
|
|
+ for _, tableName := range aboutTableRemove {
|
|
|
+ if !mg.Del(tableName, map[string]interface{}{"userid": userId}) {
|
|
|
+ log.Printf("用户合并删除相关数据出错:用户%s,表明%s\n", userId, tableName)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+//生成签名,返回手机端
|
|
|
+func CreateAppSign(userid, rid string) string {
|
|
|
+ u := &struct {
|
|
|
+ CreateTime int64 `json:"createtime"`
|
|
|
+ OpenId string `json:"openid"`
|
|
|
+ UserId string `json:"userid"`
|
|
|
+ Rid string `json:"rid"`
|
|
|
+ Sign string `json:"sign"`
|
|
|
+ Type int `json:"type"`
|
|
|
+ }{
|
|
|
+ UserId: userid,
|
|
|
+ Type: 2,
|
|
|
+ CreateTime: time.Now().Unix(),
|
|
|
+ Rid: rid,
|
|
|
+ }
|
|
|
+ u.Sign = qutil.GetMd5String(fmt.Sprintf("createtime=%d&userid=%s&rid=%s&type=%d", u.CreateTime, u.UserId, u.Rid, u.Type))
|
|
|
+ b, err := json.Marshal(u)
|
|
|
+ if err != nil {
|
|
|
+ log.Println(err)
|
|
|
+ }
|
|
|
+ sign := string(b)
|
|
|
+ sign = se.EncodeString(sign)
|
|
|
+ sign = strings.Replace(sign, "+", "%2B", -1)
|
|
|
+ return sign
|
|
|
+}
|