package controllers import ( "encoding/json" "fmt" "hongze/hongze_mobile_admin/models/response/wechat" "hongze/hongze_mobile_admin/services" "hongze/hongze_mobile_admin/utils" "strconv" ) type WeChatCommon struct { BaseNotAuth } // @Title 微信登录接口 // @Description 微信登录接口 // @Param Code query string true "微信唯一编码code" // @Success 200 {object} wechat.WxLoginResp // @router /login [get] func (this *WeChatCommon) WeChatLogin() { code := this.GetString("Code") fmt.Println("code:", code) utils.FileLog.Info("WechatLogin code:%s", code) item, err := services.WxGetUserOpenIdByCode(code) if err != nil { this.FailWithMessage(fmt.Sprintf("%v", err), fmt.Sprintf("%v", err)) return } if item.Errcode != 0 { this.FailWithMessage("获取用户信息失败", "获取access_token 失败 errcode:"+strconv.Itoa(item.Errcode)+" ;errmsg:"+item.Errmsg) } openId := item.Openid if openId == "" { this.FailWithMessage("获取用户信息失败", "获取openid失败,openid:"+item.Openid) } accessToken, err := services.WxGetAccessToken() if err != nil { this.FailWithMessage("获取用户信息失败", "获取access_token失败,err:"+err.Error()) } //获取用户信息 wxUserInfo, err := services.WxGetUserInfo(openId, accessToken) if err != nil { this.FailWithMessage("获取用户信息失败", "获取微信用户信息失败,Err:"+err.Error()) } if wxUserInfo.Errcode != 0 { userInfoJson, _ := json.Marshal(wxUserInfo) this.FailWithMessage("登录失败", "获取用户信息失败,err:"+string(userInfoJson)) return } token, adminWx, err := services.WxLogin(utils.WxPlatform, item, wxUserInfo) if err != nil { this.FailWithMessage("微信登录失败", "微信登录失败,err:"+err.Error()) return } resp := wechat.WxLoginResp{ AdminId: adminWx.AdminId, Code: 0, Authorization: token, Headimgurl: adminWx.Headimgurl, RealName: adminWx.RealName, } //登录日志 { returnResult, err := json.Marshal(resp) if err != nil { utils.FileLog.Info(this.Ctx.Input.URI() + " Err:%s" + err.Error()) } utils.FileLog.Info(this.Ctx.Input.URI()+" code: %s , return data: %s", code, string(returnResult)) } this.OkDetailed(resp, "登录成功") }