Skip to content

Commit

Permalink
upto date
Browse files Browse the repository at this point in the history
  • Loading branch information
xixilive committed May 31, 2014
1 parent d522004 commit 9846075
Show file tree
Hide file tree
Showing 34 changed files with 381 additions and 485 deletions.
2 changes: 1 addition & 1 deletion .ruby-gemset
Original file line number Diff line number Diff line change
@@ -1 +1 @@
wechat_faker
wecheat
4 changes: 2 additions & 2 deletions config.ru
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'rack'
require './wechat'
require './wecheat'

Rack::Handler::WEBrick.run WechatFaker
Rack::Handler::WEBrick.run WecheatApp
2 changes: 1 addition & 1 deletion controllers.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Wechat
module Wecheat
module Controllers
Dir[File.expand_path('./controllers/*.rb')].each{|f| require f }

Expand Down
33 changes: 21 additions & 12 deletions controllers/apis.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
module Wechat::Controllers
module Wecheat::Controllers
module Apis
def self.read_body io, parse_json = true
io.set_encoding('utf-8')
parse_json ? (JSON.parse(io.string) rescue nil) : io.string
end

def self.included base
base.class_eval do

before /^\/(message|user|menu|media)\/*/ do
@app = Wechat::Models::App.find_by_access_token(params[:access_token])
@app ||= Wecheat::Models::App.find_by_access_token(params[:access_token])
halt(json errcode: 40001) if @app.nil?
end

# to receive message from out-site app
post '/message/custom/send' do
session[:received_messages] ||= []
session[:received_messages].shift if session[:received_messages].size > 9
session[:received_messages].push request.POST
json errcode: 0, errmsg: "ok"
session[:received_messages].push({app: @app.label, response: request.POST})
json errcode: 0
end

get '/token' do
if app = Wechat::Models::App.find(params[:appid])
if app = Wecheat::Models::App.find(params[:appid])
json access_token: app.access_token, expires_in:7200
else
json errcode: 40012, errmsg: "invalid appid"
json errcode: 40012
end
end

Expand All @@ -34,9 +38,13 @@ def self.included base
end

post '/menu/create' do
@app.button = Wechat::Models::Button.new(request.POST)
@app.save
json errcode: 0, errmsg: "ok"
if data = Apis.read_body(request.body)
@app.button = Wecheat::Models::Button.new(data)
@app.save
json errcode: 0
else
json errcode: 40015, errmsg: 'invalid button data'
end
end

get '/menu/get' do
Expand All @@ -46,15 +54,16 @@ def self.included base
get '/menu/delete' do
@app.button = []
@app.save
json errcode: 0, errmsg: "ok"
json errcode: 0
end

get '/media/get' do
media = @app.media(params[:media_id])
p File.join(settings.root, media.path)
unless media.nil?
send_file File.join(root, media.path)
send_file File.join(settings.public_folder, media.path)
else
json errcode: 40007, errmsg: "invalid media"
json errcode: 40007
end
end

Expand Down
63 changes: 58 additions & 5 deletions controllers/apps.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module Wechat::Controllers
module Wecheat::Controllers
module Apps

def self.included base
base.class_eval do

before '/apps/:id*' do
@app ||= Wechat::Models::App.find(params[:id])
@app ||= Wecheat::Models::App.find(params[:id])
halt(json errcode: 40012, errmsg: "invalid appid") if @app.nil?
end

Expand All @@ -20,10 +20,11 @@ def self.included base

post '/apps/:id/test' do
begin
echostr = Wechat::Utils.rand_secret
json echo: RestClient.get(app.base_url(echostr: echostr)).to_s == echostr
echostr = Wecheat::Utils.rand_secret
echo = RestClient.get(app.base_url(echostr: echostr)).to_s.strip
json error: (echostr != echo), response: echo
rescue => e
json echo: e
json error: true, response: e.inspect
end
end

Expand All @@ -38,6 +39,58 @@ def self.included base
redirect to("/apps/#{@app.id}"), 302
end

get '/apps/:id/message' do
erb :message, locals: { app: @app }
end

post '/apps/:id/message' do
message = params[:message]
user, type = @app.user(message[:user]), message[:type]
halt 404 if user.nil?

builder = Wecheat::MessageBuilder.new.tap do |b|
b.cdata 'ToUserName', @app.label
b.cdata 'FromUserName', message[:user]
b.cdata 'MsgType', type
b.CreateTime Time.now.to_i
b.MsgId Time.now.to_i

case type
when 'text' then b.cdata 'Content', message[:content]
when 'link'
b.cdata 'Url', message[:url]
b.cdata 'Title', message[:title]
b.cdata 'Description', message[:description]

when 'location'
b.cdata 'Location_X', user.longitude
b.cdata 'Location_Y', user.latitude
b.cdata 'Scale', message[:scale]
b.cdata 'Label', message[:label]

when 'image', 'video', 'voice'
media = @app.media(message[:media_id]) || {}
b.cdata 'MediaId', media[:id].to_s
# image message
b.cdata 'PicUrl', uri(media[:path].to_s) if type == 'image'

#video message
b.cdata 'ThumbMediaId', (@app.medias_by_type(:thumb).first || {})[:id] if type == 'video'

#voice message
b.cdata 'Format', 'mp3' if type == 'voice'
end

end

begin
json error: false, response: RestClient.post(@app.base_url, xml: builder.to_hash).to_s
rescue => e
json error: e
end

end

end
end
end
Expand Down
40 changes: 20 additions & 20 deletions controllers/events.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
module Wechat::Controllers
module Wecheat::Controllers
module Events
def self.included base
base.class_eval do

before '/events/:event/:appid/:openid' do
@app ||= Wechat::Models::App.find(params[:appid])
@app ||= Wecheat::Models::App.find(params[:appid])
halt(json errcode: 40012, errmsg: "invalid appid") if @app.nil?
@user ||= @app.user(params[:openid])
halt(json errcode: 46004) if @user.nil?
end

post '/events/:type/:appid/:openid' do
xml = Wechat::MessageBuilder.new.tap do |builder|
builder.CreateTime = Time.now.to_i
builder.cdata 'ToUserName', @user.openid
builder.cdata 'FromUserName', @app.label
builder.cdata 'MsgType', 'event'
builder.cdata 'Event', params[:type]
builder = Wecheat::MessageBuilder.new.tap do |b|
b.CreateTime = Time.now.to_i
b.cdata 'ToUserName', @app.label
b.cdata 'FromUserName', @user.openid
b.cdata 'MsgType', 'event'
b.cdata 'Event', params[:type]

case params[:type]
case params[:type].to_s.downcase
when 'subscribe'
unless params[:event_key].nil?
builder.cdata 'EventKey', params[:event_key]
builder.cdata 'Ticket', @app.id
b.cdata 'EventKey', params[:event_key]
b.cdata 'Ticket', @app.id
end

when 'LOCATION','location'
builder.cdata 'Latitude', @user.latitude
builder.cdata 'Longitude', @user.longitude
builder.cdata 'Precision', @user.precision
when 'location'
b.cdata 'Latitude', @user.latitude
b.cdata 'Longitude', @user.longitude
b.cdata 'Precision', @user.precision

when 'CLICK', 'click'
builder.cdata 'EventKey', params[:event_key]
when 'click', 'view'
b.cdata 'EventKey', params[:event_key]
end
end.to_hash
end

begin
json error: false, response: RestClient.post(app.base_url, xml: xml).to_s
json error: false, response: RestClient.post(app.base_url, xml: builder.to_hash).to_s
rescue => e
json error: e
json error: true, response: e.inspect
end
end

Expand Down
11 changes: 1 addition & 10 deletions controllers/messages.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Wechat::Controllers
module Wecheat::Controllers
module Messages
def self.included base
base.class_eval do
Expand All @@ -7,15 +7,6 @@ def self.included base
json session[:received_messages]
end

get '/apps/:id/message' do
erb :message, locals: { app: Wechat::Models::App.find(params[:id]) }
end

post '/apps/:id/message' do
app = Wechat::Models::App.find(params[:id])
RestClient.post(app.url, params[:message])
end

end
end
end
Expand Down
48 changes: 26 additions & 22 deletions helpers.rb
Original file line number Diff line number Diff line change
@@ -1,42 +1,46 @@
module Wechat
module FormHelpers
def message_type_options
{
text: '文本消息',
image: '图片消息',
voice: '语音消息',
video: '视频消息',
music: '音乐消息',
news: '图文消息'
}
end

def appid_options
Wechat::Models::App.all.collect{|app| "<option value=\"#{app.id}\">#{app.id}</option>" }.join
module Wecheat
module HtmlHelpers
def app_none_url_alert app, with_link = false, text_only = false
unless app.url?
style = text_only ? 'text-danger' : 'alert alert-danger'
'<p class="%s">Needs to %s for this app!</p>' % [style, (with_link ? "<a href=\"/apps/#{app.id}\">set url</a>" : "set url")]
end
end
end

def users_options appid
app = Wechat::Models::App.find(appid)
app.users.collect{|user| "<option value=\"#{user.openid}\">#{user.nickname}</option>" }.join if app
module FormHelpers
def users_select_options collection
select_options(collection){|item| [item.openid, item.nickname] }
end

def medias_options appid, type
""
def medias_select_options collection
select_options(collection){|item| [item.id, "#{item.path} (ID:#{item.id})"] }
end

def news_options appid
def select_options collection, &block
collection.collect do |item|
options = yield(item)
"<option value=\"#{options[0]}\">#{options[1]}</option>"
end.join
end
end

module UrlHelpers
def media_url media
request.host
uri media.path
end

def article_url article
uri "/articles/#{article.id}"
end

def article_pic_url article
uri article.pic_path
end

def menu_item_url item, appid, openid
evt_key = item.type.to_s.downcase == 'click' ? item.key : CGI.escape(item.url.to_s)
'/events/%s/%s/%s?event_key=%s' % [item.type, appid, openid, evt_key]
end
end

Expand Down
7 changes: 0 additions & 7 deletions message.rb

This file was deleted.

Loading

0 comments on commit 9846075

Please sign in to comment.