更新:数据字典接口 -- 1

master
zwesy 2021-11-29 16:34:25 +08:00
parent e97b487be9
commit 35fe05805b
5 changed files with 119 additions and 26 deletions

View File

@ -3,6 +3,8 @@ namespace app\controller\api;
use app\exception\RepositoryException;
use app\repository\BusinessRepository;
use app\repository\CouponRepository;
use app\repository\DictionaryRepository;
use think\response\Json;
use app\model\{
Business as BusinessModel
@ -18,7 +20,10 @@ class Dictionary extends Base
{
protected $noNeedLogin = [
'getDistanceList',
'getBusinessCircle',
'getAgencyList',
'getBusinessTypeList',
'getCouponTypeList',
];
/**
@ -39,12 +44,12 @@ class Dictionary extends Base
/**
* 获取商圈列表
* TODO 待确认是否需要这个接口
* @return Json
*/
public function getBusinessCircle()
public function getBusinessCircle(): Json
{
$items = DictionaryRepository::getInstance()->getAllBusinessCircleList();
return $this->json(0, 'success', $items);
}
/**
@ -84,6 +89,34 @@ class Dictionary extends Base
}
/**
* 获取商家分类
*/
public function getBusinessTypeList(): Json
{
$recursionChildren = $this->request->param('recursion/d', 0);
$repo = DictionaryRepository::getInstance();
$items = $repo->getBusinessTypeList([], [], null, ['pid'=>'asc']);
$list = $items->toArray();
if ($recursionChildren > 0) {
$list = $repo->recursionChildrenList($list, 0, 'pid', 'id');
}
return $this->json(0, 'success', $list);
}
/**
* 获取优惠卷类型列表
* @return Json
*/
public function getCouponTypeList(): Json
{
$items = CouponRepository::getInstance()->getCouponTypeAll(['id', 'name'],['id'=>'asc']);
return $this->json(0, 'success', $items);
}
}

View File

@ -1,5 +1,4 @@
<?php
namespace app\model;
use think\Collection;

View File

@ -1,9 +1,9 @@
<?php
namespace app\repository;
use app\exception\RepositoryException;
use app\model\Business;
use app\model\BusinessCircle;
use app\model\BusinessFlow;
use app\model\CouponMain;
use app\model\Deduction;
@ -144,12 +144,4 @@ class BusinessRepository extends Repository
return $Flow;
}
/**
* 获取所有的商圈数据
* TODO
*/
public function getAllBusinessCircleList()
{
}
}

View File

@ -7,6 +7,7 @@ use app\model\CouponMain;
use app\model\CouponType;
use app\service\Repository;
use Exception;
use think\Collection;
use think\Db;
use think\Model;
@ -47,20 +48,19 @@ class CouponRepository extends Repository
}
/**
* 优惠券持有信息列表
* 获取优惠卷类型
*
* @param $id
* @param $keyword
* @param $page
* @param $size
* @return array
* @throws \Exception
*/
public function getCouponTypeAll()
* @param array $fields
* @param array $order
* @return Collection
*/
public function getCouponTypeAll(array $fields = [], array $order = ["id" => "desc"])
{
return CouponType::order(["id" => "desc"])->select();
try {
return CouponType::order($order)->field($fields)->select();
} catch (\Exception $e) {
return new Collection();
}
}
/**

View File

@ -0,0 +1,69 @@
<?php
namespace app\repository;
use app\model\BusinessCircle;
use app\model\Category;
use app\model\Model;
use app\service\Repository;
use think\Collection;
/**
* 数据字典域 相关
*
* Class DictionaryRepository
* @package app\repository
* @method self getInstance(Model $model = null) static
*/
class DictionaryRepository extends Repository
{
/**
* 上下级按children进行递归分组
* @param $items
* @param int $pid
* @param string $pidField 上下级关联字段
* @param string $primaryField 关联关系字段
* @return array
*/
public function recursionChildrenList($items, int $pid=0, string $pidField = 'pid', string $primaryField='id'): array
{
$list = [];
foreach ($items as $ki => $item) {
if ($item[$pidField] == $pid) {
unset($items[$ki]);
$item['children'] = $this->recursionChildrenList($items, $item[$primaryField], $pidField, $primaryField);
$list[] = $item;
}
}
return $list;
}
/**
* 获取所有的商圈数据
*/
public function getAllBusinessCircleList(array $fields=[])
{
try {
return BusinessCircle::field($fields)
->order(['sort'=>'desc', 'id'=>'asc'])
->select();
} catch (\Exception $e) {
return new Collection();
}
}
/**
* 获取商家分类数据
*/
public function getBusinessTypeList(array $where=[], array $fields = [], callable $call=null, array $order = [])
{
try {
$res = Category::findList($where, $fields, 1, 0, $call, $order);
return $res['list'];
} catch (\Exception $e) {
return new Collection();
}
}
}