index.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // 用户相关请求模块
  2. import APP from '@/services';
  3. import { geLoginID_number, isLogin } from '@/services/bus/login';
  4. import { getToken } from '@/services/bus/token';
  5. import { buildProtoReq50, parseProtoRsp50, protoMiddleware } from '@/services/socket/protobuf/buildReq';
  6. import { Callback } from '@/utils/websocket/index';
  7. import { sha256 } from 'js-sha256';
  8. import moment from 'moment';
  9. import { v4 as uuidv4 } from 'uuid';
  10. import { LoginResponse } from './interface/index';
  11. let preDate = Date.now();
  12. export const loginAction = (logidCode: string, password: string, ClientSystemInfo: Uint8Array[], isEncryt = false): Promise<LoginResponse> => {
  13. const LoginPWD = isEncryt ? password : sha256.hex(`${logidCode}${password}`);
  14. const reqParams = {
  15. LoginPWD,
  16. LoginID: logidCode,
  17. GUID: uuidv4(),
  18. LoginType: 0,
  19. ClientType: 4,
  20. Version: '2.0.0.0',
  21. DeviceID: '',
  22. }
  23. if (ClientSystemInfo.length) { // 与qt交互消息
  24. Object.assign(reqParams, { ClientSystemInfo, ClientAppID: 'muchinfo_mtpclient_5.0.1' })
  25. }
  26. return protoMiddleware(reqParams, 'LoginReq', 'LoginRsp', 1)
  27. };
  28. /**
  29. * 退出 登录 不等服务响应
  30. */
  31. export const logoutResponse = (): Promise<void> => {
  32. return new Promise((resolve, reject) => {
  33. if (isLogin()) {
  34. const loginId = geLoginID_number()!;
  35. const token = getToken();
  36. const params = {
  37. protobufName: 'LogoutReq',
  38. funCodeName: 'LogoutReq',
  39. reqParams: { LoginID: loginId, Token: token },
  40. };
  41. const package50 = buildProtoReq50(params);
  42. APP.sendTradingServer(package50, undefined, {
  43. onSuccess: (res) => {
  44. // const { isSuccess, result } = parseProtoRsp50(res, 'LogoutRsp');
  45. // if (isSuccess) {
  46. // resolve(result);
  47. // } else {
  48. // reject(result);
  49. // }
  50. },
  51. onFail: (err) => {
  52. // reject(err);
  53. },
  54. } as Callback);
  55. }
  56. });
  57. };
  58. export const tokenCheck = (): Promise<string> => {
  59. return new Promise((resolve, reject) => {
  60. if (isLogin()) {
  61. const loginId = geLoginID_number();
  62. const token = getToken();
  63. // console.log('token::::::', token)
  64. const systemDate = APP.get('systemDate');
  65. const targetDate = Date.parse(systemDate) + Date.now() - preDate;
  66. const params = {
  67. protobufName: 'TokenCheckReq',
  68. funCodeName: 'TokenCheckReq',
  69. reqParams: { LoginID: loginId, Token: token, SystemTime: targetDate },
  70. };
  71. const package50 = buildProtoReq50(params);
  72. APP.sendTradingServer(package50, undefined, {
  73. onSuccess: (res) => {
  74. const { isSuccess, result } = parseProtoRsp50(res, 'TokenCheckRsp');
  75. if (isSuccess) {
  76. console.log('token 校验成功::::::')
  77. const systemTime = Number(result.SystemTime);
  78. const time = moment(new Date(systemTime)).format('YYYY-MM-DD HH:mm:ss');
  79. APP.set('systemDate', time);
  80. preDate = Date.now();
  81. resolve('ok');
  82. } else {
  83. reject(result);
  84. console.log(`token 校验成功:${result}`)
  85. }
  86. },
  87. onFail: (err) => {
  88. reject(err);
  89. console.log(`token 校验失败:${err}`)
  90. },
  91. } as Callback);
  92. } else {
  93. reject('用户已登出!');
  94. }
  95. });
  96. };