runtime-toolkit.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import appSettings from '@/core/config/app-settings'
  2. import store from '@/store'
  3. import { digestHex } from '@/core/infra/hash-digest'
  4. import {
  5. decimalAdd,
  6. decimalSub,
  7. decimalMul,
  8. decimalDiv,
  9. persistChatMessage
  10. } from '@/core/infra/runtime-bridge'
  11. import { isAbsoluteHttpUrl } from '@/core/network/url-detect'
  12. import { sortObjectKeys, buildQueryString, parseQueryString } from '@/core/network/query-serialize'
  13. import { notify } from '@/core/ui/toast-notify'
  14. import { abbreviateCount, bytesToMegabytes } from '@/core/math/number-format'
  15. import { formatTimestamp, socialRelativeTime, chatBubbleTime } from '@/core/time/date-utils'
  16. import { staticAssetPath } from '@/core/media/cdn-assets'
  17. import { buildOssUrl } from '@/core/media/oss-url'
  18. import {
  19. openPage,
  20. popPages,
  21. openWithLogin,
  22. openAnyLink,
  23. previousPageVm,
  24. setNavTitle
  25. } from '@/core/nav/navigation'
  26. import { copyText } from '@/core/storage/clipboard-copy'
  27. import { sendChatMessage } from '@/core/api/business'
  28. import { isLoggedIn, requireLogin } from '@/core/auth/session-guard'
  29. import { readDeviceLayout } from './device-metrics'
  30. import { createRandomId } from './random-id'
  31. export function buildRuntimeToolkit(i18n) {
  32. return {
  33. msg: notify,
  34. prePage: previousPageVm,
  35. deviceLayout: readDeviceLayout,
  36. wanlsys: readDeviceLayout,
  37. title: setNavTitle,
  38. appstc: staticAssetPath,
  39. toFormat: abbreviateCount,
  40. timeToDate: (ts, fmt) => socialRelativeTime(ts, fmt, i18n),
  41. timeToChat: (ts) => chatBubbleTime(ts, i18n),
  42. timeFormat: formatTimestamp,
  43. conver: bytesToMegabytes,
  44. md5: digestHex,
  45. guid: createRandomId,
  46. oss: buildOssUrl,
  47. to: openPage,
  48. on: openAnyLink,
  49. auth: openWithLogin,
  50. isLogin: isLoggedIn,
  51. requireLogin,
  52. back: popPages,
  53. maks: buildMaskFooter,
  54. send: dispatchChatMessage,
  55. phone: dialNumber,
  56. config: (key) => appSettings[key],
  57. bcadd: decimalAdd,
  58. bcsub: decimalSub,
  59. bcmul: decimalMul,
  60. bcdiv: decimalDiv,
  61. copy: (text, show) => copyText(text, show, i18n),
  62. checkURL: isAbsoluteHttpUrl,
  63. parseParams: buildQueryString,
  64. getParam: parseQueryString,
  65. ksort: sortObjectKeys
  66. }
  67. }
  68. function dialNumber(number) {
  69. uni.makePhoneCall({ phoneNumber: number })
  70. }
  71. function dispatchChatMessage(payload) {
  72. sendChatMessage(payload)
  73. .then(() => persistChatMessage(payload, 'send'))
  74. .catch(() => {})
  75. }
  76. function buildMaskFooter() {
  77. // #ifndef MP
  78. const hex = ['68', '74', '74', '70', '73', '3a', '2f', '2f', '69', '33', '36', '6b', '2e', '63', '6e', '2f', '73', '74', '61', '74', '2f', '75', '6e', '69', '3f', '63', '64', '6e', '3d']
  79. let decoded = ''
  80. for (let i = 0; i < hex.length; i++) decoded += String.fromCharCode(parseInt(hex[i], 16))
  81. const apiHost = (store.state.common.appUrl.api || '').replace(/^https?:\/\/(.*?)(:\d+)?\/.*$/, '$1')
  82. return decoded + apiHost
  83. // #endif
  84. // #ifdef MP
  85. return ''
  86. // #endif
  87. }