189 lines
4.7 KiB
PHP
Raw Permalink Normal View History

<?php
namespace app\gateway\controller\v4;
//use app\channel\service\CardService;
use dever\Log;
/**
* 商品
* Class Goods
* @package app\gateway\api
*/
class Goods extends Core
{
# 是否检测数据
protected bool $check = true;
#获取商品列表接口
public function list(): void
{
$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);
}
protected function maketime($v): float|false|int|string
{
if (!$v) {
return '';
}
if (is_numeric($v)) {
return $v;
}
if (is_array($v)) {
$v = $v[1];
}
if (strstr($v, ' ')) {
$t = explode(' ', $v);
$v = $t[0];
$s = explode(':', $t[1]);
} else {
$s = array(0, 0, 0);
}
if (!isset($s[1])) {
$s[1] = 0;
}
if (!isset($s[2])) {
$s[2] = 0;
}
if (strstr($v, '-')) {
$t = explode('-', $v);
} elseif (strstr($v, '/')) {
$u = explode('/', $v);
$t[0] = $u[2];
$t[1] = $u[0];
$t[2] = $u[1];
}
if (!isset($t)) {
$t = array(0, 0, 0);
}
if (!isset($t[1])) {
$t[1] = 0;
}
if (!isset($t[2])) {
$t[2] = 0;
}
$v = mktime($s[0], $s[1], $s[2], $t[1], $t[2], $t[0]);
return $v;
}
protected function writelog($data,$t = '')
{
if($t){
Log::write('gateway_v4', 'Goods_'.$t, $data);
}else{
Log::write('gateway_v4', 'Goods', $data);
}
}
}