wangchuanjin 2 years ago
parent
commit
9ea493d783
6 changed files with 365 additions and 12 deletions
  1. 5 1
      common/config.go
  2. 142 0
      dfa/interestanalysis.go
  3. 45 0
      dfa/interestanalysis_test.go
  4. 14 11
      encrypt/encryptarticle.go
  5. 19 0
      mail/gmail.go
  6. 140 0
      usercenter/userCenter.go

+ 5 - 1
common/config.go

@@ -4,6 +4,7 @@ import (
 	"encoding/json"
 	"encoding/json"
 	"errors"
 	"errors"
 	"io/ioutil"
 	"io/ioutil"
+	"log"
 
 
 	"os"
 	"os"
 	"strings"
 	"strings"
@@ -22,7 +23,10 @@ func ReadConfig(config ...interface{}) {
 		r, _ = os.Open(filepath)
 		r, _ = os.Open(filepath)
 		defer r.Close()
 		defer r.Close()
 		bs, _ := ioutil.ReadAll(r)
 		bs, _ := ioutil.ReadAll(r)
-		json.Unmarshal(bs, config[1])
+		err := json.Unmarshal(bs, config[1])
+		if err != nil {
+			log.Println(config[0].(string), " json格式异常")
+		}
 	} else {
 	} else {
 		r, _ = os.Open("./config.json")
 		r, _ = os.Open("./config.json")
 		defer r.Close()
 		defer r.Close()

+ 142 - 0
dfa/interestanalysis.go

@@ -0,0 +1,142 @@
+/**
+ *兴趣分析
+ *
+ */
+package dfa
+
+import (
+	"log"
+	"strings"
+)
+
+//DFA实现
+type DFA struct {
+	link        map[string]interface{} //存放or
+	linkAnd     map[string]int         //存放and
+	linkAndWord map[string]interface{} //存放and中的拆分词
+
+}
+
+//添加词组,用于初始化,该方法是可以调用多次的
+func (d *DFA) AddWord(words ...string) {
+	if d.link == nil {
+		d.link = make(map[string]interface{})
+		d.linkAnd = make(map[string]int)
+		d.linkAndWord = make(map[string]interface{})
+	}
+	var nowMap *map[string]interface{}
+	for _, key := range words {
+		keys := strings.Split(key, "+")
+		lenkeys := len(keys)
+		if lenkeys > 1 {
+			d.linkAnd[key] = lenkeys
+			for k := 0; k < lenkeys; k++ {
+				minKey := keys[k]
+				nowMap = &d.linkAndWord
+				for i := 0; i < len(minKey); i++ {
+					kc := minKey[i : i+1]
+					if v, ok := (*nowMap)[kc]; ok {
+						nowMap, _ = v.(*map[string]interface{})
+					} else {
+						newMap := map[string]interface{}{}
+						newMap["YN"] = "N"
+						(*nowMap)[kc] = &newMap
+						nowMap = &newMap
+					}
+					if i == len(minKey)-1 {
+						(*nowMap)["YN"] = "Y"
+						if (*nowMap)["key"] == nil {
+							(*nowMap)["key"] = make(map[string]int)
+						}
+						(*nowMap)["key"].(map[string]int)[key] = k
+					}
+				}
+			}
+		} else {
+			nowMap = &d.link
+			for i := 0; i < len(key); i++ {
+				kc := key[i : i+1]
+				if v, ok := (*nowMap)[kc]; ok {
+					nowMap, _ = v.(*map[string]interface{})
+				} else {
+					newMap := map[string]interface{}{}
+					newMap["YN"] = "N"
+					(*nowMap)[kc] = &newMap
+					nowMap = &newMap
+				}
+
+				if i == len(key)-1 {
+					(*nowMap)["YN"] = "Y"
+				}
+			}
+		}
+	}
+}
+func (d *DFA) Clear() {
+	d.link = nil
+}
+
+//从给定的内容中找出匹配上的关键词
+func (d *DFA) Analy(src string) []string {
+	if d.link == nil {
+		log.Println("请先添加词组")
+		return []string{}
+	}
+	keywords := []string{}
+	tempMap := make(map[string][]bool)
+	for i := 0; i < len(src); i++ {
+		nowMap := &d.link
+		length := 0 // 匹配标识数默认为0
+		//flag := false // 敏感词结束标识位:用于敏感词只有1位的情况
+		for j := i; j < len(src); j++ {
+			word := src[j : j+1]
+			nowMap, _ = (*nowMap)[word].(*map[string]interface{})
+			if nowMap != nil {
+				length = length + 1
+				tag, _ := (*nowMap)["YN"].(string)
+				if "Y" == tag {
+					//flag = true
+					keywords = append(keywords, src[i:i+length])
+				}
+			} else {
+				break
+			}
+		}
+		nowMap = &d.linkAndWord
+		length = 0
+		for j := i; j < len(src); j++ {
+			word := src[j : j+1]
+			nowMap, _ = (*nowMap)[word].(*map[string]interface{})
+			if nowMap != nil {
+				length = length + 1
+				tag, _ := (*nowMap)["YN"].(string)
+				if "Y" == tag {
+					mkeys := (*nowMap)["key"].(map[string]int)
+					for k, v := range mkeys {
+						tempBool := tempMap[k]
+						if tempBool == nil {
+							tempBool = make([]bool, d.linkAnd[k])
+							tempMap[k] = tempBool
+						}
+						tempBool[v] = true
+					}
+				}
+			} else {
+				break
+			}
+		}
+	}
+	for k, v := range tempMap {
+		ball := true
+		for _, m := range v {
+			if !m {
+				ball = false
+				break
+			}
+		}
+		if ball {
+			keywords = append(keywords, k)
+		}
+	}
+	return keywords
+}

+ 45 - 0
dfa/interestanalysis_test.go

@@ -0,0 +1,45 @@
+package dfa
+
+import (
+	"log"
+	"strings"
+	"testing"
+	"time"
+)
+
+var d *DFA = &DFA{}
+
+func copyMap(m map[string]int) (m2 map[string]int) {
+	m2 = make(map[string]int)
+	for k, v := range m {
+		m2[k] = v
+	}
+	return m2
+}
+
+func TestAnaly(t *testing.T) {
+	d.AddWord("办公", "办+楼", "河+省", "完+你们8")
+	log.Println(strings.Split("河+南+", "+")[2])
+	t1 := time.Now()
+	log.Println(d.Analy("这胡省锦河涛写给江泽民的信我们你们于办公楼上你完就是啊。"), "=====")
+	log.Println(time.Now().Sub(t1).Seconds())
+	d.Clear()
+	//log.Println(d.Analy("这是胡锦涛写给江泽民的信啊。"))
+
+}
+
+func Test_Label(t *testing.T) {
+	log.Println("000----")
+
+	for _, v := range []int{1, 2, 3, 4, 5} {
+		log.Println(v)
+	L1:
+		for _, vv := range []string{"a", "b", "c", "d"} {
+			log.Println(vv)
+			if vv == "add" {
+				break L1
+			}
+		}
+	}
+	log.Println("111----")
+}

+ 14 - 11
encrypt/encryptarticle.go

@@ -9,6 +9,7 @@ import (
 //正文
 //正文
 var SE = &SimpleEncrypt{Key: "topnet2015topnet2015"}
 var SE = &SimpleEncrypt{Key: "topnet2015topnet2015"}
 var SE2 = &SimpleEncrypt{Key: "2017jianyu"}
 var SE2 = &SimpleEncrypt{Key: "2017jianyu"}
+var SE3 = &SimpleEncrypt{Key: "entservice"}
 
 
 //百度
 //百度
 var BSE = &SimpleEncrypt{Key: "HNtopnet2017jy"}
 var BSE = &SimpleEncrypt{Key: "HNtopnet2017jy"}
@@ -40,17 +41,19 @@ func CommonEncodeArticle(stype string, keys ...string) (id string) {
 
 
 //通用解密
 //通用解密
 func CommonDecodeArticle(stype string, id string) (res []string) {
 func CommonDecodeArticle(stype string, id string) (res []string) {
-	switch stype {
-	case "content":
-		res = BDecodeArticleId2ByCheck(id, SE, SE2)
-	case "bdprivate":
-		res = BDecodeArticleId2ByCheck(id, BSE, BSE2)
-	case "mailprivate":
-		res = BDecodeArticleId2ByCheck(id, ESE, ESE2)
-	case "indexcontent":
-		res = BDecodeArticleId2ByCheck(id, ISE, ISE2)
-	}
-	return
+        switch stype {
+        case "content":
+	      res = BDecodeArticleId2ByCheck(id, SE, SE2)
+        case "bdprivate":
+	      res = BDecodeArticleId2ByCheck(id, BSE, BSE2)
+        case "mailprivate":
+	      res = BDecodeArticleId2ByCheck(id, ESE, ESE2)
+        case "indexcontent":
+	      res = BDecodeArticleId2ByCheck(id, ISE, ISE2)
+        case "advancedProject":
+	      res = BDecodeArticleId2ByCheck(id, SE, SE2)
+        }
+        return
 }
 }
 
 
 //短地址加密,二次加密带校验和
 //短地址加密,二次加密带校验和

+ 19 - 0
mail/gmail.go

@@ -214,3 +214,22 @@ func gSend(retry int, auth *GmailAuth, m *gomail.Message, to string) bool {
 	}
 	}
 	return status
 	return status
 }
 }
+
+//先取模后轮询获取一个mail实例
+func PollingMail(email string, array []*GmailAuth, f func(g *GmailAuth) bool) bool {
+	if len(array) == 0 {
+		return false
+	}
+	index := len(email) % len(array)
+	if f(array[index]) {
+		return true
+	}
+	for i := 0; i < len(array); i++ {
+		if i == index {
+			continue
+		} else if f(array[i]) {
+			return true
+		}
+	}
+	return false
+}

+ 140 - 0
usercenter/userCenter.go

@@ -4,6 +4,7 @@ import (
 	"encoding/json"
 	"encoding/json"
 	"io/ioutil"
 	"io/ioutil"
 	"log"
 	"log"
+	"app.yhyue.com/moapp/jybase/mongodb"
 	"net/http"
 	"net/http"
 	"strings"
 	"strings"
 
 
@@ -82,6 +83,145 @@ func PostUserCenter(url, contentTpe, userid string, data map[string]interface{},
 	return dataM, true
 	return dataM, true
 }
 }
 
 
+// usercenter
+type UserInfo struct {
+	S_openid     string //微信openid
+	A_openid     string //app 微信openid
+	Phone        string //手机号
+	Nickname     string //昵称
+	Headimg      string //头像
+	Company      string //公司
+	Position     string //职位
+	Password     string //密码
+	Unionid      string //unionid
+	Base_user_id int64  //用户中台base_user的主键id,
+}
+
+//获取base_user需要的数据
+func GetInfoForBaseUser(mg mongodb.MongodbSim, userid string) *UserInfo {
+	if userid == "" {
+		return nil
+	}
+	data, ok := mg.FindById("user", userid, `{"base_user_id":1,"s_m_openid":1,"a_m_openid":1,"s_m_phone":1,"s_phone":1,"s_nickname":1,"s_jyname":1,"s_headimageurl":1,"s_headimage":1,"s_company":1,"s_password":1,"s_unionid":1}`)
+	if ok && data != nil && len(*data) > 0 {
+		userinfo := &UserInfo{
+			Base_user_id: Int64All((*data)["base_user_id"]),
+		}
+		if s_openid := ObjToString((*data)["s_m_openid"]); s_openid != "" {
+			userinfo.S_openid = s_openid
+		}
+		if a_openid := ObjToString((*data)["a_m_openid"]); a_openid != "" {
+			userinfo.A_openid = a_openid
+		}
+		phone := ObjToString((*data)["s_phone"])
+		if phone == "" {
+			phone = ObjToString((*data)["s_m_phone"])
+		}
+		if phone != "" {
+			userinfo.Phone = phone
+		}
+		nickname := ObjToString((*data)["s_nickname"])
+		if nickname == "" {
+			nickname = ObjToString((*data)["s_jyname"])
+		}
+		if nickname != "" {
+			userinfo.Nickname = nickname
+		}
+		if unionid := ObjToString((*data)["s_unionid"]); unionid != "" {
+			userinfo.Unionid = unionid
+		}
+		if password := ObjToString((*data)["s_password"]); password != "" {
+			userinfo.Password = password
+		}
+		if headimg := ObjToString((*data)["s_headimageurl"]); headimg != "" {
+			userinfo.Headimg = headimg
+		}
+		return userinfo
+	}
+	return nil
+}
+
+//获取mongodb中的base_user_id
+func GetBaseUserId(mg mongodb.MongodbSim, userid string) int64 {
+	if userid == "" {
+		return 0
+	}
+	data, ok := mg.FindById("user", userid, `{"base_user_id":1}`)
+	if ok && data != nil && len(*data) > 0 {
+		return Int64All((*data)["base_user_id"])
+	}
+	return 0
+}
+
+//更新用户中台相关
+func UpdateBaseUser(mgo mongodb.MongodbSim, href, userId string, ck *http.Cookie) {
+	formdata := map[string]interface{}{
+		"appid": "10000",
+	}
+	userinfo := GetInfoForBaseUser(mgo, userId)
+	if userinfo == nil {
+		return
+	}
+	formdata["id"] = userinfo.Base_user_id
+
+	if userinfo.A_openid != "" {
+		formdata["a_openid"] = userinfo.A_openid
+	}
+	if userinfo.S_openid != "" {
+		formdata["s_openid"] = userinfo.S_openid
+	}
+
+	if userinfo.Company != "" {
+		formdata["company"] = userinfo.Company
+	}
+
+	if userinfo.Headimg != "" {
+		formdata["headimg"] = userinfo.Headimg
+	}
+
+	if userinfo.Password != "" {
+		formdata["password"] = userinfo.Password
+	}
+
+	if userinfo.Nickname != "" {
+		formdata["nickname"] = userinfo.Nickname
+	}
+
+	if userinfo.Phone != "" {
+		formdata["phone"] = userinfo.Phone
+	}
+
+	if userinfo.Unionid != "" {
+		formdata["unionid"] = userinfo.Unionid
+	}
+
+	ret, ok := PostUserCenter(href+UserCenterUpdate, ContentType_Json, userId, formdata, ck)
+	if ret != nil && ok {
+		status := IntAllDef((*ret)["status"], 0)
+		if status != 1 {
+			log.Printf("mysql同步数据失败,base_user_id:%s ,user_id :%s  ,%+v", formdata["id"], userId, formdata)
+		}
+	}
+}
+
+//新增用户中台信息。并绑定mongodb表
+func AddBaseUser(mgo mongodb.MongodbSim, href, userId string, formdata map[string]interface{}, ck *http.Cookie) {
+	ret, ok := PostUserCenter(href+UserCenterAdd, ContentType_Json, userId, formdata, ck)
+	if ret != nil && ok {
+		base_id := Int64All((*ret)["id"])
+		if base_id == 0 {
+			log.Printf("新增用户中台获取base_user_id失败,userid:%s", userId)
+			return
+		}
+		//获取到mysql的用户id存入user表中
+		if ok := mgo.UpdateById("user", userId, map[string]interface{}{
+			"$set": map[string]interface{}{"base_user_id": base_id},
+		}); !ok {
+			log.Printf("mysql同步失败,base_user_id:%s ,userid:%s", base_id, userId)
+		}
+	}
+}
+
 type UserIdentity struct {
 type UserIdentity struct {
 	PersonId          int64  //自然人id
 	PersonId          int64  //自然人id
 	UserAccountId     int64  //个人账户id
 	UserAccountId     int64  //个人账户id