/** * 交易对配置控制 */ import { BehaviorSubject } from "rxjs"; import { InstrumentProps, IFormatCurrentPair, QueryLeverageProps, PostLeverageProps } from "@shared/types/interface"; import { TradeTypeEnum } from "@shared/types"; import { ifElse, forEach, filter, concat } from "ramda"; import { formatPair } from "../utils/formatPair"; import { LocalStore } from "@utils/localStore"; import { getCurrentPairForOriginName, getCurrentPair } from "../utils/formatRouter"; import { PlaceOrderService } from "./placeOrderService"; export class PairsService { static _instance: PairsService; // 总配置 pairs$ = new BehaviorSubject([]); hadConfig$ = new BehaviorSubject(false); // 自选 likes$ = new BehaviorSubject([]); // 业务线 tradeType$ = new BehaviorSubject(TradeTypeEnum.Linear); // 当前交易对 currentPairs$ = new BehaviorSubject(null); static get instance(): PairsService { if (PairsService._instance) { return PairsService._instance; } PairsService._instance = new PairsService(); return PairsService._instance; } setLocalPairs(list: IFormatCurrentPair[]) { const type = this.tradeType$.value; // 本地缓存交易对 const str = JSON.stringify(list); LocalStore.set(type, str); } init(instruments: any[]) { // 交易对配置 if (instruments?.length) { const arr: IFormatCurrentPair[] = []; forEach(item => { arr.push(formatPair(item)); }, instruments as InstrumentProps[]); this.setLocalPairs(arr); this.pairs$.next(arr); } } getLocalPairs() { const type = this.tradeType$.value; // 读取本地的缓存交易对 const pairsStr = LocalStore.get(type); if (pairsStr) { try { const pairs = JSON.parse(pairsStr); if (pairs?.length) { this.init(pairs); } } catch (e) {} } } initLikes(list: string[]) { // 初始化查询/本地缓存的自选 if (list?.length) { this.likes$.next(list); } } addOrRemoveLikes(symbol: string, add: boolean) { if (symbol && typeof symbol === "string") { // Todo添加/移除自选调用api const values = [...this.likes$.value]; const allLikes = ifElse( (isAdd: boolean) => isAdd, () => concat(values, [symbol]), () => filter(item => item !== symbol, values) )(add); this.likes$.next(allLikes); } } initCurrentPair(originName: string) { // 初始化当前交易对 const tradeType = this.tradeType$.value; const list = this.pairs$.value; if (tradeType && list?.length) { const symbol = getCurrentPairForOriginName(tradeType, originName); let current = getCurrentPair({ symbol: symbol, tradeType, list: list }); if (!current) { current = list[0]; // todo处理路由 } this.currentPairs$.next(current); this.updateDefaultLeverage(current?.leverage); } } updateDefaultLeverage = (leverage: any) => { // 更新当前默认杠杆 PlaceOrderService.instance.updateLeverage(leverage); }; switchRouter(symbol: string, tradeType: TradeTypeEnum) { // 路由跳转 const current = getCurrentPair({ symbol, tradeType, list: this.pairs$.value }); if (current) { const tradeTypeValue = this.tradeType$.value; if (tradeType !== tradeTypeValue) { // todo路由跳转 // const preRouter = OmitTradeTypeEnum[tradeType]; // window.location.href = ""; // return; } // 路由替换 // const url = `/${tradeTypeValue}/${symbol}`; // window.history.replaceState({ t: Date.now() }, url); this.currentPairs$.next(current); this.updateDefaultLeverage(current?.leverage); } } getLeverageConfig({ symbol, tradeType, marginType }: QueryLeverageProps) { console.log("获取杠杆", symbol, tradeType, marginType); } postLeverage(props: PostLeverageProps) { console.log(props, "提交杠杆"); } }