setting.ts 5.71 KB
Newer Older
陈小聪's avatar
陈小聪 committed
1
import { Reducer } from 'redux';
陈帅's avatar
陈帅 committed
2
import { message } from 'antd';
陈小聪's avatar
陈小聪 committed
3
import defaultSettings, { DefaultSettings } from '../../config/defaultSettings';
4
import themeColorClient from '../components/SettingDrawer/themeColorClient';
duanledexianxianxian's avatar
duanledexianxianxian committed
5 6
import { getCurrentUserSetting } from '@/services/user';
import { Effect } from 'dva';
陈帅's avatar
陈帅 committed
7

陈小聪's avatar
陈小聪 committed
8
export interface SettingModelType {
陈帅's avatar
陈帅 committed
9
  namespace: 'settings';
陈小聪's avatar
陈小聪 committed
10
  state: DefaultSettings;
duanledexianxianxian's avatar
duanledexianxianxian committed
11 12 13
  effects: {
    getSettings: Effect;
  };
陈小聪's avatar
陈小聪 committed
14
  reducers: {
duanledexianxianxian's avatar
duanledexianxianxian committed
15
    saveSettings: Reducer<DefaultSettings>;
陈帅's avatar
陈帅 committed
16 17
    getSetting: Reducer<DefaultSettings>;
    changeSetting: Reducer<DefaultSettings>;
陈小聪's avatar
陈小聪 committed
18 19
  };
}
20

huangzheng's avatar
lint  
huangzheng committed
21
const updateTheme = (newPrimaryColor?: string) => {
22
  if (newPrimaryColor) {
duanledexianxianxian's avatar
duanledexianxianxian committed
23
    console.log(newPrimaryColor);
24 25 26 27
    const timeOut = 0;
    const hideMessage = message.loading('正在切换主题!', timeOut);
    themeColorClient.changeColor(newPrimaryColor).finally(() => hideMessage());
  }
28
};
29

30
/*
陈小聪's avatar
陈小聪 committed
31 32 33
let lessNodesAppended: boolean;

const updateTheme: (primaryColor?: string) => void = primaryColor => {
kennylbj's avatar
kennylbj committed
34
  // Don't compile less in production!
陈帅's avatar
陈帅 committed
35 36
  // preview.pro.ant.design only do not use in your production;
  // preview.pro.ant.design 专用环境变量,请不要在你的项目中使用它。
37
  if (ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION !== 'site') {
afc163's avatar
afc163 committed
38 39
    return;
  }
40
  // Determine if the component is remounted
afc163's avatar
afc163 committed
41
  if (!primaryColor) {
afc163's avatar
afc163 committed
42
    return;
43
  }
afc163's avatar
afc163 committed
44
  const hideMessage = message.loading('正在编译主题!', 0);
愚道's avatar
愚道 committed
45
  function buildIt() {
陈小聪's avatar
陈小聪 committed
46
    if (!(window as any).less) {
陈帅's avatar
陈帅 committed
47 48
      console.log('no less');
      return;
愚道's avatar
愚道 committed
49 50
    }
    setTimeout(() => {
陈小聪's avatar
陈小聪 committed
51
      (window as any).less
愚道's avatar
愚道 committed
52 53 54 55 56
        .modifyVars({
          '@primary-color': primaryColor,
        })
        .then(() => {
          hideMessage();
陈帅's avatar
陈帅 committed
57
          return true;
愚道's avatar
愚道 committed
58 59 60 61 62 63 64
        })
        .catch(() => {
          message.error('Failed to update theme');
          hideMessage();
        });
    }, 200);
  }
afc163's avatar
afc163 committed
65
  if (!lessNodesAppended) {
66
    // insert less.js and color.less
afc163's avatar
afc163 committed
67 68 69 70 71 72 73 74
    const lessStyleNode = document.createElement('link');
    const lessConfigNode = document.createElement('script');
    const lessScriptNode = document.createElement('script');
    lessStyleNode.setAttribute('rel', 'stylesheet/less');
    lessStyleNode.setAttribute('href', '/color.less');
    lessConfigNode.innerHTML = `
      window.less = {
        async: true,
afc163's avatar
afc163 committed
75 76
        env: 'production',
        javascriptEnabled: true
afc163's avatar
afc163 committed
77 78
      };
    `;
afc163's avatar
afc163 committed
79
    lessScriptNode.src = 'https://gw.alipayobjects.com/os/lib/less.js/3.8.1/less.min.js';
afc163's avatar
afc163 committed
80 81 82 83 84
    lessScriptNode.async = true;
    lessScriptNode.onload = () => {
      buildIt();
      lessScriptNode.onload = null;
    };
afc163's avatar
afc163 committed
85 86 87
    document.body.appendChild(lessStyleNode);
    document.body.appendChild(lessConfigNode);
    document.body.appendChild(lessScriptNode);
afc163's avatar
afc163 committed
88
    lessNodesAppended = true;
89
  } else {
afc163's avatar
afc163 committed
90 91
    buildIt();
  }
92
};
93
*/
94

