setting.ts 4.61 KB
Newer Older
陈小聪's avatar
陈小聪 committed
1
import { Reducer } from 'redux';
duanledexianxianxian's avatar
duanledexianxianxian committed
2 3
// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable promise/catch-or-return */
陈帅's avatar
陈帅 committed
4
import { message } from 'antd';
陈小聪's avatar
陈小聪 committed
5
import defaultSettings, { DefaultSettings } from '../../config/defaultSettings';
6
import themeColorClient from '../components/SettingDrawer/themeColorClient';
duanledexianxianxian's avatar
duanledexianxianxian committed
7
import { getCurrentUserSetting, saveCurrentUserSetting } from '@/services/user';
duanledexianxianxian's avatar
duanledexianxianxian committed
8
import { Effect } from 'dva';
陈帅's avatar
陈帅 committed
9

duanledexianxianxian's avatar
duanledexianxianxian committed
10 11 12 13 14
export interface UserSetting extends DefaultSettings {
  settingId?: number;
  userId?: number;
}

陈小聪's avatar
陈小聪 committed
15
export interface SettingModelType {
陈帅's avatar
陈帅 committed
16
  namespace: 'settings';
duanledexianxianxian's avatar
duanledexianxianxian committed
17
  state: UserSetting;
duanledexianxianxian's avatar
duanledexianxianxian committed
18 19 20
  effects: {
    getSettings: Effect;
  };
陈小聪's avatar
陈小聪 committed
21
  reducers: {
duanledexianxianxian's avatar
duanledexianxianxian committed
22 23
    getSetting: Reducer<UserSetting>;
    changeSetting: Reducer<UserSetting>;
陈小聪's avatar
陈小聪 committed
24 25
  };
}
26

huangzheng's avatar
lint  
huangzheng committed
27
const updateTheme = (newPrimaryColor?: string) => {
28 29 30 31 32
  if (newPrimaryColor) {
    const timeOut = 0;
    const hideMessage = message.loading('正在切换主题!', timeOut);
    themeColorClient.changeColor(newPrimaryColor).finally(() => hideMessage());
  }
33
};
陈帅's avatar
陈帅 committed
34
const updateColorWeak: (colorWeak: boolean) => void = colorWeak => {
陈帅's avatar
陈帅 committed
35 36 37 38
  const root = document.getElementById('root');
  if (root) {
    root.className = colorWeak ? 'colorWeak' : '';
  }
afc163's avatar
afc163 committed
39 40
};

