|
@@ -400,7 +400,8 @@ func (this *WechatCommonController) Notify() {
|
|
|
switch item.Event {
|
|
|
case "subscribe":
|
|
|
fmt.Println("关注")
|
|
|
- go models.UserSubscribe(1, openId)
|
|
|
+ go subscribe(openId)
|
|
|
+
|
|
|
break
|
|
|
case "unsubscribe":
|
|
|
fmt.Println("取消关注")
|
|
@@ -479,3 +480,97 @@ type Notify struct {
|
|
|
EventKey string `xml:"EventKey"`
|
|
|
Content string `xml:"Content"`
|
|
|
}
|
|
|
+
|
|
|
+// subscribe 关注后的处理逻辑
|
|
|
+func subscribe(openId string) {
|
|
|
+ accessToken, err := services.WxGetAccessToken()
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("获取access_token失败,err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ userRecord, err := models.GetUserRecordByOpenId(openId)
|
|
|
+ if err != nil && err.Error() != utils.ErrNoRow() {
|
|
|
+ fmt.Println("通过openid获取user_record记录失败,err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = nil
|
|
|
+
|
|
|
+ // openId已存在
|
|
|
+ if userRecord != nil {
|
|
|
+ if userRecord.UserId > 0 { //已经绑定了的话,那么就去修改用户状态
|
|
|
+ models.UserSubscribe(1, openId)
|
|
|
+ } else {
|
|
|
+ // 没有绑定的话,那么校验下unionid,然后再去修改
|
|
|
+ unionId := userRecord.UnionId
|
|
|
+ if unionId == `` {
|
|
|
+ wxUserItem, err := services.WxGetUserInfo(openId, accessToken)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("获取用户信息失败,err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if wxUserItem.Unionid != `` {
|
|
|
+ unionId = wxUserItem.Unionid
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ updateCol := make([]string, 0)
|
|
|
+ userRecord.Subscribe = 1
|
|
|
+ userRecord.SubscribeTime = time.Now()
|
|
|
+ updateCol = append(updateCol, "Subscribe")
|
|
|
+ if unionId != `` {
|
|
|
+ userRecord.UnionId = unionId
|
|
|
+ // 通过unionid获取已绑定用户的user_record信息
|
|
|
+ bindUserRecord, _ := models.GetBindUserRecordByUnionId(unionId)
|
|
|
+ if bindUserRecord != nil {
|
|
|
+ userRecord.UserId = bindUserRecord.UserId
|
|
|
+ userRecord.RealName = bindUserRecord.RealName
|
|
|
+ userRecord.BindAccount = bindUserRecord.BindAccount
|
|
|
+ updateCol = append(updateCol, "UserId", "RealName", "BindAccount")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ err = userRecord.Update(updateCol)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("关注后,通过openid更新user_record异常,ERR:", err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 没有记录,那么需要获取下unionid
|
|
|
+ wxUserItem, err := services.WxGetUserInfo(openId, accessToken)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("获取用户信息失败,err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ newUserRecord := &models.UserRecord{
|
|
|
+ UserRecordId: 0,
|
|
|
+ OpenId: openId,
|
|
|
+ UnionId: wxUserItem.Unionid,
|
|
|
+ Subscribe: 1,
|
|
|
+ SubscribeTime: time.Now(),
|
|
|
+ NickName: wxUserItem.Nickname,
|
|
|
+ Sex: wxUserItem.Sex,
|
|
|
+ Province: wxUserItem.Province,
|
|
|
+ City: wxUserItem.City,
|
|
|
+ Country: wxUserItem.Country,
|
|
|
+ Headimgurl: wxUserItem.Headimgurl,
|
|
|
+ CreateTime: time.Now(),
|
|
|
+ CreatePlatform: 1,
|
|
|
+ SessionKey: "",
|
|
|
+ }
|
|
|
+ if wxUserItem.Unionid != `` {
|
|
|
+ // 通过unionid获取已绑定用户的user_record信息
|
|
|
+ bindUserRecord, _ := models.GetBindUserRecordByUnionId(wxUserItem.Unionid)
|
|
|
+ if bindUserRecord != nil {
|
|
|
+ newUserRecord.UserId = bindUserRecord.UserId
|
|
|
+ newUserRecord.RealName = bindUserRecord.RealName
|
|
|
+ newUserRecord.BindAccount = bindUserRecord.BindAccount
|
|
|
+ }
|
|
|
+ }
|
|
|
+ _, err = models.AddUserRecord(newUserRecord)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("关注后,添加user_record信息失败,err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+}
|