From d30a7ede938e0e241e80769cfd30757fa2b155f6 Mon Sep 17 00:00:00 2001 From: zwesy Date: Mon, 29 Nov 2021 13:59:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=EF=BC=9A=E4=BB=A3=E7=90=86?= =?UTF-8?q?=E5=95=86=E5=88=97=E8=A1=A8=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common.php | 28 ++++++--- app/controller/api/Base.php | 21 +++++++ app/controller/api/Dictionary.php | 89 +++++++++++++++++++++++++++ app/controller/api/User.php | 6 ++ app/repository/BusinessRepository.php | 9 +++ 5 files changed, 145 insertions(+), 8 deletions(-) create mode 100644 app/controller/api/Dictionary.php diff --git a/app/common.php b/app/common.php index a8455c0..de80d12 100644 --- a/app/common.php +++ b/app/common.php @@ -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; } diff --git a/app/controller/api/Base.php b/app/controller/api/Base.php index 63169eb..118846a 100644 --- a/app/controller/api/Base.php +++ b/app/controller/api/Base.php @@ -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); + } + } \ No newline at end of file diff --git a/app/controller/api/Dictionary.php b/app/controller/api/Dictionary.php new file mode 100644 index 0000000..28b371d --- /dev/null +++ b/app/controller/api/Dictionary.php @@ -0,0 +1,89 @@ + 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); + } + + /** + * 获取商圈列表 + * TODO 待确认是否需要这个接口 + */ + public function getBusinessCircle() + { + + + } + + /** + * 获取代理机构(渠道代理商)列表 + * (来源商家表中的渠道代理商) + * + * $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', []); + } + } + + + + +} \ No newline at end of file diff --git a/app/controller/api/User.php b/app/controller/api/User.php index 0bd49cf..b743d18 100644 --- a/app/controller/api/User.php +++ b/app/controller/api/User.php @@ -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 = [ diff --git a/app/repository/BusinessRepository.php b/app/repository/BusinessRepository.php index 93674c7..5413f37 100644 --- a/app/repository/BusinessRepository.php +++ b/app/repository/BusinessRepository.php @@ -135,4 +135,13 @@ class BusinessRepository extends Repository } return $Flow; } + + /** + * 获取所有的商圈数据 + * TODO + */ + public function getAllBusinessCircleList() + { + + } } \ No newline at end of file