请详细解释这个uniapp项目的app.vue文件中的代码
时间: 2025-01-28 20:25:27 浏览: 97
### `app.vue` 文件详解
#### 1. 样式部分
```html
<style lang="scss">
/* 注意要写在第一行,同时给 style 标签加入 lang="scss" 属性 */
@import "@/uni_modules/uview-ui/index.scss";
</style>
```
- **作用**:引入了 `uview-ui` 组件库的样式文件。
- **注意事项**:使用 `scss` 预处理器,并且需要将 `lang="scss"` 添加到 `<style>` 标签中。
#### 2. 脚本部分
```html
<script>
import config from './config'
import store from '@/store'
import { getToken } from '@/utils/auth'
// #ifdef APP-PLUS
var jpushModule = uni.requireNativePlugin("JG-JPush");
var audioObj = uni.getBackgroundAudioManager();
// #endif
export default {
data() {
return {};
},
onLaunch: function () {
this.initApp();
// #ifdef APP-PLUS
jpushModule.initJPushService();
jpushModule.setLoggerEnable(true);
plus.screen.lockOrientation("portrait-primary");
// 监听 极光推送连接状态
this.getNotificationEnabled();
jpushModule.addConnectEventListener(result => {
let connectEnable = result.connectEnable;
uni.$emit('connectStatusChange', connectEnable)
});
jpushModule.addNotificationListener(result => {
// 极光推送的消息通知回调
jpushModule.setBadge(0);
plus.runtime.setBadgeNumber(0);
let notificationEventType = result.notificationEventType;
// console.log("通知", result, notificationEventType);
});
uni.$on('connectStatusChange', (connectStatus) => {
var connectStr = '';
if (connectStatus == true) {
connectStr = '已连接';
// this.getRegistrationID();
} else {
connectStr = '未连接';
}
console.log('监听到了连接状态变化 --- ', connectStr);
this.connectStatus = connectStr;
});
jpushModule.isPushStopped(res => {
// code 0已停止推送 1未停止推送
const { code } = res;
console.log(res, '安卓连接状态');
});
// #endif
},
created() {
this.getRegistrationID();
},
methods: {
// 初始化应用
initApp() {
this.initConfig();
this.checkLogin();
},
initConfig() {
this.globalData.config = config;
},
checkLogin() {
if (!getToken()) {
this.$tab.reLaunch('/pages/login');
}
},
getRegistrationID() {
// 获取 registerID
jpushModule.getRegistrationID(result => {
let registerID = result.registerID;
console.log("@@@@@@@registerID = " + registerID);
this.registrationID = registerID;
uni.setStorageSync("registerID", registerID);
});
},
getNotificationEnabled() {
if (uni.getSystemInfoSync().platform == "ios") {
jpushModule.requestNotificationAuthorization((result) => {
let status = result.status;
if (status < 2) {
this.noticMsgTool();
}
});
} else {
jpushModule.isNotificationEnabled((result) => {
// 判断 android 是否打开权限
if (result.code == 0) {
// 如果为 0 则表示 未打开通知权限
this.noticMsgTool();
}
});
}
},
noticMsgTool() {
if (uni.getSystemInfoSync().platform == "ios") {
// 苹果打开对应的通知栏
uni.showModal({
title: '通知权限开启提醒',
content: '您还没有开启通知权限,无法接受到消息通知,请前往设置!',
showCancel: false,
confirmText: '去设置',
success: function (res) {
if (res.confirm) {
var app = plus.ios.invoke('UIApplication', 'sharedApplication');
var setting = plus.ios.invoke('NSURL', 'URLWithString:', 'app-settings:');
plus.ios.invoke(app, 'openURL:', setting);
plus.ios.deleteObject(setting);
plus.ios.deleteObject(app);
}
}
});
} else {
// android 打开对应的通知栏
var main = plus.android.runtimeMainActivity();
var pkName = main.getPackageName();
var uid = main.getApplicationInfo().plusGetAttribute("uid");
uni.showModal({
title: '通知权限开启提醒',
content: '您还没有开启通知权限,无法接受到消息通知,请前往设置!',
showCancel: false,
confirmText: '去设置',
success: function (res) {
if (res.confirm) {
var Intent = plus.android.importClass('android.content.Intent');
var Build = plus.android.importClass("android.os.Build");
// android 8.0 引导
if (Build.VERSION.SDK_INT >= 26) {
var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);
} else if (Build.VERSION.SDK_INT >= 21) {
// android 5.0-7.0
var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
intent.putExtra("app_package", pkName);
intent.putExtra("app_uid", uid);
} else {
// (<21) 其他 -- 跳转到该应用管理的详情页
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
intent.setData(uri);
}
// 跳转到该应用的系统通知设置页
main.startActivity(intent);
}
}
});
}
}
}
}
</script>
```
#### 3. 方法详解
- **`initApp()`**: 初始化应用,调用 `initConfig` 和 `checkLogin` 方法。
- **`initConfig()`**: 初始化应用配置,将 `config` 对象赋值给全局数据 `globalData.config`。
- **`checkLogin()`**: 检查用户是否已登录,如果没有登录,则重定向到登录页面。
- **`getRegistrationID()`**: 获取极光推送的注册 ID,并将其存储到本地缓存中。
- **`getNotificationEnabled()`**: 检查设备是否启用了通知权限,如果是 iOS 设备,请求授权;如果是 Android 设备,检查是否已经开启了通知权限。
- **`noticMsgTool()`**: 显示模态框提示用户开启通知权限,并提供跳转到设置页面的按钮。
#### 4. 平台特定代码 (`#ifdef APP-PLUS`)
- **`jpushModule.initJPushService()`**: 初始化极光推送服务。
- **`jpushModule.setLoggerEnable(true)`**: 开启日志记录。
- **`plus.screen.lockOrientation("portrait-primary")`**: 锁定屏幕方向为竖屏。
- **`addConnectEventListener`**: 监听极光推送连接状态的变化。
- **`addNotificationListener`**: 监听极光推送的消息通知回调。
- **`isPushStopped`**: 检查推送是否已停止。
#### 5. 事件监听
- **`uni.$on('connectStatusChange', ...)`**: 监听连接状态变化事件,并更新 `connectStatus`。
#### 6. 模态框显示
- **`uni.showModal`**: 在 iOS 和 Android 上分别显示不同的模态框,提示用户开启通知权限并提供跳转到设置页面的功能。
### 总结
这个 `app.vue` 文件主要负责初始化应用、配置极光推送服务、检查和处理通知权限等。通过平台特定代码(如 `#ifdef APP-PLUS`),实现了对不同平台的适配。
阅读全文