sql:
-- phpMyAdmin SQL Dump
-- version 4.8.5
-- https://www.phpmyadmin.net/
--
-- 主机: localhost
-- 生成日期: 2022-05-25 15:23:53
-- 服务器版本: 5.7.26
-- PHP 版本: 7.3.4
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- 数据库: `kkk`
--
-- --------------------------------------------------------
--
-- 表的结构 `goods`
--
CREATE TABLE `goods` (
`id` int(10) UNSIGNED NOT NULL,
`goods_name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`goods_img` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`cat_id` int(11) NOT NULL,
`goods_price` decimal(8,2) NOT NULL,
`is_show` tinyint(4) NOT NULL,
`goods_introduce` text COLLATE utf8_unicode_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- 转存表中的数据 `goods`
--
INSERT INTO `goods` (`id`, `goods_name`, `goods_img`, `cat_id`, `goods_price`, `is_show`, `goods_introduce`) VALUES
(2, '纤伊寻雪纺连衣裙女2020春夏新款韩版碎花裙子时尚V领很仙的小个子连衣裙 白花色 L', '/storage/goods/f6/655e2bed6b7bea027fb69a3c11a27c.jpg', 8, '0.01', 1, ' <p>商品详情</p><p><img src=\"/storage/goods/ed\\e51d8a0f3fad9ddea11880e2903ab1.jpg\" style=\"max-width:100%;\"></p><p><img src=\"/storage/goods/9e\\3a9cb6f37f31b35076c1bb6cfab249.jpg\" style=\"max-width:100%;\"></p>'),
(3, '法国KJ蕾丝聚拢文胸套装刺绣薄款无钢圈插片式调整型小胸内衣舒适透气文胸 聚拢虾粉色1 75B=34B(配内裤)', '/storage/goods/84\\f9e13fa3a6093990bf3de4e2da74a0.jpg', 8, '0.01', 1, ' <p>商品详情</p><p><img src=\"/storage/goods/13\\2ad82b0d415a8dcab51e525925212a.jpg\" style=\"max-width:100%;\"></p><p><img src=\"/storage/goods/80\\a81fa79b6a0f0f84ba705cb31aa32d.jpg\" style=\"max-width:100%;\"></p><p><br></p>'),
(4, '浪莎打底袜 120D天鹅绒收腹提臀美腿显瘦加档连裤袜 1双 肤色 120D收腹提臀', '/storage/goods/f6\\655e2bed6b7bea027fb69a3c11a27c.jpg', 10, '56.00', 1, '<p>商品详情</p><p><img src=\"/storage/goods/63\\ebb026ad52963f53e0ae851d6e42f7.jpg\" style=\"max-width:100%;\"></p><p><img src=\"/storage/goods/ed\\e51d8a0f3fad9ddea11880e2903ab1.jpg\" style=\"max-width:100%;\"></p>'),
(5, '纤伊寻雪纺连衣裙女2020春夏新款韩版碎花裙子时尚V领很仙的小个子连衣裙 白花色 L', '/storage/goods/84\\f9e13fa3a6093990bf3de4e2da74a0.jpg', 10, '89.00', 1, '<p>商品详情</p><p><img src=\"/storage/goods/80\\a81fa79b6a0f0f84ba705cb31aa32d.jpg\" style=\"max-width:100%;\"></p><p><img src=\"/storage/goods/9e\\3a9cb6f37f31b35076c1bb6cfab249.jpg\" style=\"max-width:100%;\"></p>'),
(6, 'Redmi 8 5000mAh大电量 大字体大音量大内存 3D四曲面机身 AI双摄 骁龙八核处理器 AI人脸解锁 3GB+32GB 碳岩灰 游戏智能手机 小米', '/storage/goods/b4\\a403d9874ed426bd5210a2f42c33f0.jpg', 11, '789.00', 1, '<p>商品详情</p><p><img src=\"/storage/goods/63\\ebb026ad52963f53e0ae851d6e42f7.jpg\" style=\"max-width:100%;\"></p><p><img src=\"/storage/goods/f7\\b1f9e85e6b945be48e174f9260b909.jpg\" style=\"max-width:100%;\"></p>'),
(7, 'OUHEN轻奢品牌女装 小个子印花连衣裙女2020夏季新款遮肚减龄气质时尚收腰显瘦短款裙子 白色黑花 160/M', '/storage/goods/84\\f9e13fa3a6093990bf3de4e2da74a0.jpg', 10, '56.00', 1, '<p>商品详情</p><p><img src=\"/storage/goods/ed\\e51d8a0f3fad9ddea11880e2903ab1.jpg\" style=\"max-width:100%;\"></p><p><img src=\"/storage/goods/6f\\9c4f90bae503b687b9776fd2170e93.jpg\" style=\"max-width:100%;\"></p>'),
(8, '浪莎打底袜 120D天鹅绒收腹提臀美腿显瘦加档连裤袜 1双 肤色 120D收腹提臀', '/storage/goods/84\\f9e13fa3a6093990bf3de4e2da74a0.jpg', 10, '345.00', 1, '<p>商品详情</p><p><img src=\"/storage/goods/13\\2ad82b0d415a8dcab51e525925212a.jpg\" style=\"max-width:100%;\"></p><p><img src=\"/storage/goods/6f\\9c4f90bae503b687b9776fd2170e93.jpg\" style=\"max-width:100%;\"></p>'),
(9, '绣球花盆栽室内外阳台绿植花卉绣球花苗庭院地栽植物变色无尽夏重瓣八仙花苗四季种植循环开花耐寒幸运花 无尽夏 2年苗', '/storage/goods/b4\\a403d9874ed426bd5210a2f42c33f0.jpg', 11, '89.00', 1, '<p>商品详情</p><p><img src=\"/storage/goods/ed\\e51d8a0f3fad9ddea11880e2903ab1.jpg\" style=\"max-width:100%;\"></p><p><img src=\"/storage/goods/63\\ebb026ad52963f53e0ae851d6e42f7.jpg\" style=\"max-width:100%;\"></p>'),
(10, '浪莎打底袜 120D天鹅绒收腹提臀美腿显瘦加档连裤袜 1双 肤色 120D收腹提臀', '/storage/goods/84\\f9e13fa3a6093990bf3de4e2da74a0.jpg', 11, '899.00', 1, '<p>商品详情</p><p><img src=\"/storage/goods/ed\\e51d8a0f3fad9ddea11880e2903ab1.jpg\" style=\"max-width:100%;\"></p><p><img src=\"/storage/goods/9e\\3a9cb6f37f31b35076c1bb6cfab249.jpg\" style=\"max-width:100%;\"></p>'),
(11, '666', '/storage/goods/f6\\9f50aed16fc629c65f6526b017296a.png', 1, '666.00', 1, '');
--
-- 转储表的索引
--
--
-- 表的索引 `goods`
--
ALTER TABLE `goods`
ADD PRIMARY KEY (`id`);
--
-- 在导出的表使用AUTO_INCREMENT
--
--
-- 使用表AUTO_INCREMENT `goods`
--
ALTER TABLE `goods`
MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=12;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\facade\Route;
Route::get('indexgoods','Goods/getIndexGoods');
//第一个参数是地址栏上面要写的,第二个参数是第一个参数所指向的控制器/方法
Route::get('cat_goods/:id','Goods/getCatGoods');
//第一个参数是地址栏上面要写的,第二个参数是第一个参数所指向的控制器/方法 :id代表数字比如1代表cat_goods/1
Route::get('goods/:id','Goods/getGoodsDetail');
//第一个参数是地址栏上面要写的,第二个参数是第一个参数所指向的控制器/方法 :id代表数字比如1代表goods/1
控制器:
<?php
declare (strict_types = 1);//严格模式。代表类型要对应哦。不对应是错的哦。举个例子aa("666")。比如我传过去是整形。你用字符串接收的话function aa($a):int{echo $a;},是不是要报错呢,不用严格模式就不报错
namespace app\api\controller;//所包括这个控制器文件的目录是controller.上一级api,上一级app结束.
use think\facade\Db;//引用数据库链接类
class Goods
{
public function getIndexGoods(){//获取首页接口
$goods = Db::name('goods')->where('is_show',1)->field(['goods_name','id','goods_img','goods_price'])->paginate(6)->toArray();//解析:Db::name('goods')获取goods表,where('is_show',1)条件是必须是为1,代表后台设置是显示状态的1.field(['goods_name','id','goods_img','goods_price'])代表要输出的字段有哪些.paginate(6).一页6条数据。toArray();转成数组
if($goods){//为true的话
$data = ['code'=>200,'msg'=>'success','data'=>$goods['data']];
//关于这个格式我解释一下,是随意定义的,但是建议像我这种,比较规范一些。code=>200代表响应成功.msg=>代表输出的信息.data代表数据
}else{
$data = ['code'=>440,'msg'=>'no goods'];
}
return json($data);//然后转成json
}
public function getCatGoods($id){//获取首页接口中某一个分类的接口,比如手机类 服装类(列表)
$goods = Db::name('goods')->where('is_show',1)->where('cat_id',$id)->field(['goods_name','id','goods_img','goods_price'])->select()->toArray();
//Db::name('goods')链接的这张表是goods。条件是where('is_show',1)必须是显示的状态。where('cat_id',$id)是类型是啥?比如手机 服装。field(['goods_name','id','goods_img','goods_price'])是输出的字段select()->toArray()查询然后转换成数组输出.
if($goods){
$data = ['code'=>200,'msg'=>'success','data'=>$goods];
}else{
$data = ['code'=>440,'msg'=>'no goods'];
}
return json($data);
}
public function getGoodsDetail($id){//获取某个分类下的接口下的某一个$id(详情)
$goods = Db::name('goods')->where('id',$id)->find();
//获取这张表中goods.条件是哪个id呢
$goods['goods_introduce'] = preg_replace('/(<img.+?src=")(.*?)/',"$1".config('shop.API_HOST')."$2",$goods['goods_introduce']);
//正则,$2代表第二个小括号的内容(.*?)哦.$1代表(<img.+?src=")的内容哦.其他我不懂。我是买的视频来写的
if($goods){
$data = ['code'=>200,'msg'=>'success','data'=>$goods];
}else{
$data = ['code'=>440,'msg'=>'no goods'];
}
return json($data);
}
}
postman:
//获取首页接口
//获取首页接口中某一个分类的接口,比如手机类 服装类(列表)
详情: