index.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <!doctype html>
  2. <html build-time="%BUILD_TIME%">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
  6. <script>
  7. var coverSupport =
  8. 'CSS' in window &&
  9. typeof CSS.supports === 'function' &&
  10. (CSS.supports('top: env(a)') || CSS.supports('top: constant(a)'))
  11. document.write(
  12. '<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0' +
  13. (coverSupport ? ', viewport-fit=cover' : '') +
  14. '" />',
  15. )
  16. </script>
  17. <title>JeecgBoot-uniapp3</title>
  18. <!--preload-links-->
  19. <!--app-context-->
  20. </head>
  21. <body>
  22. <div id="app"><!--app-html--></div>
  23. <script type="module" src="/src/main.ts"></script>
  24. <script>
  25. var API_BASE = 'http://192.168.0.118:9000/appapi'
  26. // 通用请求方法(带token)
  27. function apiGet(path, params) {
  28. var url = API_BASE + path
  29. if (params) {
  30. var qs = Object.keys(params).map(function(k) { return k + '=' + encodeURIComponent(params[k]) }).join('&')
  31. url += '?' + qs
  32. }
  33. var token = window.localStorage.getItem('APP_ACCESS_TOKEN') || ''
  34. return fetch(url, {
  35. method: 'GET',
  36. headers: {
  37. 'Content-Type': 'application/json',
  38. 'Authorization': 'Bearer ' + token,
  39. 'X-Tenant-Id': '1'
  40. }
  41. }).then(function(res) { return res.json() })
  42. }
  43. // 获取用户信息(参照login.vue fetchUserInfo)
  44. function fetchUserInfo(userId) {
  45. return apiGet('/system/auth/userinfo', { id: userId })
  46. .then(function(res) {
  47. console.log('[fetchUserInfo] Response:', JSON.stringify(res))
  48. if (res.code === 0 && res.data) {
  49. window.localStorage.setItem('APP_USER_INFO', JSON.stringify(res.data))
  50. console.log('[fetchUserInfo] APP_USER_INFO saved')
  51. // 获取部门成员列表
  52. if (res.data.deptId) {
  53. return fetchDeptMembers(res.data.deptId)
  54. }
  55. }
  56. })
  57. .catch(function(e) {
  58. console.error('[fetchUserInfo] Failed:', e)
  59. })
  60. }
  61. // 获取部门成员列表(参照login.vue fetchGetMemberByDeptApi)
  62. function fetchDeptMembers(deptId) {
  63. return apiGet('/bpm/user-group/user-list', { deptIds: deptId })
  64. .then(function(res) {
  65. console.log('[fetchDeptMembers] Response: ok')
  66. if (res.code === 0 && res.data && res.data.length) {
  67. window.localStorage.setItem('USER_LIST', JSON.stringify(res.data))
  68. }
  69. })
  70. .catch(function(e) {
  71. console.error('[fetchDeptMembers] Failed:', e)
  72. })
  73. }
  74. // 通知Vue应用刷新store
  75. function notifyAppRefresh(loginData) {
  76. window.dispatchEvent(new CustomEvent('app-token-login', { detail: loginData }))
  77. }
  78. window.loginByAppToken = function (token) {
  79. fetch(API_BASE + '/system/auth/loginByAppToken', {
  80. method: 'POST',
  81. headers: { 'Content-Type': 'application/json' },
  82. body: JSON.stringify({ appToken: token })
  83. })
  84. .then(function (res) { return res.json() })
  85. .then(function (res) {
  86. console.log('[loginByAppToken] Response:', JSON.stringify(res))
  87. if (res.code === 0 && res.data) {
  88. // 存储 accessToken
  89. window.localStorage.setItem('APP_ACCESS_TOKEN', res.data.accessToken)
  90. console.log('[loginByAppToken] APP_ACCESS_TOKEN saved')
  91. // 获取用户信息
  92. if (res.data.userId) {
  93. fetchUserInfo(res.data.userId).then(function() {
  94. // 通知Vue应用刷新store
  95. notifyAppRefresh({ accessToken: res.data.accessToken, userId: res.data.userId })
  96. })
  97. } else {
  98. console.warn('[loginByAppToken] No userId in response')
  99. notifyAppRefresh({ accessToken: res.data.accessToken })
  100. }
  101. } else {
  102. console.error('[loginByAppToken] Login failed:', res.msg)
  103. }
  104. })
  105. .catch(function (e) {
  106. console.error('[loginByAppToken] Failed:', e)
  107. })
  108. }
  109. window.saveToLocalStorage = function (jsonStr) {
  110. try {
  111. var data = JSON.parse(jsonStr)
  112. if (data && typeof data === 'object') {
  113. Object.keys(data).forEach(function (key) {
  114. window.localStorage.setItem(key, typeof data[key] === 'object' ? JSON.stringify(data[key]) : String(data[key]))
  115. })
  116. }
  117. console.log('[saveToLocalStorage] Success:', data)
  118. // 写入成功后,取出token调用登录接口
  119. var token = window.localStorage.getItem('token')
  120. if (token) {
  121. console.log('[saveToLocalStorage] Got token, calling loginByAppToken...')
  122. window.loginByAppToken(token)
  123. } else {
  124. console.warn('[saveToLocalStorage] No token found in data')
  125. }
  126. } catch (e) {
  127. console.error('[saveToLocalStorage] Failed:', e)
  128. }
  129. }
  130. </script>
  131. </body>
  132. </html>