lib/blsm-mp-wx.rb in blsm-mp-wx-0.3.4 vs lib/blsm-mp-wx.rb in blsm-mp-wx-0.3.5
- old
+ new
@@ -403,9 +403,36 @@
headimgurl: res['headimgurl'],
})
end
end
+ #sns登录(公众号)获取用户信息
+ def sns_mp_login_userinfo(app_id, code)
+ conn = Faraday.new(:url => 'https://api.weixin.qq.com')
+ response = conn.get do |req|
+ req.url '/sns/oauth2/access_token'
+ req.params[:appid] = app_id
+ req.params[:secret] = VdMpWx.where(app_id: app_id).take.app_secret
+ req.params[:code] = code
+ req.params[:grant_type] = 'authorization_code'
+ end
+ response_body = response.body
+ res = parse_json(response_body)
+ return res unless res['nickname']
+
+ VdWxUser.find_or_create_by({app_id: app_id, openid: res['openid']}) do |u|
+ u.update_attributes({nickname: res['nickname'],
+ sex: res['sex'],
+ unionid: res['unionid'],
+ language: res['language'],
+ city: res['city'],
+ province: res['province'],
+ country: res['country'],
+ headimgurl: res['headimgurl'],
+ })
+ end
+ end
+
private
#解析json类型的数据,并将最后的数据转为hash或array
# +json_data_str+ 要转换的json数据
def parse_json(json_data_str)
begin