陈小聪's avatar
陈小聪 committed
41
const SettingModel: SettingModelType = {
陈帅's avatar
陈帅 committed
42
  namespace: 'settings',
afc163's avatar
afc163 committed
43
  state: defaultSettings,
duanledexianxianxian's avatar
duanledexianxianxian committed
44
  effects: {
duanledexianxianxian's avatar
duanledexianxianxian committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    *saveSettings({ payload }, { call, put }) {
      const {
        navTheme,
        primaryColor,
        layout,
        contentWidth,
        fixedHeader,
        autoHideHeader,
        fixSiderbar,
        userId,
        settingId,
      } = payload;
      yield call(saveCurrentUserSetting, {
        userId,
        settingId,
        appStyle: navTheme === 'light' ? 0 : 1,
        appTheme: primaryColor,
        navigatorStyle: layout === 'sidemenu' ? 0 : 1,
        contentWidth: navTheme === 'Fluid' ? 0 : 1,
        fixedHead: fixedHeader ? 1 : 0,
        fixedSide: fixSiderbar ? 1 : 0,
        hideHead: autoHideHeader ? 1 : 0,
      });
    },
duanledexianxianxian's avatar
duanledexianxianxian committed
69 70 71
    *getSettings({ payload }, { call, put }) {
      const { data, code } = yield call(getCurrentUserSetting, payload);
      let settings = defaultSettings;
duanledexianxianxian's avatar
duanledexianxianxian committed
72
      console.log(data);
duanledexianxianxian's avatar
duanledexianxianxian committed
73 74 75 76 77 78 79 80 81 82 83 84
      if (code === 'sys.success' && data) {
        const {
          appStyle,
          appTheme,
          navigatorStyle,
          contentWidth,
          fixedHead,
          fixedSide,
          hideHead,
        } = data;
        settings = {
          ...settings,
duanledexianxianxian's avatar
duanledexianxianxian committed
85
          ...data,
duanledexianxianxian's avatar
duanledexianxianxian committed
86 87 88 89
          navTheme: appStyle === 0 ? 'light' : 'dark',
          primaryColor: appTheme,
          layout: navigatorStyle === 0 ? 'sidemenu' : 'topmenu',
          contentWidth: contentWidth === 0 ? 'Fluid' : 'Fixed',
duanledexianxianxian's avatar
duanledexianxianxian committed
90 91 92
          fixedHeader: fixedHead === 1,
          autoHideHeader: hideHead === 1,
          fixSiderbar: fixedSide === 1,
duanledexianxianxian's avatar
duanledexianxianxian committed
93 94 95 96 97 98 99 100
        };
      }
      yield put({
        type: 'saveSettings',
        payload: settings,
      });
    },
  },
jim's avatar
jim committed
101
  reducers: {
duanledexianxianxian's avatar
duanledexianxianxian committed
102
    saveSettings(state = defaultSettings, { payload }) {
陈帅's avatar
陈帅 committed
103
      const setting: Partial<DefaultSettings> = {};
jim's avatar
jim committed
104 105 106 107
      const urlParams = new URL(window.location.href);
      Object.keys(state).forEach(key => {
        if (urlParams.searchParams.has(key)) {
          const value = urlParams.searchParams.get(key);
jim's avatar
jim committed
108
          setting[key] = value === '1' ? true : value;
jim's avatar
jim committed
109 110
        }
      });
afc163's avatar
afc163 committed
111
      const { primaryColor, colorWeak } = setting;
陈帅's avatar
陈帅 committed
112

拷钉's avatar
拷钉 committed
113
      if (primaryColor && state.primaryColor !== primaryColor) {
afc163's avatar
afc163 committed
114
        updateTheme(primaryColor);
afc163's avatar
afc163 committed
115
      }
陈帅's avatar
陈帅 committed
116
      updateColorWeak(!!colorWeak);
jim's avatar
jim committed
117 118 119
      return {
        ...state,
        ...setting,
duanledexianxianxian's avatar
duanledexianxianxian committed
120
        ...payload,
jim's avatar
jim committed
121
      };
jim's avatar
jim committed
122
    },
陈帅's avatar
陈帅 committed
123
    changeSetting(state = defaultSettings, { payload }) {
jim's avatar
jim committed
124
      const urlParams = new URL(window.location.href);
duanledexianxianxian's avatar
duanledexianxianxian committed
125

afc163's avatar
afc163 committed
126
      Object.keys(defaultSettings).forEach(key => {
jim's avatar
jim committed
127 128 129 130
        if (urlParams.searchParams.has(key)) {
          urlParams.searchParams.delete(key);
        }
      });
jim's avatar
jim committed
131 132 133
      Object.keys(payload).forEach(key => {
        if (key === 'collapse') {
          return;
jim's avatar
jim committed
134
        }
jim's avatar
jim committed
135 136 137 138
        let value = payload[key];
        if (value === true) {
          value = 1;
        }
afc163's avatar
afc163 committed
139
        if (defaultSettings[key] !== value) {
jim's avatar
jim committed
140 141
          urlParams.searchParams.set(key, value);
        }
jim's avatar
jim committed
142
      });
143
      const { primaryColor, colorWeak, contentWidth } = payload;
拷钉's avatar
拷钉 committed
144
      if (primaryColor && state.primaryColor !== primaryColor) {
afc163's avatar
afc163 committed
145
        updateTheme(primaryColor);
afc163's avatar
afc163 committed
146
      }
147 148
      if (state.contentWidth !== contentWidth && window.dispatchEvent) {
        window.dispatchEvent(new Event('resize'));
149
      }
拷钉's avatar
拷钉 committed
150
      updateColorWeak(!!colorWeak);
jim's avatar
jim committed
151 152 153 154 155 156 157 158
      window.history.replaceState(null, 'setting', urlParams.href);
      return {
        ...state,
        ...payload,
      };
    },
  },
};
陈小聪's avatar
陈小聪 committed
159
export default SettingModel;