diff --git a/app/controllers/trainings_controller.rb b/app/controllers/trainings_controller.rb index 948d97be..2a9ed1c2 100644 --- a/app/controllers/trainings_controller.rb +++ b/app/controllers/trainings_controller.rb @@ -4,68 +4,38 @@ class TrainingsController < ApplicationController wechat_responder skip_before_filter :check_if_login_required - skip_before_filter :verify_signature, only: [:show, :create, :update, :test, :enroll] + skip_before_filter :verify_signature, except: [:auth, :auth_callback, :pay_callback] ROOT_URL = ENV["wechat_url"] || "#{Setting.protocol}://#{Setting.host_name}" before_filter :authenticate, except: [:auth, :auth_callback, :pay_callback] - before_filter :find_tag_id, except: [:auth, :auth_callback, :pay_callback] - - before_filter :find_training, only: [:show, :test] - - before_filter :valid_training, only: [:pay, :result, :pay_js] + before_filter :check_training_type, only: [:enroll] + before_filter :check_current_training, only: [:show, :update, :pay, :pay_js, :update_payinfo, :result] layout 'base_trainings' - - # TAG_ID = 'bigdata_hnjcxy_2019' - def show @training = current_training - - url = nil - if @training && !@training.pay? - url = enroll_training_path(id: @tag_id) - elsif @training && @training.pay? - url = result_training_path(id: @tag_id) - else - url = enroll_training_path(id: @tag_id) - end - - redirect_to url end def enroll @training = current_training || Training.new - - if @training && @training.pay? - redirect_to result_training_path(id: @tag_id) - return - end - - Rails.logger.info("##########training_type:#{@training_type}") - @training_title = @training_type == 3 ? "信息技术新工科产学研联盟师资培训班" : "工程教育认证" - Rails.logger.info("##########training_title:#{@training_title}") end def pay @training = current_training - if @training.blank? - redirect_to enroll_training_path(id: @tag_id) - return - end - # 防止重复支付,对于已支付过的,不应该再到这个页来 if @training.payed? - redirect_to result_training_path(id: @tag_id) + redirect_to result_training_path(id: friendly_id) return end if @training.training_payinfo.blank? @training.build_training_payinfo @training.training_payinfo.fee = @training.registration_fee + @training.training_payinfo.pay_type = params[:pay_type].presence || 3 end end @@ -113,36 +83,24 @@ class TrainingsController < ApplicationController end def create - @training = current_training || Training.new(params) + @training = Training.new @training.training_type = @training_type - if @training_type == 3 - @training.research_field = params[:research_field].select(&:present?).uniq.join(",") - end @training.openid = session[:wechat_open_id] - @training.save! - flash[:message] = '提交成功' + save_training - redirect_to pay_training_path(id: @tag_id) - end + flash[:message] = '报名成功' + redirect_to training_path(id: friendly_id) + end def update @training = current_training - if @training.blank? - redirect_to enroll_training_path(id: @tag_id) - return - end + save_training - @training.assign_attributes(params) - if @training.training_type == 3 - @training.research_field = params[:research_field].select(&:present?).uniq.join(",") - end - @training.save! - - flash[:message] = '提交成功' + flash[:message] = '保存成功' - redirect_to pay_training_path(id: @tag_id) + redirect_to training_path(id: friendly_id) end @@ -152,48 +110,45 @@ class TrainingsController < ApplicationController # 采用ajax调用方式,返回支付参数 def update_payinfo @training = current_training - if @training.blank? - redirect_to enroll_training_path(id: @tag_id) - return - end + training_info = @training.training_payinfo || @training.build_training_payinfo + training_info.assign_attributes(params) - attachment = nil - if params[:image] - attachment = Attachment.create!(file: params[:image], author: User.first) + # 已支付不能修改人数 + if training_info.not_payed? + training_info.num = params[:enlistNum].to_i < 1 ? 1 : params[:enlistNum].to_i + training_info.fee = @training.registration_fee(training_info.num) end - #修改以前的订单信息 - training_info = @training.training_payinfo - if training_info.present? - training_info.update_attributes(params) - else - training_info = TrainingPayinfo.new(params) - end - - training_info.num = params[:enlistNum].to_i < 1 ? 1 : params[:enlistNum].to_i + ActiveRecord::Base.transaction do + if training_info.pay_type.to_i == TrainingPayinfo::PayType_bank + attachment = nil + attachment = Attachment.create!(file: params[:image], author: User.first) if params[:image] - training_info.fee = @training.registration_fee(training_info.num) - - training_info.attachment = attachment if attachment.present? + if attachment.blank? + flash[:message] = '请先上传支付凭证' + render 'pay' + return + end - if training_info.pay_type.to_i == TrainingPayinfo::PayType_Wechat - training_info.status = TrainingPayinfo::Status_None - elsif params[:offline_later_pay].to_s == 'true' - # 线下支付-稍后支付 - training_info.status = TrainingPayinfo::Status_None - else - training_info.status = TrainingPayinfo::Status_Wait - end + training_info.attachment = attachment + end - training_info.training_id = @training.id + if training_info.not_payed? + if training_info.pay_type.to_i == TrainingPayinfo::PayType_Wechat + training_info.status = TrainingPayinfo::Status_None + else + training_info.status = TrainingPayinfo::Status_Wait + end + end - training_info.save! + training_info.save! - if params[:js] == 'true' - Rails.logger.info("### start wechat pay => fee: #{training_info.fee}") - _pay_js(0.01 || training_info.fee) - else - redirect_to url = result_training_path(id: @tag_id) + if params[:js] == 'true' + Rails.logger.info("### start wechat pay => fee: #{training_info.fee}") + _pay_js(training_info.fee) + else + redirect_to result_training_path(id: friendly_id) + end end end @@ -268,29 +223,54 @@ class TrainingsController < ApplicationController end end - def find_tag_id - @tag_id = params[:tag_id] || params[:id] - Rails.logger.info("##########TAG_ID:#{@tag_id}") - @training_type = @tag_id == "aeee0601_2019" ? 3 : @tag_id == "ceeaa06_2019" ? 4 : 0 - render_404 if @training_type == 0 - end + def save_training + @training.assign_attributes(params) + @training.training_type = training_type - def find_training - if params[:id] == @tag_id - @training = current_training - else - render_404 + if @training.training_type == 3 + @training.research_field = params[:research_field].select(&:present?).uniq.join(",") end + + @training.save! + end + + def friendly_id + @friendly_id ||= params[:friendly_id].presence || params[:id] + end + + def training_type + @training_type ||= + case friendly_id + when 'aeee0601_2019' then 3 + when 'ceeaa06_2019' then 4 + end end def current_training - Rails.logger.info("##########openid:#{session[:wechat_open_id]}, training_type: #{@training_type}") - @_current_training ||= Training.where(openid: session[:wechat_open_id], training_type: @training_type).first + @_current_training ||= begin + Rails.logger.info("##########openid:#{session[:wechat_open_id]}, friendly_id: #{friendly_id}") + + return if training_type.blank? + + Training.where(openid: session[:wechat_open_id], training_type: training_type).first + end + end + + def check_training_type + return if training_type.present? + + render_404 end - def valid_training - unless current_training - redirect_to training_path(id: @tag_id) + def check_current_training + if current_training.blank? + if training_type.blank? + render_404 + return + end + + redirect_to enroll_training_path(id: friendly_id) + return end end diff --git a/app/models/training_payinfo.rb b/app/models/training_payinfo.rb index 71f1c793..fbb7e9dc 100644 --- a/app/models/training_payinfo.rb +++ b/app/models/training_payinfo.rb @@ -42,6 +42,9 @@ class TrainingPayinfo < ActiveRecord::Base pay_type.to_i == PayType_Wechat && status != Status_Payed end + def not_payed? + status.nil? || status == Status_None + end def payed? status == Status_Payed diff --git a/app/views/layouts/base_trainings.html.erb b/app/views/layouts/base_trainings.html.erb index 4f7f4774..51115cb1 100644 --- a/app/views/layouts/base_trainings.html.erb +++ b/app/views/layouts/base_trainings.html.erb @@ -2,13 +2,16 @@
-<%= @training_title %>
+<%= @training_type == 3 ? '信息技术新工科产学研联盟师资培训班' : '工程教育认证' %>
<%= form_for @training do |f| %> - <%= hidden_field_tag(:tag_id, @tag_id) %> + <%= hidden_field_tag(:friendly_id, @friendly_id) %>基本信息