h5
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

335 lines
8.1 KiB

<script>
export default {
// computed: {
// ...mapState(['theme']),
// },
// watch: {
// theme(newTheme) {
// console.log('newTheme: ', newTheme);
// if (!newTheme) return;
// this.loadTheme();
// },
// },
async onLaunch(options) {
// this.loadTheme();
// console.log('onLaunch options: ', options);
this.checkNetwork(); // 监听网络状态
this.getSystemInfo(); // 获取系统设备信息、
this.getLocation(); // 获取当前位置信息
const token = this.$store.state.user.token || this.$storage.getStorageSync('anyringToken') || '';
if (token) {
this.$store.commit('user/setToken', token);
}
// 查询广告页和引导页并缓存
// #ifdef APP-PLUS
uni.$storage.setStorageSync('isOpenApp', true);
this.$store.commit('setIsOpenApp', true);
// 判断是否第一次打开App
const firstOpenApp = uni.$storage.getStorageSync('firstOpenApp');
this.$store.commit('setFirstOpenApp');
this.getGuide(0);
this.getGuide(1);
// #endif
// 定时查询服务和插件
if (!uni.$storage.getStorageSync('businessPlugin')) {
this.getServices();
this.getPlugins();
}
setInterval(() => {
this.getServices();
this.getPlugins();
}, 60000);
// if (!token) {
// this.$ui.showToast('获取用户信息失败, 请登录');
// // TODO: 跳转登录界面
// return;
// }
// await this.syncLocalDataToStore('1217647686598135808'); // 将localStorage里的数据同步到store里
// const token = await this.getToken();
// this.noPhone(this.$store.state.user.phone);
this.$store.dispatch('socket/initSocket');
},
methods: {
// loadTheme() {
// const path = this.theme.replace('-', '/');
// import(`./common/styles/${path}.scss`);
// },
/**
* 查询广告页和引导页并缓存
*/
async getGuide(type) {
uni.$catchReq.getGuide(type, (err, data) => {
if (err) {
console.error('err: ', err);
} else {
type === 0 ? this.$store.commit('setGuide', data) : this.$store.commit('setAdvs', data);
}
});
},
/**
* 查询服务
*/
async getServices() {
this.$store.dispatch('getBusinessPlugin');
},
/**
* 查询插件
*/
async getPlugins() {
this.$store.dispatch('getAllPlugin');
},
async getToken() {
const { token } = this.$store.state.user;
const tokenIsAvailable = this.$store.getters['user/tokenIsAvailable'];
const userId = this.$store.getters['user/userId'];
if (token && tokenIsAvailable) {
// 1.1 store里有token 且没过期直接:使用store的token
return token;
}
// 2. 根据userId获取token
if (userId) {
try {
const { token } = await this.$store.dispatch('user/getTokenByUserId', userId);
return token;
} catch (error) {
console.error('error: ', error);
return null;
}
} else {
return null;
}
},
/**
* 将localStorage里的数据同步到store里
* user, token, tokenExpiredTime
*/
syncLocalDataToStore(urlUserId) {
return new Promise((resolve, reject) => {
try {
const localUser = uni.$storage.getStorageSync('user');
const localToken = uni.$storage.getStorageSync('anyringToken');
const tokenExpiredTime = uni.$storage.getStorageSync('tokenExpiredTime');
if (!this.$store.state.user.user) {
if (localUser) {
// 同步user信息
const user = JSON.parse(localUser);
if (!urlUserId || user.id === urlUserId) {
this.$store.commit('user/setUser', user);
} else {
this.$store.commit('user/setUser', { id: urlUserId });
}
} else {
this.$store.commit('user/setUser', { id: urlUserId });
}
}
if (this.$store.state.user.token && localToken) {
// 同步token
this.$store.commit('user/setToken', localToken);
}
if (this.$store.state.user.tokenExpiredTime && tokenExpiredTime) {
// 同步tokenExpiredTime
this.$store.commit('user/setTokenExpiredTime', +tokenExpiredTime);
}
resolve();
} catch (error) {
reject(error);
}
});
},
// 检查网络状态 设置store里的网络状态变量
// 网络连接 且 不是2g 不是3g 才算是网络连接; 否则不是 将启用本地存储
checkNetwork() {
uni.getNetworkType({
success: ({ networkType }) => {
this.$store.commit('setNetworkConnected', !(networkType === 'none' || networkType === '2g' || networkType === '3g'));
},
});
// 监听网络状态的变化
uni.onNetworkStatusChange(({ isConnected, networkType }) => {
this.$store.commit('setNetworkConnected', isConnected && !(networkType === '2g' || networkType === '3g'));
});
},
// 获取系统设备信息
getSystemInfo() {
uni.getSystemInfo({
success: result => {
this.$store.commit('setSystemInfo', result);
},
fail: error => {
console.error('getSystemInfo fail:', error);
},
});
},
getLocation() {
const that = this;
uni.getLocation({
type: 'wgs84',
success(res) {
that.$store.commit('setLongitude', res.longitude);
that.$store.commit('setLatitude', res.latitude);
},
});
},
// 登录
async signin() {
try {
const data = await uni.$u.api.signin();
if (data && data.token) {
this.$store.commit('user/setUser', data);
this.$store.commit('user/setToken', data.token);
noPhone(data.phone);
} else {
uni.$ui.showToast('返回数据异常');
}
} catch (error) {
console.error('error: ', error);
uni.$ui.showToast(error || '登录失败');
}
},
/**
* 没有手机号 跳转绑定手机号的界面
* @param {string} phone
*/
async noPhone(phone) {
if (!phone) {
// TODO:
// uni.navigateTo({ url: '/pages/phone-bind/phone-bind' });
}
},
},
};
</script>
<style lang="scss">
/*每个页面公共css */
@import '@/uni_modules/vk-uview-ui/index.scss';
@import '@/common/styles/iconfont.scss';
@import '@/common/styles/app.scss';
@import '@/common/styles/tailwind.scss';
@import '@/common/styles/theme/index.scss';
page {
height: 100%;
}
.uni-swiper-slides {
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
}
.statbar {
width: 750rpx;
height: var(--status-bar-height);
.status_bar {
height: var(--status-bar-height);
width: 100%;
position: absolute;
}
}
/* #ifdef H5 */
.uni-modal {
position: fixed;
z-index: 999;
width: 80%;
max-width: 300px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #ffffff;
text-align: center;
border-radius: 3px;
overflow: hidden;
}
.uni-modal__bd {
padding: 1.3em 1.6em 1.3em;
min-height: 40px;
font-size: 15px;
line-height: 1.4;
word-wrap: break-word;
word-break: break-all;
white-space: pre-wrap;
color: #999999;
max-height: 400px;
overflow-x: hidden;
overflow-y: auto;
}
.uni-modal__ft {
position: relative;
line-height: 48px;
font-size: 18px;
display: flex;
}
.uni-modal__btn {
display: block;
flex: 1;
color: #3cc51f;
text-decoration: none;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
position: relative;
cursor: pointer;
}
.uni-modal__ft:after {
content: ' ';
position: absolute;
left: 0;
top: 0;
right: 0;
height: 1px;
border-top: 1px solid #d5d5d6;
color: #d5d5d6;
transform-origin: 0 0;
transform: scaleY(0.5);
}
.uni-modal__btn:after {
content: ' ';
position: absolute;
left: 0;
top: 0;
width: 1px;
bottom: 0;
border-left: 1px solid #d5d5d6;
color: #d5d5d6;
transform-origin: 0 0;
transform: scaleX(0.5);
}
.uni-modal__btn_default {
color: #353535;
}
.uni-modal__btn_primary {
color: #007aff;
}
/* #endif */
</style>