You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

374 lines
10 KiB

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
  1. import { Subject } from 'rxjs';
  2. import httpRequest from '../services/httpRequest';
  3. import {
  4. KTJOrg,
  5. Measurement,
  6. RailType,
  7. SaveMeasureDTO,
  8. SettingDTO,
  9. StationItem,
  10. } from '../services/apiTypes';
  11. import { MobileDatagram, SyncProgress } from '../services/mobileWsType';
  12. declare global {
  13. interface Window {
  14. WebViewJavascriptBridge: {
  15. callHandler: (
  16. name: string,
  17. args: Record<string, any>,
  18. callback: (res: string) => void
  19. ) => void;
  20. registerHandler: (
  21. name: string,
  22. func: (data: string, callback: (res: string) => void) => void
  23. ) => void;
  24. };
  25. WVJBCallbacks: Array<(bridge: typeof window.WebViewJavascriptBridge) => void>;
  26. SyncBridgeJS: {
  27. call: (methodName: string, param: string) => string;
  28. };
  29. }
  30. }
  31. declare global {
  32. interface Window {
  33. ReactNativeWebView: { postMessage: (arg: string) => void };
  34. bridgeFunc: Record<string, any>;
  35. bridgeCall: (func: string, param: string) => void;
  36. }
  37. }
  38. export function setupWebViewJavascriptBridge(
  39. callback: (bridge: typeof window.WebViewJavascriptBridge) => void
  40. ) {
  41. if (window.WebViewJavascriptBridge) {
  42. return callback(window.WebViewJavascriptBridge);
  43. }
  44. if (/android/i.test(navigator.userAgent)) {
  45. document.addEventListener(
  46. 'WebViewJavascriptBridgeReady',
  47. function () {
  48. callback(window.WebViewJavascriptBridge);
  49. },
  50. false
  51. );
  52. } else {
  53. if (window.WVJBCallbacks) {
  54. return window.WVJBCallbacks.push(callback);
  55. }
  56. window.WVJBCallbacks = [callback];
  57. var WVJBIframe = document.createElement('iframe');
  58. WVJBIframe.style.display = 'none';
  59. WVJBIframe.src = 'https://__bridge_loaded__';
  60. document.documentElement.appendChild(WVJBIframe);
  61. setTimeout(function () {
  62. document.documentElement.removeChild(WVJBIframe);
  63. }, 0);
  64. }
  65. }
  66. export type BridgeBaseResult<T = any> = {
  67. success: boolean;
  68. data: T;
  69. message: string;
  70. };
  71. type ShowModelParam = Partial<{
  72. title: string; // 非必填,但 title 和 content 至少要填一个
  73. content: string; // 非必填,但 title 和 content 至少要填一个
  74. contentAlignCenter: boolean; // 内容文本水平居中? 默认 false, 居左
  75. showCancel: boolean; // 默认 true
  76. cancelText: string; //默认 '取消'
  77. // cancelColor: string; // 默认'#333333'
  78. confirmText: string; // 默认 '确定'
  79. // confirmColor: string; // 默认 APP主题色
  80. }>;
  81. // 是否运行在 原生APP 的 WebView 中
  82. export const appWebview = navigator.userAgent.includes('iFlyTop-mobile');
  83. const bridgeSub = new Subject<MobileDatagram>();
  84. export const bridgeOb = bridgeSub.asObservable();
  85. export function emitBridgeEvent(event: MobileDatagram) {
  86. bridgeSub.next(event);
  87. }
  88. export function registerBridgeFunc() {
  89. // window.bridgeFunc = {
  90. // peripheralStatus: (param: string) => {
  91. // bridgeSub.next({ func: 'peripheralStatus', data: JSON.parse(param) });
  92. // },
  93. // };
  94. // window.bridgeCall = (func, param) => {
  95. // const res = window.bridgeFunc[func].call(null, param);
  96. // console.log('res:', res);
  97. // };
  98. // const jsFuncs = ['funcInJs'];
  99. // jsFuncs.forEach((funcName) => {
  100. // window.WebViewJavascriptBridge.registerHandler(funcName, (data, callback) => {
  101. // bridgeSub.next({ func: funcName, data: JSON.parse(data) });
  102. // callback(JSON.stringify({ success: true }));
  103. // });
  104. // });
  105. }
  106. export default class Bridge {
  107. static register(name: string, func: (data: string, callback: (res: string) => void) => void) {
  108. if (appWebview) {
  109. window.WebViewJavascriptBridge.registerHandler(name, func);
  110. }
  111. }
  112. static showModal(param: ShowModelParam) {
  113. if (appWebview) {
  114. return new Promise<'confirm' | 'cancel'>((resolve) => {
  115. window.WebViewJavascriptBridge.callHandler('showModal', param, (res) => {
  116. resolve(res as 'confirm' | 'cancel');
  117. });
  118. });
  119. } else {
  120. return Promise.resolve('confirm' as const);
  121. }
  122. }
  123. static startMeasure() {
  124. if (appWebview) {
  125. return new Promise<BridgeBaseResult>((resolve) => {
  126. // window.WebViewJavascriptBridge.callHandler('startMeasure', {}, (res) => {
  127. // resolve(JSON.parse(res));
  128. // });
  129. const res = window.SyncBridgeJS.call('startMeasure', '');
  130. resolve(JSON.parse(res));
  131. });
  132. } else {
  133. return httpRequest<BridgeBaseResult>({ url: '/api/measure/start', method: 'POST' });
  134. }
  135. }
  136. static getUploadedRecords(param: { lastId?: number; size: number }) {
  137. if (appWebview) {
  138. return new Promise<BridgeBaseResult<Measurement[]>>((resolve) => {
  139. const res = window.SyncBridgeJS.call('getUploadedRecords', JSON.stringify(param));
  140. resolve(JSON.parse(res));
  141. });
  142. } else {
  143. return httpRequest<BridgeBaseResult<Measurement[]>>({
  144. url: '/api/mobile/getUploadedRecords',
  145. method: 'GET',
  146. params: param,
  147. });
  148. }
  149. }
  150. static getUnuploadRecords(param: { lastId?: number; size: number }) {
  151. if (appWebview) {
  152. return new Promise<BridgeBaseResult<Measurement[]>>((resolve) => {
  153. const res = window.SyncBridgeJS.call('getUnuploadRecords', JSON.stringify(param));
  154. resolve(JSON.parse(res));
  155. });
  156. } else {
  157. return httpRequest<BridgeBaseResult<Measurement[]>>({
  158. url: '/api/mobile/getUnuploadRecords',
  159. method: 'GET',
  160. params: param,
  161. });
  162. }
  163. }
  164. static searchHistoryRecords(param: { keyword: string }) {
  165. if (appWebview) {
  166. return new Promise<BridgeBaseResult<Measurement[]>>((resolve) => {
  167. const res = window.SyncBridgeJS.call('searchHistoryRecords', JSON.stringify(param));
  168. resolve(JSON.parse(res));
  169. });
  170. } else {
  171. return httpRequest<BridgeBaseResult<Measurement[]>>({
  172. url: '/api/mobile/searchHistoryRecords',
  173. method: 'GET',
  174. params: param,
  175. });
  176. }
  177. }
  178. static getMeasurementDetail(param: { id: number }) {
  179. if (appWebview) {
  180. return new Promise<BridgeBaseResult<Measurement>>((resolve) => {
  181. const res = window.SyncBridgeJS.call('getMeasurementDetail', JSON.stringify(param));
  182. resolve(JSON.parse(res));
  183. });
  184. } else {
  185. return httpRequest<BridgeBaseResult<Measurement>>({
  186. url: '/api/record/detail',
  187. method: 'POST',
  188. params: param,
  189. });
  190. }
  191. }
  192. static scanPeripherals() {
  193. return httpRequest<BridgeBaseResult>({
  194. url: '/api/ble/list/start',
  195. method: 'POST',
  196. params: {},
  197. });
  198. }
  199. static stopScanPeripherals() {
  200. return httpRequest<BridgeBaseResult>({
  201. url: '/api/ble/list/stop',
  202. method: 'POST',
  203. params: {},
  204. });
  205. }
  206. static connectPeripheral(params: { mac: string }) {
  207. return httpRequest<BridgeBaseResult>({
  208. url: '/api/ble/connect',
  209. method: 'POST',
  210. params,
  211. });
  212. }
  213. static disconnectPeripheral() {
  214. return httpRequest<BridgeBaseResult>({
  215. url: '/api/ble/disconnect',
  216. method: 'POST',
  217. params: {},
  218. });
  219. }
  220. static getConfig() {
  221. return httpRequest<BridgeBaseResult<SettingDTO>>({
  222. url: '/api/system/config',
  223. method: 'POST',
  224. params: {},
  225. });
  226. }
  227. static saveConfig(params: SettingDTO) {
  228. return httpRequest<BridgeBaseResult>({
  229. url: '/api/system/config/save',
  230. method: 'POST',
  231. params,
  232. });
  233. }
  234. static getSyncTaskList(params: { pageNum: number; size: number }) {
  235. return httpRequest<BridgeBaseResult<{ list: Measurement[] }>>({
  236. url: '/api/sync/list',
  237. method: 'POST',
  238. params,
  239. });
  240. }
  241. static addSyncTask(params: { ids: number[] }) {
  242. return httpRequest<BridgeBaseResult>({
  243. url: '/api/sync/add',
  244. method: 'POST',
  245. params,
  246. });
  247. }
  248. static getSyncProcess() {
  249. return httpRequest<BridgeBaseResult<SyncProgress['data']>>({
  250. url: '/api/sync/progress',
  251. method: 'POST',
  252. params: {},
  253. });
  254. }
  255. static clearSyncList() {
  256. return httpRequest<BridgeBaseResult>({
  257. url: '/api/sync/empty-all',
  258. method: 'POST',
  259. params: {},
  260. });
  261. }
  262. static clearFinishedSync() {
  263. return httpRequest<BridgeBaseResult>({
  264. url: '/api/sync/empty-finish',
  265. method: 'POST',
  266. params: {},
  267. });
  268. }
  269. static retryFailureSync() {
  270. return httpRequest<BridgeBaseResult>({
  271. url: '/api/sync/re-fail',
  272. method: 'POST',
  273. params: {},
  274. });
  275. }
  276. static getRecordList(params: { pageNum: number; size: number }) {
  277. return httpRequest<BridgeBaseResult<{ list: Array<{ date: string; records: Measurement[] }> }>>(
  278. {
  279. url: '/api/record/list',
  280. method: 'POST',
  281. params,
  282. }
  283. );
  284. }
  285. static getRecordDetail(params: { id: number }) {
  286. return httpRequest<BridgeBaseResult<Measurement>>({
  287. url: '/api/record/detail',
  288. method: 'POST',
  289. params,
  290. });
  291. }
  292. static deleteRecords(params: { ids: number[] }) {
  293. return httpRequest<BridgeBaseResult>({
  294. url: '/api/record/delete',
  295. method: 'POST',
  296. params,
  297. });
  298. }
  299. static uploadRecords(params: { ids: number[] }) {
  300. return httpRequest<BridgeBaseResult>({
  301. url: '/api/sync/add',
  302. method: 'POST',
  303. params,
  304. });
  305. }
  306. static getBasicTrackList() {
  307. return httpRequest<BridgeBaseResult<RailType[]>>({
  308. url: '/api/basic/track-list',
  309. method: 'POST',
  310. params: {},
  311. });
  312. }
  313. static getTrackPoint(params: { code: string }) {
  314. return httpRequest<BridgeBaseResult<RailType>>({
  315. url: '/api/basic/track/get-by-code',
  316. method: 'POST',
  317. params,
  318. });
  319. }
  320. static saveMeasure(params: SaveMeasureDTO) {
  321. console.log('保存测量 入参:', params);
  322. return httpRequest<BridgeBaseResult>({
  323. url: '/api/measure/save',
  324. method: 'POST',
  325. params,
  326. });
  327. }
  328. static getOrgTree() {
  329. return httpRequest<BridgeBaseResult<KTJOrg[]>>({
  330. url: '/api/basic/org',
  331. method: 'POST',
  332. params: {},
  333. });
  334. }
  335. static getStationList(params: { tljCode: string; gwdCode: string; xmCode: string }) {
  336. return httpRequest<BridgeBaseResult<StationItem[]>>({
  337. url: '/api/basic/station',
  338. method: 'POST',
  339. params,
  340. });
  341. }
  342. static needSyncBaseData() {
  343. return httpRequest<BridgeBaseResult<{ needSync: boolean }>>({
  344. url: '/api/basic/ktj/check',
  345. method: 'POST',
  346. params: {},
  347. });
  348. }
  349. static syncBaseData() {
  350. return httpRequest<BridgeBaseResult>({
  351. url: '/api/basic/ktj/update',
  352. method: 'POST',
  353. params: {},
  354. });
  355. }
  356. }