Skip to content

Commit

Permalink
feat(admin): messagemodal 改为全局对象
Browse files Browse the repository at this point in the history
  • Loading branch information
twinh committed Mar 31, 2024
1 parent 207d1ca commit dcb37f7
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 5 deletions.
26 changes: 21 additions & 5 deletions modules/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import axios from 'axios';
import 'plugins/app/modules/bootstrap';
import { wei } from '@mxjs/app';
import config from 'config';
import getLoginPath from './get-login-path';
import $, { Ret } from 'miaoxing';
import axios from '@mxjs/axios';
import { wei, req, url } from '@mxjs/app';
import { Spin } from 'antd';
import { Loading } from '@mxjs/a-loading';
import config from 'config';
import './mxjs-preset-antd';
import getLoginPath from './get-login-path';

$.http = (...args) => axios(...args).then(res => {
res.ret = new Ret(res.data);
return res;
});

$.req = req.get.bind(req);
$.url = url.to.bind(url);
/**
* @experimental
*/
$.fullUrl = url.full.bind(url);
$.apiUrl = url.api.bind(url);

window.$ = $;

wei.setConfigs(config);

Expand Down
129 changes: 129 additions & 0 deletions modules/mxjs-preset-antd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import $ from 'miaoxing';
import { isValidElement } from 'react';
import { message, modal } from '../components/AntdApp';

const loadingOptions = {
content: '加载中...',
duration: 0,
};

let count = 0;
let loading;

function showLoading() {
count++;
if (count === 1) {
loading = message.loading({content: loadingOptions.content, duration: loadingOptions.duration});
}
}

function hideLoading() {
if (!loading) {
return;
}

if (count > 0) {
count--;
}
if (count === 0) {
loading();
}
}

$.loading = (options) => {
switch (options) {
case 'show':
return showLoading();

case 'hide':
return hideLoading();

default:
if (typeof options === 'string' || isValidElement(options)) {
options = {content: options};
}
return message.loading({...loadingOptions, ...options});
}
};

function confirm(config, type = 'confirm') {
if (typeof config.content === 'undefined') {
config = {content: config};
}

let currentConfig = addPromise({...config, show: true});

let ok;
let cancel;
let callback;
const result = new Promise(resolve => {
callback = resolve;
});
result.ok = fn => {
ok = fn;
return result;
};
result.cancel = fn => {
cancel = fn;
return result;
};

function addPromise(config) {
const onOk = config.onOk;
config.onOk = () => {
callback(true);
const result = ok && ok();
const result2 = onOk && onOk();
return result || result2;
};

const onCancel = config.onCancel;
config.onCancel = () => {
callback(false);
const result = cancel && cancel();
const result2 = onCancel && onCancel();
return result || result2;
};

return config;
}

const confirmResult = modal[type](currentConfig);

result.destroy = confirmResult.destroy;
result.update = confirmResult.update;

return result;
}

$.alert = (message, fn) => confirm(message, 'info').then(fn);
$.confirm = (message, fn) => confirm(message).then(fn);

$.suc = (...args) => message.success(...args);
$.err = (...args) => message.error(...args);
$.ret = (ret, duration, callback) => {
const result = $[ret.code === 0 ? 'suc' : 'err'](ret.message, duration, callback);

let suc;
result.suc = fn => {
suc = fn;
return result;
};

let err;
result.err = fn => {
err = fn;
return result;
};

result.then(() => {
if (ret.code === 0 && suc) {
suc();
}
if (ret.code !== 0 && err) {
err();
}
});

return result;
};

0 comments on commit dcb37f7

Please sign in to comment.