socialforge/app/services/wechat_service.rb

113 lines
4.0 KiB
Ruby
Raw Normal View History

2016-04-14 10:57:37 +08:00
#encoding: utf-8
2016-04-14 10:47:53 +08:00
class WechatService
def template_data(openid, template_id, type, id, first, key1, key2, key3, remark="")
2016-04-14 10:47:53 +08:00
data = {
touser:openid,
template_id:template_id,
2016-05-24 09:51:27 +08:00
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",
2016-04-14 10:47:53 +08:00
topcolor:"#FF0000",
data:{
first: {
value:first,
2016-05-03 11:03:34 +08:00
color:"#707070"
2016-04-14 10:47:53 +08:00
},
keyword1:{
value:key1,
2016-05-03 11:03:34 +08:00
color:"#707070"
2016-04-14 10:47:53 +08:00
},
keyword2:{
value:key2,
2016-05-03 11:03:34 +08:00
color:"#707070"
2016-04-14 10:47:53 +08:00
},
keyword3:{
value:key3,
2016-05-03 11:03:34 +08:00
color:"#707070"
2016-04-14 10:47:53 +08:00
},
remark:{
value:remark,
2016-05-03 11:03:34 +08:00
color:"#707070"
2016-04-14 10:47:53 +08:00
}
}
}
data
end
def homework_template(user_id, type, id, first, key1, key2, key3, remark="")
2016-04-14 10:47:53 +08:00
uw = UserWechat.where(user_id: user_id).first
unless uw.nil?
data = template_data uw.openid,"3e5Dj2GIx8MOcMyRKpTUEQnM7Tg0ASSCNc01NS9HCGI", type, id, first, key1, key2, key3, remark
2016-04-14 10:47:53 +08:00
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="")
2016-04-14 10:47:53 +08:00
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
2016-04-14 10:47:53 +08:00
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="")
2016-04-14 10:47:53 +08:00
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
2016-04-14 10:47:53 +08:00
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="")
2016-04-14 10:47:53 +08:00
uw = UserWechat.where(user_id: user_id).first
unless uw.nil?
data = {
touser:uw.openid,
template_id:"YTyNPZnQD8uZFBFq-Q6cCOWaq5LA9vL6RFlF2JuD5Cg",
2016-05-24 09:51:27 +08:00
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",
2016-04-14 10:47:53 +08:00
topcolor:"#FF0000",
data:{
first: {
value:first,
2016-05-03 11:03:34 +08:00
color:"#707070"
2016-04-14 10:47:53 +08:00
},
keyword1:{
value:key1,
2016-05-03 11:03:34 +08:00
color:"#707070"
2016-04-14 10:47:53 +08:00
},
keyword2:{
value:key2,
2016-05-03 11:03:34 +08:00
color:"#707070"
2016-04-14 10:47:53 +08:00
},
remark:{
value:remark,
2016-05-03 11:03:34 +08:00
color:"#707070"
2016-04-14 10:47:53 +08:00
}
}
}
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
end