render.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { Label } from 'semantic-ui-react';
  2. import {Tag} from "@douyinfe/semi-ui";
  3. export function renderText(text, limit) {
  4. if (text.length > limit) {
  5. return text.slice(0, limit - 3) + '...';
  6. }
  7. return text;
  8. }
  9. export function renderGroup(group) {
  10. if (group === '') {
  11. return <Tag size='large'>default</Tag>;
  12. }
  13. let groups = group.split(',');
  14. groups.sort();
  15. return <>
  16. {groups.map((group) => {
  17. if (group === 'vip' || group === 'pro') {
  18. return <Tag size='large' color='yellow'>{group}</Tag>;
  19. } else if (group === 'svip' || group === 'premium') {
  20. return <Tag size='large' color='red'>{group}</Tag>;
  21. }
  22. if (group === 'default') {
  23. return <Tag size='large'>{group}</Tag>;
  24. } else {
  25. return <Tag size='large' color={stringToColor(group)}>{group}</Tag>;
  26. }
  27. })}
  28. </>;
  29. }
  30. export function renderNumber(num) {
  31. if (num >= 1000000000) {
  32. return (num / 1000000000).toFixed(1) + 'B';
  33. } else if (num >= 1000000) {
  34. return (num / 1000000).toFixed(1) + 'M';
  35. } else if (num >= 10000) {
  36. return (num / 1000).toFixed(1) + 'k';
  37. } else {
  38. return num;
  39. }
  40. }
  41. export function getQuotaPerUnit() {
  42. let quotaPerUnit = localStorage.getItem('quota_per_unit');
  43. quotaPerUnit = parseFloat(quotaPerUnit);
  44. return quotaPerUnit;
  45. }
  46. export function renderQuota(quota, digits = 2) {
  47. let quotaPerUnit = localStorage.getItem('quota_per_unit');
  48. let displayInCurrency = localStorage.getItem('display_in_currency');
  49. quotaPerUnit = parseFloat(quotaPerUnit);
  50. displayInCurrency = displayInCurrency === 'true';
  51. if (displayInCurrency) {
  52. return '$' + (quota / quotaPerUnit).toFixed(digits);
  53. }
  54. return renderNumber(quota);
  55. }
  56. export function renderQuotaWithPrompt(quota, digits) {
  57. let displayInCurrency = localStorage.getItem('display_in_currency');
  58. displayInCurrency = displayInCurrency === 'true';
  59. if (displayInCurrency) {
  60. return `(等价金额:${renderQuota(quota, digits)})`;
  61. }
  62. return '';
  63. }
  64. const colors = ['amber', 'blue', 'cyan', 'green', 'grey', 'indigo',
  65. 'light-blue', 'lime', 'orange', 'pink',
  66. 'purple', 'red', 'teal', 'violet', 'yellow'
  67. ]
  68. export function stringToColor(str) {
  69. let sum = 0;
  70. // 对字符串中的每个字符进行操作
  71. for (let i = 0; i < str.length; i++) {
  72. // 将字符的ASCII值加到sum中
  73. sum += str.charCodeAt(i);
  74. }
  75. // 使用模运算得到个位数
  76. let i = sum % colors.length;
  77. return colors[i];
  78. }