|
|
@@ -0,0 +1,137 @@
|
|
|
+/**
|
|
|
+ * Unsplash 图片工具类
|
|
|
+ * 为银龄智慧应用提供高质量的图片资源
|
|
|
+ */
|
|
|
+export class UnsplashImages {
|
|
|
+ // Unsplash 主题分类
|
|
|
+ static themes = {
|
|
|
+ elderly: {
|
|
|
+ people: [
|
|
|
+ 'https://images.unsplash.com/photo-1442458370899-ae20e367c5d8?w=400&h=400&fit=crop',
|
|
|
+ 'https://images.unsplash.com/photo-1556889882-73ea40694a98?w=400&h=400&fit=crop',
|
|
|
+ 'https://images.unsplash.com/photo-1581579438747-104c53d7fbc4?w=400&h=400&fit=crop',
|
|
|
+ 'https://images.unsplash.com/photo-1558618666-fcd25c85cd64?w=400&h=400&fit=crop',
|
|
|
+ 'https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=400&h=400&fit=crop'
|
|
|
+ ],
|
|
|
+ jobs: [
|
|
|
+ 'https://images.unsplash.com/photo-1522202176988-66273c2fd55f?w=400&h=400&fit=crop',
|
|
|
+ 'https://images.unsplash.com/photo-1552664730-d307ca884978?w=400&h=400&fit=crop',
|
|
|
+ 'https://images.unsplash.com/photo-1600880292203-757bb62b4baf?w=400&h=400&fit=crop',
|
|
|
+ 'https://images.unsplash.com/photo-1551434678-e076c223a692?w=400&h=400&fit=crop'
|
|
|
+ ],
|
|
|
+ education: [
|
|
|
+ 'https://images.unsplash.com/photo-1523050854058-8df90110c9f1?w=400&h=400&fit=crop',
|
|
|
+ 'https://images.unsplash.com/photo-1503676260728-1c00da094a0b?w=400&h=400&fit=crop',
|
|
|
+ 'https://images.unsplash.com/photo-1454165804606-c3d57bc86b40?w=400&h=400&fit=crop'
|
|
|
+ ],
|
|
|
+ health: [
|
|
|
+ 'https://images.unsplash.com/photo-1576091160399-112ba8d25d1f?w=400&h=400&fit=crop',
|
|
|
+ 'https://images.unsplash.com/photo-1559757148-5c350d0d3c56?w=400&h=400&fit=crop',
|
|
|
+ 'https://images.unsplash.com/photo-1571019613454-1cb2f99b2d8b?w=400&h=400&fit=crop'
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ nature: [
|
|
|
+ 'https://images.unsplash.com/photo-1441974231531-c6227db76b6e?w=400&h=400&fit=crop',
|
|
|
+ 'https://images.unsplash.com/photo-1469474968028-56623f02e42e?w=400&h=400&fit=crop',
|
|
|
+ 'https://images.unsplash.com/photo-1426604966848-d7adac402bff?w=400&h=400&fit=crop'
|
|
|
+ ],
|
|
|
+ community: [
|
|
|
+ 'https://images.unsplash.com/photo-1517486808906-6ca8b3f8e1c1?w=400&h=400&fit=crop',
|
|
|
+ 'https://images.unsplash.com/photo-1552664730-d307ca884978?w=400&h=400&fit=crop',
|
|
|
+ 'https://images.unsplash.com/photo-1517245386807-bb43f82c33c4?w=400&h=400&fit=crop'
|
|
|
+ ]
|
|
|
+ };
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取银龄人物头像
|
|
|
+ * @param seed 用于生成唯一图片的种子
|
|
|
+ * @returns Unsplash 图片URL
|
|
|
+ */
|
|
|
+ static getElderlyAvatar(seed: string | number): string {
|
|
|
+ const index = this.hashCode(seed.toString()) % this.themes.elderly.people.length;
|
|
|
+ return this.themes.elderly.people[index];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取工作相关图片
|
|
|
+ * @param seed 用于生成唯一图片的种子
|
|
|
+ * @returns Unsplash 图片URL
|
|
|
+ */
|
|
|
+ static getJobImage(seed: string | number): string {
|
|
|
+ const index = this.hashCode(seed.toString()) % this.themes.elderly.jobs.length;
|
|
|
+ return this.themes.elderly.jobs[index];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取教育相关图片
|
|
|
+ * @param seed 用于生成唯一图片的种子
|
|
|
+ * @returns Unsplash 图片URL
|
|
|
+ */
|
|
|
+ static getEducationImage(seed: string | number): string {
|
|
|
+ const index = this.hashCode(seed.toString()) % this.themes.elderly.education.length;
|
|
|
+ return this.themes.elderly.education[index];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取健康相关图片
|
|
|
+ * @param seed 用于生成唯一图片的种子
|
|
|
+ * @returns Unsplash 图片URL
|
|
|
+ */
|
|
|
+ static getHealthImage(seed: string | number): string {
|
|
|
+ const index = this.hashCode(seed.toString()) % this.themes.elderly.health.length;
|
|
|
+ return this.themes.elderly.health[index];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取通用占位图片
|
|
|
+ * @param category 图片类别
|
|
|
+ * @param seed 用于生成唯一图片的种子
|
|
|
+ * @returns Unsplash 图片URL
|
|
|
+ */
|
|
|
+ static getPlaceholderImage(category: 'nature' | 'community' = 'nature', seed: string | number = ''): string {
|
|
|
+ const theme = this.themes[category];
|
|
|
+ if (Array.isArray(theme)) {
|
|
|
+ const index = seed ? this.hashCode(seed.toString()) % theme.length : 0;
|
|
|
+ return theme[index];
|
|
|
+ }
|
|
|
+ return this.themes.nature[0];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成哈希码用于图片选择
|
|
|
+ * @param str 输入字符串
|
|
|
+ * @returns 哈希值
|
|
|
+ */
|
|
|
+ private static hashCode(str: string): number {
|
|
|
+ let hash = 0;
|
|
|
+ for (let i = 0; i < str.length; i++) {
|
|
|
+ const char = str.charCodeAt(i);
|
|
|
+ hash = ((hash << 5) - hash) + char;
|
|
|
+ hash = hash & hash; // 转换为32位整数
|
|
|
+ }
|
|
|
+ return Math.abs(hash);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取带尺寸的图片URL
|
|
|
+ * @param url 基础URL
|
|
|
+ * @param width 宽度
|
|
|
+ * @param height 高度
|
|
|
+ * @returns 带尺寸参数的URL
|
|
|
+ */
|
|
|
+ static resizeImage(url: string, width: number, height: number): string {
|
|
|
+ return url.replace(/w=\d+&h=\d+/, `w=${width}&h=${height}`);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取模糊占位图片(用于懒加载)
|
|
|
+ * @param url 原始URL
|
|
|
+ * @returns 模糊版本URL
|
|
|
+ */
|
|
|
+ static getBlurPlaceholder(url: string): string {
|
|
|
+ return url.includes('?') ? `${url}&q=10&blur=10` : `${url}?q=10&blur=10`;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 导出实例以便直接使用
|
|
|
+export const unsplash = UnsplashImages;
|