|
|
@@ -2,116 +2,90 @@ import { v4 } from 'uuid'
|
|
|
import { Package50 } from '@/utils/websocket/package'
|
|
|
import { FunCode } from '@/constants/funcode'
|
|
|
import { loginStore, errorInfoStore } from '@/stores'
|
|
|
-import { IMessageHead } from './protobuf/proto'
|
|
|
-import { TradeParams, TradeResponse } from './interface'
|
|
|
+import { TradeResponse, TradeRequestConfig } from './interface'
|
|
|
import cryptojs from 'crypto-js'
|
|
|
import Protobuf from './protobuf'
|
|
|
import socket from '../index'
|
|
|
|
|
|
-/**
|
|
|
- * 构建消息头部
|
|
|
- */
|
|
|
-function getProtoHeader(funCode: keyof typeof FunCode, header?: IMessageHead, marketId?: number) {
|
|
|
- const { userId, firstAccountId } = loginStore.$mapGetters();
|
|
|
- // 组合请求头
|
|
|
- const protoHeader: IMessageHead = {
|
|
|
- FunCode: FunCode[funCode],
|
|
|
- UUID: v4(),
|
|
|
- UserID: userId.value,
|
|
|
- AccountID: firstAccountId.value,
|
|
|
- ...(header ?? {})
|
|
|
- }
|
|
|
- if (marketId) {
|
|
|
- protoHeader.MarketID = marketId;
|
|
|
- }
|
|
|
- return protoHeader;
|
|
|
-}
|
|
|
+export default new (class {
|
|
|
+ /**
|
|
|
+ * 向交易服务器发送请求
|
|
|
+ * @param config
|
|
|
+ * @returns
|
|
|
+ */
|
|
|
+ private send<T>({ params, reqName, rspName, marketId = 0 }: TradeRequestConfig) {
|
|
|
+ const { userId } = loginStore.$mapGetters()
|
|
|
+ const reqCode = FunCode[reqName]
|
|
|
+ const rspCode = FunCode[rspName]
|
|
|
|
|
|
-/**
|
|
|
- * 向交易服务器发送请求
|
|
|
- * @param params 请求参数
|
|
|
- * @param reqKey 请求代码
|
|
|
- * @param rspKey 回调代码
|
|
|
- * @param marketId 市场ID
|
|
|
- */
|
|
|
-function tradeServerMiddleware<Req, Rsp>(reqKey: keyof typeof FunCode, rspKey: keyof typeof FunCode, params: TradeParams<Req, Rsp>, marketId?: number): Promise<Rsp> {
|
|
|
- params.data.Header = getProtoHeader(reqKey, params.data.Header, marketId);
|
|
|
- console.log(reqKey, FunCode[reqKey], params.data);
|
|
|
+ // 默认请求头
|
|
|
+ params.Header = {
|
|
|
+ MarketID: marketId,
|
|
|
+ FunCode: reqCode,
|
|
|
+ UUID: v4(),
|
|
|
+ UserID: userId.value,
|
|
|
+ ...params.Header
|
|
|
+ }
|
|
|
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- Protobuf.requestEncode(reqKey, params.data).then((res) => {
|
|
|
- // 发送消息
|
|
|
- socket.sendTradeServer({
|
|
|
- data: {
|
|
|
- rspCode: FunCode[rspKey],
|
|
|
- payload: new Package50(FunCode[reqKey], res)
|
|
|
- },
|
|
|
- success: (raw) => {
|
|
|
- Protobuf.responseDecode<Rsp>(rspKey, raw.content).then((res) => {
|
|
|
- console.log(rspKey, FunCode[rspKey], res);
|
|
|
- resolve(res);
|
|
|
- }).catch(() => {
|
|
|
- console.error(rspKey, raw);
|
|
|
- reject('报文解析失败');
|
|
|
- })
|
|
|
- },
|
|
|
- fail: (err) => {
|
|
|
- console.error(reqKey, err);
|
|
|
- reject(err);
|
|
|
- }
|
|
|
+ console.log(reqName, reqCode, params)
|
|
|
+
|
|
|
+ return new Promise<T>((resolve, reject) => {
|
|
|
+ Protobuf.requestEncode(reqName, params).then((res) => {
|
|
|
+ // 发送消息
|
|
|
+ socket.sendTradeServer({
|
|
|
+ data: {
|
|
|
+ rspCode: rspCode,
|
|
|
+ payload: new Package50(reqCode, res)
|
|
|
+ },
|
|
|
+ success: (raw) => {
|
|
|
+ Protobuf.responseDecode<T>(rspName, raw.content).then((res) => {
|
|
|
+ console.log(rspName, rspCode, res)
|
|
|
+ resolve(res)
|
|
|
+ }).catch(() => {
|
|
|
+ console.error(rspName, raw)
|
|
|
+ reject('报文解析失败')
|
|
|
+ })
|
|
|
+ },
|
|
|
+ fail: (err) => {
|
|
|
+ console.error(reqName, err)
|
|
|
+ reject(err)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }).catch((msg) => {
|
|
|
+ console.error(reqName, msg)
|
|
|
+ reject('报文构建失败')
|
|
|
})
|
|
|
- }).catch((msg) => {
|
|
|
- console.error(reqKey, msg);
|
|
|
- reject('报文构建失败');
|
|
|
})
|
|
|
- })
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * 向交易服务器发送请求
|
|
|
- * @param reqKey
|
|
|
- * @param rspKey
|
|
|
- * @param params
|
|
|
- * @param marketId
|
|
|
- */
|
|
|
-export async function tradeServerRequest<Req, Rsp>(reqKey: keyof typeof FunCode, rspKey: keyof typeof FunCode, params: TradeParams<Req, Rsp & TradeResponse>, marketId?: number) {
|
|
|
- const { success, fail, complete } = params;
|
|
|
+ }
|
|
|
|
|
|
- return await tradeServerMiddleware(reqKey, rspKey, params, marketId).then((res) => {
|
|
|
- const { RetCode, Status, RetDesc } = { ...res };
|
|
|
- switch (RetCode) {
|
|
|
+ /**
|
|
|
+ * 向交易服务器发送请求
|
|
|
+ * @param config
|
|
|
+ * @returns
|
|
|
+ */
|
|
|
+ async sendMessage<T extends TradeResponse>(config: TradeRequestConfig) {
|
|
|
+ const res = await this.send<T>(config)
|
|
|
+ switch (res.RetCode) {
|
|
|
case 0: {
|
|
|
- success && success(res);
|
|
|
- return Promise.resolve(res);
|
|
|
- }
|
|
|
- case -1: {
|
|
|
- // 管理端错误消息
|
|
|
- return Promise.reject(RetDesc);
|
|
|
+ return Promise.resolve(res)
|
|
|
}
|
|
|
case 12018: {
|
|
|
- if (RetDesc) {
|
|
|
+ if (res.RetDesc) {
|
|
|
const { Utf8, Base64 } = cryptojs.enc
|
|
|
- const word = Base64.parse(RetDesc)
|
|
|
- // 解析base64
|
|
|
+ const word = Base64.parse(res.RetDesc) // 解析base64
|
|
|
res.RetDesc = Utf8.stringify(word)
|
|
|
}
|
|
|
- return Promise.reject(res.RetDesc);
|
|
|
+ return Promise.reject(res.RetDesc)
|
|
|
}
|
|
|
default: {
|
|
|
// 银行 业务 以 Status 作为判断依据
|
|
|
- if (Status === 0) {
|
|
|
- success && success(res);
|
|
|
- return Promise.resolve(res);
|
|
|
+ if (res.Status === 0 || res.Status == 6007) {
|
|
|
+ return Promise.resolve(res)
|
|
|
}
|
|
|
- const msg = errorInfoStore.actions.getErrorInfoByCode(RetCode || Status);
|
|
|
- const error = String(RetDesc || RetCode);
|
|
|
- return Promise.reject(msg ?? error);
|
|
|
+ const msg = errorInfoStore.actions.getErrorInfoByCode(res.RetCode)
|
|
|
+ const error = String(res.RetDesc || res.RetCode)
|
|
|
+ return Promise.reject(msg ?? error)
|
|
|
}
|
|
|
}
|
|
|
- }).catch((err) => {
|
|
|
- fail && fail(err);
|
|
|
- return Promise.reject(err);
|
|
|
- }).finally(() => {
|
|
|
- complete && complete();
|
|
|
- })
|
|
|
-}
|
|
|
+ }
|
|
|
+})
|