lib/blsm-mp-wx.rb in blsm-mp-wx-0.2.6 vs lib/blsm-mp-wx.rb in blsm-mp-wx-0.2.7

- old
+ new

@@ -1,8 +1,11 @@ # coding: utf-8 require 'cgi' require 'rails' +require 'openssl' +require 'open-uri' +require 'net/https' require 'active_model' require 'active_record' require 'active_support' require 'blsm-mp-wx/version' require 'blsm-mp-wx/model/active_record' @@ -310,9 +313,46 @@ json_obj = parse_json(response.body) return nil unless json_obj return nil unless json_obj.has_key?('total_count') json_obj.inject({}) { |memo, (key, v)| memo[key.to_s.to_sym]=v; memo } + end + + + def all_openid(app_id=nil, next_openid='', returned_openids = []) + app_id ||= self.APP_ID + p "getting openid... next_openid:#{next_openid}" + + begin + conn = Faraday.new(:url => 'https://api.weixin.qq.com') + response = conn.get do |req| + req.url '/cgi-bin/user/get' + req.headers['Content-Type'] = 'application/json' + req.params[:access_token] = access_token(app_id) + req.params[:next_openid] = next_openid + end + response_body = response.body + p response_body + jobject = JSON.parse(response_body) + + if jobject + if jobject['data'] && jobject['data']['openid'] + p "total:#{jobject['total']},count:#{jobject['count']}" + returned_openids += jobject['data']['openid'] + end + + if jobject['next_openid'].to_s != '' + return all_openid(jobject['next_openid'], returned_openids) + end + end + + if !jobject || jobject['next_openid'].to_s =='' + return returned_openids + end + rescue Exception => e + puts e + end + returned_openids end private #解析json类型的数据,并将最后的数据转为hash或array # +json_data_str+ 要转换的json数据