Files
liche/common/libraries/SsApi.php
T
2024-08-11 18:27:26 +08:00

177 lines
6.8 KiB
PHP

<?php
defined('BASEPATH') or exit('No direct script access allowed');
class SsApi
{
const CF_PLATFORM = "SYSTOPIC";
const CLUES_METHOD = 'openApi/clues'; //提交线索
const STATUS_METHOD = 'openApi/status'; //获取报名状态
const BIZ_METHOD = 'openApi/biz'; //门店信息
private $app_id = '1c156bb57cd6984a';
private $sign_key = '71fd71173b776766a2ae1209d9a2c2ed';
private $api_url = 'https://sapi.liche.cn/hd/app/'; //空间站报名数据接口
private $ci;
private $log_path;
public function __construct()
{
$this->ci = &get_instance();
if (!is_product()) {
$this->api_url = 'https://api.ss.haodian.cn/hd/app/'; //测试地址
}
$this->ci->load->helper('string');
$this->log_path = 'ss_api.log';
}
/**
* @param $id
* @param $name
* @param $mobile
* @param $en_time
* @param $remark
* @param $to_customers 是否添加到客户池
* @param $biz_id
* @return array
*/
public function postClues($id, $name, $mobile, $en_time, $remark, $to_customers = 0, $biz_id = 0)
{
$data = [
'name' => $name,
'mobile' => $mobile,
'en_time' => $en_time,
'out_id' => $id,
'cf_platform' => self::CF_PLATFORM,
'biz_id' => $biz_id,
'to_customers' => $to_customers,
'remark' => $remark,
'nonce_str' => random_string('alpha'),
'app_id' => $this->app_id,
];
$data['sign'] = $this->sign($data);
$client = new GuzzleHttp\Client();
$options = [
\GuzzleHttp\RequestOptions::HEADERS => ['Content-Type' => 'application/json'],
\GuzzleHttp\RequestOptions::JSON => $data,
];
$url = $this->api_url . self::CLUES_METHOD;
try {
debug_log("[info]#请求地址:" . $url, $this->log_path);
debug_log("[info]#请求参数:" . json_encode($data, JSON_UNESCAPED_UNICODE), $this->log_path);
$response = $client->post($url, $options);
debug_log("[info]#返回信息:" . $response->getBody(), $this->log_path);
if ($response->getStatusCode() != 200) {
debug_log("[error]#" . $response->getStatusCode(), $this->log_path);
return ['code' => 0, 'msg' => '网络错误:' . $response->getStatusCode()];
}
$reqInfo = json_decode($response->getBody());
if (!$reqInfo || $reqInfo->code != 200) {
$msg = $reqInfo ? $reqInfo->msg : '';
return ['code' => 0, 'msg' => '保存失败:' . $msg];
}
return ['code' => 1, 'msg' => '保存成功'];
} catch (Exception $e) {
debug_log("[error]#" . $e->getMessage(), $this->log_path);
return ['code' => 0, 'msg' => $e->getMessage()];
}
}
public function getStatus($out_ids)
{
if (is_array($out_ids)) {
$out_ids = implode(',', $out_ids);
}
$data = [
'out_ids' => $out_ids,
'cf_platform' => self::CF_PLATFORM,
'nonce_str' => random_string('alpha'),
'app_id' => $this->app_id,
];
$data['sign'] = $this->sign($data);
$client = new GuzzleHttp\Client();
$options = [
\GuzzleHttp\RequestOptions::HEADERS => ['Content-Type' => 'application/json'],
\GuzzleHttp\RequestOptions::JSON => $data,
];
$url = $this->api_url . self::STATUS_METHOD;
try {
debug_log("[info]#请求地址:" . $url, $this->log_path);
debug_log("[info]#请求参数:" . json_encode($data, JSON_UNESCAPED_UNICODE), $this->log_path);
$response = $client->post($url, $options);
debug_log("[info]#返回信息:" . $response->getBody(), $this->log_path);
if ($response->getStatusCode() != 200) {
debug_log("[error]#" . $response->getStatusCode(), $this->log_path);
return ['code' => 0, 'msg' => '网络错误:' . $response->getStatusCode()];
}
$reqInfo = json_decode($response->getBody(), true);
if (!$reqInfo || $reqInfo['code'] != 200) {
$msg = $reqInfo ? $reqInfo['msg'] : '';
return ['code' => 0, 'msg' => '保存失败:' . $msg];
}
return ['code' => 1, 'msg' => '保存成功', 'data' => $reqInfo['data']];
} catch (Exception $e) {
debug_log("[error]#" . $e->getMessage(), $this->log_path);
return ['code' => 0, 'msg' => $e->getMessage()];
}
}
public function getBiz($biz_id, $cache = true)
{
$cacheKey = "SYTSTOPIC_SSAPI_BIZ_{$biz_id}";
$redis = &load_cache();
if ($cache) {
$bizInfo = $redis->get($cacheKey);
if ($bizInfo) {
return ['code' => 1, 'msg' => 'success', 'data' => $bizInfo];
}
}
//数据缓存
$data = [
'biz_id' => $biz_id,
'nonce_str' => random_string('alpha'),
'app_id' => $this->app_id,
];
$data['sign'] = $this->sign($data);
$client = new GuzzleHttp\Client();
$options = [
\GuzzleHttp\RequestOptions::HEADERS => ['Content-Type' => 'application/json'],
];
$url = $this->api_url . self::BIZ_METHOD . '?' . http_build_query($data);
try {
debug_log("[info]#请求地址:" . $url, $this->log_path);
$response = $client->get($url, $options);
debug_log("[info]#返回信息:" . $response->getBody(), $this->log_path);
if ($response->getStatusCode() != 200) {
debug_log("[error]#" . $response->getStatusCode(), $this->log_path);
return ['code' => 0, 'msg' => '网络错误:' . $response->getStatusCode()];
}
$reqInfo = json_decode($response->getBody(), true);
if (!$reqInfo || $reqInfo['code'] != 200) {
$msg = $reqInfo ? $reqInfo['msg'] : '';
return ['code' => 0, 'msg' => '请求失败:' . $msg];
}
$redis->save($cacheKey, $reqInfo['data'], 60 * 60);
return ['code' => 1, 'msg' => 'success', 'data' => $reqInfo['data']];
} catch (Exception $e) {
debug_log("[error]#" . $e->getMessage(), $this->log_path);
return ['code' => 0, 'msg' => $e->getMessage()];
}
}
private function sign($param)
{
unset($param['sign']);
//按字典序排序参数
ksort($param);
$buff = "";
foreach ($param as $k => $v) {
if (!is_array($v) && strlen($v) > 0) {
$buff .= $k . "=" . ($v) . "&";
}
}
$buff = trim($buff, "&");
$buff = $buff . "&key=" . $this->sign_key;
return strtoupper(md5($buff));
}
}