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

78 lines
2.5 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-09-17 16:25:31 +08:00
// 登录
wx.login({
success: (res) => {
util.request(api.AuthLoginByWeixin, {
2026-10-14 14:44:01 +08:00
code: res.code,
[PROMOTER_KEY]: getPendingPromoter(),
}, 'POST').then((res) => {
this.loggingIn = false;
2026-09-17 16:25:31 +08:00
if (res.code === 1) {
let userInfo = res.data.userinfo;
wx.setStorageSync('token', res.data.token);
wx.setStorageSync('userInfo', userInfo);
2026-10-14 14:44:01 +08:00
//推广关系为永久绑定,登录成功后无论是否绑定都无需再次提交
clearPendingPromoter();
2026-09-17 16:25:31 +08:00
}
2026-10-14 14:44:01 +08:00
}).catch((err) => {
this.loggingIn = false;
console.error('wxLogin failed:', err);
2026-09-17 16:25:31 +08:00
});
},
2026-10-14 14:44:01 +08:00
fail: () => {
this.loggingIn = false;
},
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-08 20:34:08 +08:00
//从后台切回前台时刷新一次未读红点(不使用定时轮询,避免请求堆积导致页面卡死)
2026-10-08 16:28:21 +08:00
this.refreshUnread();
},
onHide: function () {},
globalData: {
unread: 0, //未读消息数(tabbar红点共享缓存)
2026-10-09 16:59:09 +08:00
refreshDemandId: 0, //操作后需要刷新状态的需求ID(跨页面通知列表页)
2026-10-08 16:28:21 +08:00
},
/**
* 刷新当前 tab 页 tabbar 实例的红点(非tab页无tabbar实例,自动跳过)
*/
refreshUnread: function () {
if (!wx.getStorageSync('token')) {
return;
}
const page = getCurrentPages().pop();
const tabBar = page && typeof page.getTabBar === 'function' ? page.getTabBar() : null;
if (tabBar && typeof tabBar.refreshUnread === 'function') {
tabBar.refreshUnread();
}
2026-09-17 16:25:31 +08:00
},
});