CategorySettingClass.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace bizHd\goods\classes;
  3. use bizHd\base\classes\BaseClass;
  4. use common\components\util;
  5. /**
  6. * 商品分类配置业务:商城花材分类名称、置顶、展示
  7. */
  8. class CategorySettingClass extends BaseClass
  9. {
  10. public static $baseFile = '\bizHd\goods\models\CategorySetting';
  11. /**
  12. * 校验操作权限
  13. * @param array|null $info
  14. * @param int $mainId
  15. */
  16. public static function valid($info, $mainId)
  17. {
  18. if (empty($info) || intval($info['mainId']) !== intval($mainId)) {
  19. util::fail('没有权限操作');
  20. }
  21. }
  22. /**
  23. * 读取当前门店配置,不存在则返回默认结构
  24. * @param int $mainId
  25. * @param int $shopId
  26. * @return array
  27. */
  28. public static function getOrInit($mainId, $shopId)
  29. {
  30. $info = self::getByCondition([
  31. 'mainId' => intval($mainId),
  32. 'shopId' => intval($shopId),
  33. ]);
  34. if (empty($info)) {
  35. return [
  36. 'id' => 0,
  37. 'mainId' => intval($mainId),
  38. 'shopId' => intval($shopId),
  39. 'itemCateName' => '',
  40. 'isItemCateTop' => 0,
  41. 'isItemCateShow' => 0,
  42. 'advertisement' => '',
  43. ];
  44. }
  45. return self::formatInfo($info);
  46. }
  47. /**
  48. * 保存商城分类设置(按 mainId + shopId 唯一 upsert)
  49. * @param int $mainId
  50. * @param int $shopId
  51. * @param array $data
  52. * @return array
  53. */
  54. public static function saveSetting($mainId, $shopId, $data)
  55. {
  56. $itemCateName = trim($data['itemCateName'] ?? '');
  57. if ($itemCateName === '') {
  58. util::fail('请输入商城分类名称');
  59. }
  60. if (mb_strlen($itemCateName, 'UTF-8') > 20) {
  61. util::fail('商城分类名称最多20个字');
  62. }
  63. $saveData = [
  64. 'itemCateName' => $itemCateName,
  65. 'isItemCateTop' => intval($data['isItemCateTop'] ?? 0) ? 1 : 0,
  66. 'isItemCateShow' => intval($data['isItemCateShow'] ?? 0) ? 1 : 0,
  67. ];
  68. $exists = self::getByCondition([
  69. 'mainId' => intval($mainId),
  70. 'shopId' => intval($shopId),
  71. ]);
  72. if (empty($exists)) {
  73. $saveData['mainId'] = intval($mainId);
  74. $saveData['shopId'] = intval($shopId);
  75. $saveData['advertisement'] = $data['advertisement'] ?? '';
  76. $row = self::add($saveData);
  77. return self::formatInfo($row);
  78. }
  79. // 未传 advertisement 时保留原值
  80. if (array_key_exists('advertisement', $data)) {
  81. $saveData['advertisement'] = is_string($data['advertisement'])
  82. ? $data['advertisement']
  83. : json_encode($data['advertisement'], JSON_UNESCAPED_UNICODE);
  84. }
  85. self::updateById($exists['id'], $saveData);
  86. $info = self::getById($exists['id']);
  87. return self::formatInfo($info);
  88. }
  89. /**
  90. * 仅保存分类广告 JSON(advertisement 字段)
  91. * @param int $mainId
  92. * @param int $shopId
  93. * @param string|array $advertisement
  94. * @return array
  95. */
  96. public static function saveAdvertisement($mainId, $shopId, $advertisement)
  97. {
  98. if (is_array($advertisement)) {
  99. $advertisement = json_encode($advertisement, JSON_UNESCAPED_UNICODE);
  100. }
  101. $advertisement = trim((string)$advertisement);
  102. if ($advertisement !== '') {
  103. $decoded = json_decode($advertisement, true);
  104. if (!is_array($decoded)) {
  105. util::fail('广告数据格式错误');
  106. }
  107. if (count($decoded) > 3) {
  108. util::fail('最多支持3张轮播图');
  109. }
  110. }
  111. $exists = self::getByCondition([
  112. 'mainId' => intval($mainId),
  113. 'shopId' => intval($shopId),
  114. ]);
  115. if (empty($exists)) {
  116. $row = self::add([
  117. 'mainId' => intval($mainId),
  118. 'shopId' => intval($shopId),
  119. 'itemCateName' => '',
  120. 'isItemCateTop' => 0,
  121. 'isItemCateShow' => 0,
  122. 'advertisement' => $advertisement,
  123. ]);
  124. return self::formatInfo($row);
  125. }
  126. self::updateById($exists['id'], ['advertisement' => $advertisement]);
  127. $info = self::getById($exists['id']);
  128. return self::formatInfo($info);
  129. }
  130. /**
  131. * 输出字段规范化
  132. * @param array $info
  133. * @return array
  134. */
  135. protected static function formatInfo($info)
  136. {
  137. return [
  138. 'id' => intval($info['id'] ?? 0),
  139. 'mainId' => intval($info['mainId'] ?? 0),
  140. 'shopId' => intval($info['shopId'] ?? 0),
  141. 'itemCateName' => $info['itemCateName'] ?? '',
  142. 'isItemCateTop' => intval($info['isItemCateTop'] ?? 0),
  143. 'isItemCateShow' => intval($info['isItemCateShow'] ?? 0),
  144. 'advertisement' => $info['advertisement'] ?? '',
  145. ];
  146. }
  147. }