eric_weixin 0.3.3 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.rdoc +17 -8
- data/app/assets/stylesheets/eric_weixin/wz/base.css.scss +72 -0
- data/app/controllers/eric_weixin/cms/weixin/media_news_controller.rb +1 -1
- data/app/controllers/eric_weixin/cms/weixin/weixin_users_controller.rb +24 -0
- data/app/controllers/eric_weixin/cms/weixin/xiaodian/orders_controller.rb +37 -4
- data/app/controllers/eric_weixin/wz/xiaodian/orders_controller.rb +8 -0
- data/app/models/eric_weixin/customs_service_record.rb +1 -1
- data/app/models/eric_weixin/media_news.rb +2 -2
- data/app/models/eric_weixin/public_account.rb +46 -2
- data/app/models/eric_weixin/redpack_order.rb +10 -1
- data/app/models/eric_weixin/reply_message_rule.rb +2 -1
- data/app/models/eric_weixin/weixin_user.rb +43 -20
- data/app/models/eric_weixin/xiaodian/order.rb +147 -86
- data/app/views/eric_weixin/cms/weixin/url_encodes/index.html.erb +2 -2
- data/app/views/eric_weixin/cms/weixin/weixin_users/index.html.erb +6 -0
- data/app/views/eric_weixin/cms/weixin/xiaodian/orders/index.html.erb +111 -26
- data/app/views/eric_weixin/wz/xiaodian/orders/index.html.erb +84 -0
- data/config/initializers/assets.rb +1 -0
- data/config/routes.rb +9 -0
- data/lib/eric_weixin/modules/snsapi.rb +1 -1
- data/lib/eric_weixin/version.rb +1 -1
- metadata +33 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 04e1abdd9c635cd4590d5c0195ad1ffdfdb7403c
|
4
|
+
data.tar.gz: 334908139a06070d009af276bb0854879b59c431
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: baf9f37558801003173f17a46ba2c44356e9cd45da49bf7d957e4675560c01e53df73f28e5ef85ea0b42ffa9159d3fb352b927c5d46ddf56e2679256e53bb1e2
|
7
|
+
data.tar.gz: e1231514d5bca951aa94c7072b53e5b675f0551da1ae183f0ef5b6c394c12075ce8096424e29d66c601c2961d9220c408709d9637abd8b1e3c6f0ef9fb7a7429
|
data/README.rdoc
CHANGED
@@ -3,9 +3,11 @@
|
|
3
3
|
目前EricWeixin已封装了认证服务号80%以上的接口,并且有一套后台管理界面与功能自成体系。
|
4
4
|
* 官网暂无。
|
5
5
|
* 大部分功能正在研发中。
|
6
|
+
* 要求数据库支持 utf8mb4 编码格式。 用于表情存储。 mysql 要求在 5.5及以上版本。
|
6
7
|
|
7
8
|
== requirements
|
8
9
|
从大的情况来看,基本上适用于rails4.1.4及以上版本。我自己则是在4.1.4版本上进行研发。
|
10
|
+
Gem 中使用了delayed_job 作为队列机制,用来加快响应速度。 在项目中需要配置好队列。具体可参考:
|
9
11
|
细节待更新。
|
10
12
|
|
11
13
|
==使用方式
|
@@ -31,6 +33,8 @@
|
|
31
33
|
* 微信小店商品刷新 /eric_weixin/cms/weixin/xiaodian/products
|
32
34
|
* 微信用户展示 /eric_weixin/cms/weixin/weixin_users
|
33
35
|
|
36
|
+
===客户展示链接
|
37
|
+
* 查看"我的订单" eric_weixin/weixin/orders?openid=<OPENID>
|
34
38
|
|
35
39
|
===微信小店的初始化
|
36
40
|
* 首先对所经营的分类进行归类,并且把腾讯分类同步至数据库。为了避免分类过多,一般只同步自己行业的大类,大类编号请自查,如儿童玩具大类,食品类两类更新如下:
|
@@ -48,11 +52,16 @@ EricWeixin::Xiaodian::Order.get_order_list_and_update nil, nil ,nil ,'公众账
|
|
48
52
|
* 先在项目根目录新建一个名字叫ca的目录,并设置权限,让应用程序可读。 ca目录用于存放微信官方密钥证书,主要用于发红包过程中的通信加密。
|
49
53
|
* 在weixin_public_account中设置:mch_id: 商户号, mch_key:在商户后台写的32位的那个密码,host_name_wich_schema: 当前域名,在回调时需要参考此参数。
|
50
54
|
* 调用方式:
|
51
|
-
EricWeixin::RedpackOrder.create_redpack_order wishing: 'xxx',
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
* 定时获取红包信息:使用EricWeixin::RedpackOrder的实例调用 get_info 信息。
|
55
|
+
EricWeixin::RedpackOrder.create_redpack_order wishing: 'xxx',
|
56
|
+
client_ip: '127.0.0.1', # 换成自己服务器ip
|
57
|
+
act_name: '过年送红包',
|
58
|
+
remark: '一起开心一下,祝新年快乐',
|
59
|
+
send_name: 'U果源',
|
60
|
+
re_openid: openid, # 收红包人的openid
|
61
|
+
total_amount: 100 # 金额: 单位(分)
|
62
|
+
* 定时获取红包信息:使用EricWeixin::RedpackOrder的实例调用 get_info 信息。
|
63
|
+
|
64
|
+
===队列配置
|
65
|
+
* 队列配置详情: https://github.com/collectiveidea/delayed_job。
|
66
|
+
* 可以起多个任务:
|
67
|
+
|
@@ -0,0 +1,72 @@
|
|
1
|
+
.xiaodian_orders{
|
2
|
+
width: 100%;
|
3
|
+
margin: 0px;
|
4
|
+
height: 100%;
|
5
|
+
background-color: #f2f2f2;
|
6
|
+
.no_xiaodian_order{
|
7
|
+
display: table;
|
8
|
+
height: 100%;
|
9
|
+
width: 100%;
|
10
|
+
text-align: center;
|
11
|
+
.no_order_suggestion{
|
12
|
+
display: table-cell;
|
13
|
+
vertical-align: middle;
|
14
|
+
}
|
15
|
+
}
|
16
|
+
.xiaodian_order{
|
17
|
+
font-size: 12px;
|
18
|
+
background: white;
|
19
|
+
border-bottom: 1px solid #e7e7e7;
|
20
|
+
margin-bottom: .8rem;
|
21
|
+
padding: .17rem .4rem;
|
22
|
+
img{
|
23
|
+
float: left;
|
24
|
+
width: 90px;
|
25
|
+
}
|
26
|
+
.product_info-list{
|
27
|
+
|
28
|
+
.product_info-title{
|
29
|
+
overflow: hidden;
|
30
|
+
.product_info-name{
|
31
|
+
float: left;
|
32
|
+
font-size: 16px;
|
33
|
+
}
|
34
|
+
.product_info-time{
|
35
|
+
float: right;
|
36
|
+
color: #999;
|
37
|
+
}
|
38
|
+
}
|
39
|
+
.product_info-description{
|
40
|
+
overflow: hidden;
|
41
|
+
.product_info-part{
|
42
|
+
float: left;
|
43
|
+
color: #999;
|
44
|
+
margin-right: 10px;
|
45
|
+
}
|
46
|
+
}
|
47
|
+
|
48
|
+
}
|
49
|
+
.user_info-list{
|
50
|
+
overflow: hidden;
|
51
|
+
.user_info-user{
|
52
|
+
overflow: hidden;
|
53
|
+
.user_info-part{
|
54
|
+
float: left;
|
55
|
+
width: 50%;
|
56
|
+
}
|
57
|
+
}
|
58
|
+
.user_info-address{
|
59
|
+
float: left;
|
60
|
+
}
|
61
|
+
|
62
|
+
}
|
63
|
+
.delivery_info-list{
|
64
|
+
overflow: hidden;
|
65
|
+
.delivery_info-part{
|
66
|
+
float: left;
|
67
|
+
width: 50%;
|
68
|
+
}
|
69
|
+
}
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
@@ -23,7 +23,7 @@ class EricWeixin::Cms::Weixin::MediaNewsController < EricWeixin::Cms::BaseContro
|
|
23
23
|
options[:weixin_public_account_id] = params[:public_account_id]
|
24
24
|
options[:nickname] = params[:nickname]
|
25
25
|
@weixin_users = ::EricWeixin::WeixinUser.where(weixin_public_account_id: params[:public_account_id])
|
26
|
-
@weixin_users = @weixin_users.where("nickname like ?", "%#{
|
26
|
+
@weixin_users = @weixin_users.where("nickname like ?", "%#{params[:nickname]}%") unless params[:nickname].blank?
|
27
27
|
render partial: 'select_user'
|
28
28
|
rescue Exception=>e
|
29
29
|
dispose_exception e
|
@@ -22,4 +22,28 @@ class EricWeixin::Cms::Weixin::WeixinUsersController < EricWeixin::Cms::BaseCont
|
|
22
22
|
user.reload
|
23
23
|
render text: user.remark
|
24
24
|
end
|
25
|
+
|
26
|
+
def quick_get_user_infos
|
27
|
+
public_account = ::EricWeixin::PublicAccount.find(params[:public_account_id])
|
28
|
+
if public_account.blank?
|
29
|
+
flash[:alert] = '未指定公众账号'
|
30
|
+
redirect_to action: :index
|
31
|
+
return
|
32
|
+
end
|
33
|
+
public_account.delay(:priority => 10).rebuild_users_simple
|
34
|
+
flash[:success] = '已经把快速更新微信用户信息任务添加到队列任务中'
|
35
|
+
redirect_to action: :index
|
36
|
+
end
|
37
|
+
|
38
|
+
def batch_update_user_infos
|
39
|
+
public_account = ::EricWeixin::PublicAccount.find(params[:public_account_id])
|
40
|
+
if public_account.blank?
|
41
|
+
flash[:alert] = '未指定公众账号'
|
42
|
+
redirect_to action: :index
|
43
|
+
return
|
44
|
+
end
|
45
|
+
public_account.delay(:priority => 10).update_users
|
46
|
+
flash[:success] = '已经把批量更新微信用户信息任务添加到队列任务中'
|
47
|
+
redirect_to action: :index
|
48
|
+
end
|
25
49
|
end
|
@@ -1,8 +1,6 @@
|
|
1
1
|
class EricWeixin::Cms::Weixin::Xiaodian::OrdersController < EricWeixin::Cms::BaseController
|
2
2
|
def index
|
3
|
-
@orders = EricWeixin::Xiaodian::Order.
|
4
|
-
@orders = @orders.where("order_create_time >= ?", params[:start_date].to_time.change(hour:0,min:0,sec:0).to_i) unless params[:start_date].blank?
|
5
|
-
@orders = @orders.where("order_create_time <= ?", params[:end_date].to_time.change(hour:23,min:59,sec:59).to_i) unless params[:end_date].blank?
|
3
|
+
@orders = EricWeixin::Xiaodian::Order.order_query params
|
6
4
|
@orders = @orders.order(order_create_time: :desc).paginate(per_page: params[:per_page]||6, page: params[:page]||1)
|
7
5
|
end
|
8
6
|
|
@@ -27,7 +25,42 @@ class EricWeixin::Cms::Weixin::Xiaodian::OrdersController < EricWeixin::Cms::Bas
|
|
27
25
|
end
|
28
26
|
|
29
27
|
def download_orders
|
30
|
-
file_name = EricWeixin::Xiaodian::Order.get_excel_of_orders params.permit(:start_date, :end_date)
|
28
|
+
file_name = EricWeixin::Xiaodian::Order.get_excel_of_orders params.permit(:start_date, :end_date, :deliver_status, :buyer_nick, :receiver_name, :receiver_mobile)
|
31
29
|
send_file file_name
|
32
30
|
end
|
31
|
+
|
32
|
+
def update_hb_infos
|
33
|
+
EricWeixin::RedpackOrder.delay(priority: 10).update_info_from_wx params[:public_account_id]
|
34
|
+
flash[:success] = '已经将更新红包任务放到队列'
|
35
|
+
redirect_to action: :index
|
36
|
+
end
|
37
|
+
|
38
|
+
def update_order_infos
|
39
|
+
# 默认更新本月订单
|
40
|
+
params[:start_date] ||= Time.now.beginning_of_month
|
41
|
+
params[:end_date] ||= Time.now.end_of_month
|
42
|
+
params[:start_date] = params[:start_date].to_date.change(hour:0,min:0,sec:0)
|
43
|
+
params[:end_date] = params[:end_date].to_date.change(hour:23,min:59,sec:59)
|
44
|
+
|
45
|
+
EricWeixin::Xiaodian::Order.delay(priority: 10).update_order_infos params[:start_date], params[:end_date]
|
46
|
+
flash[:success] = '已经将更新订单信息的任务放到队列'
|
47
|
+
redirect_to action: :index
|
48
|
+
end
|
49
|
+
|
50
|
+
def update_delivery_info_by_excel
|
51
|
+
begin
|
52
|
+
if params[:file].blank?
|
53
|
+
flash[:alert] = '请先选择文件,再更新快递单信息'
|
54
|
+
redirect_to action: :index
|
55
|
+
return
|
56
|
+
end
|
57
|
+
flash[:success] = EricWeixin::Xiaodian::Order.update_delivery_info_by_excel params[:file]
|
58
|
+
redirect_to action: :index
|
59
|
+
rescue Exception=> e
|
60
|
+
dispose_exception e
|
61
|
+
flash[:alert] = get_notice_str
|
62
|
+
redirect_to action: :index
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
33
66
|
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
class EricWeixin::Wz::Xiaodian::OrdersController < ApplicationController
|
2
|
+
def index
|
3
|
+
@orders = ::EricWeixin::Xiaodian::Order.all
|
4
|
+
@orders = @orders.where("openid = ?", params[:openid]).order('order_create_time DESC') unless params[:openid].blank?
|
5
|
+
@orders = @orders.paginate(page: params[:page]||1, per_page: 10)
|
6
|
+
render :layout => false
|
7
|
+
end
|
8
|
+
end
|
@@ -95,7 +95,7 @@ class EricWeixin::CustomsServiceRecord < ActiveRecord::Base
|
|
95
95
|
|
96
96
|
unless options[:nick_name].blank?
|
97
97
|
records = records.joins('LEFT JOIN weixin_users ON weixin_users.openid = weixin_customs_service_records.openid')
|
98
|
-
records = records.where("weixin_users.nickname like ?", "%#{
|
98
|
+
records = records.where("weixin_users.nickname like ?", "%#{options[:nick_name]}%")
|
99
99
|
end
|
100
100
|
|
101
101
|
records
|
@@ -17,7 +17,7 @@ class EricWeixin::MediaNews < ActiveRecord::Base
|
|
17
17
|
# "thumb_media_id" => article.media_resource.media_id,
|
18
18
|
# "author" => article.author,
|
19
19
|
# "content_source_url" => article.content_source_url,
|
20
|
-
# "content" =>
|
20
|
+
# "content" => article.content.force_encoding("UTF-8"),
|
21
21
|
# "digest" => article.digest,
|
22
22
|
# "show_cover_pic" => if article.show_cover_pic then 1 else 0 end
|
23
23
|
# }
|
@@ -52,7 +52,7 @@ class EricWeixin::MediaNews < ActiveRecord::Base
|
|
52
52
|
"thumb_media_id" => article.media_resource.media_id,
|
53
53
|
"author" => article.author,
|
54
54
|
"content_source_url" => article.content_source_url,
|
55
|
-
"content" =>
|
55
|
+
"content" => article.content.gsub("\"","'"),
|
56
56
|
"digest" => article.digest,
|
57
57
|
"show_cover_pic" => if article.show_cover_pic then 1 else 0 end
|
58
58
|
}
|
@@ -98,6 +98,50 @@ class EricWeixin::PublicAccount < ActiveRecord::Base
|
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
|
+
# 更新数据库现有的微信用户信息,用了微信的批量获取用户数据的接口
|
102
|
+
# ===参数说明
|
103
|
+
# 无
|
104
|
+
# ===调用示例
|
105
|
+
# ::EricWeixin::PublicAccount.first.update_users
|
106
|
+
def update_users
|
107
|
+
openids = self.weixin_users.pluck(:openid, :language)
|
108
|
+
index = 0
|
109
|
+
while index <= openids.count
|
110
|
+
params = {}
|
111
|
+
openid_arr = []
|
112
|
+
openids[index..index+99].each do |op|
|
113
|
+
openid_arr << {
|
114
|
+
:openid => op[0],
|
115
|
+
:lang => op[1]
|
116
|
+
}
|
117
|
+
end
|
118
|
+
index += 100
|
119
|
+
params[:user_list] = openid_arr
|
120
|
+
token = ::EricWeixin::AccessToken.get_valid_access_token public_account_id: self.id
|
121
|
+
response = RestClient.post "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=#{token}", params.to_json
|
122
|
+
response = JSON.parse response.body
|
123
|
+
response["user_info_list"].each do |user_info|
|
124
|
+
user = EricWeixin::WeixinUser.find_or_create_by(openid: user_info["openid"], weixin_public_account_id: self.id)
|
125
|
+
if user_info["subscribe"] == 1
|
126
|
+
user_params = user_info.select{|k,v|["subscribe",
|
127
|
+
"openid",
|
128
|
+
"nickname",
|
129
|
+
"sex",
|
130
|
+
"language",
|
131
|
+
"city",
|
132
|
+
"province",
|
133
|
+
"country",
|
134
|
+
"headimgurl",
|
135
|
+
"subscribe_time",
|
136
|
+
"remark"].include?(k) && !v.blank?}
|
137
|
+
user.update_attributes user_params
|
138
|
+
else
|
139
|
+
user.update_attributes subscribe: 0
|
140
|
+
end
|
141
|
+
end unless response["user_info_list"].blank?
|
142
|
+
end unless openids.count == 0
|
143
|
+
end
|
144
|
+
|
101
145
|
# 获取用户列表,并把最新的用户信息存到数据库.
|
102
146
|
# ===参数说明
|
103
147
|
# * next_openid #拉取列表的后一个用户的 next_openid,用户列表未拉取完时存在。
|
@@ -111,6 +155,7 @@ class EricWeixin::PublicAccount < ActiveRecord::Base
|
|
111
155
|
RestClient.get "https://api.weixin.qq.com/cgi-bin/user/get?access_token=#{token}&next_openid=#{next_openid}"
|
112
156
|
end
|
113
157
|
response = JSON.parse response.body
|
158
|
+
pp response
|
114
159
|
if response["count"].to_i > 0
|
115
160
|
response["data"]["openid"].each do |openid|
|
116
161
|
::EricWeixin::WeixinUser.create_weixin_user self.id, openid
|
@@ -133,13 +178,12 @@ class EricWeixin::PublicAccount < ActiveRecord::Base
|
|
133
178
|
RestClient.get "https://api.weixin.qq.com/cgi-bin/user/get?access_token=#{token}&next_openid=#{next_openid}"
|
134
179
|
end
|
135
180
|
response = JSON.parse response.body
|
181
|
+
pp response
|
136
182
|
if response["count"].to_i > 0
|
137
183
|
response["data"]["openid"].each do |openid|
|
138
184
|
users = ::EricWeixin::WeixinUser.where openid: openid
|
139
185
|
if users.blank?
|
140
|
-
|
141
186
|
::EricWeixin::WeixinUser.create_weixin_user self.id, openid
|
142
|
-
|
143
187
|
end
|
144
188
|
end
|
145
189
|
tmp_next_openid = response["next_openid"]
|
@@ -51,7 +51,7 @@ class EricWeixin::RedpackOrder < ActiveRecord::Base
|
|
51
51
|
|
52
52
|
# 定时使用 redpack_order 实例变量来完善补充红包信息。
|
53
53
|
def get_info
|
54
|
-
return
|
54
|
+
# return if self.redpacks.blank?
|
55
55
|
EricWeixin::RedpackOrder.transaction do
|
56
56
|
options = {}
|
57
57
|
options[:mch_billno] = self.mch_billno
|
@@ -90,4 +90,13 @@ class EricWeixin::RedpackOrder < ActiveRecord::Base
|
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
93
|
+
# 指定公众号,从微信服务器更新红包信息
|
94
|
+
# EricWeixin::RedpackOrder.update_info_from_wx 1
|
95
|
+
def self.update_info_from_wx public_account_id
|
96
|
+
self.where("detail_id is null and weixin_public_account_id = ? ", public_account_id).each do |r_o|
|
97
|
+
r_o.get_info
|
98
|
+
end
|
99
|
+
return
|
100
|
+
end
|
101
|
+
|
93
102
|
end
|
@@ -41,7 +41,7 @@ class EricWeixin::ReplyMessageRule < ActiveRecord::Base
|
|
41
41
|
#处理来自微信端客户所有的处理。
|
42
42
|
def process_rule(receive_message, public_account)
|
43
43
|
receive_message_log = receive_message.clone
|
44
|
-
receive_message_log[:Content] =
|
44
|
+
receive_message_log[:Content] = receive_message_log[:Content] if not receive_message_log[:Content].blank?
|
45
45
|
business_type = "#{receive_message[:MsgType]}~#{receive_message[:Event]}"
|
46
46
|
|
47
47
|
#兼容腾讯的一个坑....有的是MsgId, 有的是MsgID
|
@@ -195,6 +195,7 @@ class EricWeixin::ReplyMessageRule < ActiveRecord::Base
|
|
195
195
|
::EricWeixin::MediaNews.update_media_news_after_sending receive_message
|
196
196
|
::Weixin::Process.message_send_job_finish receive_message
|
197
197
|
''
|
198
|
+
|
198
199
|
#暂时识别不了的消息
|
199
200
|
else
|
200
201
|
"暂时未处理的场景".to_logger
|
@@ -1,5 +1,5 @@
|
|
1
1
|
class EricWeixin::WeixinUser < ActiveRecord::Base
|
2
|
-
SEX = {1=>'男'
|
2
|
+
SEX = {1 => '男', 2 => '女', 0 => '未知'}
|
3
3
|
self.table_name = 'weixin_users'
|
4
4
|
belongs_to :member_info
|
5
5
|
belongs_to :weixin_public_account, :class_name => '::EricWeixin::PublicAccount', :foreign_key => 'weixin_public_account_id'
|
@@ -22,7 +22,7 @@ class EricWeixin::WeixinUser < ActiveRecord::Base
|
|
22
22
|
@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?
|
23
23
|
@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?
|
24
24
|
@target_user = @target_user.where("subscribe = ?", user[:subscribe]) unless user[:subscribe].blank?
|
25
|
-
@target_user = @target_user.where("nickname = ?",
|
25
|
+
@target_user = @target_user.where("nickname = ?", user[:nickname]) unless user[:nickname].blank?
|
26
26
|
@target_user = @target_user.where("sex = ?", user[:sex]) unless user[:sex].blank?
|
27
27
|
@target_user = @target_user.where("language = ?", user[:language]) unless user[:language].blank?
|
28
28
|
@target_user = @target_user.where("city = ?", user[:city]) unless user[:city].blank?
|
@@ -38,7 +38,7 @@ class EricWeixin::WeixinUser < ActiveRecord::Base
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def nickname
|
41
|
-
|
41
|
+
self.attributes["nickname"]
|
42
42
|
end
|
43
43
|
|
44
44
|
##
|
@@ -63,7 +63,6 @@ class EricWeixin::WeixinUser < ActiveRecord::Base
|
|
63
63
|
end
|
64
64
|
|
65
65
|
class << self
|
66
|
-
|
67
66
|
# ===业务说明:创建、更新微信用户.
|
68
67
|
# * 微信用户在关注、取消关注时更新用户信息。
|
69
68
|
# * 当用户关注微信时,创建用户。
|
@@ -79,28 +78,42 @@ class EricWeixin::WeixinUser < ActiveRecord::Base
|
|
79
78
|
def create_weixin_user(public_account_id, openid, channel=nil)
|
80
79
|
is_new = false
|
81
80
|
public_account = ::EricWeixin::PublicAccount.find_by_id(public_account_id)
|
81
|
+
weixin_user = ::EricWeixin::WeixinUser.where(openid: openid, weixin_public_account_id: public_account.id).first
|
82
82
|
::EricWeixin::ReplyMessageRule.transaction do
|
83
83
|
is_new = false
|
84
|
-
weixin_user = ::EricWeixin::WeixinUser.where(openid: openid, weixin_public_account_id: public_account.id).first
|
85
84
|
if weixin_user.blank?
|
86
85
|
is_new = true
|
87
86
|
weixin_user = ::EricWeixin::WeixinUser.new openid: openid,
|
88
|
-
|
87
|
+
weixin_public_account_id: public_account.id
|
89
88
|
weixin_user.save!
|
90
89
|
is_new = true
|
91
90
|
end
|
92
|
-
end
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
91
|
+
end
|
92
|
+
|
93
|
+
::EricWeixin::WeixinUser.delay(:priority => -10).get_info public_account_id, openid
|
94
|
+
if not channel.blank?
|
95
|
+
weixin_user.first_register_channel = channel if weixin_user.first_register_channel.blank?
|
96
|
+
weixin_user.last_register_channel = channel
|
97
|
+
weixin_user.save!
|
98
|
+
end
|
99
|
+
return weixin_user, is_new
|
100
|
+
end
|
101
|
+
|
101
102
|
|
103
|
+
# 调腾讯接口,完善用户具体信息。
|
104
|
+
def get_info public_account_id, openid
|
105
|
+
public_account = ::EricWeixin::PublicAccount.find_by_id(public_account_id)
|
106
|
+
wx_user_data = public_account.get_user_data_from_weixin_api openid
|
107
|
+
weixin_user = ::EricWeixin::WeixinUser.where(openid: openid, weixin_public_account_id: public_account.id).first
|
108
|
+
pp wx_user_data["subscribe"]
|
109
|
+
if wx_user_data["subscribe"] == 0
|
110
|
+
weixin_user.update_attributes(wx_user_data.select { |k, v| ["subscribe"].include? k })
|
111
|
+
else
|
112
|
+
weixin_user.update_attributes(wx_user_data.select { |k, v| ["subscribe", "openid", "nickname", "sex", "language", "city", "province", "country", "headimgurl", "subscribe_time", "remark"].include? k })
|
113
|
+
end
|
102
114
|
end
|
103
115
|
|
116
|
+
|
104
117
|
# ===获取用户详情.
|
105
118
|
# 根据公众账号id和openid获取公众账号详细信息,最后返回json。
|
106
119
|
# ===输入参数说明
|
@@ -112,7 +125,6 @@ class EricWeixin::WeixinUser < ActiveRecord::Base
|
|
112
125
|
token = ::EricWeixin::AccessToken.get_valid_access_token public_account_id: public_account_id
|
113
126
|
response = RestClient.get "https://api.weixin.qq.com/cgi-bin/user/info?access_token=#{token}&openid=#{openid}&lang=zh_CN"
|
114
127
|
response = JSON.parse response.body
|
115
|
-
response["nickname"] = CGI::escape(response["nickname"]) if not response["nickname"].blank?
|
116
128
|
response
|
117
129
|
end
|
118
130
|
|
@@ -123,11 +135,22 @@ class EricWeixin::WeixinUser < ActiveRecord::Base
|
|
123
135
|
csv << ["订阅状态", "openid", "昵称", "性别", "语言", "城市", "省份", "国家", "订阅时间"]
|
124
136
|
weixin_users.each do |weixin_user|
|
125
137
|
user_fields = []
|
126
|
-
user_fields << (weixin_user.follow_status ? '订阅': '取消订阅')
|
138
|
+
user_fields << (weixin_user.follow_status ? '订阅' : '取消订阅')
|
127
139
|
user_fields << weixin_user.openid
|
128
140
|
user_fields << weixin_user.nickname
|
129
|
-
user_fields << case weixin_user.sex when 1 then
|
130
|
-
|
141
|
+
user_fields << case weixin_user.sex when 1 then
|
142
|
+
'男' when 2 then
|
143
|
+
'女'
|
144
|
+
else
|
145
|
+
'未知'
|
146
|
+
end
|
147
|
+
user_fields << case weixin_user.language when 'zh_CN' then
|
148
|
+
'简体中文' when 'zh_TW' then
|
149
|
+
'繁体中文' when 'en' then
|
150
|
+
'英文'
|
151
|
+
else
|
152
|
+
'其它'
|
153
|
+
end
|
131
154
|
user_fields << weixin_user.city
|
132
155
|
user_fields << weixin_user.province
|
133
156
|
user_fields << weixin_user.country
|
@@ -161,7 +184,7 @@ class EricWeixin::WeixinUser < ActiveRecord::Base
|
|
161
184
|
def custom_query options
|
162
185
|
users = self.all
|
163
186
|
users = users.where(subscribe: options[:subscribe]) unless options[:subscribe].blank?
|
164
|
-
users = users.where("nickname like ?", "%#{
|
187
|
+
users = users.where("nickname like ?", "%#{options[:nickname]}%") unless options[:nickname].blank?
|
165
188
|
users = users.where(sex: options[:sex]) unless options[:sex].blank?
|
166
189
|
users = users.where(city: options[:city]) unless options[:city].blank?
|
167
190
|
users = users.where(province: options[:province]) unless options[:province].blank?
|