AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

StoreBrand.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace App\Mail;
  3. use App\Models\Order;
  4. use App\Models\Store;
  5. use App\StorePlan\PlanSchema;
  6. use App\Support\MalaysianPhone;
  7. use Illuminate\Support\Facades\Storage;
  8. /**
  9. * How a store looks in the emails its shoppers get: its name, colours, fonts and corners,
  10. * taken from the design shoppers see. Plus the links the emails need.
  11. *
  12. * Emails cannot load the store's web fonts reliably, so each font comes with a safe fallback
  13. * of the same kind (serif or sans-serif).
  14. */
  15. final readonly class StoreBrand
  16. {
  17. /** Used when a store has no design yet. Calm and neutral. */
  18. private const array DEFAULT_COLORS = [
  19. 'background' => '#F6F5F2',
  20. 'surface' => '#FFFFFF',
  21. 'foreground' => '#1F1F1F',
  22. 'muted' => '#6B6B6B',
  23. 'primary' => '#1F1F1F',
  24. 'primaryForeground' => '#FFFFFF',
  25. 'accent' => '#C9A227',
  26. 'border' => '#E4E1DA',
  27. ];
  28. private const array SERIF_FONTS = ['playfair-display', 'lora', 'fraunces', 'cormorant-garamond', 'dm-serif-display', 'libre-baskerville'];
  29. /**
  30. * @param array<string, string> $colors
  31. */
  32. private function __construct(
  33. public Store $store,
  34. public string $name,
  35. public array $colors,
  36. public string $headingFont,
  37. public string $bodyFont,
  38. /** Corner rounding for cards, in pixels. */
  39. public int $radius,
  40. /** Corner rounding for buttons, in pixels. */
  41. public int $buttonRadius,
  42. public bool $malay,
  43. ) {}
  44. public static function for(Store $store): self
  45. {
  46. // What shoppers see: the published design, or the draft while the store is not live yet.
  47. $plan = $store->published_plan ?? $store->plan ?? [];
  48. $theme = $plan['theme'] ?? [];
  49. $colors = self::DEFAULT_COLORS;
  50. foreach (PlanSchema::COLORS as $key) {
  51. $value = $theme['colors'][$key] ?? null;
  52. if (is_string($value) && preg_match('/^#[0-9A-Fa-f]{6}$/', $value) === 1) {
  53. $colors[$key] = $value;
  54. }
  55. }
  56. $radius = match ($theme['radius'] ?? 'medium') {
  57. 'none' => 0,
  58. 'small' => 4,
  59. 'large' => 16,
  60. 'full' => 20,
  61. default => 8,
  62. };
  63. return new self(
  64. store: $store,
  65. name: self::logoText($plan) ?? $store->name,
  66. colors: $colors,
  67. headingFont: self::fontStack($theme['fonts']['heading'] ?? null),
  68. bodyFont: self::fontStack($theme['fonts']['body'] ?? null),
  69. radius: $radius,
  70. buttonRadius: ($theme['radius'] ?? null) === 'full' ? 999 : $radius,
  71. malay: $store->language->value === 'ms',
  72. );
  73. }
  74. /**
  75. * The uploaded logo's full web address, for the top of the email. Designed and AI-drawn logos are not
  76. * pictures that email apps can show, so those emails show the store's name instead.
  77. *
  78. * @return array{url: string, width: int, height: int}|null
  79. */
  80. public function logo(): ?array
  81. {
  82. $logo = $this->store->logo;
  83. if (($logo['type'] ?? null) !== 'upload') {
  84. return null;
  85. }
  86. $url = Storage::disk('public')->url($logo['path']);
  87. // Shown at most 48 pixels high and 220 wide, keeping its shape.
  88. $scale = min(48 / max(1, $logo['height']), 220 / max(1, $logo['width']), 1);
  89. return [
  90. 'url' => str_starts_with($url, 'http') ? $url : url($url),
  91. 'width' => (int) round($logo['width'] * $scale),
  92. 'height' => (int) round($logo['height'] * $scale),
  93. ];
  94. }
  95. /**
  96. * The email address shoppers can write to, when the merchant set one.
  97. */
  98. public function contactEmail(): ?string
  99. {
  100. return $this->store->contact_email;
  101. }
  102. public function color(string $key): string
  103. {
  104. return $this->colors[$key];
  105. }
  106. /**
  107. * The shopper's order page. A live store's page is on the store's own address; a store that is not
  108. * published yet only has the merchant's preview, so the link opens that.
  109. */
  110. public function orderUrl(Order $order): string
  111. {
  112. if (! $this->store->isPublished()) {
  113. return route('stores.preview.order', [$this->store->slug, $order->public_id]);
  114. }
  115. return $this->storeUrl().'/orders/'.$order->public_id;
  116. }
  117. /**
  118. * The store's own home page.
  119. */
  120. public function storeUrl(): string
  121. {
  122. if (! $this->store->isPublished()) {
  123. return route('stores.preview', $this->store->slug);
  124. }
  125. // Built from APP_URL, because emails are also sent from the webhook, where there is no shopper's visit.
  126. $app = parse_url((string) config('app.url'));
  127. $scheme = $app['scheme'] ?? 'https';
  128. $port = isset($app['port']) ? ':'.$app['port'] : '';
  129. return $scheme.'://'.$this->store->slug.'.'.config('storefront.domain').$port;
  130. }
  131. /**
  132. * A WhatsApp chat with the store about this order, or null when the store has no WhatsApp number.
  133. */
  134. public function whatsappUrl(Order $order): ?string
  135. {
  136. if ($this->store->whatsapp_number === null || $this->store->whatsapp_number === '') {
  137. return null;
  138. }
  139. $text = $this->malay
  140. ? "Hai {$this->store->name}, saya ada soalan tentang pesanan saya #{$order->number}."
  141. : "Hi {$this->store->name}, I have a question about my order #{$order->number}.";
  142. return MalaysianPhone::whatsappLink($this->store->whatsapp_number, $text);
  143. }
  144. public function money(int $sen): string
  145. {
  146. return 'RM '.number_format($sen / 100, 2);
  147. }
  148. /**
  149. * The words shown in the header, from the store's own header section when it has one.
  150. *
  151. * @param array<string, mixed> $plan
  152. */
  153. private static function logoText(array $plan): ?string
  154. {
  155. // Version 2 plans keep one shared header; older ones have it among the sections.
  156. $header = $plan['header'] ?? collect($plan['sections'] ?? [])->firstWhere('type', 'header');
  157. $text = trim((string) ($header['content']['logoText'] ?? ''));
  158. return $text !== '' ? $text : null;
  159. }
  160. private static function fontStack(?string $key): string
  161. {
  162. $family = PlanSchema::FONTS[$key] ?? null;
  163. $fallback = in_array($key, self::SERIF_FONTS, true)
  164. ? "Georgia, 'Times New Roman', serif"
  165. : 'Helvetica, Arial, sans-serif';
  166. return $family !== null ? "'{$family}', {$fallback}" : $fallback;
  167. }
  168. }