| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>自动登录</title>
- <script src="/js/mp_base/base.js"></script>
- </head>
- <body>
- <!-- 页面加载状态 -->
- <div id="page-loading" class="page-loading">
- <div class="loading-content">
- <div class="loading-spinner"></div>
- <div class="loading-text">正在自动登录...</div>
- </div>
- </div>
- <!-- 主要内容 -->
- <div id="app" v-cloak>
- <!-- 自动登录状态 -->
- <div class="auto-login-container">
- <div class="status-icon">
- <div v-if="status === 'loading'" class="loading-spinner"></div>
- <div v-else-if="status === 'success'" class="success-icon">✓</div>
- <div v-else-if="status === 'error'" class="error-icon">✗</div>
- </div>
-
- <div class="status-text">{{ statusText }}</div>
-
- <div v-if="status === 'error'" class="retry-section">
- <button @click="handleRetry" class="retry-btn">重试</button>
- <button @click="handleManualLogin" class="manual-btn">手动登录</button>
- </div>
- </div>
- <!-- Loading 遮罩 -->
- <div v-if="showLoading" class="loading-mask">
- <div class="loading-content">
- <div class="loading-spinner"></div>
- <div class="loading-text">{{ loadingText }}</div>
- </div>
- </div>
- <!-- Toast 提示 -->
- <div v-if="toast.show" class="toast" :class="toast.type">
- {{ toast.message }}
- </div>
- </div>
- <script>
- // 获取yhsbToken的函数
- function getYhsbToken() {
- // 从localStorage获取用户信息
- const userInfo = localStorage.getItem('userInfo');
- if (userInfo) {
- try {
- const userData = JSON.parse(userInfo);
- return userData.yhsbToken || userData.onlineToken || '';
- } catch (e) {
- console.error('解析用户信息失败:', e);
- }
- }
- // 从其他可能的存储位置获取
- return localStorage.getItem('yhsbToken') || '';
- }
- // 等待SS框架加载完成
- window.SS.ready(function () {
- // 使用SS框架的方式创建Vue实例
- window.SS.dom.initializeFormApp({
- el: '#app',
- data() {
- return {
- status: 'loading', // loading, success, error
- statusText: '正在自动登录...',
- showLoading: false,
- loadingText: '',
- toast: {
- show: false,
- message: '',
- type: 'info'
- }
- }
- },
- mounted() {
- // 隐藏页面加载状态
- const pageLoading = document.getElementById('page-loading')
- if (pageLoading) {
- pageLoading.style.display = 'none'
- }
- // 初始化页面,使用自定义结果处理
- if (typeof initPage === 'function') {
- initPage('autoLogin', this.handleResult)
- }
- // 获取URL参数
- const urlParams = this.getUrlParams()
- console.log('🔗 H5自动登录页面参数:', urlParams)
- // 开始自动登录
- this.startAutoLogin()
- },
- methods: {
- // 获取URL参数
- getUrlParams() {
- const params = {}
- const urlSearchParams = new URLSearchParams(window.location.search)
- for (const [key, value] of urlSearchParams) {
- params[key] = decodeURIComponent(value)
- }
- return params
- },
- // 处理操作结果(来自小程序的回调)
- handleResult(data) {
- console.log('🎯 收到小程序回调:', data)
- },
- // 显示Loading
- showLoadingMask(text = '加载中...') {
- this.showLoading = true
- this.loadingText = text
- },
- // 隐藏Loading
- hideLoadingMask() {
- this.showLoading = false
- this.loadingText = ''
- },
-
- // 开始自动登录
- async startAutoLogin() {
- try {
- this.status = 'loading'
- this.statusText = '正在自动登录...'
- // 从URL参数或localStorage获取yhsbToken进行自动登录
- const urlParams = this.getUrlParams()
- const yhsbToken = urlParams.yhsbToken || getYhsbToken()
- console.log('🔄 开始自动登录,yhsbToken:', yhsbToken)
- if (!yhsbToken) {
- throw new Error('未找到yhsbToken,无法自动登录')
- }
- // 使用自动登录接口(参考小程序的实现)
- const response = await request.post(
- `/service?ssServ=ssLogin&wdConfirmationCaptchaService=0&mdToken=${yhsbToken}`,
- { mdToken: yhsbToken },
- { loading: false }
- )
- if (response && response.data) {
- console.log('✅ 自动登录成功:', JSON.stringify( response.data))
- // 构建用户数据(参考登录页面的格式)
- const userData = {
- devId: response.data.devId,
- sbmc: response.data.sbmc,
- sessId: response.data.sessId ,
- xm: response.data.xm,
- yhsbToken: response.data.yhsbToken,
- onlineToken: response.data.onlineToken,
- syList:response.data.sylist,
- yhid:response.data.yhid,
- yhm:response.data.yhm
- }
- // 保存用户信息到H5本地存储(和登录页面一样)
- h5UserApi.saveUserInfo(userData)
- // 准备返回数据给小程序(和登录页面一样,只是多了isAutoLogin标记)
- const loginResult = {
- success: true,
- userInfo: userData,
- isAutoLogin: true // 标记为自动登录,用于区分返回逻辑
- }
- this.status = 'success'
- this.statusText = '自动登录成功!'
- console.log('✅ 自动登录成功,准备返回数据:', loginResult)
- // 延迟返回结果
- setTimeout(() => {
- this.returnLoginResult(loginResult)
- }, 1500)
- } else {
- throw new Error('自动登录响应数据无效')
- }
- } catch (error) {
- console.error('❌ 自动登录失败:', error)
- this.status = 'error'
- this.statusText = '自动登录失败: ' + error.message
- }
- },
- // 返回登录结果给小程序
- returnLoginResult(result) {
- console.log('🔄 返回自动登录结果给小程序:', result)
- callNative('loginSuccess', '自动登录成功', result)
- },
- // 重试自动登录
- handleRetry() {
- console.log('🔄 重试自动登录')
- this.startAutoLogin()
- },
- // 跳转手动登录
- handleManualLogin() {
- console.log('👤 跳转手动登录')
- // 跳转到登录页面
- window.location.href = '/pages/login.html' + window.location.search
- }
- }
- })
- console.log('✅ 自动登录页面初始化完成')
- })
- </script>
- <style>
- /* 防止Vue模板闪烁 */
- [v-cloak] {
- display: none !important;
- }
- /* 页面加载状态 */
- .page-loading {
- position: fixed;
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- background: #f5f5f5;
- display: flex;
- align-items: center;
- justify-content: center;
- z-index: 9999;
- }
- .page-loading .loading-content {
- text-align: center;
- }
- .page-loading .loading-spinner {
- width: 40px;
- height: 40px;
- border: 4px solid #f3f3f3;
- border-top: 4px solid #40ac6d;
- border-radius: 50%;
- animation: page-spin 1s linear infinite;
- margin: 0 auto 15px;
- }
- @keyframes page-spin {
- 0% { transform: rotate(0deg); }
- 100% { transform: rotate(360deg); }
- }
- .page-loading .loading-text {
- color: #666;
- font-size: 14px;
- }
- * {
- margin: 0;
- padding: 0;
- box-sizing: border-box;
- }
- body {
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
- background: #f5f5f5;
- min-height: 100vh;
- display: flex;
- align-items: center;
- justify-content: center;
- }
- .auto-login-container {
- background: white;
- border-radius: 12px;
- padding: 40px 30px;
- text-align: center;
- box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
- max-width: 300px;
- width: 90%;
- }
- .status-icon {
- margin-bottom: 20px;
- }
- .loading-spinner {
- width: 50px;
- height: 50px;
- border: 4px solid #f3f3f3;
- border-top: 4px solid #40ac6d;
- border-radius: 50%;
- animation: spin 1s linear infinite;
- margin: 0 auto;
- }
- .success-icon {
- width: 50px;
- height: 50px;
- background: #40ac6d;
- border-radius: 50%;
- display: flex;
- align-items: center;
- justify-content: center;
- color: white;
- font-size: 24px;
- font-weight: bold;
- margin: 0 auto;
- }
- .error-icon {
- width: 50px;
- height: 50px;
- background: #dc3545;
- border-radius: 50%;
- display: flex;
- align-items: center;
- justify-content: center;
- color: white;
- font-size: 24px;
- font-weight: bold;
- margin: 0 auto;
- }
- @keyframes spin {
- 0% { transform: rotate(0deg); }
- 100% { transform: rotate(360deg); }
- }
- .status-text {
- font-size: 16px;
- color: #333;
- margin-bottom: 20px;
- line-height: 1.5;
- word-break: keep-all;
-
- }
- .retry-section {
- display: flex;
- gap: 10px;
- justify-content: center;
- }
- .retry-btn, .manual-btn {
- padding: 10px 20px;
- border: none;
- border-radius: 6px;
- font-size: 14px;
- cursor: pointer;
- transition: all 0.3s ease;
- }
- .retry-btn {
- background: #40ac6d;
- color: white;
- }
- .retry-btn:hover {
- background: #369a5a;
- }
- .manual-btn {
- background: #6c757d;
- color: white;
- }
- .manual-btn:hover {
- background: #5a6268;
- }
- /* Loading 遮罩 */
- .loading-mask {
- position: fixed;
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- background: rgba(0, 0, 0, 0.5);
- display: flex;
- align-items: center;
- justify-content: center;
- z-index: 10000;
- }
- .loading-mask .loading-content {
- background: white;
- padding: 20px;
- border-radius: 8px;
- text-align: center;
- min-width: 120px;
- }
- .loading-mask .loading-text {
- color: #333;
- font-size: 14px;
- }
- /* Toast 提示 */
- .toast {
- position: fixed;
- top: 50%;
- left: 50%;
- transform: translate(-50%, -50%);
- background: rgba(0, 0, 0, 0.8);
- color: white;
- padding: 12px 20px;
- border-radius: 6px;
- font-size: 14px;
- z-index: 10001;
- max-width: 80%;
- text-align: center;
- }
- .toast.success {
- background: rgba(40, 167, 69, 0.9);
- }
- .toast.error {
- background: rgba(220, 53, 69, 0.9);
- }
- </style>
- </body>
- </html>
|