| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <!doctype html>
- <html build-time="%BUILD_TIME%">
- <head>
- <meta charset="UTF-8" />
- <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
- <script>
- var coverSupport =
- 'CSS' in window &&
- typeof CSS.supports === 'function' &&
- (CSS.supports('top: env(a)') || CSS.supports('top: constant(a)'))
- document.write(
- '<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0' +
- (coverSupport ? ', viewport-fit=cover' : '') +
- '" />',
- )
- </script>
- <title>JeecgBoot-uniapp3</title>
- <!--preload-links-->
- <!--app-context-->
- </head>
- <body>
- <div id="app"><!--app-html--></div>
- <script type="module" src="/src/main.ts"></script>
- <script>
- var API_BASE = 'http://192.168.0.118:9000/appapi'
- // 通用请求方法(带token)
- function apiGet(path, params) {
- var url = API_BASE + path
- if (params) {
- var qs = Object.keys(params).map(function(k) { return k + '=' + encodeURIComponent(params[k]) }).join('&')
- url += '?' + qs
- }
- var token = window.localStorage.getItem('APP_ACCESS_TOKEN') || ''
- return fetch(url, {
- method: 'GET',
- headers: {
- 'Content-Type': 'application/json',
- 'Authorization': 'Bearer ' + token,
- 'X-Tenant-Id': '1'
- }
- }).then(function(res) { return res.json() })
- }
- // 获取用户信息(参照login.vue fetchUserInfo)
- function fetchUserInfo(userId) {
- return apiGet('/system/auth/userinfo', { id: userId })
- .then(function(res) {
- console.log('[fetchUserInfo] Response:', JSON.stringify(res))
- if (res.code === 0 && res.data) {
- window.localStorage.setItem('APP_USER_INFO', JSON.stringify(res.data))
- console.log('[fetchUserInfo] APP_USER_INFO saved')
- // 获取部门成员列表
- if (res.data.deptId) {
- return fetchDeptMembers(res.data.deptId)
- }
- }
- })
- .catch(function(e) {
- console.error('[fetchUserInfo] Failed:', e)
- })
- }
- // 获取部门成员列表(参照login.vue fetchGetMemberByDeptApi)
- function fetchDeptMembers(deptId) {
- return apiGet('/bpm/user-group/user-list', { deptIds: deptId })
- .then(function(res) {
- console.log('[fetchDeptMembers] Response: ok')
- if (res.code === 0 && res.data && res.data.length) {
- window.localStorage.setItem('USER_LIST', JSON.stringify(res.data))
- }
- })
- .catch(function(e) {
- console.error('[fetchDeptMembers] Failed:', e)
- })
- }
- // 通知Vue应用刷新store
- function notifyAppRefresh(loginData) {
- window.dispatchEvent(new CustomEvent('app-token-login', { detail: loginData }))
- }
- window.loginByAppToken = function (token) {
- fetch(API_BASE + '/system/auth/loginByAppToken', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ appToken: token })
- })
- .then(function (res) { return res.json() })
- .then(function (res) {
- console.log('[loginByAppToken] Response:', JSON.stringify(res))
- if (res.code === 0 && res.data) {
- // 存储 accessToken
- window.localStorage.setItem('APP_ACCESS_TOKEN', res.data.accessToken)
- console.log('[loginByAppToken] APP_ACCESS_TOKEN saved')
- // 获取用户信息
- if (res.data.userId) {
- fetchUserInfo(res.data.userId).then(function() {
- // 通知Vue应用刷新store
- notifyAppRefresh({ accessToken: res.data.accessToken, userId: res.data.userId })
- })
- } else {
- console.warn('[loginByAppToken] No userId in response')
- notifyAppRefresh({ accessToken: res.data.accessToken })
- }
- } else {
- console.error('[loginByAppToken] Login failed:', res.msg)
- }
- })
- .catch(function (e) {
- console.error('[loginByAppToken] Failed:', e)
- })
- }
- window.saveToLocalStorage = function (jsonStr) {
- try {
- var data = JSON.parse(jsonStr)
- if (data && typeof data === 'object') {
- Object.keys(data).forEach(function (key) {
- window.localStorage.setItem(key, typeof data[key] === 'object' ? JSON.stringify(data[key]) : String(data[key]))
- })
- }
- console.log('[saveToLocalStorage] Success:', data)
- // 写入成功后,取出token调用登录接口
- var token = window.localStorage.getItem('token')
- if (token) {
- console.log('[saveToLocalStorage] Got token, calling loginByAppToken...')
- window.loginByAppToken(token)
- } else {
- console.warn('[saveToLocalStorage] No token found in data')
- }
- } catch (e) {
- console.error('[saveToLocalStorage] Failed:', e)
- }
- }
- </script>
- </body>
- </html>
|