setting.ts 5.66 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 23 24 25 26
  if (newPrimaryColor) {
    const timeOut = 0;
    const hideMessage = message.loading('正在切换主题!', timeOut);
    themeColorClient.changeColor(newPrimaryColor).finally(() => hideMessage());
  }
27
};
28

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

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

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

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

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