wangxinglong 2021-11-29 17:32:59 +08:00
commit 737ec32bea
8 changed files with 258 additions and 22 deletions

View File

@ -554,19 +554,31 @@ if (!function_exists('getFilesize')) {
if (!function_exists('arrayNullToString')) {
/**
* 数组中null值转为空字符串
* 数组|或数据集中null值转为空字符串,并以数组格式返回
* 通常用于api json 返回内容null转换
*
* @param array $data
* @param array $data 【array|collection】
* @return array
*/
function arrayNullToString(array $data): array
function arrayNullToString($data)
{
foreach ($data as $key => $val) {
if (is_array($val)) {
$data[$key] = arrayNullToString($val);
} elseif ($val === null) {
$data[$key] = '';
if ($data instanceof Collection) {
$data = $data->toArray();
}
// 判断是否可以遍历
if (is_iterable($data)) {
foreach ($data as $key => $val) {
if ($val instanceof Collection) {
$val = $val->toArray();
}
if (is_iterable($val)) {
$data[$key] = arrayNullToString($val);
} elseif ($val === null) {
$data[$key] = '';
}
}
} else {
$data = [];
}
return $data;
}

View File

@ -3,6 +3,7 @@
namespace app\controller\api;
use app\controller\BaseController;
use think\response\Json;
/**
* API控制器基础类
@ -27,4 +28,24 @@ class Base extends BaseController
{
return $this->json(4004, 'error request!');
}
/**
* 返回封装后的API数据到客户端(对返回内容进行null转空字符串过滤)
* 以json格式抛出异常
* @access protected
* @param integer $code 返回的code
* @param mixed $msg 提示信息
* @param mixed $data 要返回的数据
* @return Json
*/
protected function json(int $code = 0, $msg = '操作成功', $data = []): Json
{
$result = [
'code' => $code,
'msg' => $msg,
'data' => arrayNullToString($data)
];
return json($result);
}
}

View File

@ -0,0 +1,128 @@
<?php
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
};
/**
* APP 数据字典
*
* Class Dictionary
* @package app\controller\api
*/
class Dictionary extends Base
{
protected $noNeedLogin = [
'getDistanceList',
'getBusinessCircle',
'getAgencyList',
'getBusinessTypeList',
'getCouponTypeList',
];
/**
* 获取距离(distance)列表
* @return Json
*/
public function getDisList(): Json
{
$list = [
['id' => 1, 'name'=> "1km", 'value' => 1],
['id' => 3, 'name'=> "3km", 'value' => 3],
['id' => 5, 'name'=> "5km", 'value' => 5],
['id' => 10, 'name'=> "10km", 'value' => 10],
];
return $this->json(0, 'success', $list);
}
/**
* 获取商圈列表
* @return Json
*/
public function getBusinessCircle(): Json
{
$items = DictionaryRepository::getInstance()->getAllBusinessCircleList();
return $this->json(0, 'success', $items);
}
/**
* 获取代理机构(渠道代理商)列表
* (来源商家表中的渠道代理商)
*
* $size 0 表示不分页,获取所有数据
*
*/
public function getAgencyList(): Json
{
try {
$page = $this->request->param('page/d', 1);
$size = $this->request->param('size/d', 10);
$whereMap = [];
$whereMap[] = ['is_agency', '=', self::BOOL_TRUE];
$whereMap[] = ['is_delete', '=', self::BOOL_FALSE];
$whereMap[] = ['state', '=', BusinessModel::state_on];
$whereMap[] = ['enable', '=', self::BOOL_FALSE];
$sortOrder = ['id'=>"asc"];
$fields = ['id', 'code', 'business_name', 'business_subtitle', 'business_license',
'lat', 'lng', 'business_address', 'contact_name', 'contact_phone',
'create_time', 'type', 'type_name', 'characteristic', 'intro',
'business_circle', 'business_circle_id', 'background', 'score'
];
$repo = BusinessRepository::getInstance();
$res = $repo->findList($whereMap, $fields, $page, $size, null, $sortOrder);
return $this->json(0 , 'success', $res);
} catch (RepositoryException | \Exception $e) {
return $this->json(0 , 'success', []);
}
}
/**
* 获取商家分类
*/
public function getBusinessTypeList(): Json
{
$recursionChildren = $this->request->param('recursion/d', 0);
$pid = $this->request->param('pid/d', 0);
$whereMap = [];
if ($pid > 0) {
$whereMap[] = ['pid', '=', $pid];
}
$repo = DictionaryRepository::getInstance();
$items = $repo->getBusinessTypeList($whereMap, [], null, ['pid'=>'asc']);
$list = $items->toArray();
if ($recursionChildren > 0) {
$list = $repo->recursionChildrenList($list, $pid, '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

@ -12,6 +12,12 @@ use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
use Exception;
use think\response\Json;
/**
* 用户相关
*
* Class User
* @package app\controller\api
*/
class User extends Base
{
protected $noNeedLogin = [

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;
@ -143,4 +143,5 @@ class BusinessRepository extends Repository
}
return $Flow;
}
}

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();
}
}
}