Reinit project

This commit is contained in:
Limo Saplf
2019-08-31 20:44:15 +08:00
commit b51ae824c7
115 changed files with 20955 additions and 0 deletions

86
src/utils/http.ts Normal file
View File

@@ -0,0 +1,86 @@
import axios, { AxiosRequestConfig, AxiosResponse, AxiosInstance } from 'axios';
import { formatMessage } from 'umi-plugin-locale';
import _ from 'lodash';
import { globalState } from '@/services/common';
import { isDev } from './utils';
export interface AppResp<T = any> {
ok: boolean,
code: number;
msg: string;
data?: T;
}
const successLabel = 'color:white;background-color:green;padding: 1px 2px';
const failureLabel = 'color:white;background-color:red;padding: 1px 2px';
const originLabel = 'color:unset;background-color:unset;padding:unset';
const baseURL = 'http://yapi.faronear.org/mock/35';
const instance = axios.create({
baseURL,
});
async function filter<T>(
method: keyof AxiosInstance,
...args: any[]
): Promise<AppResp<T>> {
let ret;
try {
const { user } = globalState().user;
if (user && user.token) {
instance.defaults.headers = {
...instance(instance.defaults.headers),
Authorization: user.token,
};
} else {
instance.defaults.headers = _.omit(instance.defaults.headers, 'Authorization');
}
const { status, data } = await (instance[method] as (...args: any[]) => Promise<AxiosResponse<AppResp>>)(...args);
ret = {
ok: status >= 200 && status < 300 && data && data.code === 0,
code: data && data.code,
msg: data && data.msg,
data: data && data.data,
};
} catch (e) {
ret = {
ok: false,
msg: formatMessage({ id: 'network.error' }),
code: 404,
};
}
if (isDev) {
/* tslint:disable */
console.group(`%c${method.toUpperCase()}%c: ${baseURL}${args[0]}`, ret.ok ? successLabel : failureLabel, originLabel);
console.log(ret);
console.groupEnd();
/* tslint:enable */
}
return ret;
}
function groupUrl(url: string, params?: any): string {
const postfix = _.keys(params || {})
.map(key => `${key}=${encodeURIComponent(params[key]) || ''}`)
.join('&');
if (!postfix) return url;
return `${url}?${postfix}`;
}
function get<T = any>(url: string, params?: any, config?: AxiosRequestConfig) {
return filter<T>('get', groupUrl(url, params), config);
}
function post<T = any>(url: string, data?: any, config?: AxiosRequestConfig) {
return filter<T>('post', url, data, config);
}
export default {
get,
post,
};
export {
get,
post,
};

42
src/utils/storage.ts Normal file
View File

@@ -0,0 +1,42 @@
const userKey = 'user-key';
const areaCodeKey = 'area-code-key';
function storeUser(user: any = undefined) {
if (user === undefined) {
localStorage.removeItem(userKey);
} else {
localStorage.setItem(userKey, JSON.stringify(user));
}
}
function loadUser() {
try {
return JSON.parse(localStorage.getItem(userKey) || '{}');
} catch(e) {
return {};
}
}
const clearUser = storeUser;
function storeAreaCode(codes: any) {
localStorage.setItem(areaCodeKey, JSON.stringify(codes));
}
function loadAreaCode() {
try {
return JSON.parse(localStorage.getItem(areaCodeKey) || 'null');
} catch(e) {
return null;
}
}
export {
storeUser,
loadUser,
clearUser,
storeAreaCode,
loadAreaCode,
}

43
src/utils/transform.ts Normal file
View File

@@ -0,0 +1,43 @@
import BigNumber from 'bignumber.js';
import _ from 'lodash';
import { AreaCode, Estate } from '@/types/common';
export function encodePhone(code: AreaCode, phone: string): string {
if (code && code.areacode) return `+${code.areacode}-${phone || ''}`;
return phone;
}
export function showRate(value: BigNumber) {
return `${value.times(100).decimalPlaces(2)}%`;
}
export function calcEstate(it: Estate) {
const profit = new BigNumber(it.profit || '0');
const taxfee = new BigNumber(it.taxfee || '0');
const inPrice = new BigNumber(it.inPrice || '0');
it.profitShow = showRate(profit);
it.taxfeeShow = showRate(taxfee);
const taxfeeAmount = inPrice.times(taxfee).decimalPlaces(2);
it.taxfeeAmount = taxfeeAmount.toString();
const outPrice = inPrice.times(profit.plus(1)).decimalPlaces(2);
it.outPrice = outPrice.toString();
it.profitPrice = outPrice.minus(inPrice).minus(taxfeeAmount).toString();
}
export const supportedLangs = [
{
lang: 'zh-CN',
text: '中文',
},
{
lang: 'en-US',
text: 'English',
},
];
export function fromLangToText(lang: string) {
const target = _.find(supportedLangs, { lang });
return target && target.text;
}

15
src/utils/utils.ts Normal file
View File

@@ -0,0 +1,15 @@
import { any } from 'prop-types';
const isDev = process.env.NODE_ENV === 'development';
interface Action {
type: string;
payload?: any;
[key: string]: any;
}
const dispatch = (action: Action) => window.g_app._store.dispatch(action);
export { isDev, dispatch };
export default { isDev, dispatch };

39
src/utils/validate.ts Normal file
View File

@@ -0,0 +1,39 @@
/**
* 校验是否是合法的手机号
* @param input 输入
*/
function validPhone(input: string | number): boolean {
return /^1\d{10}$/.test(`${input || ''}`);
}
/**
* 校验手机号的输入是否合法(重点是输入过程中)
* @param input 输入
*/
function validPhoneInput(input: string | number): boolean {
return /^(?:1\d*)?$/.test(`${input || ''}`);
}
/**
* 校验是否是合法的密码
* @param input 输入
*/
function validPwd(input: string | number): boolean {
return /^[0-9a-zA-Z]{6,18}$/.test(`${input || ''}`);
}
/**
* 校验密码的输入是否合法(重点是输入过程中)
* @param input 输入
*/
function validPwdInput(input: string | number): boolean {
return /^[0-9a-zA-Z]*$/.test(`${input || ''}`);
}
export {
validPhone,
validPhoneInput,
validPwd,
validPwdInput,
}