Files
code-bang/frontend/miniapp/app.js
T

70 lines
2.4 KiB
JavaScript
Raw Normal View History

2026-09-17 16:25:31 +08:00
import updateManager from './common/updateManager';
2026-10-14 14:44:01 +08:00
import {
QUERY_KEY as PROMOTER_KEY,
savePendingPromoter,
getPendingPromoter,
clearPendingPromoter,
bindPendingPromoter,
} from './utils/promoter';
2026-09-17 16:25:31 +08:00
var util = require('utils/util.js');
var api = require('config/api.js');
App({
2026-10-14 14:44:01 +08:00
onLaunch: function (options) {
2026-09-17 16:25:31 +08:00
wx.$api = api
wx.$util = util
// 展示本地存储能力
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
2026-10-14 14:44:01 +08:00
//通过分享链接/小程序码冷启动时缓存推广人,随登录请求一起绑定
savePendingPromoter(options && options.query);
this.loggingIn = true;
2026-10-19 13:01:58 +08:00
// 登录。loginReady 给需要鉴权的请求等待,避免首屏带着空 token 发出去
this.loginReady = new Promise((resolve) => {
const finish = () => {
2026-10-14 14:44:01 +08:00
this.loggingIn = false;
2026-10-19 13:01:58 +08:00
resolve();
};
wx.login({
success: (loginRes) => {
util.request(api.AuthLoginByWeixin, {
code: loginRes.code,
[PROMOTER_KEY]: getPendingPromoter(),
}, 'POST', { auth: false }).then((res) => {
if (res.code === 1) {
let userInfo = res.data.userinfo;
util.setToken(res.data.token);
wx.setStorageSync('userInfo', userInfo);
//推广关系为永久绑定,登录成功后无论是否绑定都无需再次提交
clearPendingPromoter();
}
}).catch((err) => {
console.error('wxLogin failed:', err);
}).then(finish);
},
fail: () => {
finish();
},
});
2026-09-17 16:25:31 +08:00
});
},
2026-10-14 14:44:01 +08:00
onShow: function (options) {
//热启动从分享进入:已登录时直接绑定,冷启动由 onLaunch 的登录请求处理
savePendingPromoter(options && options.query);
if (!this.loggingIn) {
bindPendingPromoter();
}
2026-09-17 16:25:31 +08:00
updateManager();
2026-10-19 15:46:10 +08:00
// 切回前台的时间。Tab 页用它区分「从后台回来」和「切换 Tab」,避免每次 onShow 都打接口
this.globalData.appShowAt = Date.now();
2026-10-08 16:28:21 +08:00
},
onHide: function () {},
globalData: {
unread: 0, //未读消息数(tabbar红点共享缓存)
2026-10-19 16:26:50 +08:00
unreadSeq: 0, //未读数写入序号,用来丢弃过期响应
2026-10-19 15:46:10 +08:00
appShowAt: 0, //最近一次 App.onShow 的时间
2026-10-09 16:59:09 +08:00
refreshDemandId: 0, //操作后需要刷新状态的需求ID(跨页面通知列表页)
2026-10-08 16:28:21 +08:00
},
2026-09-17 16:25:31 +08:00
});