#encoding=utf-8 require 'net/http' require 'digest' class EcloudController < ApplicationController skip_before_filter :verify_authenticity_token # before_filter :check_sign, only: [:ps_new, :ps_update, :bs_new, :bs_update] before_filter :user_setup # before_filter :require_login, only: [:authorize] skip_before_filter :verify_authenticity_token, only: [:ps_new, :ps_update, :bs_new, :bs_update, :ecloud_login_callback] def index render file: 'public/react-oschina/build/index.html', :layout => false end def trustie_login end CLIENT_ID = '1022' CLIENT_SECRET = '2112037a-6d7a-432b-9081-feb1153d8668' ROOT_URl = 'http://localhost:3000' SERVER_URL = "https://221.176.54.92:9081/restful/services/" ## 签名 def sign(timestamp) Digest::MD5.hexdigest("client_id=#{CLIENT_ID}client_key=#{CLIENT_SECRET}timestamp=#{timestamp}").upcase end # 企业开通 # applyno 申请单号,唯一 # ecordercode 唯一标志一个企业的订购关系 def bs_new ActiveRecord::Base.transaction do begin ecloud = Ecloud.create!(applyno: params['applyno'], ecordercode: params['ecordercode'], opttype: params['opttype'], trial: params['trial'], bossorderid: params['bossorderid'], custid: params['custid'], custtype: params['custtype'], custcode: params['custcode'], registersource: params['registersource'], custname: params['custname'], userid: params['userid'], username: params['username'], useralias: params['useralias'], mobile: params['mobile'], email: params['email'], productcode: params['productcode'], begintime: params['begintime'], endtime: params['endtime']) services = params['services'].first EcloudService.create(opttype: services['opttype'], code: services['code'], begintime: services['begintime'], endtime: services['endtime'], ecloud_id: ecloud.try(:id)) render :json => {result: true, errmsg: ""} rescue Exception => e logger.error(e.message) render :json => {code: 500, msg: "#{e.message}"} raise ActiveRecord::Rollback end end end # 企业更新 def bs_update ActiveRecord::Base.transaction do begin ecloud = Ecloud.where(applyno: params['applyno']).first ecloud.update_attributes!(applyno: params['applyno'], ecordercode: params['ecordercode'], opttype: params['opttype'], custid: params['custid'], custcode: params['custcode'], productcode: params['productcode'], operatime: params['operatime'], effecttime: params['effecttime']) services = params['services'].first ecloud.ecloud_service.update_attributes!(packagecode: services['packagecode'], bossorderid: services['bossorderid']) render :json => {result: true, errmsg: ""} rescue Exception => e logger.error(e.message) render :json => {code: 500, msg: "#{e.message}"} raise ActiveRecord::Rollback end end end # 用户业务开通与变更接口 def ps_new begin user_params = params['users'] user_params.each do |user_param| EcloudUser.create!(custid: params['custid'], opttype: user_param['opttype'], userid: user_param['userid'], username: user_param['username'], useralias: user_param['useralias'], mobile: user_param['mobile'], email: user_param['email'], begintime: user_param['begintime'].to_s, endtime: user_param['endtime'].to_s, status: 1) end render :json => {success: true, errmsg: ""} # end rescue Exception => e logger.error(e.message) render :json => {code: 500, msg: "#{e.message}"} end end # 用户业务状态变更 # 授权statu为1,取消授权status为0 def ps_update begin user_params = params['users'] user_params.each do |user_param| ecloud_user = EcloudUser.where(userid: user_param['userid'], status: 1).first ecloud_user.update_attributes(opttype: user_param['opttype'], userid: user_param['userid'], status: 0) end render :json => {success: true, errmsg: ""} # else # render :json => {code: 404, msg: "企业ID不存在"} # end rescue Exception => e logger.error(e.message) render :json => {code: 500, msg: "#{e.message}"} end end def ecloud_login_callback unless params["test"] == 'true' #获取code logger.info "oauth2 login_callback: #{params}" raise "没有code" unless params[:code] url = "#{SERVER_URL}/oauth2/authorization?grant_type=authorization_code" + "&client_id=#{CLIENT_ID}&scope=&redirect_uri=&code=#{params[:code]}" res = post(url) logger.info "oauth2 authorization resp: #{res}" # {"access_token":"ae673b2d-88b4-46cc-aa74-0b031f24b76f","expires":6,"refresh_token":"7380cc67-a59c-4c21-9000-70e12a58d175","username":"15111030087@QW_er","uid":2147} body = decode(res) raise '登录失败' unless body["access_token"] #获取此用户信息 res = get("#{SERVER_URL}/user/info?access_token=#{body['access_token']}&userid=#{body['uid']}") logger.info "oauth2 get user info: #{res}" # {"userid":2147,"custid":2104,"custcode":"E0002018042810010054","custtype":2,"status":2,"username":"15111030087@QW_er","useralias":"15111030087","isadmin":true,"entprise":"04**004","departments":"","departmentnames":"","mobile":"15365386520","email":"15111030087@139.com"} else res = '{"userid":2147,"custid":2104,"custcode":"E0002018042810010054","custtype":2,"status":2,"username":"15111030087@QW_er","useralias":"15111030087","isadmin":true,"entprise":"04**004","departments":"","departmentnames":"","mobile":"15365386520","email":"15111030087@139.com"}' end # 同步用户 info = decode(res) user = User.find_by_ecoder_user_id(info["userid"]) unless user #新建用户 user = User.create_with_ecoder!(info) end self.logged_user = user user = UserExtensions.where(:user_id => User.current.id).first if user.gender.nil? || user.school_id.nil? || User.current.lastname.nil? redirect_to my_account_path elsif user.identity == 3 && user.school_id.nil? redirect_to my_account_path else redirect_to User.current end end private def get(url) uri = URI(url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = url.start_with?('https') http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Get.new(uri.request_uri) request['Content-Type'] = 'application/json' request['Accept'] = 'application/json' response = http.request(request) response.body end def post(url) uri = URI(url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = url.start_with?('https') http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Post.new(uri.request_uri) request['Content-Type'] = 'application/json' request['Accept'] = 'application/json' response = http.request(request) response.body end def decode(s) begin obj = ActiveSupport::JSON.decode(s) rescue ActiveSupport::JSON.parse_error logger.error("Attempted to decode invalid JSON: #{s}") end end private def check_sign sign = sign(params['timestamp']) if sign != params['sign'] render :json => {code: 501, msg: "sign的值错误"} return end end end