|
@@ -19,6 +19,16 @@ type WechatController struct {
|
|
|
BaseCommonController
|
|
|
}
|
|
|
|
|
|
+type Notify struct {
|
|
|
+ ToUserName string `xml:"ToUserName"`
|
|
|
+ FromUserName string `xml:"FromUserName"`
|
|
|
+ CreateTime int `xml:"CreateTime"`
|
|
|
+ MsgType string `xml:"MsgType"`
|
|
|
+ Event string `xml:"Event"`
|
|
|
+ EventKey string `xml:"EventKey"`
|
|
|
+ Content string `xml:"Content"`
|
|
|
+}
|
|
|
+
|
|
|
// @Title 微信获取签名接口
|
|
|
// @Description 微信获取签名接口
|
|
|
// @Param Url query string true "url地址"
|
|
@@ -28,15 +38,6 @@ func (this *WechatController) Notify() {
|
|
|
echostr := this.GetString("echostr")
|
|
|
method := this.Ctx.Input.Method()
|
|
|
|
|
|
- type Notify struct {
|
|
|
- ToUserName string `xml:"ToUserName"`
|
|
|
- FromUserName string `xml:"FromUserName"`
|
|
|
- CreateTime int `xml:"CreateTime"`
|
|
|
- MsgType string `xml:"MsgType"`
|
|
|
- Event string `xml:"Event"`
|
|
|
- EventKey string `xml:"EventKey"`
|
|
|
- Content string `xml:"Content"`
|
|
|
- }
|
|
|
if method == "POST" {
|
|
|
body := this.Ctx.Input.RequestBody
|
|
|
utils.FileLog.Info("wechat notify:" + string(body))
|
|
@@ -89,22 +90,43 @@ func (this *WechatController) Notify() {
|
|
|
func subscribe(openId string) {
|
|
|
userRecord, err := models.GetUserRecordByOpenId(openId)
|
|
|
if err != nil && err.Error() != utils.ErrNoRow() {
|
|
|
- fmt.Println("通过openid获取user_record记录失败,err:" + err.Error())
|
|
|
+ utils.FileLog.Info("通过openid获取user_record记录失败,err:" + err.Error())
|
|
|
return
|
|
|
}
|
|
|
- err = nil
|
|
|
-
|
|
|
// openId已存在
|
|
|
if userRecord != nil {
|
|
|
if userRecord.UserId > 0 && userRecord.UnionId != "" { //已经绑定了的话,那么就去修改用户状态
|
|
|
- models.UserSubscribe(1, openId)
|
|
|
+ err = models.UserSubscribe(1, openId)
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Info("关注后,通过openid修改user_record异常,err:" + err.Error())
|
|
|
+ }
|
|
|
+ user, err := models.GetUserByUnionId(userRecord.UnionId)
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Info("关注后,通过unionid获取user记录失败,err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if user != nil {
|
|
|
+ user.IsSubscribed = true
|
|
|
+ user.ModifyTime = time.Now()
|
|
|
+ err = user.Update([]string{"modify_time", "is_subscribed"})
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Info("关注后,修改绑定用户状态异常,err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ userRecord.UserId = user.UserId
|
|
|
+ err = userRecord.Update([]string{"user_id"})
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Info("关注后,修改公众号绑定用户异常,err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
} else {
|
|
|
// 没有绑定的话,那么校验下unionid,然后再去修改
|
|
|
unionId := userRecord.UnionId
|
|
|
if unionId == `` {
|
|
|
wxUserItem, err := wechat.GetUserInfo(openId)
|
|
|
if err != nil {
|
|
|
- fmt.Println("获取用户信息失败,err:" + err.Error())
|
|
|
+ utils.FileLog.Info("获取用户信息失败,err:" + err.Error())
|
|
|
return
|
|
|
}
|
|
|
if wxUserItem.UnionID != `` {
|
|
@@ -116,12 +138,12 @@ func subscribe(openId string) {
|
|
|
userRecord.SubscribeTime = time.Now()
|
|
|
err = userRecord.Update([]string{"union_id", "subscribe", "subscribe_time"})
|
|
|
if err != nil {
|
|
|
- fmt.Println("关注后,通过openid更新user_record异常,err:", err)
|
|
|
+ utils.FileLog.Info("关注后,通过openid更新user_record异常,err:" + err.Error())
|
|
|
return
|
|
|
}
|
|
|
user, err := models.GetUserByUnionId(unionId)
|
|
|
if err != nil {
|
|
|
- fmt.Println("关注后,通过unionid获取user记录失败,err:" + err.Error())
|
|
|
+ utils.FileLog.Info("关注后,通过unionid获取user记录失败,err:" + err.Error())
|
|
|
return
|
|
|
}
|
|
|
if user != nil {
|
|
@@ -129,11 +151,15 @@ func subscribe(openId string) {
|
|
|
user.ModifyTime = time.Now()
|
|
|
err = user.Update([]string{"modify_time", "is_subscribed"})
|
|
|
if err != nil {
|
|
|
- fmt.Println("关注后,修改绑定用户状态异常,err:", err)
|
|
|
+ utils.FileLog.Info("关注后,修改绑定用户状态异常,err:" + err.Error())
|
|
|
return
|
|
|
}
|
|
|
userRecord.UserId = user.UserId
|
|
|
- userRecord.Update([]string{"user_id"})
|
|
|
+ err = userRecord.Update([]string{"user_id"})
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Info("关注后,修改公众号绑定用户异常,err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
return
|
|
@@ -142,7 +168,7 @@ func subscribe(openId string) {
|
|
|
// 没有记录,那么需要获取下unionid
|
|
|
wxUserItem, err := wechat.GetUserInfo(openId)
|
|
|
if err != nil {
|
|
|
- fmt.Println("获取用户信息失败,err:" + err.Error())
|
|
|
+ utils.FileLog.Info("获取用户信息失败,err:" + err.Error())
|
|
|
return
|
|
|
}
|
|
|
newUserRecord := &models.UserRecord{
|
|
@@ -161,25 +187,29 @@ func subscribe(openId string) {
|
|
|
insertId, err := newUserRecord.Insert()
|
|
|
newUserRecord.UserRecordId = int(insertId)
|
|
|
if err != nil {
|
|
|
- fmt.Println("关注后,添加user_record信息失败,err:" + err.Error())
|
|
|
+ utils.FileLog.Info("关注后,添加user_record信息失败,err:" + err.Error())
|
|
|
return
|
|
|
}
|
|
|
// 检测用户是否绑定小程序,如果绑定修改用户状态
|
|
|
user, err := models.GetUserByUnionId(newUserRecord.UnionId)
|
|
|
if err != nil {
|
|
|
- fmt.Println("修改绑定用户状态异常,ERR:", err)
|
|
|
+ utils.FileLog.Info("修改绑定用户状态异常,err:" + err.Error())
|
|
|
return
|
|
|
}
|
|
|
if user != nil {
|
|
|
- user.IsRegistered = true
|
|
|
+ user.IsSubscribed = true
|
|
|
user.ModifyTime = time.Now()
|
|
|
- err = user.Update([]string{"is_registered", "modify_time"})
|
|
|
+ err = user.Update([]string{"is_subscribed", "modify_time"})
|
|
|
if err != nil {
|
|
|
- fmt.Println("关注后,修改绑定用户状态异常,ERR:", err)
|
|
|
+ utils.FileLog.Info("关注后,修改绑定用户状态异常,err:" + err.Error())
|
|
|
return
|
|
|
}
|
|
|
newUserRecord.UserId = user.UserId
|
|
|
- newUserRecord.Update([]string{"user_id"})
|
|
|
+ err = newUserRecord.Update([]string{"user_id"})
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Info("关注后,修改公众号绑定用户异常,err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|