From 9601d5dc322e07ddff8b64fd6a4ab0a6c7b3b197 Mon Sep 17 00:00:00 2001 From: zwesy Date: Mon, 6 Dec 2021 09:25:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=EF=BC=9A=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E9=A2=86=E5=8F=96=E7=9A=84=E4=BC=98=E6=83=A0=E5=8D=B7=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common.php | 50 ++++++++++++++++++++++++- app/controller/api/Coupon.php | 69 +++++++++++++++++++++++++++++++++++ app/model/Coupon.php | 5 +++ 3 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 app/controller/api/Coupon.php diff --git a/app/common.php b/app/common.php index 97a42a9..87816b1 100644 --- a/app/common.php +++ b/app/common.php @@ -499,7 +499,7 @@ if (!function_exists('checkMobile')) { if (!function_exists('arrayKeysFilter')) { /** - * 数组键名过滤 + * 一维数组键名过滤 * * @param array $data * @param array $allowKeys @@ -517,6 +517,29 @@ if (!function_exists('arrayKeysFilter')) { } } +if (!function_exists('multiTwoArrayKeysFilter')) { + /** + * 二维数组键名过滤 + * + * @param array $data + * @param array $allowKeys 允许的字段 + * @return array + */ + function multiTwoArrayKeysFilter(array $data, array $allowKeys): array + { + foreach ($data as $ki => $item) { + $list = []; + foreach ($item as $key => $val) { + if (in_array($key, $allowKeys)) { + $list[$key] = $val; + } + } + $data[$ki] = $list; + } + return $data; + } +} + if (!function_exists('getFilesize')) { /** * 尺寸单位转换 @@ -586,7 +609,7 @@ if (!function_exists('arrayNullToString')) { if (!function_exists('arrayKeysExcludeFilter')) { /** - * 数组键名排除过滤 + * 一维数组键名排除过滤 * * @param array $data * @param array $excludeKeys 排除的字段 @@ -603,6 +626,29 @@ if (!function_exists('arrayKeysExcludeFilter')) { } } +if (!function_exists('multiTwoArrayKeysExcludeFilter')) { + /** + * 二维维数组键名排除过滤 + * + * @param array $data + * @param array $excludeKeys 排除的字段 + * @return array + */ + function multiTwoArrayKeysExcludeFilter(array $data, array $excludeKeys): array + { + foreach ($data as $ki => $item) { + foreach ($item as $key => $val) { + if (in_array($key, $excludeKeys)) { + unset($item[$key]); + } + } + $data[$ki] = $item; + } + return $data; + } +} + + if (!function_exists('getLatelyWeekDate')) { /** * 获取本周的周一 到周日的日期 diff --git a/app/controller/api/Coupon.php b/app/controller/api/Coupon.php new file mode 100644 index 0000000..bd2d04c --- /dev/null +++ b/app/controller/api/Coupon.php @@ -0,0 +1,69 @@ +request->param('page/d', 1); + $size = $this->request->param('size/d', 10); + $type = $this->request->param('type', ''); + + $page = $page < 1 ? 1 : $page; + $size = $size < 1 ? 10 : $size; + $accountCode = $this->request->user['user_code'] ?? ''; + + + try { + $whereMap = []; + $sortOrder = ['received_time' => 'desc']; + $fields = ['*', '(end_time > NOW()) as sort_weight']; + // TODO `status`字段需要核实是否用于筛选条件 + + $whereMap[] = ['consumer_code', '=', $accountCode]; + switch ($type) { + case 'notUsed': + // 未使用(包含已过期) + $whereMap[] = ['is_verificated', '=', self::BOOL_FALSE]; + $sortOrder = ['sort_weight' => 'desc', 'end_time' => 'asc']; + break; + case 'normal': + // 未使用且未过期 + $whereMap[] = ['is_verificated', '=', self::BOOL_FALSE]; + $whereMap[] = ['end_time', '< TIME', date('Y-m-d H:i:s')]; + $fields = []; + break; + case 'used': + // 已使用 + $whereMap[] = ['is_verificated', '=', self::BOOL_TRUE]; + $sortOrder = ['verificate_time' => 'desc']; + $fields = []; + break; + } + + $res = CouponRepository::getInstance()->findList($whereMap, $fields, $page, $size, null, $sortOrder); + $res['list'] = multiTwoArrayKeysExcludeFilter($res['list']->toArray(), ['sort_weight']); + + return $this->json(0, 'success', $res); + } catch (RepositoryException | \Exception $e) { + return $this->json(5001, '优惠卷查询失败!'); + } + } +} \ No newline at end of file diff --git a/app/model/Coupon.php b/app/model/Coupon.php index a5a7c80..f781bcd 100644 --- a/app/model/Coupon.php +++ b/app/model/Coupon.php @@ -32,4 +32,9 @@ class Coupon extends Base { return $this->hasOne(Business::class,"code","business_code"); } + + public function couponMain() + { + return $this->hasOne(CouponMain::class, 'id', 'coupon_id'); + } } \ No newline at end of file