| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- import { replaceClientId } from '@/api/admin'
- import permision from '@/utils/wa-permission_1.1/permission.js'
- function isInvalidOaid(OAID) {
- if (!OAID) {
- return true
- }
- // OAID 无效占位:只包含 0 和 -
- 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')
- keychain.getDeviceIDCallback((ret) => {
- const deviceId = ret ? ret : ''
- if (deviceId) {
- uni.setStorageSync('deviceId', deviceId)
- }
- if (onComplete) {
- onComplete()
- }
- })
- // #endif
- // #ifndef APP-PLUS
- if (onComplete) {
- onComplete()
- }
- // #endif
- }
- function androidRegister() {
- // #ifdef APP-PLUS
- const idCode = uni.requireNativePlugin('Ba-IdCode')
- idCode.register(res => {
- console.log('Ba-IdCode register', res)
- })
- // #endif
- }
- function androidGetIdCodes(onComplete) {
- // #ifdef APP-PLUS
- const idCode = uni.requireNativePlugin('Ba-IdCode')
- idCode.getIdCodes(res => {
- if (res.data && res.data.AndroidID && res.data.AndroidID !== '') {
- uni.setStorageSync('deviceId', res.data.AndroidID)
- } else if (res.data && res.data.GUID && res.data.GUID !== '') {
- uni.setStorageSync('deviceId', res.data.GUID)
- } else if (res.data && res.data.IMEI && res.data.IMEI !== '') {
- uni.setStorageSync('deviceId', res.data.IMEI)
- }
- if (onComplete) {
- onComplete()
- }
- })
- // #endif
- // #ifndef APP-PLUS
- if (onComplete) {
- onComplete()
- }
- // #endif
- }
- function androidGetOAID(onComplete) {
- // #ifdef APP-PLUS
- const idCode = uni.requireNativePlugin('Ba-IdCode')
- idCode.getOAID((res) => {
- const OAID = (res.data && res.data.OAID) ? res.data.OAID : ''
- if (isInvalidOaid(OAID)) {
- androidGetIdCodes(onComplete)
- return
- }
- if (OAID) {
- uni.setStorageSync('deviceId', OAID)
- }
- if (onComplete) {
- onComplete()
- }
- })
- // #endif
- // #ifndef APP-PLUS
- if (onComplete) {
- 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 端)
- */
- export function setupAppDeviceId(onComplete) {
- // #ifdef APP-PLUS
- const systemInfo = uni.getSystemInfoSync()
- if (systemInfo.platform === 'ios') {
- getIosDeviceId(onComplete)
- } else if (systemInfo.platform === 'android') {
- androidRegister()
- androidGetOAID(onComplete)
- } else if (onComplete) {
- onComplete()
- }
- // #endif
- // #ifndef APP-PLUS
- if (onComplete) {
- onComplete()
- }
- // #endif
- }
- let pushMessageListenerRegistered = false
- /**
- * 登录成功后再注册推送消息监听,避免冷启动触发推送通道初始化
- */
- export function setupAppPushMessageListener() {
- // #ifdef APP-PLUS
- if (pushMessageListenerRegistered) {
- return
- }
- pushMessageListenerRegistered = true
- uni.onPushMessage((res) => {
- console.log('收到推送消息:', res)
- const message = res.data
- const pagePath = message.payload && message.payload.page
- if (!pagePath) {
- return
- }
- let url = pagePath.startsWith('/') ? pagePath : `/${pagePath}`
- if (message.params || (message.payload && message.payload.params)) {
- const params = message.params || message.payload.params
- const queryString = Object.keys(params).map(key => `${key}=${params[key]}`).join('&')
- url = `${url}?${queryString}`
- }
- console.log('推送消息跳转页面:', url)
- uni.navigateTo({
- url: url,
- fail: (err) => {
- console.error('页面跳转失败:', err)
- }
- })
- plus.runtime.setBadgeNumber(0)
- })
- // #endif
- }
- /**
- * 登录成功后同步推送 clientId(仅 App 端)
- */
- export function syncAppPushClientId(auto = 0) {
- // #ifdef APP-PLUS
- try {
- if (plus.os.name !== 'iOS' && isAndroid13Plus() && !areAndroidNotificationsEnabled()) {
- return
- }
- const clientInfo = plus.push.getClientInfo()
- const currentClientId = clientInfo && clientInfo.clientid ? clientInfo.clientid : ''
- const deviceId = uni.getStorageSync('deviceId')
- if (currentClientId) {
- replaceClientId({ id: currentClientId, deviceId: deviceId, auto: auto })
- }
- } catch (e) {
- console.log('syncAppPushClientId error', e)
- }
- // #endif
- }
- function finishAppPushSetup(auto) {
- setupAppPushMessageListener()
- syncAppPushClientId(auto)
- }
- /**
- * 用户登录成功后:获取设备 ID → 用户确认后申请通知权限 → 注册推送监听 → 同步推送 clientId
- * @param {{ auto?: number }} options auto=1 表示静默恢复登录,auto=0 表示用户主动登录
- */
- export function afterAppLoginSuccess(options = {}) {
- const auto = options.auto !== undefined ? options.auto : 0
- setupAppDeviceId(() => {
- if (auto === 1) {
- // 冷启动自动恢复登录:不弹通知授权,仅注册监听并尝试同步 clientId
- finishAppPushSetup(auto)
- return
- }
- requestAppNotificationPermission(() => {
- finishAppPushSetup(auto)
- })
- })
- }
|