玖伍社区重写获取商品信息API接口

由于客户定制模板导致各式各样问题出现,真的想吐槽这个系统写API为什么要通过页面来截取。定制好的模板给客户反而不能使用,已经有一两个客户开始辱骂我写的垃圾模板,给一大堆BUG的模板他,废话不说了,直接上文件、说明。

1、新增文件

在网站根目录新增一个 api.php 文件

复制以下代码到新建的api.php里面

<?php
/*
 * 获取商品列表以及单商品详情
 * @Autor: 海弟君
 * @Date: 2022-01-21 15:08:33
 * @Contact : QQ:1532332928
 */
 
header('Access-Control-Allow-Origin:*');
header("Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept");
# 关闭PHP报错
error_reporting(0);
# 获取系统数据库配置信息
$DbCpnfig = include($_SERVER['DOCUMENT_ROOT'].'/Jwxh_Application/Common/Conf/config.php');
# 连接数据库
try {
    $DB = new PDO("mysql:host={$DbCpnfig['DB_HOST']};dbname={$DbCpnfig['DB_NAME']};port={$DbCpnfig['DB_PORT']}",$DbCpnfig['DB_USER'],$DbCpnfig['DB_PWD']);
}catch(Exception $e){
	exit('链接数据库失败:'.$e->getMessage());
}
// 设置编码
$DB->exec("set names utf8");
// 用户账号
$username = addslashes($_REQUEST['username']);
// 用户密码
$password = addslashes($_REQUEST['password']);
// 商品ID
$goods_id = addslashes($_REQUEST['goods_id']);
// 获取用户数据
$user_info = $DB->query("SELECT * FROM db_user WHERE username='{$username}' and password='".md5(md5($password))."' limit 1")->fetch(PDO::FETCH_ASSOC);
// 判断用户数据是否获取成功
if(!$user_info){
    exit(json_encode(['code'=>001,'msg'=>'账号密码错误!'], JSON_UNESCAPED_UNICODE));
}
// 判断是否是获取单商品数据
if(!empty($goods_id)){
    // 获取商品数据
    $goods_info = $DB->query("SELECT * FROM db_all_goods WHERE id='{$goods_id}' limit 1")->fetch(PDO::FETCH_ASSOC);
    // 判断是否存在
    if($user_info){
        // 获取商品是否有密价
        $secret_info = $DB->query("SELECT * FROM db_secret_unit_price WHERE user_id='{$user_info['uid']}' and goods_id='{$goods_info['id']}' limit 1")->fetch(PDO::FETCH_ASSOC);
        if($secret_info && $user_info['secret'] > 0){
            $cost_price=$goods_info['cost_price'];
            switch($secret_info['secret_type']){
                case 1:
                    $secret_after_unit_price=$secret_info['secret_number'];
                    break;
                case 2:
                    $secret_after_unit_price=$cost_price+$secret_info['secret_number'];
                    break;
                case 3:
                    $secret_after_unit_price=$cost_price+$cost_price*$secret_info['secret_number'];
                    break;
                default:
                    $secret_after_unit_price=0;
                    break;
            }
            $goods_unitprice=$secret_after_unit_price;
        }else{
            switch($user_info['vip']){
                case 1:
                    $goods_unitprice=$goods_info['v1unitprice'];
                    break;
                case 2:
                    $goods_unitprice=$goods_info['v2unitprice'];
                    break;
                case 3:
                    $goods_unitprice=$goods_info['v3unitprice'];
                    break;
                case 4:
                    $goods_unitprice=$goods_info['v4unitprice'];
                    break;
                default:
                    $goods_unitprice=$goods_info['unitprice'];
                    break;
            }
        }
        $goods_unitprice_unitnum_unitprice = rtrim(rtrim(number_format($goods_unitprice*$goods_info['unitprice_unitnum'],6,'.',''), '0'), '.');
    }
    exit(json_encode([
        'code'=>200,
        'msg'=>'获取成功!',
        'data'=>array(
            'title' => $goods_info['streamline_title'],
            'money' => $goods_info['unitprice'],
            'maxnum' => $goods_info['maxbuynum_0'],
            'minnum' => $goods_info['minbuynum_0'],
            'status' => $goods_info['goods_status'],
            'order_state' => $goods_info['order_state'],
            'repeatnum' => $goods_info['repeat_add_num'],
            'price' => $goods_info['unitprice_unitnum'] .$goods_info['unit'] .'=' . $goods_unitprice_unitnum_unitprice,
            'matter' => $goods_info['modal_description'],
            'content' => $goods_info['content'],
            'shopimg' => $goods_info['thumb'],
            'type' => $goods_info['goods_type'],
        )
    ], JSON_UNESCAPED_UNICODE));
}else{
    $data = [];
    $goods_list = $DB->query("SELECT A.*,B.secret_type,B.secret_number FROM `db_all_goods` A LEFT JOIN `db_secret_unit_price` B ON A.id = B.goods_id")->fetchAll();
    foreach ($goods_list as $key => $value) {
        
        if($user_info['secret'] > 0 && $value['secret_number'] != null){
            switch($value['secret_type']){
                case 1:
                    $secret_after_unit_price=$value['secret_number'];
                    break;
                case 2:
                    $secret_after_unit_price=$value['cost_price']+$value['secret_number'];
                    break;
                case 3:
                    $secret_after_unit_price=$value['cost_price']+$value['cost_price']*$value['secret_number'];
                    break;
                default:
                    $secret_after_unit_price=0;
                    break;
            }
            $goods_unitprice=$secret_after_unit_price;
        }else{
            switch($user_info['vip']){
                case 1:
                    $goods_unitprice=$goods_info['v1unitprice'];
                    break;
                case 2:
                    $goods_unitprice=$goods_info['v2unitprice'];
                    break;
                case 3:
                    $goods_unitprice=$goods_info['v3unitprice'];
                    break;
                case 4:
                    $goods_unitprice=$goods_info['v4unitprice'];
                    break;
                default:
                    $goods_unitprice=$goods_info['unitprice'];
                    break;
            }
        }
        $goods_unitprice_unitnum_unitprice = rtrim(rtrim(number_format($goods_unitprice*$goods_info['unitprice_unitnum'],6,'.',''), '0'), '.');
        
        $data[$key]['title'] = $value['streamline_title'];
        $data[$key]['money'] = $value['unitprice'];
        $data[$key]['maxnum'] = $value['maxbuynum_0'];
        $data[$key]['minnum'] = $value['minbuynum_0'];
        $data[$key]['status'] = $value['goods_status'];
        $data[$key]['order_state'] = $value['order_state'];
        $data[$key]['repeatnum'] = $value['repeat_add_num'];
        $data[$key]['price'] = $value['unitprice_unitnum'] .$value['unit'] .'=' . $goods_unitprice_unitnum_unitprice;
        $data[$key]['matter'] = $value['modal_description'];
        $data[$key]['content'] = $value['content'];
        $data[$key]['shopimg'] = $value['thumb'];
        $data[$key]['type'] = $value['goods_type'];
    }
    exit(json_encode([
        'code'=>200,
        'msg'=>'获取成功!',
        'data'=>$data
    ], JSON_UNESCAPED_UNICODE));
}
?>

2、获取商品信息以及参数说明

请求URL:社区域名/api.php

请求方式:POST / GET

只需要传账号密码,如果是获取单商品信息加上商品id

 获取全部商品列表列示:xxx.com/api.php?username=12345&password=abcd123

获取单商品信息列示:xxx.com/api.php?username=12345&password=abcd123&goods_id=16

返回参数说明:

title商品标题
money商品售价
maxnum最大下单数量
minnum最小下单数量
status商品下单状态 0正常 1禁止
order_state下单初始状态 
repeatnum重复下单数量
price下单价格描述
matter注意事项说明
content商品详情介绍

还有,由于编辑器不能在已有表格加行,只能在下面描述了

shopimg 商品图片

type 商品分类ID

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海弟君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值