mainIndexSprite.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * 雪碧图注册表
  3. * 用途:sprite-icon 按 sheet + name 从多张 CDN 雪碧图裁切显示图标
  4. * 图片地址随 ext.json / 环境 imgHost 变化,必须用函数运行时读取,不能用 this.$constant
  5. */
  6. import constant from "@/constant/index";
  7. /** 默认图床(ext 未配置 imgHost 时的兜底) */
  8. const DEFAULT_IMG_HOST = "https://img.huaml.com";
  9. /** 未传 sheet 时默认使用的雪碧图 key */
  10. const DEFAULT_SHEET_KEY = "main";
  11. /**
  12. * 雪碧图注册表:每张图含 path、原图尺寸、icons 坐标
  13. */
  14. export const SPRITE_SHEETS = {
  15. main: {
  16. path: "/hhb/sprite_icon/main-index.webp?time=2607171640",
  17. width: 500,
  18. height: 500,
  19. icons: {
  20. "dengji-biandong": { x: 14, y: 17, w: 28, h: 30 },
  21. "yue-biandong": { x: 60, y: 17, w: 28, h: 30 },
  22. "huiyuan-quanyi": { x: 102, y: 17, w: 28, h: 30 },
  23. "lianxikefu": { x: 145, y: 17, w: 28, h: 30 },
  24. "huangguan": { x: 189, y: 20, w: 22, h: 21 },
  25. "shezhi": { x: 13, y: 62, w: 34, h: 36 },
  26. "shaixuan": { x: 71, y: 67, w: 25, h: 25 },
  27. "dianpu-fill": { x: 116, y: 63, w: 31, h: 30 },
  28. "huoche-cheng": { x: 171, y: 68, w: 23, h: 21 },
  29. "huoche-lv": { x: 204, y: 68, w: 23, h: 21 },
  30. "gou-quan": { x: 246, y: 68, w: 22, h: 21 },
  31. "huashu": { x: 4, y: 118, w: 52, h: 49 },
  32. "hua": { x: 94, y: 118, w: 52, h: 49 },
  33. "dianpu-line": { x: 167, y: 114, w: 51, h: 51 },
  34. "tuichu": { x: 243, y: 111, w: 49, h: 51 },
  35. "xiaoxi": { x: 8, y: 186, w: 43, h: 44 },
  36. "qianbao": { x: 68, y: 182, w: 55, h: 48 },
  37. "huoche-da": { x: 142, y: 182, w: 58, h: 48 },
  38. "baoguo": { x: 219, y: 182, w: 49, h: 48 },
  39. "fanghu-yuan": { x: 282, y: 182, w: 42, h: 48 },
  40. "youhuiquan-yellow": { x: 344, y: 184, w: 50, h: 50 },
  41. "gengduo-quan": { x: 274, y: 102, w: 52, h: 52 },
  42. "icon-home": { x: 274, y: 102, w: 52, h: 52 },
  43. "icon-home-active": { x: 274, y: 102, w: 52, h: 52 },
  44. "gengduo-quan": { x: 274, y: 102, w: 52, h: 52 },
  45. "icon-home": { x: 7, y: 245, w: 50, h: 50 },
  46. "icon-home-active": { x: 60, y: 245, w: 50, h: 50 },
  47. "icon-category": { x: 116, y: 245, w: 50, h: 50 },
  48. "icon-category-active": { x: 176, y: 245, w: 50, h: 50 },
  49. "icon-cart": { x: 238, y: 245, w: 50, h: 50 },
  50. "icon-cart-active": { x: 293, y: 245, w: 50, h: 50 },
  51. "icon-order": { x: 366, y: 245, w: 50, h: 50 },
  52. "icon-order-active": { x: 418, y: 245 , w: 50, h: 50 },
  53. "cart-delete": { x: 7, y: 298 , w: 46, h: 52 },//购物车-删除
  54. "notice": { x: 216, y: 15 , w: 31, h: 31 },//公告通知
  55. }
  56. },
  57. /** 进货/供货商专用雪碧图,坐标待设计稿测量后补全 */
  58. order: {
  59. path: "/hhb/sprite_icon/icon-order.webp?time=26072009",
  60. width: 600,
  61. height: 600,
  62. icons: {
  63. "ps-shop": { x: 257, y: 8 , w: 57, h: 50 },//商家配送
  64. "ps-shop-active": { x: 190, y: 8 , w: 57, h: 50 },//商家配送-active
  65. "ps-paotui": { x: 198, y: 63 , w: 59, h: 50 },//跑腿配送
  66. "ps-paotui-active": { x: 129, y: 63 , w: 59, h: 50 },//跑腿配送-active
  67. "ps-daodian": { x: 383, y: 8 , w: 48, h: 50 },//到店自取
  68. "ps-daodian-active": { x: 324, y: 8 , w: 49, h: 50 },//到店自取-active
  69. "ps-kuaidi": { x: 72, y: 63 , w: 47, h: 50 },//快递
  70. "ps-kuaidi-active": { x: 12, y: 63 , w: 46, h: 50 },//快递-active
  71. "ps-wuliu": { x: 441, y: 8 , w: 67, h: 50 },//物流
  72. "ps-wuliu-active": { x: 518, y: 8 , w: 66, h: 50 },//物流-active
  73. "huoche": { x: 7, y: 8 , w: 46, h: 50 },//配送说明-小货车
  74. "youhuiquan": { x: 267, y: 63 , w: 58, h: 50 },//优惠券
  75. "beizhu": { x: 12, y: 8 , w: 50, h: 50 },//订单-备注
  76. "shijian": { x: 70, y: 8 , w: 52, h: 52 },//配送时间
  77. "shijianduan": { x: 129, y: 8 , w: 52, h: 52 },//配送时间段
  78. "dingwei": { x: 339, y: 63 , w: 42, h: 51 },//定位
  79. }
  80. }
  81. };
  82. /** tabBar 未选中 / 选中 图标名映射(main 雪碧图) */
  83. export const TAB_SPRITE_MAP = {
  84. home: { normal: "dianpu-line", active: "dianpu-fill" },
  85. order: { normal: "huoche-lv", active: "huoche-da" },
  86. mine: { normal: "gou-quan", active: "qianbao" }
  87. };
  88. /** @deprecated 兼容旧引用 */
  89. export const MAIN_INDEX_SPRITE = {
  90. width: SPRITE_SHEETS.main.width,
  91. height: SPRITE_SHEETS.main.height
  92. };
  93. /** @deprecated 兼容旧引用 */
  94. export const MAIN_INDEX_ICONS = SPRITE_SHEETS.main.icons;
  95. function buildIconSheetIndex(sheets) {
  96. const index = {};
  97. Object.keys(sheets).forEach((sheetKey) => {
  98. const icons = sheets[sheetKey].icons || {};
  99. Object.keys(icons).forEach((name) => {
  100. if (index[name] && index[name] !== sheetKey && process.env.NODE_ENV !== "production") {
  101. console.warn(`[sprite] 图标名 "${name}" 在 ${index[name]} 与 ${sheetKey} 中重复,以 ${sheetKey} 为准`);
  102. }
  103. index[name] = sheetKey;
  104. });
  105. });
  106. return index;
  107. }
  108. const ICON_SHEET_INDEX = buildIconSheetIndex(SPRITE_SHEETS);
  109. function getImgHostBase() {
  110. let base = (constant.imgUrl || DEFAULT_IMG_HOST).replace(/\/$/, "");
  111. if (base.indexOf("http://") === 0) {
  112. base = `https://${base.slice(7)}`;
  113. }
  114. return base;
  115. }
  116. function rpx2px(rpx) {
  117. if (typeof uni !== "undefined" && typeof uni.upx2px === "function") {
  118. return uni.upx2px(rpx);
  119. }
  120. return rpx;
  121. }
  122. /**
  123. * 计算图标显示宽高与缩放比(支持按宽度 size 或按高度 height 等比缩放)
  124. * @param {{ w: number, h: number }} icon 雪碧图坐标中的原始宽高
  125. * @param {{ size?: number|string, height?: number|string }} opts
  126. * @returns {{ displayW: number, displayH: number, scale: number }}
  127. */
  128. function resolveSpriteDisplaySize(icon, opts = {}) {
  129. const heightNum = Number(opts.height);
  130. const useHeight = opts.height !== "" && opts.height != null && !Number.isNaN(heightNum) && heightNum > 0;
  131. let scale;
  132. let displayW;
  133. let displayH;
  134. if (useHeight) {
  135. displayH = Math.round(heightNum);
  136. scale = displayH / icon.h;
  137. displayW = Math.round(icon.w * scale);
  138. } else {
  139. const sizeNum = Number(opts.size);
  140. displayW = !Number.isNaN(sizeNum) && sizeNum > 0 ? sizeNum : 48;
  141. scale = displayW / icon.w;
  142. displayH = Math.round(icon.h * scale);
  143. }
  144. return { displayW, displayH, scale };
  145. }
  146. export function resolveSprite(name, sheetKey = "") {
  147. if (!name) {
  148. return null;
  149. }
  150. let key = sheetKey;
  151. if (!key) {
  152. key = ICON_SHEET_INDEX[name] || DEFAULT_SHEET_KEY;
  153. }
  154. const sheet = SPRITE_SHEETS[key];
  155. if (!sheet) {
  156. return null;
  157. }
  158. const icon = (sheet.icons || {})[name];
  159. if (!icon) {
  160. return null;
  161. }
  162. return { sheetKey: key, sheet, icon };
  163. }
  164. export function getSpriteSheetUrl(sheetKey = DEFAULT_SHEET_KEY) {
  165. const sheet = SPRITE_SHEETS[sheetKey];
  166. if (!sheet) {
  167. return "";
  168. }
  169. return `${getImgHostBase()}${sheet.path}`;
  170. }
  171. export function spriteImageStyle(name, size = 48, sheetKey = "", height = "") {
  172. const resolved = resolveSprite(name, sheetKey);
  173. if (!resolved) {
  174. if (process.env.NODE_ENV !== "production") {
  175. console.warn(`[sprite-icon] 未找到图标: name=${name}${sheetKey ? ` sheet=${sheetKey}` : ""}`);
  176. }
  177. return { wrap: "", inner: "", image: "" };
  178. }
  179. const { sheet, icon } = resolved;
  180. const { displayW, displayH, scale } = resolveSpriteDisplaySize(icon, { size, height });
  181. const sheetW = sheet.width * scale;
  182. const sheetH = sheet.height * scale;
  183. const left = -rpx2px(icon.x * scale);
  184. const top = -rpx2px(icon.y * scale);
  185. const sheetWPx = rpx2px(sheetW);
  186. const sheetHPx = rpx2px(sheetH);
  187. return {
  188. wrap: `position:relative;width:${displayW}rpx;height:${displayH}rpx;overflow:hidden;line-height:0;`,
  189. inner: `position:absolute;left:${left}px;top:${top}px;width:${sheetWPx}px;height:${sheetHPx}px;`,
  190. image: "width:100%;height:100%;display:block;"
  191. };
  192. }
  193. export function spriteStyle(name, size = 48, sheetKey = "", height = "") {
  194. const resolved = resolveSprite(name, sheetKey);
  195. if (!resolved) {
  196. return "";
  197. }
  198. const { sheetKey: key, sheet, icon } = resolved;
  199. const { displayW, displayH, scale } = resolveSpriteDisplaySize(icon, { size, height });
  200. const sheetW = sheet.width * scale;
  201. const sheetH = sheet.height * scale;
  202. const posX = rpx2px(icon.x * scale);
  203. const posY = rpx2px(icon.y * scale);
  204. const sheetWPx = rpx2px(sheetW);
  205. const sheetHPx = rpx2px(sheetH);
  206. const url = getSpriteSheetUrl(key);
  207. return [
  208. `width:${displayW}rpx`,
  209. `height:${displayH}rpx`,
  210. `background-image:url(${url})`,
  211. `background-size:${sheetWPx}px ${sheetHPx}px`,
  212. `background-position:-${posX}px -${posY}px`,
  213. "background-repeat:no-repeat",
  214. "display:inline-block",
  215. "flex-shrink:0",
  216. "vertical-align:middle"
  217. ].join(";");
  218. }
  219. export function getMainIndexSpriteUrl() {
  220. return getSpriteSheetUrl(DEFAULT_SHEET_KEY);
  221. }
  222. export function mainIndexSpriteImageStyle(name, size = 48, height = "") {
  223. return spriteImageStyle(name, size, DEFAULT_SHEET_KEY, height);
  224. }
  225. export function mainIndexSpriteStyle(name, size = 48, height = "") {
  226. return spriteStyle(name, size, DEFAULT_SHEET_KEY, height);
  227. }