Class: Wz::WeixinController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- Wz::WeixinController
- Defined in:
- app/controllers/eric_weixin/wz/weixin_controller.rb
Instance Method Summary collapse
Instance Method Details
#aa ⇒ Object
77 78 79 |
# File 'app/controllers/eric_weixin/wz/weixin_controller.rb', line 77 def aa @ee = 12 end |
#index ⇒ Object
6 7 8 |
# File 'app/controllers/eric_weixin/wz/weixin_controller.rb', line 6 def index render :text => params[:echostr] end |
#reply ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'app/controllers/eric_weixin/wz/weixin_controller.rb', line 12 def reply request_body = request.body.read public_account = PublicAccount.find_by_weixin_app_id params[:app_id] # BusinessException.raise 'ip不正确' unless Ip.is_ip_exist? public_account_id: public_account.id, # ip: get_ip "message from wechat: ".to_logger request_body.to_logger = MultiXml.parse(request_body).deep_symbolize_keys[:xml] = ReplyMessageRule.process_rule(, public_account) render xml: end |
#snsapi_api ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/controllers/eric_weixin/wz/weixin_controller.rb', line 24 def snsapi_api require "base64" weixin_public_account = PublicAccount.where(weixin_app_id: params["weixin_app_id"]).first response = RestClient.get "https://api.weixin.qq.com/sns/oauth2/access_token?appid=#{weixin_public_account.weixin_app_id}&secret=#{weixin_public_account.weixin_secret_key}&code=#{params[:code]}&grant_type=authorization_code" result_hash = JSON.parse(response.body) url = URI(Base64.decode64(params["url"])) query_array = URI.decode_www_form url.query||'' query_array << ["openid", result_hash['openid']] query_array << ["state", params["state"]] query_str = URI.encode_www_form query_array url = [url.to_s.split('?')[0], query_str].join '?' redirect_to url end |
#snsapi_userinfo ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/controllers/eric_weixin/wz/weixin_controller.rb', line 38 def snsapi_userinfo require "base64" weixin_public_account = PublicAccount.where(weixin_app_id: params["weixin_app_id"]).first url = URI(Base64.decode64(params["url"])) query_array = URI.decode_www_form url.query||'' query_array << ["state", params["state"]] if params[:code].blank? #先处理用户不同意的情况下,直接跳转到业务页面,agree参数为no query_array << ["agree", 'no'] query_str = URI.encode_www_form query_array url = [url.to_s.split('?')[0], query_str].join '?' redirect_to url return end response = RestClient.get "https://api.weixin.qq.com/sns/oauth2/access_token?appid=#{weixin_public_account.weixin_app_id}&secret=#{weixin_public_account.weixin_secret_key}&code=#{params[:code]}&grant_type=authorization_code" result_hash = JSON.parse(response.body) query_array << ["openid", result_hash['openid']] query_array << ["access_token", result_hash['access_token']] query_array << ["expires_in", result_hash['expires_in']] query_array << ['refresh_token', result_hash['refresh_token']] query_array << ['scope', result_hash['scope']] query_array << ['agree', 'yes'] response = RestClient.get "https://api.weixin.qq.com/sns/userinfo?access_token=#{result_hash['access_token']}&openid=#{result_hash['openid']}&lang=zh_CN" user_info_hash = JSON.parse(response.body) query_array << ["nickname", user_info_hash['nickname']] query_array << ["sex", WeixinUser::SEX[user_info_hash['nickname'].to_i]] query_array << ["province", user_info_hash['province']] query_array << ["city", user_info_hash['city']] query_array << ["country", user_info_hash['country']] query_array << ["headimgurl", user_info_hash['headimgurl']] query_str = URI.encode_www_form query_array url = [url.to_s.split('?')[0], query_str].join '?' redirect_to url end |