You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
485 lines
17 KiB
485 lines
17 KiB
6 years ago
|
#encoding: utf-8
|
||
|
class WechatService
|
||
|
|
||
|
def template_data(openid, template_id, type, id, first, key1, key2, key3, remark="")
|
||
|
data = {
|
||
|
touser:openid,
|
||
|
template_id:template_id,
|
||
|
url:"https://open.weixin.qq.com/connect/oauth2/authorize?appid=#{Wechat.config.appid}&redirect_uri=#{Setting.protocol}://#{Setting.host_name}/assets/wechat/app.html#/#{type}/#{id}?response_type=code&scope=snsapi_base&state=123#wechat_redirect",
|
||
|
topcolor:"#FF0000",
|
||
|
data:{
|
||
|
first: {
|
||
|
value:first,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
keyword1:{
|
||
|
value:key1,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
keyword2:{
|
||
|
value:key2,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
keyword3:{
|
||
|
value:key3,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
remark:{
|
||
|
value:remark,
|
||
|
color:"#707070"
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
data
|
||
|
end
|
||
|
|
||
|
def homework_template(user_id, type, id, first, key1, key2, key3, remark="")
|
||
|
uw = UserWechat.where(user_id: user_id).first
|
||
|
unless uw.nil?
|
||
|
data = template_data uw.openid,"3e5Dj2GIx8MOcMyRKpTUEQnM7Tg0ASSCNc01NS9HCGI", type, id, first, key1, key2, key3, remark
|
||
|
begin
|
||
|
req = Wechat.api.template_message_send Wechat::Message.to(uw.openid).template(data)
|
||
|
rescue Exception => e
|
||
|
Rails.logger.error "[homework] ===> #{e}"
|
||
|
end
|
||
|
Rails.logger.info "send over. #{req}"
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def topic_publish_template(user_id, type, id, first, key1, key2, key3, remark="")
|
||
|
uw = UserWechat.where(user_id: user_id).first
|
||
|
unless uw.nil?
|
||
|
data = template_data uw.openid,"oKzFCdk7bsIHnGbscA__N8LPQrBkUShvpjV3-kuwWDQ", type, id,first, key1, key2, key3, remark
|
||
|
Rails.logger.info "start send template message: #{data}"
|
||
|
begin
|
||
|
req = Wechat.api.template_message_send Wechat::Message.to(uw.openid).template(data)
|
||
|
rescue Exception => e
|
||
|
Rails.logger.error "[topic_publish] ===> #{e}"
|
||
|
end
|
||
|
Rails.logger.info "send over. #{req}"
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def comment_template(user_id,type, id, first, key1, key2, key3, remark="")
|
||
|
uw = UserWechat.where(user_id: user_id).first
|
||
|
unless uw.nil?
|
||
|
data = template_data uw.openid,"A_3f5v90-zK73V9Kijm-paDkl9S-NuM8Cf-1UJi92_c",type, id,first, key1, key2, key3, remark
|
||
|
Rails.logger.info "start send template message: #{data}"
|
||
|
begin
|
||
|
req = Wechat.api.template_message_send Wechat::Message.to(uw.openid).template(data)
|
||
|
rescue Exception => e
|
||
|
Rails.logger.error "[comment] ===> #{e}"
|
||
|
end
|
||
|
Rails.logger.info "send over. #{req}"
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def message_update_template(user_id, type, id, first, key1, key2, remark="")
|
||
|
uw = UserWechat.where(user_id: user_id).first
|
||
|
unless uw.nil?
|
||
|
data = {
|
||
|
touser:uw.openid,
|
||
|
template_id:"YTyNPZnQD8uZFBFq-Q6cCOWaq5LA9vL6RFlF2JuD5Cg",
|
||
|
url:"https://open.weixin.qq.com/connect/oauth2/authorize?appid=#{Wechat.config.appid}&redirect_uri=#{Setting.protocol}://#{Setting.host_name}/assets/wechat/app.html#/#{type}/#{id}?response_type=code&scope=snsapi_base&state=123#wechat_redirect",
|
||
|
topcolor:"#FF0000",
|
||
|
data:{
|
||
|
first: {
|
||
|
value:first,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
keyword1:{
|
||
|
value:key1,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
keyword2:{
|
||
|
value:key2,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
remark:{
|
||
|
value:remark,
|
||
|
color:"#707070"
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
Rails.logger.info "start send template message: #{data}"
|
||
|
begin
|
||
|
req = Wechat.api.template_message_send Wechat::Message.to(uw.openid).template(data)
|
||
|
rescue Exception => e
|
||
|
Rails.logger.error "[message_update] ===> #{e}"
|
||
|
end
|
||
|
Rails.logger.info "send over. #{req}"
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def two_keys_template(openid, template_id, type, id, first, key1, key2, remark="",uid)
|
||
|
# tmpurl = "#{Setting.protocol}://#{Setting.host_name}/wechat/user_activities#/#{type}?id=#{id}"
|
||
|
tmpurl = Wechat.config.auto_openid_url_1+"?id="+id.to_s+Wechat.config.auto_openid_url_2+type+Wechat.config.auto_openid_url_3
|
||
|
if uid && uid != 0
|
||
|
# tmpurl = "#{Setting.protocol}://#{Setting.host_name}/wechat/user_activities#/#{type}?id=#{id}"+ "&user_id="+uid.to_s
|
||
|
tmpurl = Wechat.config.auto_openid_url_1+"?id="+id.to_s+Wechat.config.auto_openid_url_2+type+"/user_id="+uid.to_s+Wechat.config.auto_openid_url_3
|
||
|
end
|
||
|
data = {
|
||
|
touser:openid,
|
||
|
template_id:template_id,
|
||
|
url:tmpurl,#/assets/wechat/app.html#/#{type}/#{id}
|
||
|
topcolor:"#FF0000",
|
||
|
data:{
|
||
|
first: {
|
||
|
value:first,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
keyword1:{
|
||
|
value:key1,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
keyword2:{
|
||
|
value:key2,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
remark:{
|
||
|
value:remark,
|
||
|
color:"#707070"
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
data
|
||
|
end
|
||
|
|
||
|
def three_keys_template(openid, template_id, type, id, first, key1, key2, key3, remark="",uid)
|
||
|
# tmpurl = "#{Setting.protocol}://#{Setting.host_name}/wechat/user_activities#/#{type}?id=#{id}"
|
||
|
tmpurl = Wechat.config.auto_openid_url_1+"?id="+id.to_s+Wechat.config.auto_openid_url_2+type+Wechat.config.auto_openid_url_3
|
||
|
if uid && uid != 0
|
||
|
# tmpurl = "#{Setting.protocol}://#{Setting.host_name}/wechat/user_activities#/#{type}?id=#{id}"+ "&user_id="+uid.to_s
|
||
|
tmpurl = Wechat.config.auto_openid_url_1+"?id="+id.to_s+Wechat.config.auto_openid_url_2+type+"/user_id="+uid.to_s+Wechat.config.auto_openid_url_3
|
||
|
end
|
||
|
|
||
|
data = {
|
||
|
touser:openid,
|
||
|
template_id:template_id,
|
||
|
url:tmpurl,#/assets/wechat/app.html#/#{type}/#{id}
|
||
|
topcolor:"#FF0000",
|
||
|
data:{
|
||
|
first: {
|
||
|
value:first,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
keyword1:{
|
||
|
value:key1,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
keyword2:{
|
||
|
value:key2,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
keyword3:{
|
||
|
value:key3,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
remark:{
|
||
|
value:remark,
|
||
|
color:"#707070"
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
data
|
||
|
end
|
||
|
|
||
|
def four_keys_template(openid, template_id, type, id, first, key1, key2, key3, key4, remark="",uid)
|
||
|
# tmpurl = "#{Setting.protocol}://#{Setting.host_name}/wechat/user_activities#/#{type}?id=#{id}"
|
||
|
tmpurl = Wechat.config.auto_openid_url_1+"?id="+id.to_s+Wechat.config.auto_openid_url_2+type+Wechat.config.auto_openid_url_3
|
||
|
if uid && uid != 0
|
||
|
# tmpurl = "#{Setting.protocol}://#{Setting.host_name}/wechat/user_activities#/#{type}?id=#{id}"+ "&user_id="+uid.to_s
|
||
|
tmpurl = Wechat.config.auto_openid_url_1+"?id="+id.to_s+Wechat.config.auto_openid_url_2+type+"/user_id="+uid.to_s+Wechat.config.auto_openid_url_3
|
||
|
end
|
||
|
|
||
|
data = {
|
||
|
touser:openid,
|
||
|
template_id:template_id,
|
||
|
url:tmpurl, #/assets/wechat/app.html#/#{type}/#{id}
|
||
|
topcolor:"#FF0000",
|
||
|
data:{
|
||
|
first: {
|
||
|
value:first,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
keyword1:{
|
||
|
value:key1,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
keyword2:{
|
||
|
value:key2,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
keyword3:{
|
||
|
value:key3,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
keyword4:{
|
||
|
value:key4,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
remark:{
|
||
|
value:remark,
|
||
|
color:"#707070"
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
data
|
||
|
end
|
||
|
|
||
|
def binding_succ_notice(user_id, first, key1, key2)
|
||
|
uw = UserWechat.where(user_id: user_id).first
|
||
|
unless uw.nil?
|
||
|
data = {
|
||
|
touser:uw.openid,
|
||
|
template_id:Wechat.config.binding_succ_notice,
|
||
|
url:Wechat.config.auto_openid_url_1+Wechat.config.auto_openid_url_2+"activites"+Wechat.config.auto_openid_url_3,
|
||
|
topcolor:"#FF0000",
|
||
|
data:{
|
||
|
first: {
|
||
|
value:first,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
keyword1:{
|
||
|
value:key1,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
keyword2:{
|
||
|
value:key2,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
remark:{
|
||
|
value:"绑定成功后可使用微信查看Trustie平台最新动态。",
|
||
|
color:"#707070"
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
begin
|
||
|
req = Wechat.api.template_message_send Wechat::Message.to(uw.openid).template(data)
|
||
|
rescue Exception => e
|
||
|
Rails.logger.error "[homework] ===> #{e}"
|
||
|
end
|
||
|
Rails.logger.info "send over. #{req}"
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def journal_notice(user_id, type, id, first, key1, key2, remark="")
|
||
|
uw = UserWechat.where(user_id: user_id).first
|
||
|
unless uw.nil?
|
||
|
data = two_keys_template uw.openid,Wechat.config.journal_notice, type, id, first, key1, key2, remark
|
||
|
begin
|
||
|
req = Wechat.api.template_message_send Wechat::Message.to(uw.openid).template(data)
|
||
|
rescue Exception => e
|
||
|
Rails.logger.error "[homework] ===> #{e}"
|
||
|
end
|
||
|
Rails.logger.info "send over. #{req}"
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def homework_message_notice(user_id, type, id, first, key1, key2, remark="")
|
||
|
uw = UserWechat.where(user_id: user_id).first
|
||
|
unless uw.nil?
|
||
|
data = two_keys_template uw.openid,Wechat.config.homework_message_notice, type, id, first, key1, key2, remark
|
||
|
begin
|
||
|
req = Wechat.api.template_message_send Wechat::Message.to(uw.openid).template(data)
|
||
|
rescue Exception => e
|
||
|
Rails.logger.error "[homework] ===> #{e}"
|
||
|
end
|
||
|
Rails.logger.info "send over. #{req}"
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def class_notice(user_id, type, id, first, key1, key2, key3, key4, remark="",uid=0)
|
||
|
uw = UserWechat.where(user_id: user_id).first
|
||
|
unless uw.nil?
|
||
|
data = four_keys_template uw.openid,Wechat.config.class_notice, type, id, first, key1, key2, key3, key4, remark, uid
|
||
|
begin
|
||
|
req = Wechat.api.template_message_send Wechat::Message.to(uw.openid).template(data)
|
||
|
rescue Exception => e
|
||
|
Rails.logger.error "[homework] ===> #{e}"
|
||
|
end
|
||
|
Rails.logger.info "send over. #{req}"
|
||
|
end
|
||
|
end
|
||
|
|
||
|
# def join_class_notice(user_id, type, id, first, key1, key2, key3,remark="")
|
||
|
# uw = UserWechat.where(user_id: user_id).first
|
||
|
# unless uw.nil?
|
||
|
# data = three_keys_template uw.openid,Wechat.config.join_class_notice, type, id, first, key1, key2, key3, remark
|
||
|
# begin
|
||
|
# req = Wechat.api.template_message_send Wechat::Message.to(uw.openid).template(data)
|
||
|
# rescue Exception => e
|
||
|
# Rails.logger.error "[join_class__notice] ===> #{e}"
|
||
|
# end
|
||
|
# Rails.logger.info "send over. #{req}"
|
||
|
# end
|
||
|
# end
|
||
|
|
||
|
def create_class_notice(user_id, type, id, first, key1, key2, key3, remark="")
|
||
|
uw = UserWechat.where(user_id: user_id).first
|
||
|
unless uw.nil?
|
||
|
data = {
|
||
|
touser:uw.openid,
|
||
|
template_id:Wechat.config.create_class_notice,
|
||
|
url:Wechat.config.auto_openid_url_1+"?id="+id.to_s+Wechat.config.auto_openid_url_2+"class"+Wechat.config.auto_openid_url_3,
|
||
|
topcolor:"#FF0000",
|
||
|
data:{
|
||
|
first: {
|
||
|
value:first,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
keyword1:{
|
||
|
value:key1,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
keyword2:{
|
||
|
value:key2,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
keyword3:{
|
||
|
value:key3,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
remark:{
|
||
|
value:remark,
|
||
|
color:"#707070"
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
#data = three_keys_template uw.openid,Wechat.config.create_class_notice, type, id, first, key1, key2, key3, remark
|
||
|
begin
|
||
|
req = Wechat.api.template_message_send Wechat::Message.to(uw.openid).template(data)
|
||
|
rescue Exception => e
|
||
|
Rails.logger.error "[wechat_create_class_notice] ===> #{e}"
|
||
|
end
|
||
|
Rails.logger.info "send over. #{req}"
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def create_project_notice(user_id, type, id, first, key1, key2,remark="")
|
||
|
uw = UserWechat.where(user_id: user_id).first
|
||
|
unless uw.nil?
|
||
|
data = {
|
||
|
touser:uw.openid,
|
||
|
template_id:Wechat.config.create_project_notice,
|
||
|
url:Wechat.config.auto_openid_url_1+"?id="+id.to_s+Wechat.config.auto_openid_url_2+"project"+Wechat.config.auto_openid_url_3,
|
||
|
topcolor:"#FF0000",
|
||
|
data:{
|
||
|
first: {
|
||
|
value:first,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
keyword1:{
|
||
|
value:key1,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
keyword2:{
|
||
|
value:key2,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
remark:{
|
||
|
value:remark,
|
||
|
color:"#707070"
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
#data = three_keys_template uw.openid,Wechat.config.create_class_notice, type, id, first, key1, key2, key3, remark
|
||
|
begin
|
||
|
req = Wechat.api.template_message_send Wechat::Message.to(uw.openid).template(data)
|
||
|
rescue Exception => e
|
||
|
Rails.logger.error "[wechat_create_project_notice] ===> #{e}"
|
||
|
end
|
||
|
Rails.logger.info "send over. #{req}"
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def project_review_notice(user_id, type, id, first, key1, key2,key3,remark="",uid=0)
|
||
|
uw = UserWechat.where(user_id: user_id).first
|
||
|
unless uw.nil?
|
||
|
data = three_keys_template uw.openid,Wechat.config.project_review_notice, type, id, first, key1, key2, key3, remark,uid
|
||
|
begin
|
||
|
req = Wechat.api.template_message_send Wechat::Message.to(uw.openid).template(data)
|
||
|
rescue Exception => e
|
||
|
Rails.logger.error "[project_review_notice] ===> #{e}"
|
||
|
end
|
||
|
Rails.logger.info "send over. #{req}"
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def join_project_notice(user_id, type, id, first, key1, key2,remark="",uid=0)
|
||
|
uw = UserWechat.where(user_id: user_id).first
|
||
|
unless uw.nil?
|
||
|
data = two_keys_template uw.openid,Wechat.config.join_project_notice, type, id, first, key1, key2,remark,uid
|
||
|
begin
|
||
|
req = Wechat.api.template_message_send Wechat::Message.to(uw.openid).template(data)
|
||
|
rescue Exception => e
|
||
|
Rails.logger.error "[join_project_notice] ===> #{e}"
|
||
|
end
|
||
|
Rails.logger.info "send over. #{req}"
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def project_issue_notice(user_id, type, id, first, key1, key2,remark="",uid=0)
|
||
|
uw = UserWechat.where(user_id: user_id).first
|
||
|
unless uw.nil?
|
||
|
data = two_keys_template uw.openid,Wechat.config.project_issue_notice, type, id, first, key1, key2,remark,0
|
||
|
begin
|
||
|
req = Wechat.api.template_message_send Wechat::Message.to(uw.openid).template(data)
|
||
|
rescue Exception => e
|
||
|
Rails.logger.error "[project_issue_notice] ===> #{e}"
|
||
|
end
|
||
|
Rails.logger.info "send over. #{req}"
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def at_notice(user_id, type, id, first, key1, key2,key3,remark="",uid=0)
|
||
|
uw = UserWechat.where(user_id: user_id).first
|
||
|
unless uw.nil?
|
||
|
data = three_keys_template uw.openid,Wechat.config.at_notice, type, id, first, key1, key2, key3, remark,uid
|
||
|
begin
|
||
|
req = Wechat.api.template_message_send Wechat::Message.to(uw.openid).template(data)
|
||
|
rescue Exception => e
|
||
|
Rails.logger.error "[at_notice] ===> #{e}"
|
||
|
end
|
||
|
Rails.logger.info "send over. #{req}"
|
||
|
end
|
||
|
end
|
||
|
|
||
|
def rebind_notice(user_id, type, id, first, key1, key2,remark="")
|
||
|
uw = UserWechat.where(user_id: user_id).first
|
||
|
unless uw.nil?
|
||
|
data = {
|
||
|
touser:uw.openid,
|
||
|
template_id:Wechat.config.rebind_notice,
|
||
|
url:Wechat.config.auto_openid_url_1+Wechat.config.auto_openid_url_2+"login"+Wechat.config.auto_openid_url_3,
|
||
|
topcolor:"#FF0000",
|
||
|
data:{
|
||
|
first: {
|
||
|
value:first,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
keyword1:{
|
||
|
value:key1,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
keyword2:{
|
||
|
value:key2,
|
||
|
color:"#707070"
|
||
|
},
|
||
|
remark:{
|
||
|
value:remark,
|
||
|
color:"#707070"
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
#data = three_keys_template uw.openid,Wechat.config.create_class_notice, type, id, first, key1, key2, key3, remark
|
||
|
begin
|
||
|
req = Wechat.api.template_message_send Wechat::Message.to(uw.openid).template(data)
|
||
|
rescue Exception => e
|
||
|
Rails.logger.error "[rebind_notice] ===> #{e}"
|
||
|
end
|
||
|
Rails.logger.info "send over. #{req}"
|
||
|
end
|
||
|
end
|
||
|
|
||
|
end
|