357 lines
9.6 KiB
PHP
Raw Normal View History

<?php
namespace app\linapi\controller\agisoAcprSupplierApi;
use app\channel\service\ChannelService;
use app\channel\service\ProductBaseService;
use app\gateway\service\CurlService;
use app\gateway\service\RedisService as redisObj;
use app\merchant\service\MerchantService;
use app\merchant\service\OrderHistoryService;
use app\merchant\service\OrderLastweekHistoryService;
use app\merchant\service\OrderService;
use app\merchant\service\ProductService;
use app\order\service\Kami91OrderService;
use think\admin\Controller;
use app\core\Service as CoreService;
use think\exception\HttpResponseException;
use think\facade\Db;
use think\facade\Log;
use app\channel\service\ProductService as CProductService;
class Product extends Controller
{
# 是否检测数据
// protected bool $check = false;
protected $merchant_id;
# code码定义
protected $code = array
(
# 成功
1 => 'ok',
# 小于0为失败
0 => '暂时未处理',
-1 => 'appid为空',
-2 => '产品错误',
-3 => 'appid无效',
-4 => '余额不足或者没有传入价格',
-5 => 'signature不能为空',
-6 => 'nonce不能为空',
-7 => 'time不能为空',
-8 => 'signature已失效',
-9 => 'signature验证失败',
-100 => '请求错误',
-101 => '订单不存在',
-102 => '订单号重复',
401 => '签名错误',
408 => '时间戳过期',
-103 => '正在处理中',
-104 => '锁卡失败',
-1000 => '系统维护中',
);
protected $secretKey = 'ez8bfsrgefga5nkx5zgccegrb25mct7w';
# 查询接口 一般用于查询数据,同步执行
# 提交接口 一般用于提交数据,异步执行
public function getList()
{
$input = input();
if (empty($input['userId'])) {
$this->no(-1);
}
// http_post($url, $data)
$getMerchantInfo = MerchantService::instance()->get($input['userId']);
if(!$getMerchantInfo){
$this->no(-3);
}
$other_param = json_encode($getMerchantInfo['other_param'],true);
if(!is_array($other_param) || !isset($other_param['agiso_AppSecret'])){
$secretKey = '';
}else{
$secretKey = $other_param['agiso_AppSecret'];
}
$mid = $getMerchantInfo['id'];
// 获取分页参数并验证
$page = intval(input('pageIndex', 1));
$pageSize = intval(input('pageSize', 10));
if ($page < 1 || $pageSize < 1 || $pageSize > 100) {
$this->yes([], '分页参数错误',9999);
}
// 计算偏移量
$offset = ($page - 1) * $pageSize;
// 查询产品列表并应用分页
$product = ProductService::instance()->db()
->where(['mid' => $mid])
->group('product_key')
->order('id desc')
->page($page, $pageSize) // 使用 page 方法进行分页
->select()
->toArray();
// 计算总记录数
$total = ProductService::instance()->db()
->where(['mid' =>$mid])
->group('product_key')
->count();
// 判断是否有下一页
$hasNextPage = ($offset + $pageSize) < $total;
if(!$product){
$this->yes([], '无产品参数',9999);
}
$ChannelProductService = CProductService::instance();
$ServiceProduct = ProductBaseService::instance();
$response_data = [];
foreach ($product as $key => $value) {
$Channel_Product = $ChannelProductService->getInfoByKey($value['product_key'],false,$value['cid']);
$Channel_Product = $Channel_Product[0];
if(!$value['cash']){
$cash = $Channel_Product['value'];
}else{
$cash = $value['cash'];
}
$cash = explode(',',$cash);
$service_info = $ServiceProduct->db()->where(['service_id' => $Channel_Product['service_id']])->select()->toArray();
if(str_contains($value['product_key'], '_cardbuy')){
$productType = 2;
}else{
$productType = 1;
}
foreach ($service_info as $k => $v){
if(in_array( $v['value'],$cash)){
$name = $v['name'];
if(!str_contains($v['name'], $v['value'])){
$name= $v['name'].'(' .$v['value'].')';
}
$response_data['items'][]=[
'productNo'=>$v['id'],
'productTitle'=>$name,
'productType'=>$productType,
'productPrice'=> sprintf("%.4f", $v['value']),
];
}
}
}
$response_data['hasNextPage']= $hasNextPage;
$this->yes($response_data);
}
public function getTemplate()
{
$input = input();
if (empty($input['userId'])) {
$this->no(-1);
}
// http_post($url, $data)
$getMerchantInfo = MerchantService::instance()->get($input['userId']);
if(!$getMerchantInfo){
$this->no(-3);
}
$old_sign = $input['sign'];
unset($input['sign']);
$sign = $this->_sign($input, $getMerchantInfo, $this->secretKey);
if($sign != $old_sign){
$this->no(401);
}
$ne_time = time() -300;
if($input['timestamp']< $ne_time){
$this->no(408);
}
if (isset($input['productNo'])) {
# 根据产品id 获取信息
$pinfo = \app\channel\service\ProductBaseService::instance()->getOne($input['productNo']);
if (!$pinfo) {
$this->no(1100);
}
$data['product_key'] = $pinfo['key'];
$data['cash'] = $pinfo['value'] ;
$pid = \app\channel\service\ProductBaseService::instance()->getProductId($pinfo['service_id']);
$data['pid'] = $pid['id'];
} else {
$this->no(1100);
}
$response_data = [
'apiType'=>2,
'productTitle'=>str_contains($pinfo['service']['name'], $pinfo['value']) ? $pinfo['service']['name'] : $pinfo['service']['name'] . '(' . $pinfo['value'] . ')',
'productType'=>1,
'productCost'=> sprintf("%.4f", $pinfo['value']),
];
$response_data['attach'][]=[
'type'=>1,
'name'=>'account',
'title'=>'充值账号',
'desc'=>'直充必须为账号,卡密随意',
'inputCheck'=>0
];
if(str_contains($pinfo['key'], '_cardbuy')){
$response_data['productType'] = 2;
}
$this->yes($response_data);
}
public function _sign($request, $merchant,$secretKey)
{
ksort($request);
if(isset($request['method'])){
unset($request['method']);
}
if(isset($request['appmid'])){
unset($request['appmid']);
}
$str = '';
foreach ($request as $k => $v){
$str .= $k . '=' . $v . '&';
}
$str = substr($str, 0,-1) ;
$str = $secretKey.$merchant['appsecret'].$str.$secretKey.$merchant['appsecret'];
$sign = MD5($str);
return strtoupper($sign);
}
protected static function log($data, $type = 'request'):void
{
\dever\Log::write('agiso', $type, $data);
// \dever\Log::write('jingdong', $type, $data);
}
public function send_post($notify_url, $post_data, $type): mixed
{
$postdate = http_build_query($post_data);
$options = array(
'http' => array(
'method' => $type,
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $postdate,
'timeout' => 15 * 60 // 超时时间(单位:s
)
);
$context = stream_context_create($options);
return file_get_contents($notify_url, false, $context);
}
protected function curl($method, $url, $param = array(), $json = false, $header = false):mixed
{
if ($param) {
$log['type'] = 'request';
$log['url'] = $url;
$log['param'] = $param;
$this->log($log);
}
$curl = CurlService::getInstance($url, $param, $method, $json, $header);
$curl->setTimeOut(3600);
return $curl->result();
}
/**
* 返回成功的消息
* @param mixed $info
* @param string $data
* @param integer $code
*/
protected function yes($data = '{-null-}', $info = 'ok', $code = 200)
{
if (is_string($data) && $data != 'ok' && $data != 'success') {
if ($data == '订单号重复') {
return $this->no(-102);
}
return $this->no(-100, $data);
}
$response = [
'code' => $code,
'message' => $info,
'data' => $data
];
throw new HttpResponseException(json($response));
}
/**
* 返回失败的消息
* @param mixed $info
* @param string $data
* @param integer $code
*/
protected function no($code = 0, $info = '', $data = null)
{
$msg = $this->code[$code] ?? 'error';
if ($info) {
$msg .= ':' . $info;
}
$response = [
'code' => $code,
'message' => $msg,
'data' => $data
];
throw new HttpResponseException(json($response));
}
}