123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- package controllers
- import (
- "encoding/json"
- "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
- }
- 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:" + item.Openid
- return
- }
- total, err := models.GetCygxUserRecordCount(openId)
- if err != nil {
- 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 total == 0 {
- _, err = models.AddCygxUserRecord(items)
- if err != nil {
- br.Msg = "获取用户信息失败"
- br.ErrMsg = "添加openid失败,Err:" + err.Error()
- return
- }
- }
- //else {
- // err = models.UpdateCygxUserRecord(items)
- // if err != nil {
- // br.Msg = "获取用户信息失败"
- // br.ErrMsg = "添加openid失败,Err:" + err.Error()
- // return
- // }
- //}
- token := utils.MD5(unionId)
- user, err := models.GetWxUserItemByUserUnionId(unionId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取用户信息失败"
- br.ErrMsg = "获取本地用户信息失败,Err:" + err.Error()
- return
- }
- totalToken, err := models.GetXzsSessionCountByToken(token)
- if err != nil {
- br.Msg = "获取用户信息失败"
- br.ErrMsg = "查询数量失败,Err:" + err.Error()
- return
- }
- if totalToken == 0 {
- itemsSession := new(models.CygxXzsSession)
- itemsSession.UnionId = unionId
- itemsSession.AccessToken = token
- itemsSession.CreatedTime = time.Now()
- if user != nil {
- itemsSession.UserId = user.UserId
- }
- err = models.AddCygxXzsSession(itemsSession)
- if err != nil {
- br.Msg = "获取用户信息失败"
- br.ErrMsg = "添加Token失败,Err:" + err.Error()
- return
- }
- }
- 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
- }
- if resp.Headimgurl == "" {
- resp.Headimgurl = user.HeadimgurlRecord
- }
- }
- if resp.Headimgurl == "" {
- resp.Headimgurl = utils.HeadimgurlDefault
- }
- 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 获取用户详情接口
- // @Param Token query string true "Token"
- // @Success 200 {object} models.UserDetailByUserLogin
- // @router /user/detail [get]
- func (this *WechatCommonController) UserInfo() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- token := this.GetString("Token")
- if token == "" {
- br.Msg = "参数错误"
- br.ErrMsg = "Token 为空"
- return
- }
- resp := new(models.UserDetailByUserLogin)
- session, err := models.GetUnionidByToken(token)
- if err != nil {
- br.Msg = "获取用户信息失败"
- br.ErrMsg = "查询session失败,Err:" + err.Error()
- return
- }
- unionId := session.UnionId
- user, err := models.GetWxUserItemByUserUnionId(unionId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取用户信息失败"
- br.ErrMsg = "获取本地用户信息失败,Err:" + err.Error()
- return
- }
- 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
- }
- if resp.Headimgurl == "" {
- resp.Headimgurl = utils.HeadimgurlDefault
- }
- resp.Token = utils.MD5(unionId)
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // @Title 获取用户详情
- // @Description 获取用户详情接口
- // @Param Token query string true "Token"
- // @Success 200 {object} models.UserDetailByUserLogin
- // @router /test [get]
- func (this *WechatCommonController) Test() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- //测试路由
- token := this.GetString("Token")
- if token == "" {
- br.Msg = "参数错误"
- br.ErrMsg = "Token 为空"
- return
- }
- resp := new(models.UserDetailByUserLogin)
- session, err := models.GetUnionidByToken(token)
- if err != nil {
- br.Msg = "获取用户信息失败"
- br.ErrMsg = "查询session失败,Err:" + err.Error()
- return
- }
- unionId := session.UnionId
- user, err := models.GetWxUserItemByUserUnionId(unionId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取用户信息失败"
- br.ErrMsg = "获取本地用户信息失败,Err:" + err.Error()
- return
- }
- 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
- }
- if resp.Headimgurl == "" {
- resp.Headimgurl = utils.HeadimgurlDefault
- }
- resp.Token = utils.MD5(unionId)
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
|