theme.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { getThemeInfo } from "@/api/api.js";
  2. /**
  3. * 处理颜色
  4. */
  5. export function hexToRgba(hex, alpha) {
  6. let sColor = hex.toLowerCase();
  7. const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
  8. if (sColor && reg.test(sColor)) {
  9. if (sColor.length === 4) {
  10. let sColorNew = "#";
  11. for (let i = 1; i < 4; i += 1) {
  12. sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1));
  13. }
  14. sColor = sColorNew;
  15. }
  16. //处理六位的颜色值
  17. let sColorChange = [];
  18. for (let i = 1; i < 7; i += 2) {
  19. sColorChange.push(parseInt("0x" + sColor.slice(i, i + 2)));
  20. }
  21. return "rgba(" + sColorChange.join(",") + "," + alpha + ")";
  22. }
  23. return sColor;
  24. }
  25. /**
  26. * 设置主题颜色
  27. * @param {Object} data 主题数据
  28. */
  29. export function setThemeColor(data) {
  30. let selectedTheme;
  31. // 处理自定义主题色数据
  32. if (data.theme_color) {
  33. let themeColor = data.theme_color;
  34. let gradientColor = data.gradient_color;
  35. let subColor = data.sub_color;
  36. let lightColor = data.light_color;
  37. selectedTheme = `
  38. --view-theme: ${hexToRgba(themeColor, 1)};
  39. --view-theme-16: ${themeColor};
  40. --view-priceColor: ${themeColor};
  41. --view-minorColor: ${subColor};
  42. --view-minorColorT: ${lightColor};
  43. --view-bntColor: ${subColor};
  44. --view-op-ten: ${hexToRgba(themeColor, 0.1)};
  45. --view-main-start: ${gradientColor};
  46. --view-main-over: ${themeColor};
  47. --view-op-point-four: ${hexToRgba(themeColor, 0.04)};
  48. --view-op-point-eight: ${hexToRgba(themeColor, 0.8)};
  49. --view-linear: linear-gradient(180deg, ${hexToRgba(
  50. themeColor,
  51. 0.2,
  52. )} 0%, rgba(255,255,255,0) 100%);
  53. --view-gradient: ${gradientColor};
  54. `;
  55. uni.setStorageSync("viewColor", selectedTheme);
  56. uni.$emit("ok", selectedTheme);
  57. }
  58. }
  59. /**
  60. * 获取并应用主题
  61. * @param {Number|String} themeId 主题ID
  62. */
  63. export function applyTheme(themeId) {
  64. let data = {};
  65. if (themeId) data.theme_id = themeId;
  66. return getThemeInfo("theme", data).then((res) => {
  67. uni.setStorageSync("is_diy", 1);
  68. uni.$emit("is_diy", 1);
  69. setThemeColor(res.data);
  70. return res.data;
  71. });
  72. }