Browse Source

上传文件至 'js'

LiuJiaxu 1 week ago
parent
commit
79b2b03b04
1 changed files with 131 additions and 0 deletions
  1. 131 0
      js/app.js

+ 131 - 0
js/app.js

@@ -0,0 +1,131 @@
+// 全局应用配置
+var app = {
+    // API基础URL
+    apiBaseUrl: 'https://chat.sinfrog.cn/api',
+    
+    // 获取本地存储的用户信息
+    getUser: function() {
+        var userJson = localStorage.getItem('user');
+        return userJson ? JSON.parse(userJson) : null;
+    },
+    
+    // 获取本地存储的令牌
+    getToken: function() {
+        return localStorage.getItem('token');
+    },
+    
+    // 检查是否已登录
+    isLoggedIn: function() {
+        return !!this.getToken();
+    },
+    
+    // 保存登录信息
+    saveLoginInfo: function(user, token) {
+        localStorage.setItem('user', JSON.stringify(user));
+        localStorage.setItem('token', token);
+    },
+    
+    // 清除登录信息
+    clearLoginInfo: function() {
+        localStorage.removeItem('user');
+        localStorage.removeItem('token');
+    },
+    
+    // 发送API请求
+    ajax: function(options) {
+        options = options || {};
+        options.url = this.apiBaseUrl + options.url;
+        
+        // 添加认证头
+        if (this.isLoggedIn() && !options.noAuth) {
+            options.headers = options.headers || {};
+            options.headers['Authorization'] = 'Bearer ' + this.getToken();
+        }
+        
+        // 处理JSON数据
+        if (options.data && typeof options.data === 'object' && !(options.data instanceof FormData)) {
+            options.headers = options.headers || {};
+            options.headers['Content-Type'] = 'application/json';
+            options.data = JSON.stringify(options.data);
+        }
+        
+        return new Promise(function(resolve, reject) {
+            mui.ajax(options.url, {
+                data: options.data,
+                dataType: options.dataType || 'json',
+                type: options.type || 'GET',
+                timeout: options.timeout || 10000,
+                headers: options.headers || {},
+                success: function(data) {
+                    resolve(data);
+                },
+                error: function(xhr, type, error) {
+                    // 处理401未授权错误
+                    if (xhr.status === 401 && !options.noAuth) {
+                        app.clearLoginInfo();
+                        mui.toast('登录已过期,请重新登录');
+                        setTimeout(function() {
+                            window.location.href = '/pages/login/login.html';
+                        }, 1500);
+                        return;
+                    }
+                    
+                    // 尝试解析错误响应
+                    var errorMsg = '请求失败';
+                    try {
+                        var errorData = JSON.parse(xhr.responseText);
+                        errorMsg = errorData.error || errorMsg;
+                    } catch (e) {}
+                    
+                    reject({
+                        status: xhr.status,
+                        message: errorMsg,
+                        type: type,
+                        xhr: xhr
+                    });
+                }
+            });
+        });
+    },
+    
+    // 格式化日期时间
+    formatDateTime: function(dateStr) {
+        var date = new Date(dateStr);
+        var now = new Date();
+        var diff = now - date;
+        
+        // 今天内
+        if (diff < 24 * 60 * 60 * 1000 && date.getDate() === now.getDate()) {
+            return this.padZero(date.getHours()) + ':' + this.padZero(date.getMinutes());
+        }
+        
+        // 昨天
+        if (diff < 48 * 60 * 60 * 1000 && date.getDate() === now.getDate() - 1) {
+            return '昨天 ' + this.padZero(date.getHours()) + ':' + this.padZero(date.getMinutes());
+        }
+        
+        // 一周内
+        if (diff < 7 * 24 * 60 * 60 * 1000) {
+            var days = ['日', '一', '二', '三', '四', '五', '六'];
+            return '星期' + days[date.getDay()] + ' ' + this.padZero(date.getHours()) + ':' + this.padZero(date.getMinutes());
+        }
+        
+        // 更早
+        return date.getFullYear() + '/' + this.padZero(date.getMonth() + 1) + '/' + this.padZero(date.getDate());
+    },
+    
+    // 数字补零
+    padZero: function(num) {
+        return num < 10 ? '0' + num : num;
+    },
+    
+    // 生成随机字符串
+    randomString: function(length) {
+        var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
+        var result = '';
+        for (var i = 0; i < length; i++) {
+            result += chars.charAt(Math.floor(Math.random() * chars.length));
+        }
+        return result;
+    }
+};