123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- namespace App\Distributor\Controllers;
- use App\Distributor\Repositories\DistProduct;
- use App\Distributor\Repositories\SitePages;
- use App\Distributor\Repositories\SitePagesTag;
- use Illuminate\Http\Request;
- use Illuminate\Routing\Controller;
- class ApiController extends Controller
- {
- /**
- * 产品下接API,默认返回50个最新的产品
- */
- public function products(Request $request)
- {
- $q = $request->get('q');
- if ($q != null) {
- // 模糊搜索
- $obj = new DistProduct();
- return $obj->model()->where('title', 'like', "%$q%")->paginate(null, ['id', 'title as text']);
- } else {
- // 获取最新的50个
- $selectOptionsNew = DistProduct::selectOptionsNew();
- return $this->changeOptions($selectOptionsNew);
- }
- }
- public function pages(Request $request)
- {
- $q = $request->get('q');
- if ($q != null) {
- // 模糊搜索
- $obj = new SitePages();
- return $obj->model()->where('title', 'like', "%$q%")->paginate(null, ['id', 'title as text']);
- } else {
- // 获取最新的50个
- $selectOptionsNew = SitePages::selectOptionsNew();
- return $this->changeOptions($selectOptionsNew);
- }
- }
- public function tag(Request $request)
- {
- $q = $request->get('q');
- if ($q != null) {
- // 模糊搜索
- $obj = new SitePagesTag();
- return $obj->model()->where('name', 'like', "%$q%")->paginate(null, ['id', 'name as text']);
- } else {
- // 获取最新的50个
- $selectOptionsNew = SitePagesTag::selectOptionsNew();
- return $this->changeOptions($selectOptionsNew);
- }
- }
- private function changeOptions($data) {
- // 初始化结果数组
- $result = [];
- // 遍历原始数据并转换格式
- foreach ($data as $id => $text) {
- $result[] = [
- 'id' => (int)$id, // 将字符串转换为整数
- 'text' => $text
- ];
- }
- return $result;
- }
- }
|