|
|
@@ -0,0 +1,113 @@
|
|
|
+import http from '@/http/api.js'
|
|
|
+
|
|
|
+/**
|
|
|
+ * 获取推送CID及设备信息
|
|
|
+ * @param {Boolean} showToast 是否显示错误提示,默认 true
|
|
|
+ * @returns {Promise<Object>} 返回 Promise,成功时 resolve({cid, platform, brand})
|
|
|
+ */
|
|
|
+const getPushCid = (showToast = true) => {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ // 获取系统信息以得到平台和品牌
|
|
|
+ const systemInfo = uni.getSystemInfoSync();
|
|
|
+ const platform = systemInfo.platform;
|
|
|
+ // 手机品牌
|
|
|
+ const brand = systemInfo.brand || 'unknown';
|
|
|
+
|
|
|
+ uni.getPushClientId({
|
|
|
+ success: (res) => {
|
|
|
+ const cid = res.cid;
|
|
|
+ console.log('[PushUtil] 获取CID成功:', cid, '平台:', platform, '品牌:', brand);
|
|
|
+
|
|
|
+ // 存储到本地缓存
|
|
|
+ uni.setStorageSync('push_cid', cid);
|
|
|
+ uni.setStorageSync('push_platform', platform);
|
|
|
+ uni.setStorageSync('push_brand', brand);
|
|
|
+
|
|
|
+ // 返回包含设备信息的对象
|
|
|
+ resolve({
|
|
|
+ cid: cid,
|
|
|
+ platform: platform,
|
|
|
+ brand: brand
|
|
|
+ });
|
|
|
+ },
|
|
|
+ fail: (err) => {
|
|
|
+ console.error('[PushUtil] 获取CID失败:', err);
|
|
|
+ // 如果需要弹窗提示(方便调试)
|
|
|
+ if (showToast) {
|
|
|
+ uni.showModal({
|
|
|
+ title: '提示',
|
|
|
+ content: '获取推送标识失败,请确认已使用自定义基座运行。错误: ' + JSON.stringify(err),
|
|
|
+ showCancel: false
|
|
|
+ });
|
|
|
+ }
|
|
|
+ reject(err);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 将 CID 及设备信息上传到业务服务器
|
|
|
+ * @param {String} cid 设备标识
|
|
|
+ * @param {String} platform 平台信息
|
|
|
+ * @param {String} brand 设备品牌
|
|
|
+ */
|
|
|
+const uploadCidToServer = (cid, platform, brand) => {
|
|
|
+ if (!cid) {
|
|
|
+ console.warn('[PushUtil] CID为空,停止上传');
|
|
|
+ return Promise.reject('CID为空');
|
|
|
+ }
|
|
|
+
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ // 使用POST请求,将设备信息一起上传
|
|
|
+ http.request({
|
|
|
+ url: '/blade-sales-part/app/push/saveUserCid',
|
|
|
+ method: 'post',
|
|
|
+ data: {
|
|
|
+ cid: cid,
|
|
|
+ platform: platform || '',
|
|
|
+ deviceBrand: brand || ''
|
|
|
+ }
|
|
|
+ }).then(res => {
|
|
|
+ resolve(res);
|
|
|
+ }).catch(err => {
|
|
|
+ reject(err);
|
|
|
+ });
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 完整的推送初始化流程(建议在App.vue调用)
|
|
|
+ * 获取CID并上传到服务器,包含平台和品牌信息
|
|
|
+ */
|
|
|
+const initPushService = (showToast = true) => {
|
|
|
+ getPushCid(showToast)
|
|
|
+ .then(deviceInfo => {
|
|
|
+ console.log('[PushUtil] 设备信息:', deviceInfo);
|
|
|
+ return uploadCidToServer(deviceInfo.cid, deviceInfo.platform, deviceInfo.brand);
|
|
|
+ })
|
|
|
+ .then(res => {
|
|
|
+ console.log('[PushUtil] 设备信息上传成功:', res);
|
|
|
+ })
|
|
|
+ .catch(err => {
|
|
|
+ console.error('[PushUtil] 推送服务初始化失败:', err);
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 监听推送消息 (建议在 App.vue 调用一次)
|
|
|
+ */
|
|
|
+const onPushMessage = () => {
|
|
|
+ uni.onPushMessage((res) => {
|
|
|
+ console.log('[PushUtil] 收到推送消息:', res);
|
|
|
+ // 您可以在这里做一些全局处理,比如播放提示音、增加角标等
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+// 导出方法
|
|
|
+export default {
|
|
|
+ getPushCid,
|
|
|
+ uploadCidToServer,
|
|
|
+ initPushService,
|
|
|
+ onPushMessage
|
|
|
+};
|