Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 重构主题色配置功能 #110

Merged
merged 3 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/My/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const options = computed(() => {
const data = [
[
{ title: "绑定", url: "/pages/bind/index", badge: notificationState.my.bind },
{ title: "实验室", url: "/pages/lab/index" }
{ title: "主题", url: "/pages/lab/index" }
],
[
{ title: "反馈", url: "/pages/connect/index" },
Expand Down
10 changes: 9 additions & 1 deletion src/components/ThemeConfig/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@
background-image: url('~@/assets/photos/background-pink.svg');
}

.theme.purple {
--wjh-color-primary-light: var(--wjh-color-purple-500);
--wjh-color-primary: var(--wjh-color-purple-600);
--wjh-color-primary-dark: var(--wjh-color-purple-700);

background-image: url('~@/assets/photos/background-purple.svg');
}

.background {
position: fixed;
display: flex;
Expand All @@ -41,4 +49,4 @@
background-repeat: no-repeat;
background-color: var(--wjh-color-background-page);
color: var(--wjh-color-text);
}
}
3 changes: 3 additions & 0 deletions src/page.config.ts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

page.config.js 不是放在 src 目录下的呀。它是放在每个页面的文件夹下,应该是用来替换 index.config.ts

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default definePageConfig({
enablePageMeta: true,
});
91 changes: 64 additions & 27 deletions src/pages/lab/index.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<theme-config class="lab-view background">
<title-bar title="实验室" back-button />
<title-bar title="主题" back-button />
<scroll-view :scroll-y="true">
<view class="flex-column">
<card class="lab-card">
Expand All @@ -12,30 +12,13 @@
</view>
<view class="theme-config">
<view class="theme-config-title">主题色彩</view>
<view class="tab-bar">
<view class="tab-bar" >
<text
v-for="item in hadThemeList"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里有类型错误,hadThemeList 的类型不是 ArrayLike @xixiIBN5100

class="tab"
:class="currentTab === 'green' ? 'active' : undefined"
@tap="() => handleTabClick('green')"
> 绿
</text>
<text
class="tab"
:class="currentTab === 'yellow' ? 'active' : undefined"
@tap="() => handleTabClick('yellow')"
> 黄
</text>
<text
class="tab"
:class="currentTab === 'blue' ? 'active' : undefined"
@tap="() => handleTabClick('blue')"
> 蓝
</text>
<text
class="tab"
:class="currentTab === 'pink' ? 'active' : undefined"
@tap="() => handleTabClick('pink')"
> 粉
:class="currentTab === item.name ? 'active' : undefined"
@tap="() => handleTabClick(item.name)"
> {{ nameMap[item.name] }}
</text>
</view>
</view>
Expand All @@ -61,31 +44,85 @@ import { Card, TitleBar, ThemeConfig } from "@/components";
import DarkModeToggle from "./features/DarkModeToggle.vue";
import { labText } from "@/constants/copywriting";
import { getCopyRight } from "@/utils";
import { ref, computed,watch } from "vue";
import {ref, computed, watch, onMounted} from "vue";
import { serviceStore } from "@/store";
import store from "@/store";
import "./index.scss";
import {useRequest} from "@/hooks";
import {UserService} from "@/services";
import Taro from "@tarojs/taro";

let activeTheme = ''
let idMap = {}
let nameMap = {
green: "绿",
yellow: "黄",
blue: "蓝",
purple: "紫",
pink: "粉"
}
const isEmpty = ref(false);

const emptyText = computed(() => {
return labText.empty;
});

const hadThemeList = computed(() => {
return serviceStore.theme.hadTheme;
});
const themeMode = ref(serviceStore.theme.themeMode);
const currentTab = ref(themeMode);


useRequest(UserService.getUserTheme, {
manual: false,
onSuccess: (res) => {
if(res.data.code === 1 && res.data.msg === "OK") {
store.commit("setHadTheme", res.data.data.theme_list);
res.data.data.theme_list.forEach((item:any) => {
idMap[item.name] = item.id;
})
} else {
Taro.showToast({
icon: "none",
title: res.data.msg
});
}
},
onError: (e:Error) =>{
return `失败\r\n${e.message || "网络错误"}`;
}
})

const { run } = useRequest(UserService.setTheme, {
manual: true,
onSuccess: (res) => {
if(res.data.code === 1 && res.data.msg === "OK") {
store.commit("setThemeMode", currentTab);
currentTab.value = activeTheme;
Taro.showToast({ title: "设置成功" });
} else {
Taro.showToast({
icon: "none",
title: res.data.msg
});
}
},
onError: (e:Error) =>{
return `失败\r\n${e.message || "网络错误"}`;
}
})

watch(() => serviceStore.theme.themeMode, (newValue) => {
currentTab.value = newValue;
themeMode.value = newValue;
});

const setThemeMode = (currentTab: string) => {
store.commit("setThemeMode", currentTab);
run({ id:idMap[currentTab]} )
};

const handleTabClick = (theme: string) => {
currentTab.value = theme;
setThemeMode(theme);
activeTheme = theme;
};
</script>
4 changes: 4 additions & 0 deletions src/services/api/apiList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const api = {
login: "/api/user/bind/yxy/login",
}
},
theme: {
get: "/api/func/theme/get",
set: "/api/func/theme/choose",
},
logout: "/api/user/del",
changePassword: "/api/user/repass",
},
Expand Down
33 changes: 32 additions & 1 deletion src/services/services/userService.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,44 @@
import Taro from "@tarojs/taro";
import store, {serviceStore} from "@/store";
import { fetch, FetchResult } from "@/utils";
import { api } from "../api/apiList";
import { api } from "@/services";
import { updateDateStateWithSession } from "../utils/updateDateState";
import errCodeHandler from "../utils/errHandler";
import { ServerCode } from "../api/codes";
import request from "../request";

