|
|
@@ -0,0 +1,163 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace bizHd\goods\classes;
|
|
|
+
|
|
|
+use bizHd\base\classes\BaseClass;
|
|
|
+use common\components\util;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 商品分类配置业务:商城花材分类名称、置顶、展示
|
|
|
+ */
|
|
|
+class CategorySettingClass extends BaseClass
|
|
|
+{
|
|
|
+ public static $baseFile = '\bizHd\goods\models\CategorySetting';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验操作权限
|
|
|
+ * @param array|null $info
|
|
|
+ * @param int $mainId
|
|
|
+ */
|
|
|
+ public static function valid($info, $mainId)
|
|
|
+ {
|
|
|
+ if (empty($info) || intval($info['mainId']) !== intval($mainId)) {
|
|
|
+ util::fail('没有权限操作');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 读取当前门店配置,不存在则返回默认结构
|
|
|
+ * @param int $mainId
|
|
|
+ * @param int $shopId
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public static function getOrInit($mainId, $shopId)
|
|
|
+ {
|
|
|
+ $info = self::getByCondition([
|
|
|
+ 'mainId' => intval($mainId),
|
|
|
+ 'shopId' => intval($shopId),
|
|
|
+ ]);
|
|
|
+ if (empty($info)) {
|
|
|
+ return [
|
|
|
+ 'id' => 0,
|
|
|
+ 'mainId' => intval($mainId),
|
|
|
+ 'shopId' => intval($shopId),
|
|
|
+ 'itemCateName' => '',
|
|
|
+ 'isItemCateTop' => 0,
|
|
|
+ 'isItemCateShow' => 0,
|
|
|
+ 'advertisement' => '',
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ return self::formatInfo($info);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存商城分类设置(按 mainId + shopId 唯一 upsert)
|
|
|
+ * @param int $mainId
|
|
|
+ * @param int $shopId
|
|
|
+ * @param array $data
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public static function saveSetting($mainId, $shopId, $data)
|
|
|
+ {
|
|
|
+ $itemCateName = trim($data['itemCateName'] ?? '');
|
|
|
+ if ($itemCateName === '') {
|
|
|
+ util::fail('请输入商城分类名称');
|
|
|
+ }
|
|
|
+ if (mb_strlen($itemCateName, 'UTF-8') > 20) {
|
|
|
+ util::fail('商城分类名称最多20个字');
|
|
|
+ }
|
|
|
+
|
|
|
+ $saveData = [
|
|
|
+ 'itemCateName' => $itemCateName,
|
|
|
+ 'isItemCateTop' => intval($data['isItemCateTop'] ?? 0) ? 1 : 0,
|
|
|
+ 'isItemCateShow' => intval($data['isItemCateShow'] ?? 0) ? 1 : 0,
|
|
|
+ ];
|
|
|
+
|
|
|
+ $exists = self::getByCondition([
|
|
|
+ 'mainId' => intval($mainId),
|
|
|
+ 'shopId' => intval($shopId),
|
|
|
+ ]);
|
|
|
+
|
|
|
+ if (empty($exists)) {
|
|
|
+ $saveData['mainId'] = intval($mainId);
|
|
|
+ $saveData['shopId'] = intval($shopId);
|
|
|
+ $saveData['advertisement'] = $data['advertisement'] ?? '';
|
|
|
+ $row = self::add($saveData);
|
|
|
+ return self::formatInfo($row);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 未传 advertisement 时保留原值
|
|
|
+ if (array_key_exists('advertisement', $data)) {
|
|
|
+ $saveData['advertisement'] = is_string($data['advertisement'])
|
|
|
+ ? $data['advertisement']
|
|
|
+ : json_encode($data['advertisement'], JSON_UNESCAPED_UNICODE);
|
|
|
+ }
|
|
|
+
|
|
|
+ self::updateById($exists['id'], $saveData);
|
|
|
+ $info = self::getById($exists['id']);
|
|
|
+ return self::formatInfo($info);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 仅保存分类广告 JSON(advertisement 字段)
|
|
|
+ * @param int $mainId
|
|
|
+ * @param int $shopId
|
|
|
+ * @param string|array $advertisement
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public static function saveAdvertisement($mainId, $shopId, $advertisement)
|
|
|
+ {
|
|
|
+ if (is_array($advertisement)) {
|
|
|
+ $advertisement = json_encode($advertisement, JSON_UNESCAPED_UNICODE);
|
|
|
+ }
|
|
|
+ $advertisement = trim((string)$advertisement);
|
|
|
+ if ($advertisement !== '') {
|
|
|
+ $decoded = json_decode($advertisement, true);
|
|
|
+ if (!is_array($decoded)) {
|
|
|
+ util::fail('广告数据格式错误');
|
|
|
+ }
|
|
|
+ if (count($decoded) > 3) {
|
|
|
+ util::fail('最多支持3张轮播图');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $exists = self::getByCondition([
|
|
|
+ 'mainId' => intval($mainId),
|
|
|
+ 'shopId' => intval($shopId),
|
|
|
+ ]);
|
|
|
+
|
|
|
+ if (empty($exists)) {
|
|
|
+ $row = self::add([
|
|
|
+ 'mainId' => intval($mainId),
|
|
|
+ 'shopId' => intval($shopId),
|
|
|
+ 'itemCateName' => '',
|
|
|
+ 'isItemCateTop' => 0,
|
|
|
+ 'isItemCateShow' => 0,
|
|
|
+ 'advertisement' => $advertisement,
|
|
|
+ ]);
|
|
|
+ return self::formatInfo($row);
|
|
|
+ }
|
|
|
+
|
|
|
+ self::updateById($exists['id'], ['advertisement' => $advertisement]);
|
|
|
+ $info = self::getById($exists['id']);
|
|
|
+ return self::formatInfo($info);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 输出字段规范化
|
|
|
+ * @param array $info
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ protected static function formatInfo($info)
|
|
|
+ {
|
|
|
+ return [
|
|
|
+ 'id' => intval($info['id'] ?? 0),
|
|
|
+ 'mainId' => intval($info['mainId'] ?? 0),
|
|
|
+ 'shopId' => intval($info['shopId'] ?? 0),
|
|
|
+ 'itemCateName' => $info['itemCateName'] ?? '',
|
|
|
+ 'isItemCateTop' => intval($info['isItemCateTop'] ?? 0),
|
|
|
+ 'isItemCateShow' => intval($info['isItemCateShow'] ?? 0),
|
|
|
+ 'advertisement' => $info['advertisement'] ?? '',
|
|
|
+ ];
|
|
|
+ }
|
|
|
+}
|