diff --git a/app/api/mobile/apis/activities.rb b/app/api/mobile/apis/activities.rb index 9329ea860..c7f8509ae 100644 --- a/app/api/mobile/apis/activities.rb +++ b/app/api/mobile/apis/activities.rb @@ -9,10 +9,10 @@ module Mobile params do requires :page, type: Integer + requires :openid, type: String end - get ':id' do - #uw = UserWechat.find params[:openid] - user = User.find params[:id] + post do + user = UserWechat.find_by_openid(params[:openid]).user shield_project_ids = ShieldActivity.where("container_type='User' and container_id=#{user.id} and shield_type='Project'").map(&:shield_id) shield_course_ids = ShieldActivity.where("container_type='User' and container_id=#{user.id} and shield_type='Course'").map(&:shield_id) page = params[:page] ? params[:page] : 0 diff --git a/app/api/mobile/apis/new_comment.rb b/app/api/mobile/apis/new_comment.rb index cbd739e8c..8b27803ad 100644 --- a/app/api/mobile/apis/new_comment.rb +++ b/app/api/mobile/apis/new_comment.rb @@ -11,11 +11,12 @@ module Mobile params do requires :type, type: String requires :content, type: String + requires :openid, type: String end post ':id' do type = params[:type] result = 1 - current_user = User.find 8686 + current_user = UserWechat.find_by_openid(params[:openid]).user if params[:content]!="" && current_user case type when "HomeworkCommon" @@ -35,6 +36,8 @@ module Mobile comment.comments = params[:content] comment.author = current_user if news.comments << comment + data = comment + present :data, data, with: Mobile::Entities::Comment result = 2 else result = 3 @@ -50,6 +53,8 @@ module Mobile reply.parent_id = params[:id] reply.subject = "RE: #{topic.subject}" if topic.children << reply + data = reply + present :data, data, with: Mobile::Entities::Message result = 2 else result = 3 diff --git a/app/api/mobile/entities/comment.rb b/app/api/mobile/entities/comment.rb index 803d0c6d0..ce40bb2e5 100644 --- a/app/api/mobile/entities/comment.rb +++ b/app/api/mobile/entities/comment.rb @@ -9,7 +9,7 @@ module Mobile elsif f.is_a?(::Comment) if f.respond_to?(field) if field == :created_on - format_time(f.send(field)) + time_from_now(f.send(field)) else f.send(field) end diff --git a/app/controllers/wechats_controller.rb b/app/controllers/wechats_controller.rb index e9dc03f0e..dac449834 100644 --- a/app/controllers/wechats_controller.rb +++ b/app/controllers/wechats_controller.rb @@ -175,18 +175,18 @@ class WechatsController < ActionController::Base def get_open_id begin raise "非法操作, code不存在" unless params[:code] - openid = get_openid(params[:code]) + openid = get_openid_from_code(params[:code]) raise "无法获取到openid" unless openid - render :text => {status:0, openid: openid}.to_json + render :json => {status:0, openid: openid} rescue Exception=>e - render :text => {status: -1, msg: e.message}.to_json + render :json => {status: -1, msg: e.message} end end def bind begin raise "非法操作, code不存在" unless params[:code] - openid = get_openid(params[:code]) + openid = get_openid_from_code(params[:code]) raise "无法获取到openid" unless openid raise "此微信号已绑定用户, 不能得复绑定" if user_binded?(openid) @@ -200,9 +200,9 @@ class WechatsController < ActionController::Base openid: openid, user: user ) - render :text => {status:0, msg: "绑定成功"}.to_json + render :json => {status:0, msg: "绑定成功"} rescue Exception=>e - render :text => {status: -1, msg: e.message}.to_json + render :json => {status: -1, msg: e.message} end end @@ -212,12 +212,14 @@ class WechatsController < ActionController::Base end private - def get_openid(code) + def get_openid_from_code(code) url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=#{Wechat.config.appid}&secret=#{Wechat.config.secret}&code=#{code}&grant_type=authorization_code" - JSON.parse(URI.parse(url).read)["openid"] + logger.debug url + body = URI.parse(url).read + logger.debug body + JSON.parse(body)["openid"] end - def user_binded?(openid) uw = UserWechat.where(openid: openid).first end diff --git a/config/routes.rb b/config/routes.rb index 2313088cc..327c6af8d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1165,7 +1165,7 @@ RedmineApp::Application.routes.draw do collection do get :login post :bind - get :get_open_id + post :get_open_id end end diff --git a/public/assets/wechat/activities.html b/public/assets/wechat/activities.html index 51bdc474f..7ac787481 100644 --- a/public/assets/wechat/activities.html +++ b/public/assets/wechat/activities.html @@ -1,146 +1,145 @@ - - -
-