import Taro, { getCurrentInstance } from '@tarojs/taro';
|
import { Component } from 'react';
|
import { Provider } from 'react-redux';
|
import './utils/promise.finally';
|
import configStore from './store';
|
import { dispatchGetWechatId } from './actions/user_weapp';
|
import { dispatchGetPlatformId as dispatchGetPlatformIdTt } from './actions/user_tt';
|
import { dispatchGetPlatformId as dispatchGetPlatformIdAlipay } from './actions/user_alipay';
|
import { dispatchGetPlatformId as dispatchGetPlatformIdSwan } from './actions/user_swan';
|
import { dispatchGetPlatformId as dispatchGetPlatformIdJd } from './actions/user_jd';
|
import { dispatchGetPlatformId as dispatchGetPlatformIdKwai } from './actions/user_kwai';
|
import { dispatchGetPlatformId as dispatchGetPlatformIdXhs } from './actions/user_xhs';
|
import { getAuthCode } from './utils/alipay';
|
import xpReport from './utils/report';
|
import gioReport from './utils/gio';
|
import { getStorage } from './utils/tools';
|
import { jumpTo } from './utils/jump';
|
import { clearSubscribeCount } from './utils/subscribe';
|
|
import './app.less';
|
import { dispatchPreRouterUpdate } from './actions/router';
|
import { privicyShowManager } from './components/privacy-popup/utils';
|
import { initPrivacyPopupState } from './reducers/privacyPopup';
|
import { preloadLoginBgImageOnWx } from './utils/login-bg-utils';
|
|
const store = configStore();
|
const ENV = process.env.TARO_ENV;
|
|
gioReport.init();
|
|
export default class App extends Component {
|
$instance: any = getCurrentInstance();
|
idData: any = {};
|
loadingIdData: boolean = false;
|
sessionId: string = xpReport.getSessionId();
|
navBarData: any = {};
|
scene: string = '';
|
isOnLaunch: boolean = false;
|
jdXid: string = '';
|
cityStoreParas: any = {};
|
reserveDialogScene: any = {};
|
onLaunchQuery: any = {};
|
carTypeList: [];
|
|
private getIdXhsPromise: Promise<any> | null = null; // 用于存储获取小红书ID的Promise,避免重复请求,因为 utils 中有很多地方都会调用 getIdData,做一个避免重复请求的机制
|
|
async onLaunch(options) {
|
const { scene, query } = options || {};
|
console.log('onLaunch---scene', scene);
|
console.log('onLaunch---options', options);
|
|
this.onLaunchQuery = query || {};
|
|
// 很多逻辑依赖于场景值,提前设置,确保成功
|
if (scene) {
|
this.scene = scene;
|
await Taro.setStorageSync('scene', scene);
|
if (scene === 1007 || scene === 1008) {
|
this.xpReportSend({ pageId: 'P10034', buttonId: 'B007' });
|
}
|
}
|
|
// 冷启动进入时,清除订阅频控计数
|
clearSubscribeCount();
|
|
if (ENV === 'weapp') {
|
const miniProgramVersion = Taro.getAccountInfoSync().miniProgram.version
|
console.log('onLaunch---miniProgramVersion', miniProgramVersion);
|
|
|
// 先从本地存储检查
|
const localHasShown = privicyShowManager.getHasShow()
|
// 初次加载时,从本地存储初始化状态
|
store.dispatch(initPrivacyPopupState(localHasShown))
|
}
|
|
if (scene !== 1154) {
|
// 检查更新
|
this.handleAutoUpdate();
|
}
|
|
|
|
this.isOnLaunch = true;
|
|
if (scene !== 1154) {
|
// 获取用户 openId 和 unionId
|
const idData = await this.getIdData();
|
console.log('idData: ', idData);
|
}
|
|
this.xpReportSend({ pageId: 'P10107', buttonId: 'B001' });
|
}
|
|
componentDidShow() {
|
if (ENV === 'weapp' && Taro.canIUse('onBeforePageUnload') && Taro.canIUse('offBeforePageUnload')) {
|
// @ts-ignore
|
wx.onBeforePageUnload(this.beforePageUnload);
|
}
|
|
const router: any = this.$instance.router || {};
|
console.log('didShow---scene', router.scene)
|
if (!this.scene && router.scene) {
|
this.scene = router.scene;
|
}
|
if (router.scene === 1154) {
|
return;
|
}
|
|
const { hasLogin } = router.query || {};
|
|
// 预加载登录背景图
|
preloadLoginBgImageOnWx()
|
|
if (hasLogin === '1') {
|
// 尝试获取用户信息 无感操作
|
getStorage('otp')
|
.then(res => {
|
if (!res) {
|
jumpTo({ url: '/pages/user/login' });
|
}
|
})
|
.catch(console.error);
|
}
|
|
}
|
|
componentDidHide() {
|
if (ENV === 'weapp' && Taro.canIUse('offBeforePageUnload')) {
|
// @ts-ignore
|
wx.offBeforePageUnload(this.beforePageUnload);
|
}
|
}
|
|
componentDidCatchError() { }
|
// 记录前一个页面的router,需求来源于 - https://xiaopeng.feishu.cn/docx/Ue1md2FpaorLt5xhmE1cN67Enld
|
beforePageUnload= (res) => {
|
try {
|
const { page } = res;
|
const { route = '', options = {} } = page;
|
dispatchPreRouterUpdate({ route: route, option: options })(store.dispatch)
|
}
|
catch (error) {
|
console.error('获取preRouter失败', error.message);
|
}
|
}
|
|
/**
|
* @description 设置导航栏信息
|
*/
|
getNavBarInfo = () => {
|
if (JSON.stringify(this.navBarData) !== "{}") {
|
return this.navBarData;
|
}
|
// 获取系统信息
|
const systemInfo: any = Taro.getSystemInfoSync() || {};
|
// 胶囊按钮位置信息
|
const menuButtonInfo = Taro.getMenuButtonBoundingClientRect();
|
// 导航栏高度 = 状态栏到胶囊的间距(胶囊距上距离-状态栏高度) * 2 + 胶囊高度 + 状态栏高度
|
const navBarHeight = (menuButtonInfo.top - systemInfo.statusBarHeight) * 2 + menuButtonInfo.height + systemInfo.statusBarHeight;
|
const menuBottom = menuButtonInfo.top - systemInfo.statusBarHeight;
|
const menuRight = systemInfo.screenWidth - menuButtonInfo.right;
|
const menuHeight = menuButtonInfo.height;
|
|
this.navBarData = {
|
navBarHeight,
|
menuBottom,
|
menuRight,
|
menuHeight,
|
menuButtonInfo,
|
statusBarHeight: systemInfo.statusBarHeight,
|
isIOS: (systemInfo.system || '').toLowerCase().indexOf('ios') >= 0,
|
};
|
|
return this.navBarData;
|
}
|
|
getScene = () => {
|
if (this.scene) {
|
return Promise.resolve(this.scene);
|
} else if (!this.isOnLaunch) {
|
return new Promise(resolve => {
|
setTimeout(() => {
|
resolve(this.getScene());
|
}, 100);
|
});
|
} else {
|
return getStorage('scene');
|
}
|
}
|
|
getIdData = async (refresh?) => {
|
const scene = await this.getScene();
|
// 微信朋友圈分享的场景下
|
if (ENV === 'weapp' && scene === 1154) {
|
return Promise.resolve({ openId: '', unionId: '' });
|
}
|
|
const updateIds = () => {
|
if (ENV === 'weapp') {
|
return this.getIdWeapp();
|
} else if (ENV === 'tt') {
|
return this.getIdTt();
|
} else if (ENV === 'alipay') {
|
return this.getIdAlipay();
|
} else if (ENV === 'swan') {
|
return this.getIdSwan();
|
} else if (ENV === 'jd') {
|
return this.getIdJd();
|
} else if (ENV === 'kwai') {
|
return this.getIdKwai();
|
} else if(ENV === 'xhs') {
|
return this.getIdXhs();
|
}
|
return this.getIdWeapp();
|
};
|
|
if (!this.idData.openId && refresh) {
|
this.idData.isRequest = false
|
}
|
const otp = await getStorage('otp');
|
if (!this.idData.unionId && otp && ENV === 'weapp' && !this.loadingIdData) {
|
this.loadingIdData = true;
|
return updateIds()
|
.catch(() => updateIds().catch(console.error))
|
.finally(() => {
|
this.loadingIdData = false
|
this.idData.isRequest = true
|
});
|
} else if (this.idData.openId) {
|
return Promise.resolve(this.idData);
|
}
|
else if (this.loadingIdData) {
|
return new Promise(resolve => {
|
setTimeout(() => {
|
resolve(this.getIdData());
|
}, 300);
|
});
|
}
|
else {
|
this.loadingIdData = true;
|
return updateIds()
|
.catch(() => updateIds().catch(console.error))
|
.finally(() => {
|
this.loadingIdData = false;
|
this.idData.isRequest = true
|
});
|
}
|
}
|
|
getIdWeapp = () => {
|
return new Promise((resolve, reject) => {
|
Taro.login({
|
success: (loginRes) => {
|
const code = loginRes.code;
|
dispatchGetWechatId({ code })(store.dispatch)
|
.then(({ openId, unionId }) => {
|
openId && Taro.setStorage({ key: 'wxOpenId', data: openId });
|
unionId && Taro.setStorage({ key: 'wxUnionId', data: unionId });
|
this.idData = { openId, unionId };
|
resolve(this.idData);
|
})
|
.catch(() => {
|
reject(new Error('获取openId失败'))
|
});
|
},
|
fail: () => {
|
reject(new Error('获取openId失败'))
|
}
|
})
|
.then(() => {
|
Taro.login({
|
success: (loginRes: any) => Taro.setStorage({ key: 'loginCode', data: loginRes.code }),
|
})
|
return this.idData; // 把前面获取到的 idData 返回出去
|
});
|
});
|
}
|
|
getIdTt = async () => {
|
const otp = await getStorage('otp');
|
if (this.idData.isRequest && !otp) {
|
return Promise.resolve(this.idData)
|
}
|
return new Promise((resolve, reject) => {
|
Taro.login({
|
success: (loginRes) => {
|
const code = loginRes.code;
|
dispatchGetPlatformIdTt({ code })(store.dispatch)
|
.then(({ openId, unionId }) => {
|
openId && Taro.setStorage({ key: 'wxOpenId', data: openId });
|
unionId && Taro.setStorage({ key: 'wxUnionId', data: unionId });
|
this.idData = { openId, unionId };
|
resolve(this.idData);
|
})
|
.catch(() => {
|
reject(new Error('获取openId失败'))
|
});
|
},
|
fail: () => {
|
reject(new Error('获取openId失败'))
|
}
|
})
|
.then(() => {
|
Taro.login({
|
success: (loginRes: any) => Taro.setStorage({ key: 'loginCode', data: loginRes.code }),
|
})
|
return this.idData; // 把前面获取到的 idData 返回出去
|
});
|
})
|
}
|
|
getIdAlipay = () => {
|
return getAuthCode()
|
.then(code => {
|
return dispatchGetPlatformIdAlipay({ code })(store.dispatch);
|
})
|
.then(({ userId, unionId }) => {
|
userId && Taro.setStorage({ key: 'wxOpenId', data: userId });
|
unionId && Taro.setStorage({ key: 'wxUnionId', data: unionId });
|
this.idData = { openId: userId, unionId };
|
return this.idData;
|
})
|
.catch(() => {
|
throw new Error('获取openId失败');
|
});
|
}
|
|
getIdSwan = async () => {
|
const otp = await getStorage('otp');
|
if (this.idData.isRequest && !otp) {
|
return Promise.resolve(this.idData)
|
}
|
return new Promise((resolve, reject) => {
|
// @ts-ignore
|
swan.getLoginCode({
|
success: (loginRes) => {
|
const code = loginRes.code;
|
dispatchGetPlatformIdSwan({ code })(store.dispatch)
|
.then(({ openId, unionId }) => {
|
openId && Taro.setStorage({ key: 'wxOpenId', data: openId });
|
unionId && Taro.setStorage({ key: 'wxUnionId', data: unionId });
|
this.idData = { openId, unionId };
|
resolve(this.idData);
|
})
|
.catch(() => {
|
reject(new Error('获取openId失败'))
|
});
|
},
|
fail: (err) => {
|
console.log(err);
|
reject(new Error('获取openId失败'))
|
}
|
});
|
})
|
.then(() => {
|
// @ts-ignore
|
swan.getLoginCode({
|
success: (loginRes: any) => Taro.setStorage({ key: 'loginCode', data: loginRes.code }),
|
})
|
return this.idData; // 把前面获取到的 idData 返回出去
|
});
|
}
|
|
getIdJd = () => {
|
return new Promise((resolve, reject) => {
|
Taro.login({
|
success: (loginRes) => {
|
const code = loginRes.code;
|
// 开发模拟上返回的是过期的code,无法使用
|
if (code === '001kIh0w3HLd3X2nsy2w3ghMKN2kIh0H') {
|
resolve(this.idData);
|
return;
|
}
|
dispatchGetPlatformIdJd({ code })(store.dispatch)
|
.then(({ openId, unionId }) => {
|
openId && Taro.setStorage({ key: 'wxOpenId', data: openId });
|
unionId && Taro.setStorage({ key: 'wxUnionId', data: unionId });
|
this.idData = { openId, unionId };
|
resolve(this.idData);
|
})
|
.catch(() => {
|
reject(new Error('获取openId失败'))
|
});
|
},
|
fail: () => {
|
reject(new Error('获取openId失败'))
|
}
|
})
|
.then(() => {
|
Taro.login({
|
success: (loginRes: any) => Taro.setStorage({ key: 'loginCode', data: loginRes.code }),
|
})
|
return this.idData; // 把前面获取到的 idData 返回出去
|
});
|
});
|
}
|
|
getIdKwai = () => {
|
return new Promise((resolve, reject) => {
|
Taro.login({
|
success: (loginRes) => {
|
const code = loginRes.code;
|
dispatchGetPlatformIdKwai({ code })(store.dispatch)
|
.then(({ openId, unionId }) => {
|
openId && Taro.setStorage({ key: 'wxOpenId', data: openId });
|
unionId && Taro.setStorage({ key: 'wxUnionId', data: unionId });
|
this.idData = { openId, unionId };
|
resolve(this.idData);
|
})
|
.catch(() => {
|
reject(new Error('获取openId失败'))
|
});
|
},
|
fail: () => {
|
reject(new Error('获取openId失败'))
|
}
|
})
|
.then(() => {
|
Taro.login({
|
success: (loginRes: any) => Taro.setStorage({ key: 'loginCode', data: loginRes.code }),
|
})
|
return this.idData; // 把前面获取到的 idData 返回出去
|
});
|
});
|
}
|
|
getIdXhs = () => {
|
if(this.getIdXhsPromise) return this.getIdXhsPromise;
|
this.getIdXhsPromise = new Promise((resolve, reject) => {
|
// @ts-ignore
|
xhs.login({
|
success: (loginRes) => {
|
const code = loginRes.code;
|
dispatchGetPlatformIdXhs({ code })(store.dispatch)
|
.then(({ openId, unionId }) => {
|
openId && Taro.setStorage({ key: 'wxOpenId', data: openId });
|
unionId && Taro.setStorage({ key: 'wxUnionId', data: unionId });
|
this.idData = { openId, unionId };
|
|
resolve(this.idData);
|
})
|
.catch((err) => {
|
reject(err)
|
}).finally(() => {
|
this.getIdXhsPromise = null; // 清除 Promise,允许下次调用重新获取
|
});
|
},
|
fail: (err) => {
|
reject(err)
|
}
|
})
|
})
|
.then(() => {
|
// @ts-ignore
|
xhs.login({
|
success: (loginRes: any) => {Taro.setStorage({ key: 'loginCode', data: loginRes.code });},
|
})
|
return this.idData // 把前面获取到的 idData 返回出去
|
});
|
|
return this.getIdXhsPromise
|
}
|
|
getStore = () => {
|
return store;
|
}
|
|
getOnLaunchQuery = () => {
|
return this.onLaunchQuery;
|
}
|
|
xpReportSend = (option) => {
|
gioReport.send(option);
|
xpReport.send(option);
|
}
|
|
xpReportShow = () => {
|
gioReport.show();
|
xpReport.show();
|
}
|
|
xpReportHide = () => {
|
gioReport.hide();
|
xpReport.hide();
|
}
|
|
handleAutoUpdate = () => {
|
const updateManager = Taro.getUpdateManager()
|
updateManager.onCheckForUpdate(function (res) {
|
// 请求完新版本信息的回调
|
console.log('onCheckForUpdate', res.hasUpdate)
|
})
|
updateManager.onUpdateReady(function () {
|
Taro.showModal({
|
title: '更新提示',
|
content: '新版本已经准备好,是否重启应用?',
|
showCancel: false,
|
success: function (res) {
|
if (res.confirm) {
|
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
|
updateManager.applyUpdate()
|
}
|
}
|
})
|
})
|
}
|
|
setJdXid = xid => {
|
this.jdXid = xid;
|
Taro.setStorage({ key: 'jdXid', data: xid });
|
}
|
|
getJdXid = () => {
|
return this.jdXid || Taro.getStorageSync('jdXid') || '';
|
}
|
|
// 在 App 类中的 render() 函数没有实际作用
|
// 请勿修改此函数
|
render() {
|
return (
|
<Provider store={store}>
|
{this.props.children}
|
</Provider>
|
)
|
}
|
}
|