@ -38,8 +38,6 @@ class EcloudController < ApplicationController
ROOT_URl = 'http://localhost:3000'
ROOT_URl = 'http://localhost:3000'
SERVER_URL = " https://221.176.54.92:9081/restful/services/ "
SERVER_URL = " https://221.176.54.92:9081/restful/services/ "
## 签名
## 签名
def sign ( timestamp )
def sign ( timestamp )
Digest :: MD5 . hexdigest ( " client_id= #{ CLIENT_ID } client_key= #{ CLIENT_SECRET } timestamp= #{ timestamp } " ) . upcase
Digest :: MD5 . hexdigest ( " client_id= #{ CLIENT_ID } client_key= #{ CLIENT_SECRET } timestamp= #{ timestamp } " ) . upcase
@ -50,61 +48,87 @@ class EcloudController < ApplicationController
# applyno 申请单号,唯一
# applyno 申请单号,唯一
# ecordercode 唯一标志一个企业的订购关系
# ecordercode 唯一标志一个企业的订购关系
# params['opttype']: 操作类型。0开通; 1变更; 2试用转商用; 4再次开通
# params['opttype']: 操作类型。0开通; 1变更; 2试用转商用; 4再次开通
# 业务列表opttype: 0新增; 1注销; 2修改
# serviceparas: 但是由于企业版是按成员收费的, 所以serviceparas里面会传成员数; 个人版是包月计费的, serviceparas不会传成员数
def bs_new
def bs_new
ActiveRecord :: Base . transaction do
ActiveRecord :: Base . transaction do
begin
begin
ecloud_id = 0
if params [ 'opttype' ] == 0 # 开通企业/个人业务
if params [ 'opttype' ] == 0 # 开通企业/个人业务
ecloud = Ecloud . create! ( eloud_params )
ecloud = Ecloud . create! ( eloud_params )
ecloud_id = ecloud . try ( :id )
create_service ( params [ 'services' ] , ecloud . try ( :id ) )
create_product_params ( params [ 'productparas' ] , ecloud . try ( :id ) ) if params [ 'productparas' ] . present?
# 为管理员添加一条记录
# 开通的时候都是用户的opttype也是0
EcloudUser . create! ( custid : params [ 'custid' ] , opttype : params [ 'opttype' ] , userid : params [ 'userid' ] ,
username : params [ 'username' ] , useralias : params [ 'useralias' ] ,
mobile : params [ 'mobile' ] , email : params [ 'email' ] , begintime : params [ 'begintime' ] ,
endtime : params [ 'endtime' ] )
elsif params [ 'opttype' ] == 2 # 试用转商用 # 变更企业/个人业务
elsif params [ 'opttype' ] == 2 # 试用转商用 # 变更企业/个人业务
ecloud = Ecloud . where ( custid : params [ 'custid' ] , custcode : params [ 'custcode' ] ) . first
# 业务列表:注销业务(注销试用的套餐),另一个业务项的操作代码是:新增业务(开通商用的套餐)
ecloud . update_attributes! ( eloud_params )
# 需要通过产品服务编号和业务编码来区分哪个产品
ecloud . ecloud_productparas . destroy
ecloud = Ecloud . where ( custid : params [ 'custid' ] , custcode : params [ 'custcode' ] , productcode : params [ 'productcode' ] , trial : true ) . first
ecloud . ecloud_services . destroy
ecloud_id = ecloud . try ( :id )
# 注销试用的套餐
elsif params [ 'opttype' ] == 1 # 业务变更
## 需要注销的套餐; 试用转商用是不会有批量操作的, 所以可以使用first
ecloud = Ecloud . where ( bossorderid : params [ 'bossorderid' ] ) . first
des_service = params [ 'services' ] . select { | s | s [ 'opttype' ] == 1 } . first
ecloud . update_attributes! ( eloud_params )
esd = EcloudService . where ( ecloud_id : ecloud . try ( :id ) , code : des_service [ 'code' ] , productcode : params [ 'productcode' ] ) . first
ecloud . ecloud_productparas . destroy
ecloud . update_attribute ( :trial , params [ 'trial' ] )
ecloud . ecloud_services . destroy
esd . update_attributes! ( opttype : des_service [ 'opttype' ] , begintime : des_service [ 'begintime' ] , endtime : des_service [ 'endtime' ] )
ecloud_id = ecloud . try ( :id )
# 试用转商用是不会有批量操作的
# 新增业务
add_service = params [ 'services' ] . select { | s | s [ 'opttype' ] == 0 }
create_service ( add_service , ecloud . try ( :id ) )
#
elsif params [ 'opttype' ] == 4 # 再次重复开通
elsif params [ 'opttype' ] == 1 # 业务变更
REDO
ecloud = Ecloud . where ( custid : params [ 'custid' ] , custcode : params [ 'custcode' ] , productcode : params [ 'productcode' ] ) . first
ecloud = Ecloud . where ( bossorderid : params [ 'bossorderid' ] ) . first
# 套餐变更
end
# 新增业务
add_service = params [ 'services' ] . select { | s | s [ 'opttype' ] == 0 }
create_service ( add_service , ecloud . try ( :id ) ) if add_service . present?
# 业务列表
# 如果有注销业务,注销业务
services = params [ 'services' ]
des_services = params [ 'services' ] . select { | s | s [ 'opttype' ] == 1 }
if services . present?
if des_services . present?
services . each do | service |
des_services . each do | ds |
es = EcloudService . create ( opttype : service [ 'opttype' ] , code : service [ 'code' ] , begintime : service [ 'begintime' ] ,
esd = EcloudService . where ( ecloud_id : ecloud . try ( :id ) , code : ds [ 'code' ] ) . first
endtime : service [ 'endtime' ] , ecloud_id : ecloud_id )
esd . update_attributes! ( opttype : ds [ 'opttype' ] , begintime : ds [ 'begintime' ] , endtime : ds [ 'endtime' ] )
if service [ 'serviceparas' ] . present?
service [ 'serviceparas' ] . each do | servicepara |
EcloudServieceServicepara . create! ( ecloud_service_id : es . id , key : servicepara [ 'key' ] , value : servicepara [ 'value' ] )
end
end
end
end
end
end
# 应用开通参数
# 变更业务
productparas = params [ 'productparas' ]
edt_services = params [ 'services' ] . select { | s | s [ 'opttype' ] == 2 }
if productparas . present?
if edt_services . present?
productparas . each do | productpara |
edt_services . each do | es |
EcloudProductpara . create! ( key : productpara [ 'key' ] , value : productpara [ 'value' ] , ecloud_id : ecloud_id )
ese = EcloudService . where ( ecloud_id : ecloud . try ( :id ) , code : es [ 'code' ] ) . first
ese . update_attributes! ( opttype : es [ 'opttype' ] , begintime : es [ 'begintime' ] , endtime : es [ 'endtime' ] )
create_serviceparas es [ 'serviceparas' ] . first , ese . id
end
end
end
end
# 非试用情况下,为管理员单独创建一条账号,企业账号
unless params [ 'trial' ]
# ecloud = Ecloud.where(bossorderid: params['bossorderid']).first
EcloudUser . create! ( ecloud_id : ecloud . try ( :id ) , opttype : params [ 'opttype' ] , userid : params [ 'userid' ] ,
# ecloud.update_attributes!(eloud_params)
username : params [ 'username' ] , useralias : params [ 'useralias' ] ,
# ecloud.ecloud_productparas.destroy
mobile : params [ 'mobile' ] , email : params [ 'email' ] )
# ecloud.ecloud_services.destroy
#
# ecloud_id = ecloud.try(:id)
elsif params [ 'opttype' ] == 4 # 再次重复开通
ecloud = Ecloud . where ( custid : params [ 'custid' ] , custcode : params [ 'custcode' ] , productcode : params [ 'productcode' ] ) . first
create_service ( params [ 'services' ] , ecloud . try ( :id ) )
create_product_params ( params [ 'productparas' ] , ecloud . try ( :id ) ) if params [ 'productparas' ] . present?
end
end
#
# # 非试用情况下,为管理员单独创建一条账号,企业账号
# unless params['trial']
# EcloudUser.create!(ecloud_id: ecloud.try(:id), opttype: params['opttype'], userid: params['userid'],
# username: params['username'], useralias: params['useralias'],
# mobile: params['mobile'], email: params['email'])
# end
render :json = > { result : true , errmsg : " " }
render :json = > { result : true , errmsg : " " }
rescue Exception = > e
rescue Exception = > e
logger . error ( e . message )
logger . error ( e . message )
@ -115,17 +139,31 @@ class EcloudController < ApplicationController
end
end
# 企业/个人业务变更、注销
# 企业/个人业务变更、注销
# 处理业务平台退订、暂停(欠费暂停,信控暂停等)、暂停后恢复应用业务
# 这个接口是不会新增业务的
# 试用退订的时候bossorderid传空字符
def bs_update
def bs_update
ActiveRecord :: Base . transaction do
ActiveRecord :: Base . transaction do
begin
begin
ecloud = Ecloud . where ( custid : params [ 'custid' ] , custcode : params [ 'custcode' ] ) . first
case params [ 'opttype' ]
ecloud . update_attributes! ( applyno : params [ 'applyno' ] , ecordercode : params [ 'ecordercode' ] , opttype : params [ 'opttype' ] ,
when 0 # 退订
custid : params [ 'custid' ] , custcode : params [ 'custcode' ] , productcode : params [ 'productcode' ] ,
opttype = 6
operatime : params [ 'operatime' ] , effecttime : params [ 'effecttime' ] )
when 1 # 暂停
services = params [ 'services' ] . first
opttype = 7
ecloud . ecloud_service . update_attributes! ( packagecode : services [ 'packagecode' ] , bossorderid : services [ 'bossorderid' ] )
when 2 # 恢复
opttype = 8
end
if params [ 'bossorderid' ] . present?
ecloud = Ecloud . where ( bossorderid : params [ 'bossorderid' ] ) . first
ecloud . update_attribute ( :opttype , opttype )
else
# 试用退订
params [ 'services' ] . each do | service |
ecloud_id = EcloudService . where ( code : service [ 'packagecode' ] ) . first . try ( :ecloud_id )
Ecloud . find ( ecloud_id ) . update_attribute ( :opttype , opttype )
end
end
render :json = > { result : true , errmsg : " " }
render :json = > { result : true , errmsg : " " }
rescue Exception = > e
rescue Exception = > e
logger . error ( e . message )
logger . error ( e . message )
render :json = > { code : 500 , msg : " #{ e . message } " }
render :json = > { code : 500 , msg : " #{ e . message } " }
@ -136,37 +174,55 @@ class EcloudController < ApplicationController
# 用户业务开通与变更接口
# 用户业务开通与变更接口
# 授权statu为1, 取消授权status为0
# 授权statu为1, 取消授权status为0
# user['opttype']: 操作类型0: 开通; 1: 变更; 3: 取消授权; 4: 暂停; 5: 恢复;
def ps_new
def ps_new
ActiveRecord :: Base . transaction do
begin
begin
user_params = params [ 'users' ]
user_params = params [ 'users' ]
user_params . each do | user_param |
user_params . each do | user_param |
if user_param [ 'opttype' ] == 0 # 开通
EcloudUser . create! ( custid : params [ 'custid' ] , opttype : user_param [ 'opttype' ] , userid : user_param [ 'userid' ] ,
EcloudUser . create! ( custid : params [ 'custid' ] , opttype : user_param [ 'opttype' ] , userid : user_param [ 'userid' ] ,
username : user_param [ 'username' ] , useralias : user_param [ 'useralias' ] ,
username : user_param [ 'username' ] , useralias : user_param [ 'useralias' ] ,
mobile : user_param [ 'mobile' ] , email : user_param [ 'email' ] , begintime : user_param [ 'begintime' ] . to_s ,
mobile : user_param [ 'mobile' ] , email : user_param [ 'email' ] , begintime : user_param [ 'begintime' ] . to_s ,
endtime : user_param [ 'endtime' ] . to_s , status : 1 )
endtime : user_param [ 'endtime' ] . to_s )
elsif user_param [ 'opttype' ] == 1 # 变更
ecloud_user = EcloudUser . where ( custid : params [ 'custid' ] , userid : user_param [ 'userid' ] ) . first
ecloud_user . update_attributes! ( opttype : user_param [ 'opttype' ] , 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 )
end
end
end
render :json = > { success : true , errmsg : " " }
render :json = > { success : true , errmsg : " " }
# end
# end
rescue Exception = > e
rescue Exception = > e
logger . error ( e . message )
logger . error ( e . message )
render :json = > { code : 500 , msg : " #{ e . message } " }
render :json = > { code : 500 , msg : " #{ e . message } " }
raise ActiveRecord :: Rollback
end
end
end
end
end
# 用户业务状态变更
# 用户业务状态变更
# 授权statu为1, 取消授权status为0
# user['opttype']: 操作类型0: 开通; 1: 变更; 3: 取消授权; 4: 暂停; 5: 恢复;
def ps_update
def ps_update
begin
begin
user_params = params [ 'users' ]
user_params = params [ 'users' ]
user_params . each do | user_param |
user_params . each do | user_param |
ecloud_user = EcloudUser . where ( userid : user_param [ 'userid' ] , status : 1 ) . first
case user_param [ 'opttype' ]
ecloud_user . update_attributes ( opttype : user_param [ 'opttype' ] , userid : user_param [ 'userid' ] , status : 0 )
when 0 # 取消授权
opttype = 3
when 1 # 暂停
opttype = 4
when 2 # 恢复
opttype = 5
end
ecloud_user = EcloudUser . where ( custid : params [ 'custid' ] , userid : user_param [ 'userid' ] ) . first
ecloud_user . update_attributes! ( opttype : opttype , operatime : params [ 'operatime' ] , effecttime : params [ 'effecttime' ] )
end
end
render :json = > { success : true , errmsg : " " }
render :json = > { success : true , errmsg : " " }
# else
# render :json => {code: 404, msg: "企业ID不存在"}
# end
rescue Exception = > e
rescue Exception = > e
logger . error ( e . message )
logger . error ( e . message )
render :json = > { code : 500 , msg : " #{ e . message } " }
render :json = > { code : 500 , msg : " #{ e . message } " }
@ -174,6 +230,8 @@ class EcloudController < ApplicationController
end
end
def ecloud_login_callback
def ecloud_login_callback
unless params [ " test " ] == 'true'
#获取code
#获取code
logger . info " oauth2 login_callback: #{ params } "
logger . info " oauth2 login_callback: #{ params } "
@ -184,29 +242,41 @@ class EcloudController < ApplicationController
res = post ( url )
res = post ( url )
logger . info " oauth2 authorization resp: #{ res } "
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 )
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 " ]
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' ] } " )
res = get ( " #{ SERVER_URL } /user/info?access_token= #{ body [ 'access_token' ] } &userid= #{ body [ 'uid' ] } " )
logger . info " oauth2 get user info: #{ res } "
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)
info = decode ( res )
#
# user = User.find_by_oschina_user_id(info["id"])
user = User . find_by_ecoder_user_id ( info [ " userid " ] )
# unless user
unless user
# user = User.create_with_oschina!(info)
#新建用户
# end
user = User . create_with_ecoder! ( info )
#
end
# @current_user = user
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
render :index
end
end
@ -257,6 +327,28 @@ class EcloudController < ApplicationController
endtime : params [ 'endtime' ] }
endtime : params [ 'endtime' ] }
end
end
# 新增套餐
def create_service services , ecloud_id
services . each do | service |
ese = EcloudService . create! ( opttype : service [ 'opttype' ] , code : service [ 'code' ] , begintime : service [ 'begintime' ] ,
endtime : service [ 'endtime' ] , ecloud_id : ecloud_id )
if service [ 'serviceparas' ] . present?
create_serviceparas ( service [ 'serviceparas' ] . first , ese . id )
end
end
end
def create_serviceparas params_serviceparas , ese_id
if params_serviceparas . present?
EcloudServieceServicepara . create ( key : params_serviceparas [ 'key' ] , value : params_serviceparas [ 'value' ] , ecloud_service_id : ese_id )
end
end
def create_product_params params , ecloud_id
params . each do | p |
EcloudProductpara . create ( key : p [ 'key' ] , value : p [ 'value' ] , ecloud_id : ecloud_id )
end
end
def check_sign
def check_sign
sign = sign ( params [ 'timestamp' ] )
sign = sign ( params [ 'timestamp' ] )
@ -267,7 +359,8 @@ class EcloudController < ApplicationController
end
end
def save_para
def save_para
EcloudLog . create ( url : request . url , para_value : params , applyno : params [ 'applyno' ] , custid : params [ 'custid' ] , custcode : params [ 'custcode' ] )
EcloudLog . create ( url : request . url , para_value : params , applyno : params [ 'applyno' ] , custid : params [ 'custid' ] ,
custcode : params [ 'custcode' ] , userid : params [ 'userid' ] , username : params [ 'username' ] )
end
end
end
end