package controllers import ( "encoding/json" "encoding/xml" "fmt" "hongze/hongze_cygxzs/models" "hongze/hongze_cygxzs/services" "hongze/hongze_cygxzs/utils" "strconv" "time" ) type WechatController struct { BaseAuthController } type WechatCommonController struct { BaseCommonController } // @Title 微信登录小助手接口 // @Description 微信登录小助手接口 // @Param Code query string true "微信唯一编码code" // @Success 200 {object} models.UserDetailByUserLogin // @router /loginByxzs [get] func (this *WechatCommonController) WechatLoginByxzs() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() code := this.GetString("Code") if code == "" { br.Msg = "参数错误" br.ErrMsg = "Code 为空" return } var token string item, err := services.WxGetUserOpenIdByCodeXzs(code) if err != nil { br.Msg = "获取用户信息失败" br.ErrMsg = "获取用户信息失败,Err:" + err.Error() return } if item.Errcode != 0 { br.Msg = "获取用户信息失败" br.ErrMsg = "获取access_token 失败 errcode:" + strconv.Itoa(item.Errcode) + " ;errmsg:" + item.Errmsg return } openId := item.Openid if openId == "" { br.Msg = "获取用户信息失败" br.ErrMsg = "获取openid失败,openid:" + item.Openid return } resp := new(models.UserDetailByUserLogin) accessToken, err := services.GetWxAccessTokenByXzs() if err != nil { br.Msg = "获取用户信息失败" br.ErrMsg = "获取access_token失败,err:" + err.Error() return } if accessToken == "" { br.Msg = "获取用户信息失败" br.ErrMsg = "access_token 为空," return } wxUserInfo, err := services.WxGetUserInfo(openId, accessToken) if err != nil { br.Msg = "获取用户信息失败" br.ErrMsg = "获取微信用户信息失败,Err:" + err.Error() return } if wxUserInfo.Errcode != 0 { userInfoJson, _ := json.Marshal(wxUserInfo) br.Msg = "登录失败" br.ErrMsg = "获取用户信息失败,err:" + string(userInfoJson) return } unionId := wxUserInfo.Unionid if unionId == "" { br.Msg = "获取用户信息失败" br.ErrMsg = "获取unionid失败,unionid:" + wxUserInfo.Unionid return } total, err := models.GetCygxUserRecordCount(openId) if err != nil { br.Msg = "获取用户信息失败" br.ErrMsg = "查询数量失败,Err:" + err.Error() return } user, err := models.GetWxUserItemByUserUnionId(unionId) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "获取用户信息失败" br.ErrMsg = "获取本地用户信息失败,Err:" + err.Error() return } items := new(models.CygxUserRecord) items.OpenId = openId items.UnionId = 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() if user != nil { items.CygxUserId = user.UserId items.CygxBindAccount = user.Mobile } if total == 0 { _, err = models.AddCygxUserRecord(items) if err != nil { br.Msg = "获取用户信息失败" br.ErrMsg = "添加openid失败,Err:" + err.Error() return } } else { err = models.UpdateCygxUserRecordMobileByOpenId(user.UserId, user.Mobile, openId) if err != nil { br.Msg = "获取用户信息失败" br.ErrMsg = "更新openid绑定关系失败,Err:" + err.Error() return } } timeUnix := time.Now().Unix() timeUnixStr := strconv.FormatInt(timeUnix, 10) totalItem, err := models.GetTokenByOpenId(openId) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "获取用户信息失败" br.ErrMsg = "查询数量失败,Err:" + err.Error() return } if totalItem == nil { token := utils.MD5(unionId) + utils.MD5(timeUnixStr) itemsSession := new(models.CygxXzsSession) itemsSession.UnionId = unionId itemsSession.OpenId = openId itemsSession.AccessToken = token itemsSession.CreatedTime = time.Now() itemsSession.LastUpdatedTime = time.Now() itemsSession.ExpireTime = time.Now().AddDate(0, 3, 0) if user != nil { itemsSession.UserId = user.UserId } err = models.AddCygxXzsSession(itemsSession) if err != nil { br.Msg = "获取用户信息失败" br.ErrMsg = "添加Token失败,Err:" + err.Error() return } } else { token = totalItem.AccessToken } if user == nil { resp.HasPermission = 3 } else { permissionStr, err := models.GetCompanyPermission(user.CompanyId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取客户信息失败,Err:" + err.Error() return } if permissionStr != "" { resp.Permission = permissionStr resp.Mobile = user.Mobile resp.RealName = user.RealName resp.CompanyName = user.CompanyName resp.HasPermission = 1 } else { resp.Mobile = user.Mobile resp.RealName = user.RealName resp.HasPermission = 2 } resp.Headimgurl = user.HeadimgurlRecord } resp.Token = token br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // @Title 微信获取签名接口 // @Description 微信获取签名接口 // @Param Url query string true "url地址" // @Success 200 {object} models.WechatSign // @router /getWxSign [get] func (this *WechatCommonController) GetWxSign() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() getUrl := this.GetString("Url") accessToken, err := services.GetWxAccessTokenByXzs() if err != nil { br.Msg = "获取用户信息失败" br.ErrMsg = "获取access_token失败,err:" + err.Error() return } if accessToken == "" { br.Msg = "获取用户信息失败" br.ErrMsg = "access_token 为空," return } ticket, err := services.GetWxTicket(accessToken) if err != nil { br.Msg = "获取Ticket失败,请联系客服" br.ErrMsg = "获取Ticket失败,Err" + err.Error() return } if ticket == "" { br.Msg = "获取Ticket失败,请联系客服" br.ErrMsg = "ticket为空" + ticket return } nonceStr := utils.GetRandStringNoSpecialChar(16) signature, nonceString, timestamp := services.GetWxSignature(ticket, getUrl, nonceStr) resp := new(models.WechatSign) resp.AppId = utils.WxPublicAppId resp.NonceStr = nonceString resp.Timestamp = timestamp resp.Url = getUrl resp.Signature = signature br.Ret = 200 br.Success = true br.Msg = "获取签名成功" br.Data = resp } // @Title 获取用户详情 // @Description 获取用户详情接口 // @Success 200 {object} models.UserDetailByUserLogin // @router /user/detail [get] func (this *WechatController) UserInfo() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() user := this.User if user == nil { br.Msg = "请登录" br.ErrMsg = "请登录,用户信息为空" br.Ret = 408 return } resp := new(models.UserDetailByUserLogin) if user.Mobile == "" { resp.HasPermission = 3 } else { permissionStr, err := models.GetCompanyPermission(user.CompanyId) if err != nil { br.Msg = "获取信息失败" br.ErrMsg = "获取客户信息失败,Err:" + err.Error() return } if permissionStr != "" { resp.Permission = permissionStr resp.Mobile = user.Mobile resp.RealName = user.RealName resp.CompanyName = user.CompanyName resp.HasPermission = 1 //fmt.Println(user) go services.UpdateCygxUserRecordMobile(user.Mobile, user.UserId) //更新用户手机号与Openid绑定关系 } else { resp.Mobile = user.Mobile resp.RealName = user.RealName resp.HasPermission = 2 } resp.Headimgurl = user.Headimgurl } br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // @Title 微信获取签名接口 // @Description 微信获取签名接口 // @Param Url query string true "url地址" // @Success 200 {object} models.WechatSign // @router /notify [get,post] func (this *WechatCommonController) Notify() { echostr := this.GetString("echostr") method := this.Ctx.Input.Method() if method == "POST" { body := this.Ctx.Input.RequestBody utils.FileLog.Info("wechat notify:" + string(body)) item := new(models.Notify) err := xml.Unmarshal(body, &item) if err != nil { utils.FileLog.Info("xml.Unmarshal:" + err.Error()) } contactMsg := "感谢慧眼识金的你\r\n\r\n【登录】后就可以收到我们近期所有的研究更新与活动提醒哦~\r\n\r\n想降噪请点击【推送规则】,我们将只为您推送所关注赛道的最新内容\r\n\r\n过往所有纪要和可交互图表请戳【投研素材】 " var openId, returnResult string if item.MsgType != "" { openId = item.FromUserName } xmlTpl := ` %s ` createTime := strconv.FormatInt(time.Now().Unix(), 10) xmlTpl = fmt.Sprintf(xmlTpl, openId, utils.WxId, createTime, contactMsg) total, err := models.GetCygxUserRecordCount(openId) if err != nil { utils.FileLog.Info("GetCygxUserRecordCount:" + err.Error()) } 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 } if unionId == "" { utils.FileLog.Info("获取unionid失败,openId:" + openId) return } wxUser, err := models.GetUserRecordByUnionId(unionId) if err != nil { utils.FileLog.Info("GetUserDetailBuOpenid:" + err.Error()) } if wxUser == nil { utils.FileLog.Info("用户不存在openId:" + openId) return } if item.MsgType == "event" { switch item.Event { case "subscribe": fmt.Println("关注") go models.UserSubscribe(1, wxUser.UserId) go models.CygxUserSubscribe(1, unionId) break case "unsubscribe": fmt.Println("取消关注") go models.UserSubscribe(0, wxUser.UserId) go models.CygxUserSubscribe(0, unionId) break case "CLICK": returnResult = xmlTpl break default: utils.FileLog.Info("wechat notify event:" + item.Event) } this.Ctx.WriteString(xmlTpl) } else if item.MsgType == "text" { returnResult = xmlTpl this.Ctx.WriteString(returnResult) } else { returnResult = xmlTpl } this.Ctx.WriteString(returnResult) } else { this.Ctx.WriteString(echostr) } }