Class: Sap::SapAnywhereInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/resource/sap/sap_anywhere_interface.rb

Defined Under Namespace

Modules: Code

Instance Method Summary collapse

Instance Method Details

#check_access_token(response, params) ⇒ Object

Note:

根据http返回code对access_token进行验证

根据http返回code对access_token进行验证

Parameters:

  • response (string)

    来源



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/resource/sap/sap_anywhere_interface.rb', line 194

def check_access_token(response, params)
  #
  # Rails.logger.info "code是#{response.try(:code)}"

  if response.blank? || [Code::UNAUTHORIZED, Code::FORBIDDEN].include?(response.code)
    sap = SapAnywhereAccount.where(source: params[:source]).last
    response = RestClient.get sap.get_access_token_url

    body = self.handle_response(response.body)

    sap.update!(access_token: body.access_token)

    Rails.cache.write([SapAnywhereAccount.class_name, params[:source]], body.access_token)


  end

end

#check_function_and_shopObject

Note:

功能验证a

功能验证



243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/resource/sap/sap_anywhere_interface.rb', line 243

def check_function_and_shop
  source_list = SystemSource.option_for_select
   shop_ids_map = source_list.each_with_object({}) do |source_list, map|
    source = source_list.source
    accounts = SapFunctionAccount.where(source: source, function: self.to_s.split(':').third, status: SapFunctionAccount::Status::ENABLED)
    key = source
    if accounts.present?
      Rails.logger.info "aaaaaaaa"
      map[key] = accounts.map(&:shop_id)
    end
  end
end

#convert_payment_type(order) ⇒ Object

转换云店家的支付类型,对应上sap的支付类型 params 类型Order return string sap上支付类型的对应id



230
231
232
233
234
235
236
237
238
239
# File 'lib/resource/sap/sap_anywhere_interface.rb', line 230

def convert_payment_type(order)
  if order.try(:payment_type) == ::Order::PaymentType::Alipay
    # 在线支付
    id = '1'
  elsif order.try(:payment_type) == ::Order::PaymentType::CASH_ON_DELIVERY
    # 货到付款
    id = '2'
  end
  id
end

#convert_price_unit(order) ⇒ Object

转换云店家中的货币单位,对应上sap上的货币 params 类型:order return string, string



216
217
218
219
220
221
222
223
224
225
# File 'lib/resource/sap/sap_anywhere_interface.rb', line 216

def convert_price_unit(order)
  if order.try(:price_unit) == 1
    code = 'RMB'
    isoCode = 'CNY'
  else
    code = 'USD'
    isoCode = 'USD'
  end
  return code
end

#delete(params) ⇒ Object

Note:

post请求的的数据

delete请求的的数据

Parameters:

  • source (string)

    来源

  • request_name (string)

    请求资源名

  • id (Integer)

    id



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/resource/sap/sap_anywhere_interface.rb', line 118

def delete(params)

  response = RestClient.delete get_access_token_url(params)
  handle_response(response)
rescue => e

  check_access_token(e.try(:response), params)
  # puts e.message
  if [Code::UNAUTHORIZED, Code::FORBIDDEN].include?(e.try(:response).code)

    Rails.logger.info "重新发出请求"
    retry
  elsif Sap::SapAnywhereInterface::Code::BADREQUEST == e.try(:response).code
    Rails.logger.info "请求的链接有误(例如:错误的id)"
  end
end

#get(params, time = 0) ⇒ Object

Note:

获得get请求的数据

获得get请求的数据

Parameters:

  • source (string)

    来源

  • request_name (string)

    请求资源名 /Products /Products/id /Products/count /Products/CustomFieldsMeta

Returns:

  • 解析后的数据



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/resource/sap/sap_anywhere_interface.rb', line 31

def get(params, time = 0)

  get_params = get_access_token_url(params)

  response = RestClient.get get_params

  handle_response(response)

rescue => e
  check_access_token(e.try(:response), params)
  if response.blank? || [Code::UNAUTHORIZED, Code::FORBIDDEN].include?(e.try(:response).code)
    time += 1
    Rails.logger.info "重新发出请求"
    if time < 4
    Rails.logger.info "time#{time}"
     retry
    end
  elsif Sap::SapAnywhereInterface::Code::BADREQUEST == e.try(:response).code
    Rails.logger.info "请求的链接有误(例如:错误的id)"
  end
