package controllers import ( "encoding/json" "fmt" "hongze/hongze_api/models" "hongze/hongze_api/services" "hongze/hongze_api/utils" "strconv" "time" ) // @Title pc端微信登录接口 // @Description pc端微信登录接口 // @Param Code query string true "微信唯一编码code" // @Success 200 {object} models.WxLoginResp // @router /pc/login [get] func (this *WechatCommonController) PcWechatLogin() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() code := this.GetString("Code") fmt.Println("code:", code) item, err := services.PcWxGetUserOpenIdByCode(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 //accessToken, err := services.PcWxGetAccessToken() //utils.FileLog.Info("accessToken:%s", accessToken) //if err != nil { // br.Msg = "获取用户信息失败" // br.ErrMsg = "获取access_token失败,err:" + err.Error() // return //} utils.FileLog.Info("item.AccessToken):%s", item.AccessToken) //获取用户信息 wxPcUserInfo, err := services.PcWxGetUserInfo(openId, item.AccessToken) if err != nil { br.Msg = "获取用户信息失败" br.ErrMsg = "获取微信用户信息失败,Err:" + err.Error() return } if wxPcUserInfo.Errcode != 0 { userInfoJson, _ := json.Marshal(wxPcUserInfo) br.Msg = "登录失败" br.ErrMsg = "获取用户信息失败,err:" + string(userInfoJson) return } if err != nil{ br.Msg = "微信登录失败" br.ErrMsg = "微信登录失败,err:" + err.Error() return } wxUserInfo := &services.WxUserInfo{ Openid :wxPcUserInfo.Openid, Nickname :wxPcUserInfo.Nickname, Sex :wxPcUserInfo.Sex, Language :wxPcUserInfo.Language, City :wxPcUserInfo.City, Province :wxPcUserInfo.Province, Country :wxPcUserInfo.Country, Headimgurl :wxPcUserInfo.Headimgurl, //SubscribeTime :0, Unionid :wxPcUserInfo.Unionid, //Remark :wxPcUserInfo.Province, //Groupid :wxPcUserInfo.Groupid, //SubscribeScene string `json:"subscribe_scene"` //Errcode int `json:"errcode"` //Errmsg string `json:"errmsg"` } token,userId,firstLogin,permission,err := services.WxLogin(utils.WxPcPlatform,code,item,wxUserInfo) newUser, _ := services.GetWxUserItemByOpenId(wxUserInfo.Openid) utils.FileLog.Info("获取用户信息:%d", userId) var email,headimgurl,mobile,companyName, status, endDate, productName string //用户信息赋值 if newUser != nil && userId > 0{ email = newUser.Email headimgurl = newUser.Headimgurl mobile = newUser.Mobile cp, err := models.GetCompanyProductsByUserId(userId) if err != nil { br.Msg = "登录失败" br.ErrMsg = "登录失败,获取客户信息失败:" + err.Error() return } for _, v := range cp { companyName = v.CompanyName if v.EndDate == "0000-00-00" { v.EndDate = "" } if status != "" { status = status + "/" + v.Status if v.EndDate != "" { endDate = endDate + "/" + v.Status + ":" + v.EndDate } productName = productName + "/" + v.ProductName } else { status = v.Status if v.EndDate != "" { endDate = v.Status + ":" + v.EndDate } productName = v.ProductName } } } //新增登录日志 { loginLog := new(models.WxUserLog) loginLog.UserId = userId loginLog.OpenId = openId loginLog.CreateTime = time.Now() loginLog.Handle="wechat_pc_login" loginLog.Remark=token go models.AddWxUserLog(loginLog) } resp := new(models.WxLoginResp) resp.UserId = userId resp.Code = 0 resp.FirstLogin = firstLogin resp.Authorization = token resp.UserPermission = permission resp.Email = email resp.Headimgurl = headimgurl resp.Mobile = mobile resp.CompanyName = companyName resp.EndDate = endDate resp.Status = status resp.ProductName = productName br.Ret = 200 br.Success = true br.Msg = "登录成功" br.Data = resp } //func init() { // accessToken:=`40_6_YtSyR4Kg9MYbODrNVc1mpztpbZDPWb8aiNzgq96vfvSLGb59dTZk6dprQuhCdsPrqYRKFvaAAij0_NzR-rnMCb0Bxsngov9eLBuk4WQoI` // openId:=`oNUl652NjgjPz_C4M1ezCIro_s98` // item,err:=services.WxGetUserInfo(openId, accessToken) // fmt.Println(err) // fmt.Println(item) //} //func init() { // fmt.Println("start accessToken") // accessToken, err := services.PcWxGetAccessToken() // fmt.Println(err) // utils.FileLog.Info("accessToken:%s",accessToken) // fmt.Println("end accessToken") //}