浏览代码

no message

xingzai 1 年之前
父节点
当前提交
a8fc00993e
共有 5 个文件被更改,包括 112 次插入22 次删除
  1. 35 14
      controllers/wechat.go
  2. 17 4
      models/cygx_user_record.go
  3. 7 0
      models/user.go
  4. 25 0
      services/user_record.go
  5. 28 4
      utils/email.go

+ 35 - 14
controllers/wechat.go

@@ -301,25 +301,44 @@ func (this *WechatCommonController) Notify() {
 		</xml>`
 		</xml>`
 		createTime := strconv.FormatInt(time.Now().Unix(), 10)
 		createTime := strconv.FormatInt(time.Now().Unix(), 10)
 		xmlTpl = fmt.Sprintf(xmlTpl, openId, utils.WxId, createTime, contactMsg)
 		xmlTpl = fmt.Sprintf(xmlTpl, openId, utils.WxId, createTime, contactMsg)
-		accessToken, err := services.GetWxAccessTokenByXzs()
+
+		total, err := models.GetCygxUserRecordCount(openId)
 		if err != nil {
 		if err != nil {
-			utils.FileLog.Info("accessToken:" + err.Error())
-		}
-		if accessToken == "" {
-			utils.FileLog.Info("access_token 为空 openId:" + openId)
+			utils.FileLog.Info("GetCygxUserRecordCount:" + err.Error())
 		}
 		}
-		wxUserInfo, err := services.WxGetUserInfo(openId, accessToken)
-		if err != nil {
-			utils.FileLog.Info("accessToken:" + err.Error())
-			return
-		}
-		if wxUserInfo.Errcode != 0 {
-			userInfoJson, _ := json.Marshal(wxUserInfo)
-			utils.FileLog.Info("获取用户信息失败,err:" + string(userInfoJson))
+		var unionId string
+		if total == 0 {
+			accessToken, err := services.GetWxAccessTokenByXzs()
+			if err != nil {
+				utils.FileLog.Info("accessToken:" + err.Error())
+			}
+			if accessToken == "" {
+				utils.FileLog.Info("access_token 为空 openId:" + openId)
+			}
+			wxUserInfo, err := services.WxGetUserInfo(openId, accessToken)
+			if err != nil {
+				utils.FileLog.Info("accessToken:" + err.Error())
+				return
+			}
+			if wxUserInfo.Errcode != 0 {
+				userInfoJson, _ := json.Marshal(wxUserInfo)
+				utils.FileLog.Info("获取用户信息失败,err:" + string(userInfoJson))
+			}
+			unionId = wxUserInfo.Unionid
+			if unionId != "" {
+				services.AddCygxUserRecord(wxUserInfo)
+			}
+		} else {
+			userRecordDetail, err := models.GetCygxUserRecordByOpenid(openId)
+			if err != nil {
+				utils.FileLog.Info("GetCygxUserRecordByOpenid:" + err.Error())
+				return
+			}
+			unionId = userRecordDetail.UnionId
 		}
 		}
-		unionId := wxUserInfo.Unionid
 		if unionId == "" {
 		if unionId == "" {
 			utils.FileLog.Info("获取unionid失败,openId:" + openId)
 			utils.FileLog.Info("获取unionid失败,openId:" + openId)
+			return
 		}
 		}
 		wxUser, err := models.GetUserRecordByUnionId(unionId)
 		wxUser, err := models.GetUserRecordByUnionId(unionId)
 		if err != nil {
 		if err != nil {
@@ -334,10 +353,12 @@ func (this *WechatCommonController) Notify() {
 			case "subscribe":
 			case "subscribe":
 				fmt.Println("关注")
 				fmt.Println("关注")
 				go models.UserSubscribe(1, wxUser.UserId)
 				go models.UserSubscribe(1, wxUser.UserId)
+				go models.CygxUserSubscribe(1, unionId)
 				break
 				break
 			case "unsubscribe":
 			case "unsubscribe":
 				fmt.Println("取消关注")
 				fmt.Println("取消关注")
 				go models.UserSubscribe(0, wxUser.UserId)
 				go models.UserSubscribe(0, wxUser.UserId)
+				go models.CygxUserSubscribe(0, unionId)
 				break
 				break
 			case "CLICK":
 			case "CLICK":
 				returnResult = xmlTpl
 				returnResult = xmlTpl

+ 17 - 4
models/cygx_user_record.go

@@ -18,14 +18,14 @@ type CygxUserRecord struct {
 	CreateTime   time.Time `description:"创建时间,关系添加时间、用户授权时间"`
 	CreateTime   time.Time `description:"创建时间,关系添加时间、用户授权时间"`
 }
 }
 
 
-//添加
+// 添加
 func AddCygxUserRecord(item *CygxUserRecord) (lastId int64, err error) {
 func AddCygxUserRecord(item *CygxUserRecord) (lastId int64, err error) {
 	o := orm.NewOrm()
 	o := orm.NewOrm()
 	lastId, err = o.Insert(item)
 	lastId, err = o.Insert(item)
 	return
 	return
 }
 }
 
 
-//获取数量
+// 获取数量
 func GetCygxUserRecordCount(openId string) (count int, err error) {
 func GetCygxUserRecordCount(openId string) (count int, err error) {
 	o := orm.NewOrm()
 	o := orm.NewOrm()
 	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_user_record WHERE open_id=? `
 	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_user_record WHERE open_id=? `
@@ -33,7 +33,7 @@ func GetCygxUserRecordCount(openId string) (count int, err error) {
 	return
 	return
 }
 }
 
 
-//修改
+// 修改
 func UpdateCygxUserRecord(item *CygxUserRecord) (err error) {
 func UpdateCygxUserRecord(item *CygxUserRecord) (err error) {
 	o := orm.NewOrm()
 	o := orm.NewOrm()
 	msql := ` UPDATE cygx_user_record SET nick_name = ?,sex=?,province=?,city=? ,country=? ,headimgurl=?  WHERE open_id = ? `
 	msql := ` UPDATE cygx_user_record SET nick_name = ?,sex=?,province=?,city=? ,country=? ,headimgurl=?  WHERE open_id = ? `
@@ -41,10 +41,23 @@ func UpdateCygxUserRecord(item *CygxUserRecord) (err error) {
 	return
 	return
 }
 }
 
 
-//修改
+// 修改
 func UpdateUserRecord(item *CygxUserRecord) (err error) {
 func UpdateUserRecord(item *CygxUserRecord) (err error) {
 	o := orm.NewOrm()
 	o := orm.NewOrm()
 	msql := ` UPDATE user_record SET nick_name = ?,sex=?,province=?,city=? ,country=? ,headimgurl=?  WHERE union_id = ? AND create_platform = 4  `
 	msql := ` UPDATE user_record SET nick_name = ?,sex=?,province=?,city=? ,country=? ,headimgurl=?  WHERE union_id = ? AND create_platform = 4  `
 	_, err = o.Raw(msql, item.NickName, item.Sex, item.Province, item.City, item.Country, item.Headimgurl, item.UnionId).Exec()
 	_, err = o.Raw(msql, item.NickName, item.Sex, item.Province, item.City, item.Country, item.Headimgurl, item.UnionId).Exec()
 	return
 	return
 }
 }
+
+// 根据Openid获取用户详情
+func GetCygxUserRecordByOpenid(opendId string) (item *CygxUserRecord, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT
+			*
+		FROM
+			cygx_user_record 
+		WHERE open_id = ?
+			LIMIT 1 `
+	err = o.Raw(sql, opendId).QueryRow(&item)
+	return
+}

+ 7 - 0
models/user.go

@@ -238,3 +238,10 @@ func UserSubscribe(subscribeType, userId int) (err error) {
 	_, err = o.Raw(sql, subscribeType, userId).Exec()
 	_, err = o.Raw(sql, subscribeType, userId).Exec()
 	return
 	return
 }
 }
+
+func CygxUserSubscribe(subscribeType int, unionid string) (err error) {
+	o := orm.NewOrm()
+	sql := `UPDATE cygx_user_record SET subscribe=?,subscribe_time=NOW() WHERE union_id = ? `
+	_, err = o.Raw(sql, subscribeType, unionid).Exec()
+	return
+}

+ 25 - 0
services/user_record.go

@@ -0,0 +1,25 @@
+package services
+
+import (
+	"hongze/hongze_cygxzs/models"
+	"hongze/hongze_cygxzs/utils"
+	"time"
+)
+
+func AddCygxUserRecord(wxUserInfo *WxUserInfo) {
+	items := new(models.CygxUserRecord)
+	items.OpenId = wxUserInfo.Openid
+	items.UnionId = wxUserInfo.Unionid
+	items.NickName = wxUserInfo.Nickname
+	items.Sex = wxUserInfo.Sex
+	items.Province = wxUserInfo.Province
+	items.City = wxUserInfo.City
+	items.Country = wxUserInfo.Country
+	items.Headimgurl = wxUserInfo.Headimgurl
+	items.CreateTime = time.Now()
+	_, err := models.AddCygxUserRecord(items)
+	if err != nil {
+		go utils.SendAlarmMsg("发送邮件:用户关注查研观向小助手时记录相关信息失败,Openid:"+wxUserInfo.Openid+err.Error(), 2)
+	}
+
+}

+ 28 - 4
utils/email.go

@@ -1,13 +1,19 @@
 package utils
 package utils
 
 
 import (
 import (
+	"encoding/json"
 	"fmt"
 	"fmt"
+	"github.com/rdlucklib/rdluck_tools/http"
 	"gopkg.in/gomail.v2"
 	"gopkg.in/gomail.v2"
 	"mime"
 	"mime"
 	"strings"
 	"strings"
 )
 )
 
 
-//发送邮件
+var (
+	AlarmMsgUrl = "http://127.0.0.1:8606/api/alarm/send"
+)
+
+// 发送邮件
 func SendEmail(title, content string, touser string) bool {
 func SendEmail(title, content string, touser string) bool {
 	if RunMode == "debug" {
 	if RunMode == "debug" {
 		return false
 		return false
@@ -34,7 +40,25 @@ func SendEmail(title, content string, touser string) bool {
 	return true
 	return true
 }
 }
 
 
-//发送邮件
+// projectName-项目名称
+// runMode-运行模式
+// msgBody-消息内容
+// level:消息基本,1:提示消息,2:警告消息,3:严重错误信息,默认为1 提示消息
+func SendAlarmMsg(msgBody string, level int) {
+	params := make(map[string]interface{})
+	params["ProjectName"] = APPNAME
+	params["RunMode"] = RunMode
+	params["MsgBody"] = msgBody
+	params["Level"] = level
+	param, err := json.Marshal(params)
+	if err != nil {
+		FileLog.Info("SendAlarmMsg json.Marshal Err:" + err.Error())
+		return
+	}
+	http.Post(AlarmMsgUrl, string(param))
+}
+
+// 发送邮件
 func SendEmailByHz(title, content string, touser string) (result bool, err error) {
 func SendEmailByHz(title, content string, touser string) (result bool, err error) {
 	//if RunMode == "debug" {
 	//if RunMode == "debug" {
 	//	result = false
 	//	result = false
@@ -64,7 +88,7 @@ func SendEmailByHz(title, content string, touser string) (result bool, err error
 	return
 	return
 }
 }
 
 
-//发送带有文件的邮件
+// 发送带有文件的邮件
 func SendEmailHaveFile(title, content string, fileName, touser string) bool {
 func SendEmailHaveFile(title, content string, fileName, touser string) bool {
 	var arr []string
 	var arr []string
 	sub := strings.Index(touser, ";")
 	sub := strings.Index(touser, ";")
@@ -89,7 +113,7 @@ func SendEmailHaveFile(title, content string, fileName, touser string) bool {
 	return true
 	return true
 }
 }
 
 
-//发送邮件
+// 发送邮件
 func SendEmailByHongze(title, content string, touser, attachPath, attachName string) bool {
 func SendEmailByHongze(title, content string, touser, attachPath, attachName string) bool {
 	var arr []string
 	var arr []string
 	sub := strings.Index(touser, ";")
 	sub := strings.Index(touser, ";")