Class: Wz::WeixinController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/eric_weixin/wz/weixin_controller.rb

Instance Method Summary collapse

Instance Method Details

#aaObject



77
78
79
# File 'app/controllers/eric_weixin/wz/weixin_controller.rb', line 77

def aa
  @ee = 12
end

#indexObject



6
7
8
# File 'app/controllers/eric_weixin/wz/weixin_controller.rb', line 6

def index
  render :text => params[:echostr]
end

#replyObject



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
   = 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
  weixin_message = MultiXml.parse(request_body).deep_symbolize_keys[:xml]
  message = ReplyMessageRule.process_rule(weixin_message, )
  render xml: message
end

#snsapi_apiObject



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"
   = PublicAccount.where(weixin_app_id: params["weixin_app_id"]).first
  response = RestClient.get "https://api.weixin.qq.com/sns/oauth2/access_token?appid=#{.weixin_app_id}&secret=#{.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_userinfoObject



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"
   = 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_app_id}&secret=#{.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"
   = JSON.parse(response.body)
  query_array << ["nickname", ['nickname']]
  query_array << ["sex", WeixinUser::SEX[['nickname'].to_i]]
  query_array << ["province", ['province']]
  query_array << ["city", ['city']]
  query_array << ["country", ['country']]
  query_array << ["headimgurl", ['headimgurl']]

  query_str = URI.encode_www_form query_array
  url = [url.to_s.split('?')[0], query_str].join '?'
  redirect_to url
end