You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pgfqe6ch8/app/controllers/ecloud_controller.rb

201 lines
6.8 KiB

#encoding=utf-8
6 years ago
require 'net/http'
6 years ago
require 'digest'
6 years ago
class EcloudController < ApplicationController
skip_before_filter :verify_authenticity_token
6 years ago
# before_filter :check_sign, only: [:ps_new, :ps_update, :bs_new, :bs_update]
6 years ago
before_filter :user_setup
# before_filter :require_login, only: [:authorize]
6 years ago
skip_before_filter :verify_authenticity_token, only: [:ps_new, :ps_update, :bs_new, :bs_update, :ecloud_login_callback]
6 years ago
def index
render file: 'public/react-oschina/build/index.html', :layout => false
end
def trustie_login
end
6 years ago
CLIENT_ID = '1022'
CLIENT_SECRET = '2112037a-6d7a-432b-9081-feb1153d8668'
6 years ago
ROOT_URl = 'http://localhost:3000'
SERVER_URL = "https://221.176.54.92:9081/restful/services/"
6 years ago
6 years ago
## 签名
def sign(timestamp)
6 years ago
Digest::MD5.hexdigest("client_id=#{CLIENT_ID}client_key=#{CLIENT_SECRET}timestamp=#{timestamp}").upcase
6 years ago
end
# 企业开通
# applyno 申请单号,唯一
6 years ago
# ecordercode 唯一标志一个企业的订购关系
def bs_new
6 years ago
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
6 years ago
end
end
# 企业更新
def bs_update
6 years ago
ActiveRecord::Base.transaction do
begin
ecloud = Ecloud.where(applyno: params['applyno']).first
6 years ago
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: ""}
6 years ago
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_param = params['users'].first
# ecloud_user = EcloudUser.where(:custid => params['custid'], :userid => user_param['userid']).first
# if ecloud_user.present?
# render :json => {code: 500, msg: "你已开通过该业务"}
# else
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)
render :json => {success: true, errmsg: ""}
# end
rescue Exception => e
logger.error(e.message)
render :json => {code: 500, msg: "#{e.message}"}
end
end
# 用户业务状态变更
def ps_update
begin
ecloud = Ecloud.where(applyno: params['applyno']).first
user_param = params['users'].first
6 years ago
ecloud_user = EcloudUser.where(ecloud_id: ecloud.id).first
6 years ago
ecloud_user.update_attributes(opttype: user_param['opttype'], userid: user_param['userid'])
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
6 years ago
def ecloud_login_callback
#获取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]}"
6 years ago
res = post(url)
logger.info "oauth2 authorization resp: #{res}"
6 years ago
body = decode(res)
#{"access_token":"21a80f20ff736b54aecd002b60210943","token_type":"bearer","expires_in":86400,"refresh_token":"be92e2c137a8c6dd22f0d8c4a622b3aeceb054087a95d293130f04ec60fd3e3f","scope":"user_info","created_at":1542684088}
raise '登录失败' unless body["access_token"]
#获取此用户信息
# res = get("https://gitee.com/api/v5/user?access_token=#{body["access_token"]}")
res = get("#{SERVER_URL}/user/info?access_token=#{body['access_token']}&userid=#{body['uid']}")
logger.info "oauth2 get user info: #{res}"
6 years ago
# 同步用户
# info = decode(res)
#
# user = User.find_by_oschina_user_id(info["id"])
# unless user
# user = User.create_with_oschina!(info)
# end
#
# @current_user = user
6 years ago
render :index
end
private
def get(url)
uri = URI(url)
res = Net::HTTP.start(uri.host, uri.port, use_ssl: url.start_with?('https')) do |http|
req = Net::HTTP::Get.new(uri)
#req['Content-Type'] = 'application/json'
# The body needs to be a JSON string, use whatever you know to parse Hash to JSON
#req.body = {a: 1}.to_json
http.request(req)
end
res.body
end
def post(url)
uri = URI(url)
res = Net::HTTP.start(uri.host, uri.port, use_ssl: url.start_with?('https')) do |http|
req = Net::HTTP::Post.new(uri)
#req['Content-Type'] = 'application/json'
# The body needs to be a JSON string, use whatever you know to parse Hash to JSON
#req.body = {a: 1}.to_json
http.request(req)
end
res.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
6 years ago
def check_sign
6 years ago
sign = sign(params['timestamp'])
if sign != params['sign']
render :json => {code: 501, msg: "sign的值错误"}
return
end
end
6 years ago
6 years ago
end