render.sjs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import {
  2. defaultAvatar,
  3. defaultCardImage,
  4. } from './constants.sjs';
  5. function getImageUrl(url, prefix) {
  6. url = url || defaultAvatar;
  7. return url.length === 32 ? `${prefix}${url}` : url;
  8. }
  9. function getCardStyle(extInfo, imgSrcPrefix, count = 0) {
  10. const styles = [];
  11. const { certBackgroundUrl = '' } = extInfo || {};
  12. const url = certBackgroundUrl || defaultCardImage || '';
  13. styles.push(`backgroundImage:url(${getImageUrl(url, imgSrcPrefix)})`);
  14. if (count) styles.push(`width: calc(${1 / count * 100}%${count > 1 ? ' - 0.32rem' : ''})`);
  15. return styles.join(';');
  16. }
  17. function getCardList(list, certMap) {
  18. return list.map((item) => {
  19. const { certType } = item.extInfo || {};
  20. let certCardInfo = certMap[certType] || {};
  21. certCardInfo = { ...certCardInfo, certType };
  22. return { ...item, certCardInfo };
  23. });
  24. }
  25. function getHeaderStyle(extInfo, imgSrcPrefix) {
  26. const styles = [];
  27. const { certCodeBackgroundUrl } = extInfo || {};
  28. if (certCodeBackgroundUrl) {
  29. styles.push(`background-image:url(${getImageUrl(certCodeBackgroundUrl, imgSrcPrefix)})`);
  30. }
  31. return styles.join(';');
  32. }
  33. export default {
  34. getImageUrl,
  35. getCardList,
  36. getCardStyle,
  37. getHeaderStyle,
  38. };