Skip to content

Instantly share code, notes, and snippets.

@1c7
Last active April 15, 2025 03:38
Show Gist options
  • Save 1c7/b990fec3c25f4a2d030d97bd0ba8350d to your computer and use it in GitHub Desktop.
Save 1c7/b990fec3c25f4a2d030d97bd0ba8350d to your computer and use it in GitHub Desktop.
易销云,调用 API
# app/services/ciopass_com/api.rb
=begin
在 IRB 或其他 Ruby 环境中测试此代码
# 加载依赖库
require 'faraday'
require 'json'
require 'digest/md5'
# 创建服务实例
api = CiopassCom::Api.new(mobile_phone: '', password: '')
# 调用方法并获取结果
result = api.get_app_token
# 打印结果
puts result
=end
require 'faraday'
require 'json'
require 'digest/md5'
module CiopassCom
class Api
# 配置 API 基础信息
# BASE_URL = 'http://120.25.57.73' # 根据实际情况替换为实际的 IP 地址
BASE_URL = 'http://open11.ciopaas.com'
ENDPOINT = '/Api/Login/getAppToken'
def initialize(mobile_phone:, password:, base_url: nil)
@mobile_phone = mobile_phone
@password = password
@base_url = base_url || BASE_URL
end
# 获取 appId 和 appToken 的方法
def get_app_token
# 构造请求体
request_body = {
mobilePhone: @mobile_phone,
password: @password,
from: 'web', # 默认客户端类型
check_time: Time.now.to_i # 当前时间戳
}.to_json
# 生成 sign
sign = generate_sign(request_body)
# 构造完整请求 URL
url = "#{@base_url}#{ENDPOINT}?sign=#{sign}"
# 发送 HTTP POST 请求
send_post_request(url, request_body)
end
private
# 生成签名
def generate_sign(data)
md5_hash = Digest::MD5.hexdigest(data).upcase
md5_hash[8..17] # 截取第 9 到 18 个字符
end
# 使用 Faraday 发送 HTTP POST 请求
def send_post_request(url, body)
connection = Faraday.new do |conn|
conn.request :json # 自动将请求体转换为 JSON 格式
conn.response :json, content_type: /\bjson$/ # 自动解析 JSON 响应
conn.adapter Faraday.default_adapter
end
response = connection.post(url, body)
response.body
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment