94 lines
2.4 KiB
PHP
94 lines
2.4 KiB
PHP
<?php
|
|
namespace App\Domains\Virtual\Http\Controllers;
|
|
|
|
use App\Core\Controller;
|
|
use Illuminate\Http\Request;
|
|
use App\Domains\Virtual\Repositories\CompanyRepository;
|
|
use App\Domains\Virtual\Repositories\PackageRepository;
|
|
|
|
class FetchController extends Controller
|
|
{
|
|
protected $request;
|
|
|
|
/**
|
|
* 构造函数,自动注入.
|
|
*/
|
|
public function __construct(Request $request)
|
|
{
|
|
$this->request = $request;
|
|
}
|
|
|
|
/**
|
|
* 使用名称查找企业
|
|
*
|
|
* @return void
|
|
*/
|
|
public function companies(CompanyRepository $companyRepository)
|
|
{
|
|
return res($this->search($companyRepository), '', 201);
|
|
}
|
|
|
|
/**
|
|
* 使用名称查找套餐
|
|
*
|
|
* @return void
|
|
*/
|
|
public function packages(PackageRepository $packageRepository)
|
|
{
|
|
$packages = $this->search($packageRepository, ['type', 'name', 'carrier_operator']);
|
|
|
|
if ($this->request->has('type')) {
|
|
$type = $this->request->ids('type');
|
|
$packages = $packages->filter(function ($item) use ($type) {
|
|
return !in_array($item->type, $type);
|
|
});
|
|
}
|
|
|
|
return res($packages, '', 201);
|
|
}
|
|
|
|
/**
|
|
* 统一搜索归并
|
|
*
|
|
* @param [type] $repository
|
|
* @param string $field
|
|
* @param integer $limit
|
|
* @param string $primaryKey
|
|
* @return void
|
|
*/
|
|
protected function search($repository, $field = 'name', $primaryKey = 'id')
|
|
{
|
|
$search = $this->request->get('search');
|
|
$limit = $this->request->get('limit', 0);
|
|
|
|
$field = array_wrap($field);
|
|
$field = array_merge([$primaryKey, 'status'], $field);
|
|
|
|
$results = $repository->select($field)->get();
|
|
|
|
if ($search) {
|
|
$results = $results->filter(function ($item) use ($search, $field) {
|
|
$result = true;
|
|
|
|
if (strpos($item[$field], $search) === false) {
|
|
$result = false;
|
|
}
|
|
|
|
if (strpos(pinyin_abbr($item[$field]), $search) === false) {
|
|
$result = false;
|
|
}
|
|
|
|
if (strpos(implode('', pinyin($item[$field])), $search) === false) {
|
|
$result = false;
|
|
}
|
|
|
|
return $result;
|
|
});
|
|
}
|
|
|
|
$sorted = $results->sortBy($primaryKey)->values()->all();
|
|
|
|
return $sorted;
|
|
}
|
|
}
|