end

#get_access_token(params) ⇒ Object

Note:

根据来源获得sap的access_token

根据来源获得sap的access_token

Parameters:

  • source (string)


19
20
21
22
23
# File 'lib/resource/sap/sap_anywhere_interface.rb', line 19

def get_access_token(params)

  Rails.cache.read([SapAnywhereAccount.class_name, params[:source]])

end

#get_access_token_url(params) ⇒ Object

Note:

得到请求url

得到请求url

Parameters:

  • source (string)

    来源

  • request_name (string)

    请求资源名



181
182
183
184
185
186
187
188
# File 'lib/resource/sap/sap_anywhere_interface.rb', line 181

def get_access_token_url(params)
  access_token = get_access_token(params)
  # Rails.logger.info "获得的access_token是#{access_token}"
  url = "#{SapSetting.sap_request_url}#{params[:request_name]}access_token=#{access_token}"
  Rails.logger.info "#{url}"
  return url

end

#handle_response(response) ⇒ Object

Note:

对于RestClient的返回值进行数据分析

对于RestClient的返回值进行数据分析

Parameters:

  • response (string)


164
165
166
167
168
169
170
171
172
173
174
# File 'lib/resource/sap/sap_anywhere_interface.rb', line 164

def handle_response(response)
  decode_data = MultiJson.decode response.body
  if decode_data.kind_of? Array
    decode_data.map do |m|
      yhash m
    end
  else
    yhash decode_data
  end

end

#patch(params) ⇒ Object

Note:

patch请求更新的数据

patch请求更新的数据

Parameters:

  • source (string)

    来源

  • request_name (string)

    请求资源名

  • id (Integer)

    id

  • object (json)

    对象



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/resource/sap/sap_anywhere_interface.rb', line 143

def patch(params)

  response = RestClient.patch(get_access_token_url(params), params.to_json, accept: :json)
  handle_response(response)
rescue => e

  check_access_token(e.try(:response), params)
  # puts e.message
  if [Code::UNAUTHORIZED, Code::FORBIDDEN].include?(e.try(:response).code)

    Rails.logger.info "重新发出请求"
    retry
  elsif Sap::SapAnywhereInterface::Code::BADREQUEST == e.try(:response).code
    Rails.logger.info "请求的链接有误(例如:错误的id)"
  end
end

#post(params) ⇒ Object

Note:

post请求的创建的数据

post请求的创建的数据

Parameters:

  • source (string)

    来源

  • request_name (string)

    请求资源名

  • object (json)

    对象



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/resource/sap/sap_anywhere_interface.rb', line 60

def post(params)
  # p "传到post中的参数是#{params}"
  url = get_access_token_url(params)
  # Rails.logger.info "URL地址是#{url}"
  access_token = get_access_token(params)
  # Rails.logger.info "access_token是#{access_token}"

  options = params.values.select(&:present?).third.to_json

  # p "optionstojson后是#{params.values.select(&:present?).third.to_hash}"

  response = RestClient.post(url, options, :content_type => :json)

  return response

  # Rails.logger.info response
  #
  # Rails.logger.info "end=="
  #
  # Rails.logger.info '返回的结果'
  #
  # Rails.logger.info "order的值#{params[:order]}是是是"
  # Rails.logger.info "response返回值#{response}是是是"
  # handle_response(response) # post请求返回的是数字id,调用handle_response会报错
rescue => e
  # Rails.logger.info body.ai
  yloge e
  Rails.logger.info "错误信息#{e}是是是"
  check_access_token(e.try(:response), params)

# def post(params_url, params)
#   Rails.logger.info "params#{params}"
#   response = RestClient.post(get_access_token_url(params_url), params)
#
#   response_json = JSON.parse(response)
#   handle_response(response)
# rescue => e
#
#   check_access_token(e.try(:response), params_url)

  # puts e.message
  if [Code::UNAUTHORIZED, Code::FORBIDDEN].include?(e.try(:response).code)

    Rails.logger.info "重新发出请求"
    #retry
  elsif Sap::SapAnywhereInterface::Code::BADREQUEST == e.try(:response).code
    Rails.logger.info "请求的链接有误(例如:错误的id)"
  end

end