export default class UserService {
static getUserTheme = () => {
return request<{
code: number,
msg: string,
theme_list: {
id?: number;
name?: string;
theme_config?: string;
type: string;
}[]
}>(
api.user.theme.get, {
method: "GET",
header: { "Cookie": serviceStore.sessionID },
}
);
}

static setTheme = (data: { id : number }) => {
return request<{
code: number,
msg: string,
data: null,
}>(
api.user.theme.set, {
method: "POST",
header: { "Cookie": serviceStore.sessionID },
data
}
);
}
static logout = (data?: { iid: string, stuid: string }) => {
return request<{
code: number,
Expand Down
11 changes: 11 additions & 0 deletions src/store/service/theme.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import { DarkModeTheme, DarkModeOption } from "@/types/DarkMode";
interface ThemeList {
id?: number;
name?: string;
theme_config?: string;
type: string;
}

export interface ThemeStoreType {
hadTheme: ThemeList
themeMode: string;
darkMode: DarkModeOption
}

export const ThemeStore = {
state: {
hadTheme: [],
themeMode: "green",
darkMode: {
mode: "light",
isAdapted: false
}
},
mutations: {
setHadTheme(state: ThemeStoreType, value: ThemeList) {
state.hadTheme = value;
},
setThemeMode(state: ThemeStoreType, value: string) {
state.themeMode = value;
},
Expand Down
3 changes: 3 additions & 0 deletions src/style/darkmode/light.scss
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@
--wjh-color-red-700: #{$red-700};
--wjh-color-red-800: #{$red-800};

--wjh-color-purple-500: #{$purple-500};
--wjh-color-purple-600: #{$purple-600};
--wjh-color-purple-700: #{$purple-700};


--wjh-color-pink-500: #{$pink-500};
--wjh-color-pink-600: #{$pink-600};
Expand Down
7 changes: 6 additions & 1 deletion src/style/variables/color.scss
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,14 @@ $red-dark-600: #da8484;
$red-dark-700: #e8a8a8;
$red-dark-800: #e8c1c0;

$purple-600: #8191cf;
$purple-500: #d2d7f6;
$purple-600: #8f9de1;
$purple-700: #6b76ad;

$purple-dark-500: #afbdf5;
$purple-dark-600: #8191cf;
$purple-dark-700: #6379d3;


$pink-500: #f9e5eb;
$pink-600: #fdb1c7;
Expand Down
Loading