APP_STATUS.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace App\Module\OpenAPI\Enums;
  3. /**
  4. * 应用状态枚举
  5. */
  6. enum APP_STATUS: string
  7. {
  8. case PENDING = 'PENDING'; // 待审核
  9. case APPROVED = 'APPROVED'; // 已审核通过
  10. case REJECTED = 'REJECTED'; // 已拒绝
  11. case ACTIVE = 'ACTIVE'; // 激活状态
  12. case SUSPENDED = 'SUSPENDED'; // 暂停状态
  13. case DISABLED = 'DISABLED'; // 禁用状态
  14. case EXPIRED = 'EXPIRED'; // 已过期
  15. /**
  16. * 获取状态的中文描述
  17. *
  18. * @return string
  19. */
  20. public function getLabel(): string
  21. {
  22. return match ($this) {
  23. self::PENDING => '待审核',
  24. self::APPROVED => '已审核',
  25. self::REJECTED => '已拒绝',
  26. self::ACTIVE => '激活',
  27. self::SUSPENDED => '暂停',
  28. self::DISABLED => '禁用',
  29. self::EXPIRED => '已过期',
  30. };
  31. }
  32. /**
  33. * 获取状态的颜色
  34. *
  35. * @return string
  36. */
  37. public function getColor(): string
  38. {
  39. return match ($this) {
  40. self::PENDING => 'warning',
  41. self::APPROVED => 'info',
  42. self::REJECTED => 'danger',
  43. self::ACTIVE => 'success',
  44. self::SUSPENDED => 'warning',
  45. self::DISABLED => 'secondary',
  46. self::EXPIRED => 'dark',
  47. };
  48. }
  49. /**
  50. * 获取状态的图标
  51. *
  52. * @return string
  53. */
  54. public function getIcon(): string
  55. {
  56. return match ($this) {
  57. self::PENDING => 'fa-clock',
  58. self::APPROVED => 'fa-check',
  59. self::REJECTED => 'fa-times',
  60. self::ACTIVE => 'fa-play',
  61. self::SUSPENDED => 'fa-pause',
  62. self::DISABLED => 'fa-stop',
  63. self::EXPIRED => 'fa-calendar-times',
  64. };
  65. }
  66. /**
  67. * 获取状态描述
  68. *
  69. * @return string
  70. */
  71. public function getDescription(): string
  72. {
  73. return match ($this) {
  74. self::PENDING => '应用已提交,等待管理员审核',
  75. self::APPROVED => '应用已通过审核,可以激活使用',
  76. self::REJECTED => '应用审核未通过,需要修改后重新提交',
  77. self::ACTIVE => '应用正常运行中,可以调用API',
  78. self::SUSPENDED => '应用已暂停,暂时无法调用API',
  79. self::DISABLED => '应用已禁用,无法调用API',
  80. self::EXPIRED => '应用已过期,需要续期后才能使用',
  81. };
  82. }
  83. /**
  84. * 判断是否可以调用API
  85. *
  86. * @return bool
  87. */
  88. public function canCallApi(): bool
  89. {
  90. return $this === self::ACTIVE;
  91. }
  92. /**
  93. * 判断是否可以编辑
  94. *
  95. * @return bool
  96. */
  97. public function canEdit(): bool
  98. {
  99. return in_array($this, [
  100. self::PENDING,
  101. self::REJECTED,
  102. self::SUSPENDED,
  103. ]);
  104. }
  105. /**
  106. * 判断是否需要审核
  107. *
  108. * @return bool
  109. */
  110. public function needsApproval(): bool
  111. {
  112. return $this === self::PENDING;
  113. }
  114. /**
  115. * 获取可以转换到的状态
  116. *
  117. * @return array
  118. */
  119. public function getTransitionableStates(): array
  120. {
  121. return match ($this) {
  122. self::PENDING => [self::APPROVED, self::REJECTED],
  123. self::APPROVED => [self::ACTIVE, self::REJECTED],
  124. self::REJECTED => [self::PENDING],
  125. self::ACTIVE => [self::SUSPENDED, self::DISABLED, self::EXPIRED],
  126. self::SUSPENDED => [self::ACTIVE, self::DISABLED],
  127. self::DISABLED => [self::ACTIVE],
  128. self::EXPIRED => [self::ACTIVE],
  129. };
  130. }
  131. /**
  132. * 获取所有状态的选项数组
  133. *
  134. * @return array
  135. */
  136. public static function getOptions(): array
  137. {
  138. $options = [];
  139. foreach (self::cases() as $case) {
  140. $options[$case->value] = $case->getLabel();
  141. }
  142. return $options;
  143. }
  144. /**
  145. * 获取可用状态(可以调用API的状态)
  146. *
  147. * @return array
  148. */
  149. public static function getActiveStates(): array
  150. {
  151. return [self::ACTIVE];
  152. }
  153. /**
  154. * 获取不可用状态
  155. *
  156. * @return array
  157. */
  158. public static function getInactiveStates(): array
  159. {
  160. return [
  161. self::PENDING,
  162. self::REJECTED,
  163. self::SUSPENDED,
  164. self::DISABLED,
  165. self::EXPIRED,
  166. ];
  167. }
  168. /**
  169. * 获取需要管理员处理的状态
  170. *
  171. * @return array
  172. */
  173. public static function getAdminActionStates(): array
  174. {
  175. return [
  176. self::PENDING,
  177. self::SUSPENDED,
  178. self::EXPIRED,
  179. ];
  180. }
  181. }