332 lines
11 KiB
PHP
Raw Normal View History

2024-09-29 15:43:18 +08:00
<?php
namespace app\gateway\controller\api;
use app\channel\service\CardService;
use app\merchant\service\MerchantService;
use app\merchant\service\BeeService;
use dever\Log;
use think\App;
use think\exception\HttpResponseException;
/**
* 接口处理
* Class Bee
* @package app\gateway\api
*/
class Bee extends Coremf
{
# 是否检测数据
protected bool $check = false;
protected mixed $appkey = "";
protected int $mid = 2;#此处为小蜜蜂商户固定id若修改请自行填入
protected mixed $appSecret = "";
protected mixed $host = "https://shop.task.mf178.cn";//正式环境网关,测试环境请注释
//protected $host='http://test.shop.center.mf178.cn';//测试地址网关,生产环境请注释
protected array $merchants = [];
public function __construct(App $app)
{
parent::__construct($app);
$this->merchants = MerchantService::instance()->getTypeInfo('7', '1');
}
public function TaskCron(): void
{
if (empty($this->merchants)) {
$this->logBee('没有需要查询的商户-' . date('Y-m-d H:i:s', time()));
exit;
}
foreach ($this->merchants as $key => $v) {
$this->appkey = "";
$this->appSecret = "";
$tempConfig = BeeService::instance()->getTaskInfo($this->merchants[$key]['id']);
if (!$tempConfig) {
$this->logBee('商户-' . $this->merchants[$key]['id'] . '-任务未配置' . date('Y-m-d H:i:s', time()));
continue;
}
if ((time() - $tempConfig["lasttimes"]) < $tempConfig["times"] * 60) continue;
$this->appkey = $this->merchants[$key]['agentId'];
$this->appSecret = $this->merchants[$key]['agentkey'];
$this->mid = $this->merchants[$key]['id'];
$this->app->db->name('merchant_bee_task')->where(array('id' => $tempConfig['id']))->update(array('lasttimes' => time()));
$this->getOrders($tempConfig);
}
}
#获取签名
public function getmfSign($params = []): array
{
if (empty($params) || empty($this->appkey) || empty($this->appSecret)) {
$this->logBee('签名参数缺失' . date("Y-m-d,H:i:s", time()));
throw new \Exception('参数错误');
}
$params = array_merge($params, ['app_key' => $this->appkey, 'timestamp' => time()]);
ksort($params);
$buff = '';
foreach ($params as $k => $v) {
if ($k != "sign") {
$buff .= $k . $v;
}
}
$buff .= $this->appSecret;
$params['sign'] = md5($buff);
return $params;
}
#用户可做单渠道信息查询 - 手工单
public function getVender($mid)
{
$data = array();
$this->mid = $mid;
$merchantsInfo = MerchantService::instance()->getInfo($this->mid);
$this->appkey = $merchantsInfo['agentId'];
$this->appSecret = $merchantsInfo['agentkey'];
$params = [
'data' => json_encode($data, JSON_UNESCAPED_UNICODE)
];
$params = $this->getmfSign($params);
$this->host .= '/userapi/sgd/getVender';
$result = $this->send_post($this->host, $params);
$result = json_decode($result, true);
if (0 != $result['code']) {
$this->logBee('可做单渠道查询失败-' . $result['message'] . '-' . date("Y-m-d,H:i:s", time()));
// throw new \Exception($result['message']);
}
return $result['data'] ?? [];
}
#订单获取 - 手工单
#mid,vender_id,amount,operator_id,order_num,prov_code,times(间隔时间),lasttimes(上次执行时间)merchant_bee_task
#上海,云南,内蒙古,北京,吉林,四川,天津,宁夏,安徽,山东,山西,广东,广西,江苏,江西,河北,河南,浙江,海南,湖北,湖南,甘肃,福建,西藏,贵州,辽宁,重庆,陕西,青海,黑龙江
public function getOrders($getData): void
{
$data = [
'vender_id' => $getData['vender_id'], //todo 具体参数请先请求接口getVender获取
'data' => [
'amount' => $getData['amount'], //todo 具体参数请先请求接口getVender获取
'operator_id' => $getData['operator_id'], //todo 具体参数请先请求接口getVender获取
'order_num' => $getData['order_num'],
'prov_code' => $getData['prov_code']
],
];
// var_dump($data);die;
if (empty($data)
|| empty($data['vender_id'])
|| (empty($data['data']) || !is_array($data['data']))
) {
$this->logBee('订单获取参数错误-' . date("Y-m-d,H:i:s", time()));
exit;
}
ksort($data['data']);
$data['data'] = json_encode($data['data'], JSON_UNESCAPED_UNICODE);
$params = $this->getmfSign($data);
$this->host .= '/userapi/sgd/getOrder';
$result = $this->send_post($this->host, $params);
$result = json_decode($result, true);
if (0 != $result['code']) {
$this->logBee($this->mid . '-订单获取失败-' . $result['message'] . '-' . date("Y-m-d,H:i:s", time()));
exit;
} else {
$this->logBee($this->mid . '-订单获取成功-' . json_encode($result['data']) . '-' . date("Y-m-d,H:i:s", time()));
}
if (count($result['data']) > 1) {
$datas = $result['data'];
foreach ($datas as $key => $v) {
$tempdata = $datas[$key];
$tempAmount = explode('|', $tempdata['target_desc']);
$tempdata['cash'] = $tempAmount[2];
$res = $this->submit($tempdata);
if ($res != 'ok') {
$this->logBee($this->mid . '-订单' . $tempdata['user_order_id'] . '进单失败-' . date("Y-m-d,H:i:s", time()));
} else {
$this->logBee($this->mid . '-订单' . $tempdata['user_order_id'] . '进单成功-' . date("Y-m-d,H:i:s", time()));
}
}
} else {
$tempdata = $result['data'][0];
$tempAmount = explode('|', $tempdata['target_desc']);
$tempdata['cash'] = $tempAmount[2];
$res = $this->submit($tempdata);
if ($res != 'ok') {
$this->logBee($this->mid . '-订单' . $tempdata['user_order_id'] . '进单失败-' . date("Y-m-d,H:i:s", time()));
} else {
$this->logBee($this->mid . '-订单' . $tempdata['user_order_id'] . '进单成功-' . date("Y-m-d,H:i:s", time()));
}
}
}
# 记录日志
protected function logBee($data): void
{
Log::write('gateway', 'bee', $data);
}
public function submit($sbmitparam): string
{
$param = array();
# 1是mid2是产品key, 3是卡号4是金额5是短信代充充值卡卡号6是密码
$product_key = 'dhcz';
$param['mobile'] = $sbmitparam['target'];
$param['card'] = $param['mobile'];
$param['cash'] = $sbmitparam['cash'];
$param['order'] = $sbmitparam['user_order_id'];
$result = \app\channel\service\ChannelService::instance()->use($this->mid, $product_key, $param, false);
return 'ok';
}
#手动提交
public function sdsubmit()
{
// $sbmitparam=array(
// 'target'=>'13681848435',
// 'cash'=>'50',
// 'user_order_id'=>'23112800034585'
// );
$inputdata = input();
$sbmitparam = array(
'target' => $inputdata['target'],
'cash' => $inputdata['cash'],
'user_order_id' => $inputdata['order_id']
);
$this->mid = $inputdata['mid'];
$param = array();
# 1是mid2是产品key, 3是卡号4是金额5是短信代充充值卡卡号6是密码
$product_key = 'dhcz';
$param['mobile'] = $sbmitparam['target'];
$param['card'] = $param['mobile'];
$param['cash'] = $sbmitparam['cash'];
// $config = explode('_', $order);
$param['order'] = $sbmitparam['user_order_id'];
// var_dump();
$result = \app\channel\service\ChannelService::instance()->use($this->mid, $product_key, $param, false);
return 'ok';
}
#订单结果状态上报 - 手工单-是手动处理状态码
public function sdupdateStatus()
{
// $data = [
// 'data' => [
// 'user_order_id' => '23112800015880',
// 'rsp_info' => '做单失败', //可选,具体定义详见文档
// 'status' => 8,
//// 'voucher' => '', //可选,具体定义详见文档
// ]
// ];
$inputdata = input();
$data = [
'data' => [
'user_order_id' => $inputdata['orderid'],
'rsp_info' => $inputdata['info'], //可选,具体定义详见文档
'status' => $inputdata['status'],
]
];
if (empty($data)
|| (empty($data['data']) || !is_array($data['data']))
) {
$this->logBee('订单上报结果失败参数错误-' . date("Y-m-d,H:i:s", time()));
throw new \Exception('参数错误');
}
ksort($data['data']);
$data['data'] = json_encode($data['data'], JSON_UNESCAPED_UNICODE);
$params = $this->getmfSign($data);
$this->host .= '/userapi/sgd/updateStatus';
$result = $this->send_post($this->host, $params);
$result = json_decode($result, true);
if (0 != $result['code']) {
$this->logBee('订单结果上报失败-' . date("Y-m-d,H:i:s", time()));
throw new \Exception($result['message']);
} else {
$this->logBee('订单结果上报成功-' . json_encode($result['data']) . '-' . date("Y-m-d,H:i:s", time()));
}
}
#订单查询 - 手工单
public function searchOrders(): void
{
$data = [
'data' => [
// 'user_order_id' => '23112800015880',
]
];
if (!isset($_GET['vender_id']) || $_GET['vender_id'] == "") {
throw new \Exception('vender_id为空');
}
$data['vender_id'] = $_GET['vender_id'];
if (isset($_GET['user_order_id']) || $_GET['user_order_id'] != "") $data['data']['user_order_id'] = $_GET['user_order_id'];
if (isset($_GET['status']) || $_GET['status'] != "") $data['data']['status'] = $_GET['status'];
if (empty($data)
|| (empty($data['data']) || !is_array($data['data']))
) {
throw new \Exception('参数错误');
}
ksort($data['data']);
$data['data'] = json_encode($data['data'], JSON_UNESCAPED_UNICODE);
$params = $this->getmfSign($data);
$url = $this->host . '/userapi/sgd/queryOrder';
$result = $this->send_post($url, $params);
$result = json_decode($result, true);
if (0 != $result['code']) {
$this->logBee('订单查询失败-' . $result['message'] . date("Y-m-d,H:i:s", time()));
throw new \Exception($result['message']);
} else {
$this->logBee('订单查询成功-' . json_encode($result['data']) . '-' . date("Y-m-d,H:i:s", time()));
}
}
}