Class: EricWeixin::WeixinUser
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- EricWeixin::WeixinUser
- Defined in:
- app/models/eric_weixin/weixin_user.rb
Constant Summary collapse
- SEX =
{1=>'男' , 2=>'女', 0=>'未知'}
Class Method Summary collapse
-
.connect_member(options) ⇒ Object
绑定会员.
-
.create_weixin_user(public_account_id, openid, channel = nil) ⇒ Object
业务说明:创建、更新微信用户.
- .custom_query(options) ⇒ Object
- .export_users_to_csv(public_account_id) ⇒ Object
-
.get_user_data_from_weixin_api(public_account_id, openid) ⇒ Object
获取用户详情.
-
.search_weixin_user(user) ⇒ Object
在 WeixinUser 模型中查询符合给定哈希的微信用户.
Instance Method Summary collapse
-
#follow_status ⇒ Object
关注状态,关注返回true, 否则返回false.
- #nickname ⇒ Object
-
#set_remark(name) ⇒ Object
设置备注名.
Class Method Details
.connect_member(options) ⇒ Object
绑定会员
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'app/models/eric_weixin/weixin_user.rb', line 142 def connect_member() openid = [:openid] user_name = [:username] password = [:password] ::EricWeixin::WeixinUser.transaction do member_info = MemberInfo.where(user_name: user_name, password: password).first BusinessException.raise '该会员不存在' if member_info.blank? weixin_user = ::EricWeixin::WeixinUser.find_by_openid(openid) BusinessException.raise '该微信用户不存在' if weixin_user.blank? weixin_user.member_info = member_info weixin_user.save! member_info end end |
.create_weixin_user(public_account_id, openid, channel = nil) ⇒ Object
业务说明:创建、更新微信用户.
-
微信用户在关注、取消关注时更新用户信息。
-
当用户关注微信时,创建用户。
-
当用户取消关注时,把用户标记为取消关注
参数说明
-
public_account_id::公众账号的数据库存储id
-
openid::用户的openid,微信服务器传送过来。
调用方法
::EricWeixin::WeixinUser.create_weixin_user 'adsfkj', 'sdfdf'
::EricWeixin::WeixinUser.create_weixin_user 'adsfkj', 'sdfdf'
返回
正常情况下返回当前微信用户 ::EricWeixin::WeixinUser
,抛出异常时错误查看异常信息。
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'app/models/eric_weixin/weixin_user.rb', line 79 def create_weixin_user(public_account_id, openid, channel=nil) is_new = false public_account = ::EricWeixin::PublicAccount.find_by_id(public_account_id) ::EricWeixin::ReplyMessageRule.transaction do is_new = false weixin_user = ::EricWeixin::WeixinUser.where(openid: openid, weixin_public_account_id: public_account.id).first if weixin_user.blank? is_new = true weixin_user = ::EricWeixin::WeixinUser.new openid: openid, weixin_public_account_id: public_account.id weixin_user.save! is_new = true end wx_user_data = public_account.get_user_data_from_weixin_api openid weixin_user.update_attributes(wx_user_data.select{|k,v| ["subscribe", "openid", "nickname", "sex", "language", "city", "province", "country", "headimgurl", "subscribe_time", "remark"].include? k }) if not channel.blank? weixin_user.first_register_channel = channel if weixin_user.first_register_channel.blank? weixin_user.last_register_channel = channel weixin_user.save! end return weixin_user, is_new end end |
.custom_query(options) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'app/models/eric_weixin/weixin_user.rb', line 160 def custom_query users = self.all users = users.where(subscribe: [:subscribe]) unless [:subscribe].blank? users = users.where("nickname like ?", "%#{CGI::escape([:nickname])}%") unless [:nickname].blank? users = users.where(sex: [:sex]) unless [:sex].blank? users = users.where(city: [:city]) unless [:city].blank? users = users.where(province: [:province]) unless [:province].blank? users = users.where(weixin_public_account_id: [:weixin_public_account_id]) unless [:weixin_public_account_id].blank? users = users.where("subscribe_time >= ?", [:start_date].to_date.to_time.to_i) unless [:start_date].blank? users = users.where("subscribe_time <= ?", ([:end_date].to_date+1.day).to_time.to_i) unless [:end_date].blank? users = users.where("last_register_channel = ?", [:last_register_channel]) unless [:last_register_channel].blank? users = users.where("first_register_channel = ?", [:first_register_channel]) unless [:first_register_channel].blank? users end |
.export_users_to_csv(public_account_id) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'app/models/eric_weixin/weixin_user.rb', line 118 def export_users_to_csv(public_account_id) require 'csv' weixin_users = ::EricWeixin::WeixinUser.where(weixin_public_account_id: public_account_id) CSV.generate do |csv| csv << ["订阅状态", "openid", "昵称", "性别", "语言", "城市", "省份", "国家", "订阅时间"] weixin_users.each do |weixin_user| user_fields = [] user_fields << (weixin_user.follow_status ? '订阅': '取消订阅') user_fields << weixin_user.openid user_fields << weixin_user.nickname user_fields << case weixin_user.sex when 1 then '男' when 2 then '女' else '未知' end user_fields << case weixin_user.language when 'zh_CN' then '简体中文' when 'zh_TW' then '繁体中文' when 'en' then '英文' else '其它' end user_fields << weixin_user.city user_fields << weixin_user.province user_fields << weixin_user.country user_fields << Time.at(weixin_user.subscribe_time.to_i).chinese_format csv << user_fields end csv end end |
.get_user_data_from_weixin_api(public_account_id, openid) ⇒ Object
获取用户详情.
根据公众账号id和openid获取公众账号详细信息,最后返回json。
输入参数说明
-
public_account_id 公众账号的id,你懂得,就是public_accounts表中的id。
-
openid WX分配给用户的id
返回
WX返回的用户详细信息,以json格式返回。#
110 111 112 113 114 115 116 |
# File 'app/models/eric_weixin/weixin_user.rb', line 110 def get_user_data_from_weixin_api public_account_id, openid token = ::EricWeixin::AccessToken.get_valid_access_token public_account_id: public_account_id response = RestClient.get "https://api.weixin.qq.com/cgi-bin/user/info?access_token=#{token}&openid=#{openid}&lang=zh_CN" response = JSON.parse response.body response["nickname"] = CGI::escape(response["nickname"]) if not response["nickname"].blank? response end |
.search_weixin_user(user) ⇒ Object
在 WeixinUser 模型中查询符合给定哈希的微信用户. 支持该模型的所有参数作为查询条件
参数说明
-
nickname ::微信用户的昵称
-
openid ::用户的openid,微信服务器传送过来。
调用方法
::EricWeixin::WeixinUser.create_weixin_user 'adsfkj', 'sdfdf'
::EricWeixin::WeixinUser.create_weixin_user 'adsfkj', 'sdfdf'
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'app/models/eric_weixin/weixin_user.rb', line 18 def self.search_weixin_user user @target_user = ::EricWeixin::WeixinUser.all @target_user = @target_user.where("id = ?", user[:id]) unless user[:id].blank? @target_user = @target_user.where("openid = ?", user[:openid]) unless user[:openid].blank? @target_user = @target_user.where("created_at > ? AND created_at < ?", user[:created_at_start], user[:created_at_end]) unless user[:created_at_start].blank? || user[:created_at_end].blank? @target_user = @target_user.where("updated_at > ? AND updated_at < ?", user[:updated_at_start], user[:updated_at_end]) unless user[:updated_at_start].blank? || user[:updated_at_end].blank? @target_user = @target_user.where("subscribe = ?", user[:subscribe]) unless user[:subscribe].blank? @target_user = @target_user.where("nickname = ?", CGI::escape(user[:nickname])) unless user[:nickname].blank? @target_user = @target_user.where("sex = ?", user[:sex]) unless user[:sex].blank? @target_user = @target_user.where("language = ?", user[:language]) unless user[:language].blank? @target_user = @target_user.where("city = ?", user[:city]) unless user[:city].blank? @target_user = @target_user.where("province = ?", user[:province]) unless user[:province].blank? @target_user = @target_user.where("country = ?", user[:country]) unless user[:country].blank? @target_user = @target_user.where("subscribe_time > ? AND subscribe_time < ?", user[:subscribe_time_start], user[:subscribe_time_end]) unless user[:subscribe_time_start].blank? || user[:subscribe_time_end].blank? @target_user = @target_user.where("remark = ?", user[:remark]) unless user[:remark].blank? @target_user = @target_user.where("member_info_id = ?", user[:member_info_id]) unless user[:member_info_id].blank? @target_user = @target_user.where("weixin_public_account_id = ?", user[:weixin_public_account_id]) unless user[:weixin_public_account_id].blank? @target_user = @target_user.where("last_register_channel = ?", user[:last_register_channel]) unless user[:last_register_channel].blank? @target_user = @target_user.where("first_register_channel = ?", user[:first_register_channel]) unless user[:first_register_channel].blank? @target_user end |
Instance Method Details
#follow_status ⇒ Object
关注状态,关注返回true, 否则返回false.
46 47 48 |
# File 'app/models/eric_weixin/weixin_user.rb', line 46 def follow_status self.subscribe.to_i == 1 end |
#nickname ⇒ Object
40 41 42 |
# File 'app/models/eric_weixin/weixin_user.rb', line 40 def nickname CGI::unescape(self.attributes["nickname"]) rescue '无法正常显示' end |
#set_remark(name) ⇒ Object
设置备注名
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/models/eric_weixin/weixin_user.rb', line 51 def set_remark name token = ::EricWeixin::AccessToken.get_valid_access_token public_account_id: self.weixin_public_account_id url = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token=#{token}" response = RestClient.post url, {:openid => self.openid, :remark => name}.to_json response.to_debug response = JSON.parse response.body if response["errcode"].to_i == 0 ::EricWeixin::WeixinUser.create_weixin_user self.weixin_public_account_id, self.openid else BusinessException.raise "设置备注名报错,错误代码为#{response["errcode"]}" end end |