init: initial commit
This commit is contained in:
+138
@@ -0,0 +1,138 @@
|
||||
// app.js
|
||||
const { api } = require('./utils/request');
|
||||
|
||||
App({
|
||||
globalData: {
|
||||
authState: 'loading',
|
||||
authMessage: '',
|
||||
userId: null,
|
||||
token: null,
|
||||
isConnected: true,
|
||||
networkType: 'wifi'
|
||||
},
|
||||
|
||||
onLaunch: function() {
|
||||
this.removeLegacyDataCaches();
|
||||
this._authListeners = [];
|
||||
// 冷启动始终换取新 token,所有业务请求共享这一次登录。
|
||||
this.silentLogin().catch(function() {});
|
||||
this.checkNetwork();
|
||||
wx.onNetworkStatusChange((res) => {
|
||||
this.globalData.isConnected = res.isConnected;
|
||||
this.globalData.networkType = res.networkType;
|
||||
});
|
||||
},
|
||||
|
||||
setAuthState: function(state, message) {
|
||||
this.globalData.authState = state;
|
||||
this.globalData.authMessage = message || '';
|
||||
(this._authListeners || []).slice().forEach(function(listener) { listener(state, message || ''); });
|
||||
},
|
||||
observeAuth: function(listener) {
|
||||
if (!this._authListeners) this._authListeners = [];
|
||||
this._authListeners.push(listener);
|
||||
listener(this.globalData.authState, this.globalData.authMessage);
|
||||
return () => { this._authListeners = this._authListeners.filter(function(item) { return item !== listener; }); };
|
||||
},
|
||||
ensureAuth: function() {
|
||||
if (this._authPromise) return this._authPromise;
|
||||
if (this.globalData.authState === 'error') return Promise.reject({ error: this.globalData.authMessage, authFailed: true });
|
||||
if (this.globalData.authState === 'ready' && wx.getStorageSync('token')) return Promise.resolve();
|
||||
return this.silentLogin();
|
||||
},
|
||||
// 多个旧 token 请求同时返回 401 时,只刷新一次;较晚返回的旧请求复用新 token。
|
||||
refreshAuth: function(failedToken) {
|
||||
if (this._authPromise) return this._authPromise;
|
||||
if (this.globalData.authState === 'ready' && wx.getStorageSync('token') !== failedToken) return Promise.resolve();
|
||||
return this.silentLogin();
|
||||
},
|
||||
|
||||
removeLegacyDataCaches: function() {
|
||||
try {
|
||||
var keys = wx.getStorageInfoSync().keys || [];
|
||||
var exact = {
|
||||
indexCache: true, profileCache: true, discoverStatsCache: true,
|
||||
songsCache: true, albumsCache: true, diaryCache: true,
|
||||
timelineCache: true, timelineBgCache: true, checkinsCache: true,
|
||||
coupleInfoCache: true, petCache: true, petAchievementCache: true,
|
||||
period_cache_data: true, bg_cache: true, subscribe_status_cache: true,
|
||||
_albums_inited: true, myLocation: true
|
||||
};
|
||||
keys.forEach(function(key) {
|
||||
if (exact[key] || /^(offline_|albumCache_|diaryDetailCache_|timelineDetailCache_|checkinDetailCache_|historyCache_|period_cache_data:|timelineBg_)/.test(key)) {
|
||||
wx.removeStorageSync(key);
|
||||
}
|
||||
});
|
||||
} catch (e) { console.warn('清理旧数据缓存失败', e); }
|
||||
},
|
||||
|
||||
// ★ 获取用户位置(使用 wx.getFuzzyLocation 模糊定位)
|
||||
getLocationRegion: function() {
|
||||
return new Promise(function(resolve) {
|
||||
wx.getFuzzyLocation({
|
||||
type: 'wgs84',
|
||||
success: function(loc) {
|
||||
// 直接存储经纬度字符串,不依赖地图 API
|
||||
var region = loc.latitude.toFixed(4) + ',' + loc.longitude.toFixed(4);
|
||||
resolve(region);
|
||||
},
|
||||
fail: function(err) {
|
||||
console.log('获取模糊位置失败:', err);
|
||||
resolve('');
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
silentLogin: function() {
|
||||
if (this._authPromise) return this._authPromise;
|
||||
this.setAuthState('loading');
|
||||
this._authPromise = this.performLogin().then((res) => {
|
||||
this.setAuthState('ready');
|
||||
return res;
|
||||
}).catch((error) => {
|
||||
wx.removeStorageSync('token');
|
||||
this.globalData.token = null;
|
||||
var failure = { error: (error && error.error) || '登录失败,请检查网络后重试', authFailed: true };
|
||||
this.setAuthState('error', failure.error);
|
||||
throw failure;
|
||||
}).finally(() => { this._authPromise = null; });
|
||||
return this._authPromise;
|
||||
},
|
||||
performLogin: async function() {
|
||||
var loginRes = await new Promise(function(resolve, reject) {
|
||||
var done = false;
|
||||
var timer = setTimeout(function() {
|
||||
if (!done) { done = true; reject({ error: '登录超时,请重试' }); }
|
||||
}, 10000);
|
||||
wx.login({
|
||||
success: function(res) { if (!done) { done = true; clearTimeout(timer); resolve(res); } },
|
||||
fail: function(err) { if (!done) { done = true; clearTimeout(timer); reject(err); } }
|
||||
});
|
||||
});
|
||||
if (!loginRes.code) throw { error: '未能获取微信登录凭证,请重试' };
|
||||
// 定位授权不阻塞登录,城市信息由页面后续的定位接口更新。
|
||||
var res = await api.wechatLogin(loginRes.code, '');
|
||||
if (!res || !res.token || !res.user_id) throw { error: '登录响应不完整,请重试' };
|
||||
wx.setStorageSync('token', res.token);
|
||||
wx.setStorageSync('userId', res.user_id);
|
||||
wx.setStorageSync('coupleId', res.couple_id || '');
|
||||
wx.setStorageSync('displayId', String(res.display_id || ''));
|
||||
wx.setStorageSync('nickname', res.nickname || '');
|
||||
wx.setStorageSync('region', res.region || '');
|
||||
wx.setStorageSync('hasLoggedIn', true);
|
||||
this.globalData.token = res.token;
|
||||
this.globalData.userId = res.user_id;
|
||||
return res;
|
||||
},
|
||||
|
||||
// 检查当前网络状态
|
||||
checkNetwork: function() {
|
||||
wx.getNetworkType({
|
||||
success: (res) => {
|
||||
this.globalData.isConnected = res.networkType !== 'none';
|
||||
this.globalData.networkType = res.networkType;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user