陈帅's avatar
陈帅 committed
95
const updateColorWeak: (colorWeak: boolean) => void = colorWeak => {
陈帅's avatar
陈帅 committed
96 97 98 99
  const root = document.getElementById('root');
  if (root) {
    root.className = colorWeak ? 'colorWeak' : '';
  }
afc163's avatar
afc163 committed
100 101
};

陈小聪's avatar
陈小聪 committed
102
const SettingModel: SettingModelType = {
陈帅's avatar
陈帅 committed
103
  namespace: 'settings',
afc163's avatar
afc163 committed
104
  state: defaultSettings,
duanledexianxianxian's avatar
duanledexianxianxian committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
  effects: {
    *getSettings({ payload }, { call, put }) {
      const { data, code } = yield call(getCurrentUserSetting, payload);
      let settings = defaultSettings;
      if (code === 'sys.success' && data) {
        const {
          appStyle,
          appTheme,
          navigatorStyle,
          contentWidth,
          fixedHead,
          fixedSide,
          hideHead,
        } = data;
        settings = {
          ...settings,
          navTheme: appStyle === 0 ? 'light' : 'dark',
          primaryColor: appTheme,
          layout: navigatorStyle === 0 ? 'sidemenu' : 'topmenu',
          contentWidth: contentWidth === 0 ? 'Fluid' : 'Fixed',
duanledexianxianxian's avatar
duanledexianxianxian committed
125 126 127
          fixedHeader: fixedHead === 1,
          autoHideHeader: hideHead === 1,
          fixSiderbar: fixedSide === 1,
duanledexianxianxian's avatar
duanledexianxianxian committed
128 129 130 131 132 133 134 135
        };
      }
      yield put({
        type: 'saveSettings',
        payload: settings,
      });
    },
  },
jim's avatar
jim committed
136
  reducers: {
duanledexianxianxian's avatar
duanledexianxianxian committed
137 138 139 140 141 142
    saveSettings(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
陈帅's avatar
陈帅 committed
143 144
    getSetting(state = defaultSettings) {
      const setting: Partial<DefaultSettings> = {};
jim's avatar
jim committed
145 146 147 148
      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
149
          setting[key] = value === '1' ? true : value;
jim's avatar
jim committed
150 151
        }
      });
afc163's avatar
afc163 committed
152
      const { primaryColor, colorWeak } = setting;
陈帅's avatar
陈帅 committed
153

拷钉's avatar
拷钉 committed
154
      if (primaryColor && state.primaryColor !== primaryColor) {
afc163's avatar
afc163 committed
155
        updateTheme(primaryColor);
afc163's avatar
afc163 committed
156
      }
陈帅's avatar
陈帅 committed
157
      updateColorWeak(!!colorWeak);
jim's avatar
jim committed
158 159 160 161
      return {
        ...state,
        ...setting,
      };
jim's avatar
jim committed
162
    },
陈帅's avatar
陈帅 committed
163
    changeSetting(state = defaultSettings, { payload }) {
jim's avatar
jim committed
164
      const urlParams = new URL(window.location.href);
afc163's avatar
afc163 committed
165
      Object.keys(defaultSettings).forEach(key => {
jim's avatar
jim committed
166 167 168 169
        if (urlParams.searchParams.has(key)) {
          urlParams.searchParams.delete(key);
        }
      });
jim's avatar
jim committed
170 171 172
      Object.keys(payload).forEach(key => {
        if (key === 'collapse') {
          return;
jim's avatar
jim committed
173
        }
jim's avatar
jim committed
174 175 176 177
        let value = payload[key];
        if (value === true) {
          value = 1;
        }
afc163's avatar
afc163 committed
178
        if (defaultSettings[key] !== value) {
jim's avatar
jim committed
179 180
          urlParams.searchParams.set(key, value);
        }
jim's avatar
jim committed
181
      });
182
      const { primaryColor, colorWeak, contentWidth } = payload;
拷钉's avatar
拷钉 committed
183
      if (primaryColor && state.primaryColor !== primaryColor) {
afc163's avatar
afc163 committed
184
        updateTheme(primaryColor);
afc163's avatar
afc163 committed
185
      }
186 187
      if (state.contentWidth !== contentWidth && window.dispatchEvent) {
        window.dispatchEvent(new Event('resize'));
188
      }
拷钉's avatar
拷钉 committed
189
      updateColorWeak(!!colorWeak);
jim's avatar
jim committed
190 191 192 193 194 195 196 197
      window.history.replaceState(null, 'setting', urlParams.href);
      return {
        ...state,
        ...payload,
      };
    },
  },
};
陈小聪's avatar
陈小聪 committed
198
export default SettingModel;