|
|
@@ -1,4 +1,5 @@
|
|
|
import { replaceClientId } from '@/api/admin'
|
|
|
+import permision from '@/utils/wa-permission_1.1/permission.js'
|
|
|
|
|
|
function isInvalidOaid(OAID) {
|
|
|
if (!OAID) {
|
|
|
@@ -8,6 +9,34 @@ function isInvalidOaid(OAID) {
|
|
|
return /^[0|-]+$/.test(OAID)
|
|
|
}
|
|
|
|
|
|
+function isAndroid13Plus() {
|
|
|
+ // #ifdef APP-PLUS
|
|
|
+ try {
|
|
|
+ const Build = plus.android.importClass('android.os.Build')
|
|
|
+ return Build.VERSION.SDK_INT >= 33
|
|
|
+ } catch (e) {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ // #endif
|
|
|
+ return false
|
|
|
+}
|
|
|
+
|
|
|
+function areAndroidNotificationsEnabled() {
|
|
|
+ // #ifdef APP-PLUS
|
|
|
+ try {
|
|
|
+ const main = plus.android.runtimeMainActivity()
|
|
|
+ let NotificationManagerCompat = plus.android.importClass('androidx.core.app.NotificationManagerCompat')
|
|
|
+ if (!NotificationManagerCompat) {
|
|
|
+ NotificationManagerCompat = plus.android.importClass('android.support.v4.app.NotificationManagerCompat')
|
|
|
+ }
|
|
|
+ return NotificationManagerCompat.from(main).areNotificationsEnabled()
|
|
|
+ } catch (e) {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ // #endif
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
function getIosDeviceId(onComplete) {
|
|
|
// #ifdef APP-PLUS
|
|
|
const keychain = uni.requireNativePlugin('Univalsoft-DeviceId')
|
|
|
@@ -84,6 +113,58 @@ function androidGetOAID(onComplete) {
|
|
|
// #endif
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * 登录成功后、同步推送前,由用户确认再申请通知权限(Android 13+)
|
|
|
+ */
|
|
|
+export function requestAppNotificationPermission(onComplete) {
|
|
|
+ // #ifdef APP-PLUS
|
|
|
+ if (plus.os.name === 'iOS') {
|
|
|
+ if (onComplete) {
|
|
|
+ onComplete()
|
|
|
+ }
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if (!isAndroid13Plus() || areAndroidNotificationsEnabled()) {
|
|
|
+ if (onComplete) {
|
|
|
+ onComplete()
|
|
|
+ }
|
|
|
+ return
|
|
|
+ }
|
|
|
+ uni.showModal({
|
|
|
+ title: '提示',
|
|
|
+ content: '开启通知权限后,可及时接收订单等重要消息',
|
|
|
+ confirmText: '开启',
|
|
|
+ cancelText: '暂不',
|
|
|
+ success(res) {
|
|
|
+ const done = () => {
|
|
|
+ if (onComplete) {
|
|
|
+ onComplete()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!res.confirm) {
|
|
|
+ done()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ permision.requestAndroidPermission('android.permission.POST_NOTIFICATIONS').then(() => {
|
|
|
+ done()
|
|
|
+ }).catch(() => {
|
|
|
+ done()
|
|
|
+ })
|
|
|
+ },
|
|
|
+ fail() {
|
|
|
+ if (onComplete) {
|
|
|
+ onComplete()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ // #endif
|
|
|
+ // #ifndef APP-PLUS
|
|
|
+ if (onComplete) {
|
|
|
+ onComplete()
|
|
|
+ }
|
|
|
+ // #endif
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* 登录成功后获取设备标识(仅 App 端)
|
|
|
*/
|
|
|
@@ -125,12 +206,19 @@ export function syncAppPushClientId(auto = 0) {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 用户登录成功后:获取设备 ID 并同步推送 clientId
|
|
|
+ * 用户登录成功后:获取设备 ID → 用户确认后申请通知权限 → 同步推送 clientId
|
|
|
* @param {{ auto?: number }} options auto=1 表示静默恢复登录,auto=0 表示用户主动登录
|
|
|
*/
|
|
|
export function afterAppLoginSuccess(options = {}) {
|
|
|
const auto = options.auto !== undefined ? options.auto : 0
|
|
|
setupAppDeviceId(() => {
|
|
|
- syncAppPushClientId(auto)
|
|
|
+ if (auto === 1) {
|
|
|
+ // 冷启动自动恢复登录:不弹通知授权,仅尝试同步 clientId
|
|
|
+ syncAppPushClientId(auto)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ requestAppNotificationPermission(() => {
|
|
|
+ syncAppPushClientId(auto)
|
|
|
+ })
|
|
|
})
|
|
|
}
|